2016-08-23 06:09:14 +00:00
|
|
|
angular.module('templates', [])
|
2016-11-17 12:50:46 +00:00
|
|
|
.controller('TemplatesController', ['$scope', '$q', '$state', '$filter', 'Config', 'Info', 'Container', 'ContainerHelper', 'Image', 'Volume', 'Network', 'Templates', 'TemplateHelper', 'Messages', 'Settings',
|
|
|
|
function ($scope, $q, $state, $filter, Config, Info, Container, ContainerHelper, Image, Volume, Network, Templates, TemplateHelper, Messages, Settings) {
|
2016-10-27 06:55:44 +00:00
|
|
|
$scope.state = {
|
|
|
|
selectedTemplate: null,
|
|
|
|
showAdvancedOptions: false
|
|
|
|
};
|
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-11-17 12:50:46 +00:00
|
|
|
$scope.pagination_count = Settings.pagination_count;
|
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
|
|
|
|
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') {
|
|
|
|
if ($scope.swarm && $scope.formValues.network.Scope === 'global') {
|
|
|
|
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-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
|
|
|
|
|
|
|
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 imageConfig = {
|
|
|
|
fromImage: template.image.split(':')[0],
|
|
|
|
tag: template.image.split(':')[1] ? template.image.split(':')[1] : 'latest'
|
|
|
|
};
|
|
|
|
var createVolumeQueries = prepareVolumeQueries(template, containerConfig);
|
|
|
|
$q.all(createVolumeQueries).then(function (d) {
|
|
|
|
pullImageAndCreateContainer(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-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) {
|
|
|
|
$scope.swarm = c.swarm;
|
2016-09-23 04:54:58 +00:00
|
|
|
Info.get({}, function(info) {
|
2016-09-23 06:02:03 +00:00
|
|
|
if ($scope.swarm && !_.startsWith(info.ServerVersion, 'swarm')) {
|
2016-09-23 04:54:58 +00:00
|
|
|
$scope.swarm_mode = true;
|
|
|
|
}
|
|
|
|
});
|
2016-08-31 06:03:41 +00:00
|
|
|
var containersToHideLabels = c.hiddenLabels;
|
|
|
|
Network.query({}, function (d) {
|
|
|
|
var networks = d;
|
|
|
|
if ($scope.swarm) {
|
|
|
|
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
|
|
|
}]);
|