2017-03-12 16:24:15 +00:00
|
|
|
// @@OLD_SERVICE_CONTROLLER: this service should be rewritten to use services.
|
|
|
|
// See app/components/templates/templatesController.js as a reference.
|
2016-09-23 04:54:58 +00:00
|
|
|
angular.module('createService', [])
|
2017-07-12 07:51:51 +00:00
|
|
|
.controller('CreateServiceController', ['$q', '$scope', '$state', 'Service', 'ServiceHelper', 'SecretHelper', 'SecretService', 'VolumeService', 'NetworkService', 'ImageHelper', 'LabelHelper', 'Authentication', 'ResourceControlService', 'Notifications', 'FormValidator', 'RegistryService', 'HttpRequestHelper',
|
|
|
|
function ($q, $scope, $state, Service, ServiceHelper, SecretHelper, SecretService, VolumeService, NetworkService, ImageHelper, LabelHelper, Authentication, ResourceControlService, Notifications, FormValidator, RegistryService, HttpRequestHelper) {
|
2016-09-23 04:54:58 +00:00
|
|
|
|
|
|
|
$scope.formValues = {
|
|
|
|
Name: '',
|
|
|
|
Image: '',
|
2017-06-20 11:00:32 +00:00
|
|
|
Registry: {},
|
2016-09-23 04:54:58 +00:00
|
|
|
Mode: 'replicated',
|
|
|
|
Replicas: 1,
|
|
|
|
Command: '',
|
2016-12-25 09:14:26 +00:00
|
|
|
EntryPoint: '',
|
2016-09-23 04:54:58 +00:00
|
|
|
WorkingDir: '',
|
|
|
|
User: '',
|
|
|
|
Env: [],
|
2016-11-07 04:57:33 +00:00
|
|
|
Labels: [],
|
2016-11-25 06:21:06 +00:00
|
|
|
ContainerLabels: [],
|
2016-09-23 04:54:58 +00:00
|
|
|
Volumes: [],
|
|
|
|
Network: '',
|
|
|
|
ExtraNetworks: [],
|
2016-12-25 20:31:22 +00:00
|
|
|
Ports: [],
|
|
|
|
Parallelism: 1,
|
2017-05-04 06:57:08 +00:00
|
|
|
PlacementConstraints: [],
|
2017-07-10 07:33:09 +00:00
|
|
|
PlacementPreferences: [],
|
2016-12-25 20:31:22 +00:00
|
|
|
UpdateDelay: 0,
|
2017-05-27 07:23:49 +00:00
|
|
|
FailureAction: 'pause',
|
2017-07-12 07:51:51 +00:00
|
|
|
Secrets: [],
|
|
|
|
AccessControlData: new AccessControlFormData()
|
2016-09-23 04:54:58 +00:00
|
|
|
};
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
$scope.state = {
|
|
|
|
formValidationError: ''
|
|
|
|
};
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
$scope.addPortBinding = function() {
|
2017-05-04 07:43:20 +00:00
|
|
|
$scope.formValues.Ports.push({ PublishedPort: '', TargetPort: '', Protocol: 'tcp', PublishMode: 'ingress' });
|
2016-09-23 04:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removePortBinding = function(index) {
|
|
|
|
$scope.formValues.Ports.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.addExtraNetwork = function() {
|
|
|
|
$scope.formValues.ExtraNetworks.push({ Name: '' });
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeExtraNetwork = function(index) {
|
|
|
|
$scope.formValues.ExtraNetworks.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.addVolume = function() {
|
2017-03-27 12:44:39 +00:00
|
|
|
$scope.formValues.Volumes.push({ Source: '', Target: '', ReadOnly: false, Type: 'volume' });
|
2016-09-23 04:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeVolume = function(index) {
|
|
|
|
$scope.formValues.Volumes.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2017-05-27 07:23:49 +00:00
|
|
|
$scope.addSecret = function() {
|
|
|
|
$scope.formValues.Secrets.push({});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeSecret = function(index) {
|
|
|
|
$scope.formValues.Secrets.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
$scope.addEnvironmentVariable = function() {
|
|
|
|
$scope.formValues.Env.push({ name: '', value: ''});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeEnvironmentVariable = function(index) {
|
|
|
|
$scope.formValues.Env.splice(index, 1);
|
|
|
|
};
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2017-05-04 06:57:08 +00:00
|
|
|
$scope.addPlacementConstraint = function() {
|
|
|
|
$scope.formValues.PlacementConstraints.push({ key: '', operator: '==', value: '' });
|
|
|
|
};
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2017-05-04 06:57:08 +00:00
|
|
|
$scope.removePlacementConstraint = function(index) {
|
|
|
|
$scope.formValues.PlacementConstraints.splice(index, 1);
|
|
|
|
};
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2017-05-04 06:57:08 +00:00
|
|
|
$scope.addPlacementPreference = function() {
|
2017-07-10 07:33:09 +00:00
|
|
|
$scope.formValues.PlacementPreferences.push({ strategy: 'spread', value: '' });
|
2017-05-04 06:57:08 +00:00
|
|
|
};
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2017-05-04 06:57:08 +00:00
|
|
|
$scope.removePlacementPreference = function(index) {
|
|
|
|
$scope.formValues.PlacementPreferences.splice(index, 1);
|
|
|
|
};
|
2017-05-27 07:23:49 +00:00
|
|
|
|
2016-11-07 04:57:33 +00:00
|
|
|
$scope.addLabel = function() {
|
2017-07-10 07:33:09 +00:00
|
|
|
$scope.formValues.Labels.push({ key: '', value: ''});
|
2016-11-07 04:57:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeLabel = function(index) {
|
|
|
|
$scope.formValues.Labels.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2016-11-25 06:21:06 +00:00
|
|
|
$scope.addContainerLabel = function() {
|
2017-07-10 07:33:09 +00:00
|
|
|
$scope.formValues.ContainerLabels.push({ key: '', value: ''});
|
2016-11-25 06:21:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeContainerLabel = function(index) {
|
|
|
|
$scope.formValues.ContainerLabels.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
function prepareImageConfig(config, input) {
|
2017-06-20 11:00:32 +00:00
|
|
|
var imageConfig = ImageHelper.createImageConfigForContainer(input.Image, input.Registry.URL);
|
2016-12-13 20:33:24 +00:00
|
|
|
config.TaskTemplate.ContainerSpec.Image = imageConfig.fromImage + ':' + imageConfig.tag;
|
2016-09-23 04:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function preparePortsConfig(config, input) {
|
|
|
|
var ports = [];
|
|
|
|
input.Ports.forEach(function (binding) {
|
2017-03-30 10:00:16 +00:00
|
|
|
var port = {
|
2017-05-04 07:43:20 +00:00
|
|
|
Protocol: binding.Protocol,
|
|
|
|
PublishMode: binding.PublishMode
|
2017-03-30 10:00:16 +00:00
|
|
|
};
|
|
|
|
if (binding.TargetPort) {
|
|
|
|
port.TargetPort = +binding.TargetPort;
|
|
|
|
if (binding.PublishedPort) {
|
|
|
|
port.PublishedPort = +binding.PublishedPort;
|
|
|
|
}
|
|
|
|
ports.push(port);
|
2016-09-23 04:54:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
config.EndpointSpec.Ports = ports;
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareSchedulingConfig(config, input) {
|
|
|
|
if (input.Mode === 'replicated') {
|
|
|
|
config.Mode.Replicated = {
|
|
|
|
Replicas: input.Replicas
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
config.Mode.Global = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 09:14:26 +00:00
|
|
|
function commandToArray(cmd) {
|
2017-05-23 18:56:10 +00:00
|
|
|
var tokens = [].concat.apply([], cmd.split('\'').map(function(v,i) {
|
2016-12-25 09:14:26 +00:00
|
|
|
return i%2 ? v : v.split(' ');
|
|
|
|
})).filter(Boolean);
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
function prepareCommandConfig(config, input) {
|
2016-12-25 09:14:26 +00:00
|
|
|
if (input.EntryPoint) {
|
|
|
|
config.TaskTemplate.ContainerSpec.Command = commandToArray(input.EntryPoint);
|
|
|
|
}
|
2016-09-23 04:54:58 +00:00
|
|
|
if (input.Command) {
|
2016-12-25 09:14:26 +00:00
|
|
|
config.TaskTemplate.ContainerSpec.Args = commandToArray(input.Command);
|
2016-09-23 04:54:58 +00:00
|
|
|
}
|
|
|
|
if (input.User) {
|
|
|
|
config.TaskTemplate.ContainerSpec.User = input.User;
|
|
|
|
}
|
|
|
|
if (input.WorkingDir) {
|
|
|
|
config.TaskTemplate.ContainerSpec.Dir = input.WorkingDir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareEnvConfig(config, input) {
|
|
|
|
var env = [];
|
|
|
|
input.Env.forEach(function (v) {
|
2017-05-05 04:25:48 +00:00
|
|
|
if (v.name) {
|
2017-05-23 18:56:10 +00:00
|
|
|
env.push(v.name + '=' + v.value);
|
2016-09-23 04:54:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
config.TaskTemplate.ContainerSpec.Env = env;
|
|
|
|
}
|
|
|
|
|
2016-11-07 04:57:33 +00:00
|
|
|
function prepareLabelsConfig(config, input) {
|
2017-07-10 07:33:09 +00:00
|
|
|
config.Labels = LabelHelper.fromKeyValueToLabelHash(input.Labels);
|
|
|
|
config.TaskTemplate.ContainerSpec.Labels = LabelHelper.fromKeyValueToLabelHash(input.ContainerLabels);
|
2016-11-07 04:57:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
function prepareVolumes(config, input) {
|
|
|
|
input.Volumes.forEach(function (volume) {
|
|
|
|
if (volume.Source && volume.Target) {
|
2017-05-25 09:13:29 +00:00
|
|
|
config.TaskTemplate.ContainerSpec.Mounts.push(volume);
|
2016-09-23 04:54:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareNetworks(config, input) {
|
|
|
|
var networks = [];
|
|
|
|
if (input.Network) {
|
|
|
|
networks.push({ Target: input.Network });
|
|
|
|
}
|
|
|
|
input.ExtraNetworks.forEach(function (network) {
|
|
|
|
networks.push({ Target: network.Name });
|
|
|
|
});
|
|
|
|
config.Networks = _.uniqWith(networks, _.isEqual);
|
|
|
|
}
|
|
|
|
|
2016-12-25 20:31:22 +00:00
|
|
|
function prepareUpdateConfig(config, input) {
|
|
|
|
config.UpdateConfig = {
|
|
|
|
Parallelism: input.Parallelism || 0,
|
|
|
|
Delay: input.UpdateDelay || 0,
|
|
|
|
FailureAction: input.FailureAction
|
|
|
|
};
|
|
|
|
}
|
2017-07-10 07:33:09 +00:00
|
|
|
|
2017-05-04 06:57:08 +00:00
|
|
|
function preparePlacementConfig(config, input) {
|
|
|
|
config.TaskTemplate.Placement.Constraints = ServiceHelper.translateKeyValueToPlacementConstraints(input.PlacementConstraints);
|
2017-07-10 07:33:09 +00:00
|
|
|
config.TaskTemplate.Placement.Preferences = ServiceHelper.translateKeyValueToPlacementPreferences(input.PlacementPreferences);
|
2017-05-04 06:57:08 +00:00
|
|
|
}
|
2016-12-25 20:31:22 +00:00
|
|
|
|
2017-05-27 07:23:49 +00:00
|
|
|
function prepareSecretConfig(config, input) {
|
|
|
|
if (input.Secrets) {
|
|
|
|
var secrets = [];
|
|
|
|
angular.forEach(input.Secrets, function(secret) {
|
|
|
|
if (secret.model) {
|
2017-06-29 06:37:05 +00:00
|
|
|
var s = SecretHelper.secretConfig(secret.model);
|
|
|
|
s.File.Name = s.SecretName;
|
|
|
|
secrets.push(s);
|
2017-05-27 07:23:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
config.TaskTemplate.ContainerSpec.Secrets = secrets;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
function prepareConfiguration() {
|
|
|
|
var input = $scope.formValues;
|
|
|
|
var config = {
|
|
|
|
Name: input.Name,
|
|
|
|
TaskTemplate: {
|
|
|
|
ContainerSpec: {
|
|
|
|
Mounts: []
|
2017-05-04 06:57:08 +00:00
|
|
|
},
|
|
|
|
Placement: {}
|
2016-09-23 04:54:58 +00:00
|
|
|
},
|
|
|
|
Mode: {},
|
|
|
|
EndpointSpec: {}
|
|
|
|
};
|
|
|
|
prepareSchedulingConfig(config, input);
|
|
|
|
prepareImageConfig(config, input);
|
|
|
|
preparePortsConfig(config, input);
|
|
|
|
prepareCommandConfig(config, input);
|
|
|
|
prepareEnvConfig(config, input);
|
2016-11-07 04:57:33 +00:00
|
|
|
prepareLabelsConfig(config, input);
|
2016-09-23 04:54:58 +00:00
|
|
|
prepareVolumes(config, input);
|
|
|
|
prepareNetworks(config, input);
|
2016-12-25 20:31:22 +00:00
|
|
|
prepareUpdateConfig(config, input);
|
2017-05-27 07:23:49 +00:00
|
|
|
prepareSecretConfig(config, input);
|
2017-05-04 06:57:08 +00:00
|
|
|
preparePlacementConfig(config, input);
|
2016-09-23 04:54:58 +00:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
function createNewService(config, accessControlData) {
|
2017-06-20 11:00:32 +00:00
|
|
|
|
|
|
|
var registry = $scope.formValues.Registry;
|
|
|
|
var authenticationDetails = registry.Authentication ? RegistryService.encodedCredentials(registry) : '';
|
|
|
|
HttpRequestHelper.setRegistryAuthenticationHeader(authenticationDetails);
|
2017-05-23 18:56:10 +00:00
|
|
|
Service.create(config).$promise
|
|
|
|
.then(function success(data) {
|
|
|
|
var serviceIdentifier = data.ID;
|
|
|
|
var userId = Authentication.getUserDetails().ID;
|
|
|
|
return ResourceControlService.applyResourceControl('service', serviceIdentifier, userId, accessControlData, []);
|
|
|
|
})
|
|
|
|
.then(function success() {
|
|
|
|
Notifications.success('Service successfully created');
|
|
|
|
$state.go('services', {}, {reload: true});
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to create service');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
2016-09-23 04:54:58 +00:00
|
|
|
$('#createServiceSpinner').hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +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;
|
|
|
|
}
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
$scope.create = function createService() {
|
|
|
|
$('#createServiceSpinner').show();
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2017-07-12 07:51:51 +00:00
|
|
|
var accessControlData = $scope.formValues.AccessControlData;
|
2017-05-23 18:56:10 +00:00
|
|
|
var userDetails = Authentication.getUserDetails();
|
|
|
|
var isAdmin = userDetails.role === 1 ? true : false;
|
|
|
|
|
|
|
|
if (!validateForm(accessControlData, isAdmin)) {
|
|
|
|
$('#createServiceSpinner').hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
var config = prepareConfiguration();
|
2017-05-23 18:56:10 +00:00
|
|
|
createNewService(config, accessControlData);
|
2016-09-23 04:54:58 +00:00
|
|
|
};
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
function initView() {
|
2017-05-27 07:23:49 +00:00
|
|
|
$('#loadingViewSpinner').show();
|
2017-07-24 09:59:09 +00:00
|
|
|
var apiVersion = $scope.applicationState.endpoint.apiVersion;
|
2017-05-27 07:23:49 +00:00
|
|
|
$q.all({
|
|
|
|
volumes: VolumeService.volumes(),
|
|
|
|
networks: NetworkService.retrieveSwarmNetworks(),
|
2017-07-24 09:59:09 +00:00
|
|
|
secrets: apiVersion >= 1.25 ? SecretService.secrets() : []
|
2017-05-27 07:23:49 +00:00
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
$scope.availableVolumes = data.volumes;
|
|
|
|
$scope.availableNetworks = data.networks;
|
|
|
|
$scope.availableSecrets = data.secrets;
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to initialize view');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
|
|
|
$('#loadingViewSpinner').hide();
|
2016-09-23 04:54:58 +00:00
|
|
|
});
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initView();
|
2016-09-23 04:54:58 +00:00
|
|
|
}]);
|