2016-08-23 06:09:14 +00:00
|
|
|
angular.module('templates', [])
|
2017-03-12 16:24:15 +00:00
|
|
|
.controller('TemplatesController', ['$scope', '$q', '$state', '$anchorScroll', 'Config', 'ContainerService', 'ContainerHelper', 'ImageService', 'NetworkService', 'TemplateService', 'TemplateHelper', 'VolumeService', 'Messages', 'Pagination', 'ResourceControlService', 'Authentication',
|
|
|
|
function ($scope, $q, $state, $anchorScroll, Config, ContainerService, ContainerHelper, ImageService, NetworkService, TemplateService, TemplateHelper, VolumeService, Messages, Pagination, ResourceControlService, Authentication) {
|
2016-10-27 06:55:44 +00:00
|
|
|
$scope.state = {
|
|
|
|
selectedTemplate: null,
|
2017-01-24 01:28:40 +00:00
|
|
|
showAdvancedOptions: false,
|
|
|
|
pagination_count: Pagination.getPaginationCount('templates')
|
2016-10-27 06:55:44 +00:00
|
|
|
};
|
2016-08-31 06:03:41 +00:00
|
|
|
$scope.formValues = {
|
2017-03-12 16:24:15 +00:00
|
|
|
Ownership: $scope.applicationState.application.authentication ? 'private' : '',
|
2016-08-31 06:03:41 +00:00
|
|
|
network: "",
|
2016-10-27 06:55:44 +00:00
|
|
|
name: "",
|
2016-08-31 06:03:41 +00:00
|
|
|
};
|
2016-08-23 06:09:14 +00:00
|
|
|
|
2017-01-24 01:28:40 +00:00
|
|
|
$scope.changePaginationCount = function() {
|
|
|
|
Pagination.setPaginationCount('templates', $scope.state.pagination_count);
|
|
|
|
};
|
|
|
|
|
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-02-10 01:11:36 +00:00
|
|
|
$scope.createTemplate = function() {
|
|
|
|
$('#createContainerSpinner').show();
|
|
|
|
var template = $scope.state.selectedTemplate;
|
|
|
|
var templateConfiguration = createTemplateConfiguration(template);
|
2017-02-13 05:16:14 +00:00
|
|
|
var generatedVolumeCount = TemplateHelper.determineRequiredGeneratedVolumeCount(template.Volumes);
|
|
|
|
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 = [];
|
|
|
|
if ($scope.formValues.Ownership === 'private') {
|
|
|
|
angular.forEach(data, function (volume) {
|
|
|
|
volumeResourceControlQueries.push(ResourceControlService.setVolumeResourceControl(Authentication.getUserDetails().ID, volume.Name));
|
|
|
|
});
|
|
|
|
}
|
2017-02-10 01:11:36 +00:00
|
|
|
TemplateService.updateContainerConfigurationWithVolumes(templateConfiguration.container, template, data);
|
2017-03-12 16:24:15 +00:00
|
|
|
return $q.all(volumeResourceControlQueries).then(ImageService.pullImage(templateConfiguration.image));
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
return ContainerService.createAndStartContainer(templateConfiguration.container);
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
Messages.send('Container Started', data.Id);
|
2017-03-12 16:24:15 +00:00
|
|
|
if ($scope.formValues.Ownership === 'private') {
|
|
|
|
ResourceControlService.setContainerResourceControl(Authentication.getUserDetails().ID, data.Id)
|
|
|
|
.then(function success(data) {
|
|
|
|
$state.go('containers', {}, {reload: true});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$state.go('containers', {}, {reload: true});
|
|
|
|
}
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Messages.error('Failure', err, err.msg);
|
|
|
|
})
|
|
|
|
.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-02-10 01:11:36 +00:00
|
|
|
var selectedItem = -1;
|
|
|
|
$scope.selectTemplate = function(idx) {
|
|
|
|
$('#template_' + idx).toggleClass("container-template--selected");
|
|
|
|
if (selectedItem === idx) {
|
|
|
|
unselectTemplate();
|
|
|
|
} else {
|
|
|
|
selectTemplate(idx);
|
|
|
|
}
|
|
|
|
};
|
2016-08-24 06:32:54 +00:00
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
function unselectTemplate() {
|
|
|
|
selectedItem = -1;
|
|
|
|
$scope.state.selectedTemplate = null;
|
2016-10-27 06:55:44 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
function selectTemplate(idx) {
|
|
|
|
$('#template_' + selectedItem).toggleClass("container-template--selected");
|
|
|
|
selectedItem = idx;
|
|
|
|
var selectedTemplate = $scope.templates[idx];
|
|
|
|
$scope.state.selectedTemplate = selectedTemplate;
|
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 {
|
|
|
|
$scope.formValues.network = _.find($scope.availableNetworks, function(o) { return o.Name === "bridge"; });
|
|
|
|
}
|
2017-02-10 01:11:36 +00:00
|
|
|
$anchorScroll('selectedTemplate');
|
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';
|
|
|
|
} else if (network.Name !== "bridge") {
|
|
|
|
containerMapping = 'BY_CONTAINER_NAME';
|
2016-08-31 06:03:41 +00:00
|
|
|
}
|
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') {
|
|
|
|
networks = NetworkService.filterGlobalNetworks(networks);
|
|
|
|
$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-02-10 01:11:36 +00:00
|
|
|
Config.$promise.then(function (c) {
|
|
|
|
$q.all({
|
|
|
|
templates: TemplateService.getTemplates(),
|
|
|
|
containers: ContainerService.getContainers(0, c.hiddenLabels),
|
2017-02-13 05:16:14 +00:00
|
|
|
networks: NetworkService.getNetworks(),
|
|
|
|
volumes: VolumeService.getVolumes()
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
$scope.templates = data.templates;
|
|
|
|
$scope.runningContainers = data.containers;
|
|
|
|
$scope.availableNetworks = filterNetworksBasedOnProvider(data.networks);
|
2017-02-13 05:16:14 +00:00
|
|
|
$scope.availableVolumes = data.volumes.Volumes;
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
$scope.templates = [];
|
|
|
|
Messages.error("Failure", err, "An error occured during apps initialization.");
|
|
|
|
})
|
|
|
|
.finally(function final(){
|
|
|
|
$('#loadTemplatesSpinner').hide();
|
2016-11-22 00:21:36 +00:00
|
|
|
});
|
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
|
|
|
}]);
|