2016-08-23 06:09:14 +00:00
|
|
|
angular.module('templates', [])
|
2017-01-24 01:28:40 +00:00
|
|
|
.controller('TemplatesController', ['$scope', '$q', '$state', '$filter', '$anchorScroll', 'Config', 'Info', 'Container', 'ContainerHelper', 'Image', 'ImageHelper', 'Volume', 'Network', 'Templates', 'TemplateHelper', 'Messages', 'Pagination',
|
|
|
|
function ($scope, $q, $state, $filter, $anchorScroll, Config, Info, Container, ContainerHelper, Image, ImageHelper, Volume, Network, Templates, TemplateHelper, Messages, Pagination) {
|
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 = {
|
|
|
|
network: "",
|
2016-10-27 06:55:44 +00:00
|
|
|
name: "",
|
|
|
|
ports: []
|
2016-08-31 06:03:41 +00:00
|
|
|
};
|
2016-08-23 06:09:14 +00:00
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
var selectedItem = -1;
|
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);
|
|
|
|
};
|
|
|
|
|
2016-10-27 06:55:44 +00:00
|
|
|
$scope.addPortBinding = function() {
|
|
|
|
$scope.formValues.ports.push({ hostPort: '', containerPort: '', protocol: 'tcp' });
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removePortBinding = function(index) {
|
|
|
|
$scope.formValues.ports.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
// TODO: centralize, already present in createContainerController
|
|
|
|
function createContainer(config) {
|
|
|
|
Container.create(config, function (d) {
|
2016-09-02 03:25:20 +00:00
|
|
|
if (d.message) {
|
|
|
|
$('#createContainerSpinner').hide();
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error('Error', {}, d.message);
|
2016-09-02 03:25:20 +00:00
|
|
|
} else {
|
2016-09-02 01:51:49 +00:00
|
|
|
Container.start({id: d.Id}, {}, function (cd) {
|
2016-09-02 03:25:20 +00:00
|
|
|
if (cd.message) {
|
|
|
|
$('#createContainerSpinner').hide();
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error('Error', {}, cd.message);
|
2016-09-02 03:25:20 +00:00
|
|
|
} else {
|
|
|
|
$('#createContainerSpinner').hide();
|
|
|
|
Messages.send('Container Started', d.Id);
|
|
|
|
$state.go('containers', {}, {reload: true});
|
|
|
|
}
|
2016-08-31 06:03:41 +00:00
|
|
|
}, function (e) {
|
|
|
|
$('#createContainerSpinner').hide();
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error("Failure", e, 'Unable to start container');
|
2016-08-31 06:03:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}, function (e) {
|
2016-08-23 06:09:14 +00:00
|
|
|
$('#createContainerSpinner').hide();
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error("Failure", e, 'Unable to create container');
|
2016-08-31 06:03:41 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-23 06:09:14 +00:00
|
|
|
|
2016-09-02 03:25:20 +00:00
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
// TODO: centralize, already present in createContainerController
|
|
|
|
function pullImageAndCreateContainer(imageConfig, containerConfig) {
|
|
|
|
Image.create(imageConfig, function (data) {
|
2016-08-23 06:09:14 +00:00
|
|
|
var err = data.length > 0 && data[data.length - 1].hasOwnProperty('error');
|
|
|
|
if (err) {
|
|
|
|
var detail = data[data.length - 1];
|
|
|
|
$('#createContainerSpinner').hide();
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error("Error", {}, detail.error);
|
2016-08-23 06:09:14 +00:00
|
|
|
} else {
|
|
|
|
createContainer(containerConfig);
|
|
|
|
}
|
2016-08-31 06:03:41 +00:00
|
|
|
}, function (e) {
|
|
|
|
$('#createContainerSpinner').hide();
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error("Failure", e, "Unable to pull image");
|
2016-08-31 06:03:41 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-23 06:09:14 +00:00
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
function getInitialConfiguration() {
|
|
|
|
return {
|
|
|
|
Env: [],
|
|
|
|
OpenStdin: false,
|
|
|
|
Tty: false,
|
|
|
|
ExposedPorts: {},
|
|
|
|
HostConfig: {
|
|
|
|
RestartPolicy: {
|
|
|
|
Name: 'no'
|
|
|
|
},
|
|
|
|
PortBindings: {},
|
|
|
|
Binds: [],
|
|
|
|
NetworkMode: $scope.formValues.network.Name,
|
|
|
|
Privileged: false
|
2016-08-23 06:09:14 +00:00
|
|
|
},
|
2016-08-31 06:03:41 +00:00
|
|
|
Volumes: {},
|
|
|
|
name: $scope.formValues.name
|
|
|
|
};
|
|
|
|
}
|
2016-08-24 06:32:54 +00:00
|
|
|
|
2016-10-27 06:55:44 +00:00
|
|
|
function preparePortBindings(config, ports) {
|
|
|
|
var bindings = {};
|
|
|
|
ports.forEach(function (portBinding) {
|
|
|
|
if (portBinding.containerPort) {
|
|
|
|
var key = portBinding.containerPort + "/" + portBinding.protocol;
|
|
|
|
var binding = {};
|
|
|
|
if (portBinding.hostPort && portBinding.hostPort.indexOf(':') > -1) {
|
|
|
|
var hostAndPort = portBinding.hostPort.split(':');
|
|
|
|
binding.HostIp = hostAndPort[0];
|
|
|
|
binding.HostPort = hostAndPort[1];
|
|
|
|
} else {
|
|
|
|
binding.HostPort = portBinding.hostPort;
|
|
|
|
}
|
|
|
|
bindings[key] = [binding];
|
|
|
|
config.ExposedPorts[key] = {};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
config.HostConfig.PortBindings = bindings;
|
|
|
|
}
|
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
function createConfigFromTemplate(template) {
|
|
|
|
var containerConfig = getInitialConfiguration();
|
|
|
|
containerConfig.Image = template.image;
|
|
|
|
if (template.env) {
|
|
|
|
template.env.forEach(function (v) {
|
|
|
|
if (v.value || v.set) {
|
|
|
|
var val;
|
|
|
|
if (v.type && v.type === 'container') {
|
2017-01-22 23:14:34 +00:00
|
|
|
if ($scope.applicationState.endpoint.mode.provider === 'DOCKER_SWARM' && $scope.formValues.network.Scope === 'global') {
|
2016-08-31 06:03:41 +00:00
|
|
|
val = $filter('swarmcontainername')(v.value);
|
|
|
|
} else {
|
|
|
|
var container = v.value;
|
|
|
|
val = container.NetworkSettings.Networks[Object.keys(container.NetworkSettings.Networks)[0]].IPAddress;
|
|
|
|
}
|
2016-08-24 06:32:54 +00:00
|
|
|
} else {
|
2016-08-31 06:03:41 +00:00
|
|
|
val = v.set ? v.set : v.value;
|
2016-08-24 06:32:54 +00:00
|
|
|
}
|
2016-08-31 06:03:41 +00:00
|
|
|
containerConfig.Env.push(v.name + "=" + val);
|
2016-08-24 06:32:54 +00:00
|
|
|
}
|
2016-08-31 06:03:41 +00:00
|
|
|
});
|
|
|
|
}
|
2016-10-27 06:55:44 +00:00
|
|
|
preparePortBindings(containerConfig, $scope.formValues.ports);
|
2016-12-13 20:33:24 +00:00
|
|
|
prepareImageConfig(containerConfig, template);
|
2016-08-31 06:03:41 +00:00
|
|
|
return containerConfig;
|
2016-08-23 06:09:14 +00:00
|
|
|
}
|
2016-08-31 06:03:41 +00:00
|
|
|
|
2016-12-13 20:33:24 +00:00
|
|
|
function prepareImageConfig(config, template) {
|
2016-12-31 00:25:42 +00:00
|
|
|
var image = template.image;
|
2016-12-16 00:39:24 +00:00
|
|
|
var registry = template.registry || '';
|
2016-12-13 20:33:24 +00:00
|
|
|
var imageConfig = ImageHelper.createImageConfigForContainer(image, registry);
|
|
|
|
config.Image = imageConfig.fromImage + ':' + imageConfig.tag;
|
|
|
|
$scope.imageConfig = imageConfig;
|
|
|
|
}
|
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
function prepareVolumeQueries(template, containerConfig) {
|
|
|
|
var volumeQueries = [];
|
|
|
|
if (template.volumes) {
|
|
|
|
template.volumes.forEach(function (vol) {
|
|
|
|
volumeQueries.push(
|
|
|
|
Volume.create({}, function (d) {
|
2016-09-02 03:25:20 +00:00
|
|
|
if (d.message) {
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error("Unable to create volume", {}, d.message);
|
2016-09-02 03:25:20 +00:00
|
|
|
} else {
|
2016-08-31 06:03:41 +00:00
|
|
|
Messages.send("Volume created", d.Name);
|
|
|
|
containerConfig.Volumes[vol] = {};
|
|
|
|
containerConfig.HostConfig.Binds.push(d.Name + ':' + vol);
|
|
|
|
}
|
|
|
|
}, function (e) {
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error("Failure", e, "Unable to create volume");
|
2016-08-31 06:03:41 +00:00
|
|
|
}).$promise
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return volumeQueries;
|
2016-08-23 06:09:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
$scope.createTemplate = function() {
|
|
|
|
$('#createContainerSpinner').show();
|
2016-10-27 06:55:44 +00:00
|
|
|
var template = $scope.state.selectedTemplate;
|
2016-08-31 06:03:41 +00:00
|
|
|
var containerConfig = createConfigFromTemplate(template);
|
|
|
|
var createVolumeQueries = prepareVolumeQueries(template, containerConfig);
|
|
|
|
$q.all(createVolumeQueries).then(function (d) {
|
2016-12-13 20:33:24 +00:00
|
|
|
pullImageAndCreateContainer($scope.imageConfig, containerConfig);
|
2016-08-23 06:09:14 +00:00
|
|
|
});
|
2016-08-31 06:03:41 +00:00
|
|
|
};
|
2016-08-23 06:09:14 +00:00
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
$scope.selectTemplate = function(id) {
|
|
|
|
$('#template_' + id).toggleClass("container-template--selected");
|
|
|
|
if (selectedItem === id) {
|
|
|
|
selectedItem = -1;
|
2016-10-27 06:55:44 +00:00
|
|
|
$scope.state.selectedTemplate = null;
|
2016-08-31 06:03:41 +00:00
|
|
|
} else {
|
|
|
|
$('#template_' + selectedItem).toggleClass("container-template--selected");
|
|
|
|
selectedItem = id;
|
2016-10-27 06:55:44 +00:00
|
|
|
var selectedTemplate = $scope.templates[id];
|
|
|
|
$scope.state.selectedTemplate = selectedTemplate;
|
|
|
|
$scope.formValues.ports = selectedTemplate.ports ? TemplateHelper.getPortBindings(selectedTemplate.ports) : [];
|
2016-12-13 20:25:23 +00:00
|
|
|
$anchorScroll('selectedTemplate');
|
2016-08-31 06:03:41 +00:00
|
|
|
}
|
2016-08-23 06:09:14 +00:00
|
|
|
};
|
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
function initTemplates() {
|
|
|
|
Templates.get(function (data) {
|
2016-11-22 00:21:36 +00:00
|
|
|
$scope.templates = data.map(function(tpl,index){
|
|
|
|
tpl.index = index;
|
|
|
|
return tpl;
|
|
|
|
});
|
2016-08-31 06:03:41 +00:00
|
|
|
$('#loadTemplatesSpinner').hide();
|
|
|
|
}, function (e) {
|
|
|
|
$('#loadTemplatesSpinner').hide();
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error("Failure", e, "Unable to retrieve apps list");
|
2016-10-08 21:49:24 +00:00
|
|
|
$scope.templates = [];
|
2016-08-31 06:03:41 +00:00
|
|
|
});
|
2016-08-23 06:09:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 06:03:41 +00:00
|
|
|
Config.$promise.then(function (c) {
|
|
|
|
var containersToHideLabels = c.hiddenLabels;
|
|
|
|
Network.query({}, function (d) {
|
|
|
|
var networks = d;
|
2017-01-22 23:14:34 +00:00
|
|
|
if ($scope.applicationState.endpoint.mode.provider === 'DOCKER_SWARM' || $scope.applicationState.endpoint.mode.provider === 'DOCKER_SWARM_MODE') {
|
2016-08-31 06:03:41 +00:00
|
|
|
networks = d.filter(function (network) {
|
|
|
|
if (network.Scope === 'global') {
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$scope.globalNetworkCount = networks.length;
|
|
|
|
networks.push({Scope: "local", Name: "bridge"});
|
|
|
|
networks.push({Scope: "local", Name: "host"});
|
|
|
|
networks.push({Scope: "local", Name: "none"});
|
|
|
|
} else {
|
|
|
|
$scope.formValues.network = _.find(networks, function(o) { return o.Name === "bridge"; });
|
2016-08-24 08:26:51 +00:00
|
|
|
}
|
2016-08-31 06:03:41 +00:00
|
|
|
$scope.availableNetworks = networks;
|
|
|
|
}, function (e) {
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error("Failure", e, "Unable to retrieve networks");
|
2016-08-24 08:26:51 +00:00
|
|
|
});
|
2016-08-31 06:03:41 +00:00
|
|
|
Container.query({all: 0}, function (d) {
|
|
|
|
var containers = d;
|
|
|
|
if (containersToHideLabels) {
|
|
|
|
containers = ContainerHelper.hideContainers(d, containersToHideLabels);
|
|
|
|
}
|
|
|
|
$scope.runningContainers = containers;
|
|
|
|
}, function (e) {
|
2016-09-02 05:51:41 +00:00
|
|
|
Messages.error("Failure", e, "Unable to retrieve running containers");
|
2016-08-31 06:03:41 +00:00
|
|
|
});
|
|
|
|
initTemplates();
|
2016-08-24 06:32:54 +00:00
|
|
|
});
|
2016-08-23 06:09:14 +00:00
|
|
|
}]);
|