2016-08-23 06:09:14 +00:00
|
|
|
angular.module('templates', [])
|
2017-07-12 07:51:51 +00:00
|
|
|
.controller('TemplatesController', ['$scope', '$q', '$state', '$stateParams', '$anchorScroll', '$filter', 'ContainerService', 'ContainerHelper', 'ImageService', 'NetworkService', 'TemplateService', 'TemplateHelper', 'VolumeService', 'Notifications', 'Pagination', 'ResourceControlService', 'Authentication', 'FormValidator',
|
|
|
|
function ($scope, $q, $state, $stateParams, $anchorScroll, $filter, ContainerService, ContainerHelper, ImageService, NetworkService, TemplateService, TemplateHelper, VolumeService, Notifications, Pagination, ResourceControlService, Authentication, FormValidator) {
|
2016-10-27 06:55:44 +00:00
|
|
|
$scope.state = {
|
|
|
|
selectedTemplate: null,
|
2017-01-24 01:28:40 +00:00
|
|
|
showAdvancedOptions: false,
|
2017-04-05 08:13:32 +00:00
|
|
|
hideDescriptions: $stateParams.hide_descriptions,
|
2017-05-23 18:56:10 +00:00
|
|
|
formValidationError: '',
|
2017-05-18 21:00:08 +00:00
|
|
|
filters: {
|
|
|
|
Categories: '!',
|
|
|
|
Platform: '!'
|
|
|
|
}
|
2016-10-27 06:55:44 +00:00
|
|
|
};
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
$scope.formValues = {
|
2017-05-18 21:00:08 +00:00
|
|
|
network: '',
|
2017-07-12 07:51:51 +00:00
|
|
|
name: '',
|
|
|
|
AccessControlData: new AccessControlFormData()
|
2016-08-31 06:03:41 +00:00
|
|
|
};
|
2016-08-23 06:09:14 +00:00
|
|
|
|
2017-02-13 05:16:14 +00:00
|
|
|
$scope.addVolume = function () {
|
|
|
|
$scope.state.selectedTemplate.Volumes.push({ containerPath: '', name: '', readOnly: false, type: 'auto' });
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeVolume = function(index) {
|
|
|
|
$scope.state.selectedTemplate.Volumes.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2016-10-27 06:55:44 +00:00
|
|
|
$scope.addPortBinding = function() {
|
2017-02-10 01:11:36 +00:00
|
|
|
$scope.state.selectedTemplate.Ports.push({ hostPort: '', containerPort: '', protocol: 'tcp' });
|
2016-10-27 06:55:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removePortBinding = function(index) {
|
2017-02-10 01:11:36 +00:00
|
|
|
$scope.state.selectedTemplate.Ports.splice(index, 1);
|
2016-10-27 06:55:44 +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;
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
$scope.createTemplate = function() {
|
|
|
|
$('#createContainerSpinner').show();
|
2017-05-23 18:56:10 +00:00
|
|
|
|
|
|
|
var userDetails = Authentication.getUserDetails();
|
2017-07-12 07:51:51 +00:00
|
|
|
var accessControlData = $scope.formValues.AccessControlData;
|
2017-05-23 18:56:10 +00:00
|
|
|
var isAdmin = userDetails.role === 1 ? true : false;
|
|
|
|
|
|
|
|
if (!validateForm(accessControlData, isAdmin)) {
|
|
|
|
$('#createContainerSpinner').hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
var template = $scope.state.selectedTemplate;
|
|
|
|
var templateConfiguration = createTemplateConfiguration(template);
|
2017-02-13 05:16:14 +00:00
|
|
|
var generatedVolumeCount = TemplateHelper.determineRequiredGeneratedVolumeCount(template.Volumes);
|
2017-05-23 18:56:10 +00:00
|
|
|
var generatedVolumeIds = [];
|
2017-02-13 05:16:14 +00:00
|
|
|
VolumeService.createXAutoGeneratedLocalVolumes(generatedVolumeCount)
|
2017-02-10 01:11:36 +00:00
|
|
|
.then(function success(data) {
|
2017-03-12 16:24:15 +00:00
|
|
|
var volumeResourceControlQueries = [];
|
2017-05-23 18:56:10 +00:00
|
|
|
angular.forEach(data, function (volume) {
|
|
|
|
var volumeId = volume.Id;
|
|
|
|
generatedVolumeIds.push(volumeId);
|
2017-03-13 09:09:34 +00:00
|
|
|
});
|
2017-05-23 18:56:10 +00:00
|
|
|
TemplateService.updateContainerConfigurationWithVolumes(templateConfiguration, template, data);
|
2017-06-29 14:05:39 +00:00
|
|
|
return ImageService.pullImage(template.Image, { URL: template.Registry }, true);
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.then(function success(data) {
|
2017-03-20 11:01:35 +00:00
|
|
|
return ContainerService.createAndStartContainer(templateConfiguration);
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.then(function success(data) {
|
2017-05-23 18:56:10 +00:00
|
|
|
var containerIdentifier = data.Id;
|
|
|
|
var userId = userDetails.ID;
|
|
|
|
return ResourceControlService.applyResourceControl('container', containerIdentifier, userId, accessControlData, generatedVolumeIds);
|
|
|
|
})
|
|
|
|
.then(function success() {
|
|
|
|
Notifications.success('Container successfully created');
|
|
|
|
$state.go('containers', {}, {reload: true});
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
2017-04-12 19:47:22 +00:00
|
|
|
Notifications.error('Failure', err, err.msg);
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.finally(function final() {
|
2016-08-31 06:03:41 +00:00
|
|
|
$('#createContainerSpinner').hide();
|
|
|
|
});
|
2017-02-10 01:11:36 +00:00
|
|
|
};
|
2016-08-23 06:09:14 +00:00
|
|
|
|
2017-05-18 21:00:08 +00:00
|
|
|
$scope.unselectTemplate = function() {
|
|
|
|
var currentTemplateIndex = $scope.state.selectedTemplate.index;
|
|
|
|
$('#template_' + currentTemplateIndex).toggleClass('template-container--selected');
|
|
|
|
$scope.state.selectedTemplate = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.selectTemplate = function(index, pos) {
|
|
|
|
if ($scope.state.selectedTemplate && $scope.state.selectedTemplate.index !== index) {
|
|
|
|
$scope.unselectTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
var templates = $filter('filter')($scope.templates, $scope.state.filters, true);
|
|
|
|
var template = templates[pos];
|
|
|
|
if (template === $scope.state.selectedTemplate) {
|
|
|
|
$scope.unselectTemplate();
|
2017-02-10 01:11:36 +00:00
|
|
|
} else {
|
2017-05-18 21:00:08 +00:00
|
|
|
selectTemplate(index, pos, templates);
|
2017-02-10 01:11:36 +00:00
|
|
|
}
|
|
|
|
};
|
2016-08-24 06:32:54 +00:00
|
|
|
|
2017-05-18 21:00:08 +00:00
|
|
|
function selectTemplate(index, pos, filteredTemplates) {
|
|
|
|
$('#template_' + index).toggleClass('template-container--selected');
|
|
|
|
var selectedTemplate = filteredTemplates[pos];
|
2017-02-10 01:11:36 +00:00
|
|
|
$scope.state.selectedTemplate = selectedTemplate;
|
2017-05-18 21:00:08 +00:00
|
|
|
|
2017-02-10 20:32:34 +00:00
|
|
|
if (selectedTemplate.Network) {
|
|
|
|
$scope.formValues.network = _.find($scope.availableNetworks, function(o) { return o.Name === selectedTemplate.Network; });
|
|
|
|
} else {
|
2017-05-18 21:00:08 +00:00
|
|
|
$scope.formValues.network = _.find($scope.availableNetworks, function(o) { return o.Name === 'bridge'; });
|
2017-02-10 20:32:34 +00:00
|
|
|
}
|
2017-05-18 21:00:08 +00:00
|
|
|
|
|
|
|
$anchorScroll('view-top');
|
2016-08-23 06:09:14 +00:00
|
|
|
}
|
2016-08-31 06:03:41 +00:00
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
function createTemplateConfiguration(template) {
|
|
|
|
var network = $scope.formValues.network;
|
|
|
|
var name = $scope.formValues.name;
|
|
|
|
var containerMapping = determineContainerMapping(network);
|
|
|
|
return TemplateService.createTemplateConfiguration(template, name, network, containerMapping);
|
2016-12-13 20:33:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
function determineContainerMapping(network) {
|
|
|
|
var endpointProvider = $scope.applicationState.endpoint.mode.provider;
|
|
|
|
var containerMapping = 'BY_CONTAINER_IP';
|
|
|
|
if (endpointProvider === 'DOCKER_SWARM' && network.Scope === 'global') {
|
|
|
|
containerMapping = 'BY_SWARM_CONTAINER_NAME';
|
2017-05-18 21:00:08 +00:00
|
|
|
} else if (network.Name !== 'bridge') {
|
2017-02-10 01:11:36 +00:00
|
|
|
containerMapping = 'BY_CONTAINER_NAME';
|
2016-08-31 06:03:41 +00:00
|
|
|
}
|
2017-03-19 18:07:22 +00:00
|
|
|
return containerMapping;
|
2016-08-23 06:09:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
function filterNetworksBasedOnProvider(networks) {
|
|
|
|
var endpointProvider = $scope.applicationState.endpoint.mode.provider;
|
|
|
|
if (endpointProvider === 'DOCKER_SWARM' || endpointProvider === 'DOCKER_SWARM_MODE') {
|
2017-03-31 20:12:58 +00:00
|
|
|
if (endpointProvider === 'DOCKER_SWARM') {
|
|
|
|
networks = NetworkService.filterGlobalNetworks(networks);
|
2017-04-05 08:13:32 +00:00
|
|
|
} else {
|
2017-03-31 20:12:58 +00:00
|
|
|
networks = NetworkService.filterSwarmModeAttachableNetworks(networks);
|
|
|
|
}
|
2017-02-10 01:11:36 +00:00
|
|
|
$scope.globalNetworkCount = networks.length;
|
|
|
|
NetworkService.addPredefinedLocalNetworks(networks);
|
2016-08-31 06:03:41 +00:00
|
|
|
}
|
2017-02-10 01:11:36 +00:00
|
|
|
return networks;
|
|
|
|
}
|
2016-08-23 06:09:14 +00:00
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
function initTemplates() {
|
2017-04-05 08:13:32 +00:00
|
|
|
var templatesKey = $stateParams.key;
|
2017-06-01 08:14:55 +00:00
|
|
|
$q.all({
|
|
|
|
templates: TemplateService.getTemplates(templatesKey),
|
|
|
|
containers: ContainerService.getContainers(0),
|
|
|
|
networks: NetworkService.networks(),
|
|
|
|
volumes: VolumeService.getVolumes()
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
$scope.templates = data.templates;
|
|
|
|
var availableCategories = [];
|
|
|
|
angular.forEach($scope.templates, function(template) {
|
|
|
|
availableCategories = availableCategories.concat(template.Categories);
|
2016-11-22 00:21:36 +00:00
|
|
|
});
|
2017-06-01 08:14:55 +00:00
|
|
|
$scope.availableCategories = _.sortBy(_.uniq(availableCategories));
|
|
|
|
$scope.runningContainers = data.containers;
|
|
|
|
$scope.availableNetworks = filterNetworksBasedOnProvider(data.networks);
|
|
|
|
$scope.availableVolumes = data.volumes.Volumes;
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
$scope.templates = [];
|
|
|
|
Notifications.error('Failure', err, 'An error occured during apps initialization.');
|
|
|
|
})
|
|
|
|
.finally(function final(){
|
|
|
|
$('#loadTemplatesSpinner').hide();
|
2016-08-31 06:03:41 +00:00
|
|
|
});
|
2016-08-23 06:09:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
initTemplates();
|
2016-08-23 06:09:14 +00:00
|
|
|
}]);
|