1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-02 04:15:28 +02:00
portainer/app/portainer/views/init/admin/initAdminController.js
Dmitry Salakhov fc9511dc97 UI
2021-04-07 13:21:58 +12:00

98 lines
3 KiB
JavaScript

angular.module('portainer.app').controller('InitAdminController', [
'$async',
'$scope',
'$state',
'Notifications',
'Authentication',
'StateManager',
'SettingsService',
'UserService',
'EndpointService',
'BackupService',
'StatusService',
function ($async, $scope, $state, Notifications, Authentication, StateManager, SettingsService, UserService, EndpointService, BackupService, StatusService) {
$scope.logo = StateManager.getState().application.logo;
$scope.formValues = {
Username: 'admin',
Password: '',
ConfirmPassword: '',
enableTelemetry: true,
};
$scope.state = {
actionInProgress: false,
showInitPassword: true,
showRestorePortainer: false,
};
$scope.togglePanel = function () {
$scope.state.showInitPassword = !$scope.state.showInitPassword;
$scope.state.showRestorePortainer = !$scope.state.showRestorePortainer;
};
$scope.createAdminUser = function () {
var username = $scope.formValues.Username;
var password = $scope.formValues.Password;
$scope.state.actionInProgress = true;
UserService.initAdministrator(username, password)
.then(function success() {
return Authentication.login(username, password);
})
.then(function success() {
return SettingsService.update({ enableTelemetry: $scope.formValues.enableTelemetry });
})
.then(() => {
return StateManager.initialize();
})
.then(function () {
return EndpointService.endpoints(0, 100);
})
.then(function success(data) {
if (data.value.length === 0) {
$state.go('portainer.init.endpoint');
} else {
$state.go('portainer.home');
}
})
.catch(function error(err) {
Notifications.error('Failure', err, 'Unable to create administrator user');
})
.finally(function final() {
$scope.state.actionInProgress = false;
});
};
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) {}
}
throw 'Timeout to wait for Portainer restarting';
}
$scope.uploadBackup = async function () {
$scope.state.backupInProgress = true;
const file = $scope.formValues.BackupFile;
const password = $scope.formValues.Password;
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 {
$scope.state.backupInProgress = false;
}
};
},
]);