2017-01-31 23:26:29 +00:00
|
|
|
angular.module('portainer.helpers')
|
2017-02-10 01:11:36 +00:00
|
|
|
.factory('TemplateHelper', ['$filter', function TemplateHelperFactory($filter) {
|
2017-01-31 23:26:29 +00:00
|
|
|
'use strict';
|
2017-02-10 01:11:36 +00:00
|
|
|
var helper = {};
|
|
|
|
|
|
|
|
helper.getDefaultContainerConfiguration = function() {
|
|
|
|
return {
|
|
|
|
Env: [],
|
|
|
|
OpenStdin: false,
|
|
|
|
Tty: false,
|
|
|
|
ExposedPorts: {},
|
|
|
|
HostConfig: {
|
|
|
|
RestartPolicy: {
|
|
|
|
Name: 'no'
|
|
|
|
},
|
|
|
|
PortBindings: {},
|
|
|
|
Binds: [],
|
|
|
|
Privileged: false
|
|
|
|
},
|
|
|
|
Volumes: {}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
helper.portArrayToPortConfiguration = function(ports) {
|
|
|
|
var portConfiguration = {
|
|
|
|
bindings: {},
|
|
|
|
exposedPorts: {}
|
|
|
|
};
|
|
|
|
ports.forEach(function (p) {
|
|
|
|
if (p.containerPort) {
|
2017-05-18 21:00:08 +00:00
|
|
|
var key = p.containerPort + '/' + p.protocol;
|
2017-02-10 01:11:36 +00:00
|
|
|
var binding = {};
|
|
|
|
if (p.hostPort) {
|
|
|
|
binding.HostPort = p.hostPort;
|
|
|
|
if (p.hostPort.indexOf(':') > -1) {
|
|
|
|
var hostAndPort = p.hostPort.split(':');
|
|
|
|
binding.HostIp = hostAndPort[0];
|
|
|
|
binding.HostPort = hostAndPort[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
portConfiguration.bindings[key] = [binding];
|
|
|
|
portConfiguration.exposedPorts[key] = {};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return portConfiguration;
|
|
|
|
};
|
|
|
|
|
|
|
|
helper.EnvToStringArray = function(templateEnvironment, containerMapping) {
|
|
|
|
var env = [];
|
|
|
|
templateEnvironment.forEach(function(envvar) {
|
|
|
|
if (envvar.value || envvar.set) {
|
|
|
|
var value = envvar.set ? envvar.set : envvar.value;
|
|
|
|
if (envvar.type && envvar.type === 'container') {
|
|
|
|
if (containerMapping === 'BY_CONTAINER_IP') {
|
|
|
|
var container = envvar.value;
|
|
|
|
value = container.NetworkSettings.Networks[Object.keys(container.NetworkSettings.Networks)[0]].IPAddress;
|
|
|
|
} else if (containerMapping === 'BY_CONTAINER_NAME') {
|
|
|
|
value = $filter('containername')(envvar.value);
|
|
|
|
} else if (containerMapping === 'BY_SWARM_CONTAINER_NAME') {
|
|
|
|
value = $filter('swarmcontainername')(envvar.value);
|
|
|
|
}
|
2017-01-31 23:26:29 +00:00
|
|
|
}
|
2017-05-18 21:00:08 +00:00
|
|
|
env.push(envvar.name + '=' + value);
|
2017-02-10 01:11:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return env;
|
2017-01-31 23:26:29 +00:00
|
|
|
};
|
2017-02-10 01:11:36 +00:00
|
|
|
|
2017-05-18 20:49:55 +00:00
|
|
|
helper.getConsoleConfiguration = function(interactiveFlag) {
|
|
|
|
var consoleConfiguration = {
|
|
|
|
openStdin: false,
|
|
|
|
tty: false
|
|
|
|
};
|
|
|
|
if (interactiveFlag === true) {
|
|
|
|
consoleConfiguration.openStdin = true;
|
|
|
|
consoleConfiguration.tty = true;
|
|
|
|
}
|
|
|
|
return consoleConfiguration;
|
|
|
|
};
|
|
|
|
|
2017-02-13 05:16:14 +00:00
|
|
|
helper.createVolumeBindings = function(volumes, generatedVolumesPile) {
|
|
|
|
volumes.forEach(function (volume) {
|
|
|
|
if (volume.containerPath) {
|
|
|
|
var binding;
|
|
|
|
if (volume.type === 'auto') {
|
|
|
|
binding = generatedVolumesPile.pop().Name + ':' + volume.containerPath;
|
|
|
|
} else if (volume.type !== 'auto' && volume.name) {
|
|
|
|
binding = volume.name + ':' + volume.containerPath;
|
|
|
|
}
|
|
|
|
if (volume.readOnly) {
|
|
|
|
binding += ':ro';
|
|
|
|
}
|
|
|
|
volume.binding = binding;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
helper.determineRequiredGeneratedVolumeCount = function(volumes) {
|
|
|
|
var count = 0;
|
|
|
|
volumes.forEach(function (volume) {
|
|
|
|
if (volume.type === 'auto') {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return count;
|
|
|
|
};
|
|
|
|
|
2017-04-05 08:13:32 +00:00
|
|
|
helper.filterLinuxServerIOTemplates = function(templates) {
|
|
|
|
return templates.filter(function f(template) {
|
|
|
|
var valid = false;
|
2017-05-18 21:00:08 +00:00
|
|
|
if (template.Categories) {
|
|
|
|
angular.forEach(template.Categories, function(category) {
|
2017-04-05 08:13:32 +00:00
|
|
|
if (_.startsWith(category, 'Network')) {
|
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}).map(function(template, idx) {
|
|
|
|
template.index = idx;
|
|
|
|
return template;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
return helper;
|
2017-01-31 23:26:29 +00:00
|
|
|
}]);
|