2017-02-10 01:11:36 +00:00
|
|
|
angular.module('portainer.services')
|
2017-02-10 05:11:00 +00:00
|
|
|
.factory('TemplateService', ['$q', 'Template', 'TemplateHelper', 'ImageHelper', 'ContainerHelper', function TemplateServiceFactory($q, Template, TemplateHelper, ImageHelper, ContainerHelper) {
|
2017-02-10 01:11:36 +00:00
|
|
|
'use strict';
|
|
|
|
var service = {};
|
|
|
|
|
2017-04-05 08:13:32 +00:00
|
|
|
service.getTemplates = function(key) {
|
2017-02-10 01:11:36 +00:00
|
|
|
var deferred = $q.defer();
|
2017-04-05 08:13:32 +00:00
|
|
|
Template.get({key: key}).$promise
|
2017-02-10 01:11:36 +00:00
|
|
|
.then(function success(data) {
|
|
|
|
var templates = data.map(function (tpl, idx) {
|
2017-05-18 21:00:08 +00:00
|
|
|
var template;
|
|
|
|
if (key === 'linuxserver.io') {
|
|
|
|
template = new TemplateLSIOViewModel(tpl);
|
|
|
|
} else {
|
|
|
|
template = new TemplateViewModel(tpl);
|
|
|
|
}
|
2017-02-10 01:11:36 +00:00
|
|
|
template.index = idx;
|
|
|
|
return template;
|
|
|
|
});
|
2017-05-18 21:00:08 +00:00
|
|
|
if (key === 'linuxserver.io') {
|
|
|
|
templates = TemplateHelper.filterLinuxServerIOTemplates(templates);
|
|
|
|
}
|
2017-02-10 01:11:36 +00:00
|
|
|
deferred.resolve(templates);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve templates', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.createTemplateConfiguration = function(template, containerName, network, containerMapping) {
|
2017-03-20 11:01:35 +00:00
|
|
|
var imageConfiguration = ImageHelper.createImageConfigForContainer(template.Image, template.Registry);
|
2017-02-10 01:11:36 +00:00
|
|
|
var containerConfiguration = service.createContainerConfiguration(template, containerName, network, containerMapping);
|
|
|
|
containerConfiguration.Image = imageConfiguration.fromImage + ':' + imageConfiguration.tag;
|
2017-03-20 11:01:35 +00:00
|
|
|
return containerConfiguration;
|
2017-02-10 01:11:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
service.createContainerConfiguration = function(template, containerName, network, containerMapping) {
|
|
|
|
var configuration = TemplateHelper.getDefaultContainerConfiguration();
|
|
|
|
configuration.HostConfig.NetworkMode = network.Name;
|
2017-03-10 14:43:57 +00:00
|
|
|
configuration.HostConfig.Privileged = template.Privileged;
|
2017-02-10 01:11:36 +00:00
|
|
|
configuration.name = containerName;
|
|
|
|
configuration.Image = template.Image;
|
2017-02-10 05:11:00 +00:00
|
|
|
configuration.Env = TemplateHelper.EnvToStringArray(template.Env, containerMapping);
|
|
|
|
configuration.Cmd = ContainerHelper.commandStringToArray(template.Command);
|
2017-02-10 01:11:36 +00:00
|
|
|
var portConfiguration = TemplateHelper.portArrayToPortConfiguration(template.Ports);
|
|
|
|
configuration.HostConfig.PortBindings = portConfiguration.bindings;
|
|
|
|
configuration.ExposedPorts = portConfiguration.exposedPorts;
|
2017-05-18 20:49:55 +00:00
|
|
|
var consoleConfiguration = TemplateHelper.getConsoleConfiguration(template.Interactive);
|
|
|
|
configuration.OpenStdin = consoleConfiguration.openStdin;
|
|
|
|
configuration.Tty = consoleConfiguration.tty;
|
2017-02-10 01:11:36 +00:00
|
|
|
return configuration;
|
|
|
|
};
|
|
|
|
|
2017-02-13 05:16:14 +00:00
|
|
|
service.updateContainerConfigurationWithVolumes = function(configuration, template, generatedVolumesPile) {
|
|
|
|
var volumes = template.Volumes;
|
|
|
|
TemplateHelper.createVolumeBindings(volumes, generatedVolumesPile);
|
|
|
|
volumes.forEach(function (volume) {
|
|
|
|
if (volume.binding) {
|
|
|
|
configuration.Volumes[volume.containerPath] = {};
|
|
|
|
configuration.HostConfig.Binds.push(volume.binding);
|
|
|
|
}
|
2017-02-10 01:11:36 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return service;
|
|
|
|
}]);
|