feat(backup): reload if restore fails (#5404)

pull/5416/head
Chaim Lev-Ari 3 years ago committed by GitHub
parent 8d01b45445
commit 456995353b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,8 @@ angular.module('portainer.app').controller('InitAdminController', [
'BackupService',
'StatusService',
function ($scope, $state, Notifications, Authentication, StateManager, SettingsService, UserService, EndpointService, BackupService, StatusService) {
$scope.uploadBackup = uploadBackup;
$scope.logo = StateManager.getState().application.logo;
$scope.formValues = {
@ -25,6 +27,8 @@ angular.module('portainer.app').controller('InitAdminController', [
showRestorePortainer: false,
};
createAdministratorFlow();
$scope.togglePanel = function () {
$scope.state.showInitPassword = !$scope.state.showInitPassword;
$scope.state.showRestorePortainer = !$scope.state.showRestorePortainer;
@ -74,39 +78,58 @@ angular.module('portainer.app').controller('InitAdminController', [
Notifications.error('Failure', err, 'Unable to verify administrator account existence');
});
}
createAdministratorFlow();
async function waitPortainerRestart() {
for (let i = 0; i < 10; i++) {
await new Promise((resolve) => setTimeout(resolve, 5 * 1000));
try {
const status = await StatusService.status();
if (status && status.Version) {
return;
}
} catch (e) {
// pass
}
}
throw 'Timeout to wait for Portainer restarting';
}
$scope.uploadBackup = async function () {
async function uploadBackup() {
$scope.state.backupInProgress = true;
const file = $scope.formValues.BackupFile;
const password = $scope.formValues.Password;
restoreAndRefresh(() => BackupService.uploadBackup(file, password));
}
async function restoreAndRefresh(restoreAsyncFn) {
$scope.state.backupInProgress = true;
try {
await restoreAsyncFn();
} catch (err) {
Notifications.error('Failure', err, 'Unable to restore the backup');
$scope.state.backupInProgress = false;
return;
}
try {
await BackupService.uploadBackup(file, password);
await waitPortainerRestart();
Notifications.success('The backup has successfully been restored');
$state.go('portainer.auth');
} catch (err) {
Notifications.error('Failure', err, 'Unable to restore the backup');
} finally {
Notifications.error('Failure', err, 'Unable to check for status');
await wait(2);
location.reload();
}
$scope.state.backupInProgress = false;
}
};
async function waitPortainerRestart() {
for (let i = 0; i < 10; i++) {
await wait(5);
try {
const status = await StatusService.status();
if (status && status.Version) {
return;
}
} catch (e) {
// pass
}
}
throw new Error('Timeout to wait for Portainer restarting');
}
},
]);
function wait(seconds = 0) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}

Loading…
Cancel
Save