2019-03-21 07:46:49 +02:00
|
|
|
import _ from 'lodash-es';
|
|
|
|
|
2018-02-01 13:27:52 +01:00
|
|
|
angular.module('portainer.app').factory('TemplateHelper', [
|
2018-08-22 18:33:06 +03:00
|
|
|
function TemplateHelperFactory() {
|
2017-02-01 12:26:29 +13:00
|
|
|
'use strict';
|
2017-02-10 14:11:36 +13:00
|
|
|
var helper = {};
|
|
|
|
|
|
|
|
helper.getDefaultContainerConfiguration = function () {
|
|
|
|
return {
|
|
|
|
Env: [],
|
|
|
|
OpenStdin: false,
|
|
|
|
Tty: false,
|
|
|
|
ExposedPorts: {},
|
|
|
|
HostConfig: {
|
|
|
|
RestartPolicy: {
|
|
|
|
Name: 'no',
|
2020-04-11 00:54:53 +03:00
|
|
|
},
|
2017-02-10 14:11:36 +13:00
|
|
|
PortBindings: {},
|
|
|
|
Binds: [],
|
2017-10-27 10:48:11 +02:00
|
|
|
Privileged: false,
|
|
|
|
ExtraHosts: [],
|
2017-02-10 14:11:36 +13:00
|
|
|
},
|
2018-01-07 03:24:51 +10:00
|
|
|
Volumes: {},
|
|
|
|
Labels: {},
|
2020-04-11 00:54:53 +03:00
|
|
|
};
|
2017-02-10 14:11:36 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
helper.portArrayToPortConfiguration = function (ports) {
|
|
|
|
var portConfiguration = {
|
|
|
|
bindings: {},
|
|
|
|
exposedPorts: {},
|
|
|
|
};
|
|
|
|
ports.forEach(function (p) {
|
|
|
|
if (p.containerPort) {
|
2017-05-18 23:00:08 +02:00
|
|
|
var key = p.containerPort + '/' + p.protocol;
|
2017-02-10 14:11:36 +13: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];
|
2020-04-11 00:54:53 +03:00
|
|
|
}
|
2017-02-10 14:11:36 +13:00
|
|
|
}
|
|
|
|
portConfiguration.bindings[key] = [binding];
|
|
|
|
portConfiguration.exposedPorts[key] = {};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return portConfiguration;
|
|
|
|
};
|
|
|
|
|
2018-01-07 03:24:51 +10:00
|
|
|
helper.updateContainerConfigurationWithLabels = function (labelsArray) {
|
|
|
|
var labels = {};
|
|
|
|
labelsArray.forEach(function (l) {
|
2019-11-24 19:25:30 -05:00
|
|
|
if (l.name) {
|
|
|
|
if (l.value) {
|
|
|
|
labels[l.name] = l.value;
|
|
|
|
} else {
|
|
|
|
labels[l.name] = '';
|
2020-04-11 00:54:53 +03:00
|
|
|
}
|
2019-11-24 19:25:30 -05:00
|
|
|
}
|
2018-01-07 03:24:51 +10:00
|
|
|
});
|
|
|
|
return labels;
|
2020-04-11 00:54:53 +03:00
|
|
|
};
|
|
|
|
|
2018-08-22 18:33:06 +03:00
|
|
|
helper.EnvToStringArray = function (templateEnvironment) {
|
2017-02-10 14:11:36 +13:00
|
|
|
var env = [];
|
|
|
|
templateEnvironment.forEach(function (envvar) {
|
|
|
|
if (envvar.value || envvar.set) {
|
|
|
|
var value = envvar.set ? envvar.set : envvar.value;
|
2017-05-18 23:00:08 +02:00
|
|
|
env.push(envvar.name + '=' + value);
|
2020-04-11 00:54:53 +03:00
|
|
|
}
|
|
|
|
});
|
2017-02-10 14:11:36 +13:00
|
|
|
return env;
|
2018-01-07 03:24:51 +10:00
|
|
|
};
|
|
|
|
|
2018-08-22 18:33:06 +03:00
|
|
|
helper.getConsoleConfiguration = function (interactiveFlag) {
|
2017-02-10 14:11:36 +13:00
|
|
|
var consoleConfiguration = {
|
|
|
|
openStdin: false,
|
2017-05-18 22:49:55 +02:00
|
|
|
tty: false,
|
2020-04-11 00:54:53 +03:00
|
|
|
};
|
2017-02-10 14:11:36 +13:00
|
|
|
if (interactiveFlag === true) {
|
|
|
|
consoleConfiguration.openStdin = true;
|
2017-05-18 23:00:08 +02:00
|
|
|
consoleConfiguration.tty = true;
|
2017-02-10 14:11:36 +13:00
|
|
|
}
|
|
|
|
return consoleConfiguration;
|
2017-02-01 12:26:29 +13:00
|
|
|
};
|
2017-02-10 14:11:36 +13:00
|
|
|
|
2017-05-18 22:49:55 +02:00
|
|
|
helper.createVolumeBindings = function (volumes, generatedVolumesPile) {
|
|
|
|
volumes.forEach(function (volume) {
|
|
|
|
if (volume.container) {
|
|
|
|
var binding;
|
2017-02-13 18:16:14 +13:00
|
|
|
if (volume.type === 'auto') {
|
2017-05-18 22:49:55 +02:00
|
|
|
binding = generatedVolumesPile.pop().Id + ':' + volume.container;
|
|
|
|
} else if (volume.type !== 'auto' && volume.bind) {
|
|
|
|
binding = volume.bind + ':' + volume.container;
|
2020-04-11 00:54:53 +03:00
|
|
|
}
|
2018-07-03 20:31:02 +02:00
|
|
|
if (volume.readonly) {
|
2017-02-13 18:16:14 +13:00
|
|
|
binding += ':ro';
|
2020-04-11 00:54:53 +03:00
|
|
|
}
|
2017-02-13 18:16:14 +13:00
|
|
|
volume.binding = binding;
|
2020-04-11 00:54:53 +03:00
|
|
|
}
|
|
|
|
});
|
2017-05-18 22:49:55 +02:00
|
|
|
};
|
|
|
|
|
2017-02-13 18:16:14 +13:00
|
|
|
helper.determineRequiredGeneratedVolumeCount = function (volumes) {
|
|
|
|
var count = 0;
|
|
|
|
volumes.forEach(function (volume) {
|
|
|
|
if (volume.type === 'auto') {
|
2018-07-03 20:31:02 +02:00
|
|
|
++count;
|
2017-02-13 18:16:14 +13:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return count;
|
|
|
|
};
|
|
|
|
|
|
|
|
helper.getUniqueCategories = function (templates) {
|
|
|
|
var categories = [];
|
|
|
|
for (var i = 0; i < templates.length; i++) {
|
|
|
|
var template = templates[i];
|
|
|
|
categories = categories.concat(template.Categories);
|
|
|
|
}
|
2018-07-03 20:31:02 +02:00
|
|
|
return _.uniq(categories);
|
2017-04-05 10:13:32 +02:00
|
|
|
};
|
|
|
|
|
2017-02-10 14:11:36 +13:00
|
|
|
return helper;
|
2017-02-01 12:26:29 +13:00
|
|
|
},
|
|
|
|
]);
|