2018-09-03 10:08:03 +00:00
|
|
|
angular.module('portainer.docker')
|
|
|
|
.controller('CreateServiceController', ['$q', '$scope', '$state', '$timeout', 'Service', 'ServiceHelper', 'ConfigService', 'ConfigHelper', 'SecretHelper', 'SecretService', 'VolumeService', 'NetworkService', 'ImageHelper', 'LabelHelper', 'Authentication', 'ResourceControlService', 'Notifications', 'FormValidator', 'PluginService', 'RegistryService', 'HttpRequestHelper', 'NodeService', 'SettingsService', 'WebhookService','EndpointProvider',
|
|
|
|
function ($q, $scope, $state, $timeout, Service, ServiceHelper, ConfigService, ConfigHelper, SecretHelper, SecretService, VolumeService, NetworkService, ImageHelper, LabelHelper, Authentication, ResourceControlService, Notifications, FormValidator, PluginService, RegistryService, HttpRequestHelper, NodeService, SettingsService, WebhookService,EndpointProvider) {
|
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: [],
|
2017-12-21 08:53:34 +00:00
|
|
|
HostsEntries: [],
|
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: [],
|
2017-12-05 19:12:54 +00:00
|
|
|
UpdateDelay: '0s',
|
2017-08-09 13:30:50 +00:00
|
|
|
UpdateOrder: 'stop-first',
|
2017-05-27 07:23:49 +00:00
|
|
|
FailureAction: 'pause',
|
2017-07-12 07:51:51 +00:00
|
|
|
Secrets: [],
|
2017-11-06 08:47:31 +00:00
|
|
|
Configs: [],
|
2017-09-20 06:32:19 +00:00
|
|
|
AccessControlData: new AccessControlFormData(),
|
|
|
|
CpuLimit: 0,
|
|
|
|
CpuReservation: 0,
|
|
|
|
MemoryLimit: 0,
|
|
|
|
MemoryReservation: 0,
|
|
|
|
MemoryLimitUnit: 'MB',
|
2017-12-07 20:05:45 +00:00
|
|
|
MemoryReservationUnit: 'MB',
|
|
|
|
RestartCondition: 'any',
|
|
|
|
RestartDelay: '5s',
|
|
|
|
RestartMaxAttempts: 0,
|
2017-12-22 09:05:31 +00:00
|
|
|
RestartWindow: '0s',
|
|
|
|
LogDriverName: '',
|
2018-09-03 10:08:03 +00:00
|
|
|
LogDriverOpts: [],
|
|
|
|
Webhook: false
|
2016-09-23 04:54:58 +00:00
|
|
|
};
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
$scope.state = {
|
2017-11-12 19:27:28 +00:00
|
|
|
formValidationError: '',
|
2017-11-12 21:39:12 +00:00
|
|
|
actionInProgress: false
|
2017-05-23 18:56:10 +00:00
|
|
|
};
|
|
|
|
|
2017-09-20 06:32:19 +00:00
|
|
|
$scope.refreshSlider = function () {
|
|
|
|
$timeout(function () {
|
|
|
|
$scope.$broadcast('rzSliderForceRender');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2017-12-21 08:53:34 +00:00
|
|
|
$scope.addHostsEntry = function() {
|
|
|
|
$scope.formValues.HostsEntries.push({});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeHostsEntry = function(index) {
|
|
|
|
$scope.formValues.HostsEntries.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
$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-11-06 08:47:31 +00:00
|
|
|
$scope.addConfig = function() {
|
|
|
|
$scope.formValues.Configs.push({});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeConfig = function(index) {
|
|
|
|
$scope.formValues.Configs.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2017-05-27 07:23:49 +00:00
|
|
|
$scope.addSecret = function() {
|
2017-11-20 13:44:23 +00:00
|
|
|
$scope.formValues.Secrets.push({ overrideTarget: false });
|
2017-05-27 07:23:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$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);
|
|
|
|
};
|
|
|
|
|
2018-08-22 15:33:06 +00:00
|
|
|
$scope.addLogDriverOpt = function() {
|
2017-12-22 09:05:31 +00:00
|
|
|
$scope.formValues.LogDriverOpts.push({ name: '', value: ''});
|
|
|
|
};
|
2018-02-01 12:27:52 +00:00
|
|
|
|
2017-12-22 09:05:31 +00:00
|
|
|
$scope.removeLogDriverOpt = function(index) {
|
|
|
|
$scope.formValues.LogDriverOpts.splice(index, 1);
|
2018-02-01 12:27:52 +00:00
|
|
|
};
|
2017-12-22 09:05:31 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-07 07:32:09 +00:00
|
|
|
function createMountObjectFromVolume(volumeObject, target, readonly) {
|
|
|
|
return {
|
|
|
|
Target: target,
|
|
|
|
Source: volumeObject.Id,
|
|
|
|
Type: 'volume',
|
|
|
|
ReadOnly: readonly,
|
|
|
|
VolumeOptions: {
|
|
|
|
Labels: volumeObject.Labels,
|
|
|
|
DriverConfig: {
|
|
|
|
Name: volumeObject.Driver,
|
|
|
|
Options: volumeObject.Options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-23 04:54:58 +00:00
|
|
|
function prepareVolumes(config, input) {
|
|
|
|
input.Volumes.forEach(function (volume) {
|
|
|
|
if (volume.Source && volume.Target) {
|
2017-11-07 07:32:09 +00:00
|
|
|
if (volume.Type !== 'volume') {
|
|
|
|
config.TaskTemplate.ContainerSpec.Mounts.push(volume);
|
|
|
|
} else {
|
|
|
|
var volumeObject = volume.Source;
|
|
|
|
var mount = createMountObjectFromVolume(volumeObject, volume.Target, volume.ReadOnly);
|
|
|
|
config.TaskTemplate.ContainerSpec.Mounts.push(mount);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-21 08:53:34 +00:00
|
|
|
function prepareHostsEntries(config, input) {
|
|
|
|
var hostsEntries = [];
|
2018-02-01 12:27:52 +00:00
|
|
|
if (input.HostsEntries) {
|
2017-12-21 08:53:34 +00:00
|
|
|
input.HostsEntries.forEach(function (host_ip) {
|
2018-02-01 12:27:52 +00:00
|
|
|
if (host_ip.value && host_ip.value.indexOf(':') && host_ip.value.split(':').length === 2) {
|
2017-12-21 08:53:34 +00:00
|
|
|
var keyVal = host_ip.value.split(':');
|
|
|
|
// Hosts file format, IP_address canonical_hostname
|
2018-02-01 12:27:52 +00:00
|
|
|
hostsEntries.push(keyVal[1] + ' ' + keyVal[0]);
|
|
|
|
}
|
2017-12-21 08:53:34 +00:00
|
|
|
});
|
|
|
|
if (hostsEntries.length > 0) {
|
|
|
|
config.TaskTemplate.ContainerSpec.Hosts = hostsEntries;
|
2018-02-01 12:27:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 08:53:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 20:31:22 +00:00
|
|
|
function prepareUpdateConfig(config, input) {
|
|
|
|
config.UpdateConfig = {
|
|
|
|
Parallelism: input.Parallelism || 0,
|
2017-12-07 20:05:45 +00:00
|
|
|
Delay: ServiceHelper.translateHumanDurationToNanos(input.UpdateDelay) || 0,
|
2017-08-09 13:30:50 +00:00
|
|
|
FailureAction: input.FailureAction,
|
|
|
|
Order: input.UpdateOrder
|
2016-12-25 20:31:22 +00:00
|
|
|
};
|
|
|
|
}
|
2017-07-10 07:33:09 +00:00
|
|
|
|
2017-12-07 20:05:45 +00:00
|
|
|
function prepareRestartPolicy(config, input) {
|
|
|
|
config.TaskTemplate.RestartPolicy = {
|
|
|
|
Condition: input.RestartCondition || 'any',
|
|
|
|
Delay: ServiceHelper.translateHumanDurationToNanos(input.RestartDelay) || 5000000000,
|
|
|
|
MaxAttempts: input.RestartMaxAttempts || 0,
|
|
|
|
Window: ServiceHelper.translateHumanDurationToNanos(input.RestartWindow) || 0
|
2018-02-01 12:27:52 +00:00
|
|
|
};
|
2017-12-07 20:05:45 +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-11-06 08:47:31 +00:00
|
|
|
function prepareConfigConfig(config, input) {
|
|
|
|
if (input.Configs) {
|
|
|
|
var configs = [];
|
|
|
|
angular.forEach(input.Configs, function(config) {
|
|
|
|
if (config.model) {
|
|
|
|
var s = ConfigHelper.configConfig(config.model);
|
|
|
|
s.File.Name = config.FileName || s.File.Name;
|
|
|
|
configs.push(s);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
config.TaskTemplate.ContainerSpec.Configs = configs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2017-11-20 13:44:23 +00:00
|
|
|
if (secret.overrideTarget && secret.target && secret.target !== '') {
|
|
|
|
s.File.Name = secret.target;
|
|
|
|
}
|
2017-06-29 06:37:05 +00:00
|
|
|
secrets.push(s);
|
2017-05-27 07:23:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
config.TaskTemplate.ContainerSpec.Secrets = secrets;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 06:32:19 +00:00
|
|
|
function prepareResourcesCpuConfig(config, input) {
|
|
|
|
// CPU Limit
|
|
|
|
if (input.CpuLimit > 0) {
|
|
|
|
config.TaskTemplate.Resources.Limits.NanoCPUs = input.CpuLimit * 1000000000;
|
|
|
|
}
|
|
|
|
// CPU Reservation
|
|
|
|
if (input.CpuReservation > 0) {
|
|
|
|
config.TaskTemplate.Resources.Reservations.NanoCPUs = input.CpuReservation * 1000000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareResourcesMemoryConfig(config, input) {
|
|
|
|
// Memory Limit - Round to 0.125
|
|
|
|
var memoryLimit = (Math.round(input.MemoryLimit * 8) / 8).toFixed(3);
|
|
|
|
memoryLimit *= 1024 * 1024;
|
|
|
|
if (input.MemoryLimitUnit === 'GB') {
|
|
|
|
memoryLimit *= 1024;
|
|
|
|
}
|
|
|
|
if (memoryLimit > 0) {
|
|
|
|
config.TaskTemplate.Resources.Limits.MemoryBytes = memoryLimit;
|
|
|
|
}
|
|
|
|
// Memory Resevation - Round to 0.125
|
|
|
|
var memoryReservation = (Math.round(input.MemoryReservation * 8) / 8).toFixed(3);
|
|
|
|
memoryReservation *= 1024 * 1024;
|
|
|
|
if (input.MemoryReservationUnit === 'GB') {
|
|
|
|
memoryReservation *= 1024;
|
|
|
|
}
|
|
|
|
if (memoryReservation > 0) {
|
|
|
|
config.TaskTemplate.Resources.Reservations.MemoryBytes = memoryReservation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 09:05:31 +00:00
|
|
|
function prepareLogDriverConfig(config, input) {
|
2018-02-01 12:27:52 +00:00
|
|
|
var logOpts = {};
|
2017-12-22 09:05:31 +00:00
|
|
|
if (input.LogDriverName) {
|
2018-02-01 12:27:52 +00:00
|
|
|
config.TaskTemplate.LogDriver = { Name: input.LogDriverName };
|
|
|
|
if (input.LogDriverName !== 'none') {
|
2017-12-22 09:05:31 +00:00
|
|
|
input.LogDriverOpts.forEach(function (opt) {
|
|
|
|
if (opt.name) {
|
|
|
|
logOpts[opt.name] = opt.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (Object.keys(logOpts).length !== 0 && logOpts.constructor === Object) {
|
|
|
|
config.TaskTemplate.LogDriver.Options = logOpts;
|
2018-02-01 12:27:52 +00:00
|
|
|
}
|
2017-12-22 09:05:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
},
|
2017-09-20 06:32:19 +00:00
|
|
|
Placement: {},
|
|
|
|
Resources: {
|
|
|
|
Limits: {},
|
|
|
|
Reservations: {}
|
|
|
|
}
|
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);
|
2017-12-21 08:53:34 +00:00
|
|
|
prepareHostsEntries(config, input);
|
2016-12-25 20:31:22 +00:00
|
|
|
prepareUpdateConfig(config, input);
|
2017-11-06 08:47:31 +00:00
|
|
|
prepareConfigConfig(config, input);
|
2017-05-27 07:23:49 +00:00
|
|
|
prepareSecretConfig(config, input);
|
2017-05-04 06:57:08 +00:00
|
|
|
preparePlacementConfig(config, input);
|
2017-09-20 06:32:19 +00:00
|
|
|
prepareResourcesCpuConfig(config, input);
|
|
|
|
prepareResourcesMemoryConfig(config, input);
|
2017-12-07 20:05:45 +00:00
|
|
|
prepareRestartPolicy(config, input);
|
2017-12-22 09:05:31 +00:00
|
|
|
prepareLogDriverConfig(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);
|
2018-09-03 10:08:03 +00:00
|
|
|
|
|
|
|
var serviceIdentifier;
|
2017-05-23 18:56:10 +00:00
|
|
|
Service.create(config).$promise
|
|
|
|
.then(function success(data) {
|
2018-09-03 10:08:03 +00:00
|
|
|
serviceIdentifier = data.ID;
|
|
|
|
return $q.when($scope.formValues.Webhook && WebhookService.createServiceWebhook(serviceIdentifier, EndpointProvider.endpointID()));
|
|
|
|
})
|
|
|
|
.then(function success() {
|
2017-05-23 18:56:10 +00:00
|
|
|
var userId = Authentication.getUserDetails().ID;
|
|
|
|
return ResourceControlService.applyResourceControl('service', serviceIdentifier, userId, accessControlData, []);
|
|
|
|
})
|
|
|
|
.then(function success() {
|
|
|
|
Notifications.success('Service successfully created');
|
2018-02-01 12:27:52 +00:00
|
|
|
$state.go('docker.services', {}, {reload: true});
|
2017-05-23 18:56:10 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to create service');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
2017-11-12 21:39:12 +00:00
|
|
|
$scope.state.actionInProgress = false;
|
2016-09-23 04:54:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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() {
|
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();
|
2017-11-14 07:54:35 +00:00
|
|
|
var isAdmin = userDetails.role === 1;
|
2017-05-23 18:56:10 +00:00
|
|
|
|
|
|
|
if (!validateForm(accessControlData, isAdmin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-12 21:39:12 +00:00
|
|
|
$scope.state.actionInProgress = true;
|
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-10-04 06:39:59 +00:00
|
|
|
function initSlidersMaxValuesBasedOnNodeData(nodes) {
|
|
|
|
var maxCpus = 0;
|
|
|
|
var maxMemory = 0;
|
|
|
|
for (var n in nodes) {
|
|
|
|
if (nodes[n].CPUs && nodes[n].CPUs > maxCpus) {
|
|
|
|
maxCpus = nodes[n].CPUs;
|
|
|
|
}
|
|
|
|
if (nodes[n].Memory && nodes[n].Memory > maxMemory) {
|
|
|
|
maxMemory = nodes[n].Memory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (maxCpus > 0) {
|
|
|
|
$scope.state.sliderMaxCpu = maxCpus / 1000000000;
|
|
|
|
} else {
|
|
|
|
$scope.state.sliderMaxCpu = 32;
|
|
|
|
}
|
|
|
|
if (maxMemory > 0) {
|
|
|
|
$scope.state.sliderMaxMemory = Math.floor(maxMemory / 1000 / 1000);
|
|
|
|
} else {
|
|
|
|
$scope.state.sliderMaxMemory = 32768;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
function initView() {
|
2017-07-24 09:59:09 +00:00
|
|
|
var apiVersion = $scope.applicationState.endpoint.apiVersion;
|
2017-08-11 07:46:55 +00:00
|
|
|
|
2017-05-27 07:23:49 +00:00
|
|
|
$q.all({
|
|
|
|
volumes: VolumeService.volumes(),
|
2018-06-11 13:13:19 +00:00
|
|
|
networks: NetworkService.networks(true, true, false),
|
2017-11-06 08:47:31 +00:00
|
|
|
secrets: apiVersion >= 1.25 ? SecretService.secrets() : [],
|
|
|
|
configs: apiVersion >= 1.30 ? ConfigService.configs() : [],
|
2017-09-26 03:36:51 +00:00
|
|
|
nodes: NodeService.nodes(),
|
2017-12-22 09:05:31 +00:00
|
|
|
settings: SettingsService.publicSettings(),
|
|
|
|
availableLoggingDrivers: PluginService.loggingPlugins(apiVersion < 1.25)
|
2017-05-27 07:23:49 +00:00
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
$scope.availableVolumes = data.volumes;
|
|
|
|
$scope.availableNetworks = data.networks;
|
|
|
|
$scope.availableSecrets = data.secrets;
|
2018-02-01 12:27:52 +00:00
|
|
|
$scope.availableConfigs = data.configs;
|
2017-12-22 09:05:31 +00:00
|
|
|
$scope.availableLoggingDrivers = data.availableLoggingDrivers;
|
2018-02-01 12:27:52 +00:00
|
|
|
initSlidersMaxValuesBasedOnNodeData(data.nodes);
|
2017-12-22 09:05:31 +00:00
|
|
|
$scope.allowBindMounts = data.settings.AllowBindMountsForRegularUsers;
|
2017-09-26 03:36:51 +00:00
|
|
|
var userDetails = Authentication.getUserDetails();
|
2017-11-14 07:54:35 +00:00
|
|
|
$scope.isAdmin = userDetails.role === 1;
|
2017-05-27 07:23:49 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to initialize view');
|
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
|
|
|
}]);
|