2018-02-01 12:27:52 +00:00
|
|
|
angular.module('portainer.docker')
|
2018-02-27 07:19:21 +00:00
|
|
|
.controller('CreateStackController', ['$scope', '$state', 'StackService', 'Authentication', 'Notifications', 'FormValidator', 'ResourceControlService', 'FormHelper',
|
|
|
|
function ($scope, $state, StackService, Authentication, Notifications, FormValidator, ResourceControlService, FormHelper) {
|
2017-10-15 17:24:40 +00:00
|
|
|
|
|
|
|
$scope.formValues = {
|
|
|
|
Name: '',
|
2018-02-27 07:19:21 +00:00
|
|
|
StackFileContent: '',
|
2017-10-15 17:24:40 +00:00
|
|
|
StackFile: null,
|
|
|
|
RepositoryURL: '',
|
2017-11-01 09:30:02 +00:00
|
|
|
Env: [],
|
2017-10-15 17:24:40 +00:00
|
|
|
RepositoryPath: 'docker-compose.yml',
|
|
|
|
AccessControlData: new AccessControlFormData()
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.state = {
|
|
|
|
Method: 'editor',
|
2017-11-12 19:27:28 +00:00
|
|
|
formValidationError: '',
|
2017-11-12 21:39:12 +00:00
|
|
|
actionInProgress: false
|
2017-10-15 17:24:40 +00:00
|
|
|
};
|
|
|
|
|
2017-11-01 09:30:02 +00:00
|
|
|
$scope.addEnvironmentVariable = function() {
|
|
|
|
$scope.formValues.Env.push({ name: '', value: ''});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeEnvironmentVariable = function(index) {
|
|
|
|
$scope.formValues.Env.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2017-10-15 17:24:40 +00:00
|
|
|
function validateForm(accessControlData, isAdmin) {
|
|
|
|
$scope.state.formValidationError = '';
|
|
|
|
var error = '';
|
|
|
|
error = FormValidator.validateAccessControl(accessControlData, isAdmin);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
$scope.state.formValidationError = error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-27 07:19:21 +00:00
|
|
|
function createStack(name, method) {
|
2017-11-01 09:30:02 +00:00
|
|
|
var env = FormHelper.removeInvalidEnvVars($scope.formValues.Env);
|
2017-10-15 17:24:40 +00:00
|
|
|
|
|
|
|
if (method === 'editor') {
|
2018-02-27 07:19:21 +00:00
|
|
|
var stackFileContent = $scope.formValues.StackFileContent;
|
2017-11-01 09:30:02 +00:00
|
|
|
return StackService.createStackFromFileContent(name, stackFileContent, env);
|
2017-10-15 17:24:40 +00:00
|
|
|
} else if (method === 'upload') {
|
|
|
|
var stackFile = $scope.formValues.StackFile;
|
2017-11-01 09:30:02 +00:00
|
|
|
return StackService.createStackFromFileUpload(name, stackFile, env);
|
2017-10-15 17:24:40 +00:00
|
|
|
} else if (method === 'repository') {
|
|
|
|
var gitRepository = $scope.formValues.RepositoryURL;
|
|
|
|
var pathInRepository = $scope.formValues.RepositoryPath;
|
2017-11-01 09:30:02 +00:00
|
|
|
return StackService.createStackFromGitRepository(name, gitRepository, pathInRepository, env);
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.deployStack = function () {
|
|
|
|
var name = $scope.formValues.Name;
|
2018-02-27 07:19:21 +00:00
|
|
|
var method = $scope.state.Method;
|
2017-10-15 17:24:40 +00:00
|
|
|
|
|
|
|
var accessControlData = $scope.formValues.AccessControlData;
|
|
|
|
var userDetails = Authentication.getUserDetails();
|
2017-11-14 07:54:35 +00:00
|
|
|
var isAdmin = userDetails.role === 1;
|
2017-10-15 17:24:40 +00:00
|
|
|
var userId = userDetails.ID;
|
|
|
|
|
2018-02-27 07:19:21 +00:00
|
|
|
if (method === 'editor' && $scope.formValues.StackFileContent === '') {
|
|
|
|
$scope.state.formValidationError = 'Stack file content must not be empty';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-15 17:24:40 +00:00
|
|
|
if (!validateForm(accessControlData, isAdmin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-12 21:39:12 +00:00
|
|
|
$scope.state.actionInProgress = true;
|
2018-02-27 07:19:21 +00:00
|
|
|
createStack(name, method)
|
2017-10-15 17:24:40 +00:00
|
|
|
.then(function success(data) {
|
|
|
|
Notifications.success('Stack successfully deployed');
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.warning('Deployment error', err.err.data.err);
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
return ResourceControlService.applyResourceControl('stack', name, userId, accessControlData, []);
|
|
|
|
})
|
|
|
|
.then(function success() {
|
2018-02-01 12:27:52 +00:00
|
|
|
$state.go('docker.stacks');
|
2017-10-15 17:24:40 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to apply resource control on the stack');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
2017-11-12 21:39:12 +00:00
|
|
|
$scope.state.actionInProgress = false;
|
2017-10-15 17:24:40 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-02-27 07:19:21 +00:00
|
|
|
$scope.editorUpdate = function(cm) {
|
|
|
|
$scope.formValues.StackFileContent = cm.getValue();
|
2017-10-15 17:24:40 +00:00
|
|
|
};
|
|
|
|
}]);
|