1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-02 20:35:25 +02:00

refactor(app): introduce new project structure for the frontend (#1623)

This commit is contained in:
Anthony Lapenna 2018-02-01 13:27:52 +01:00 committed by GitHub
parent e6422a6d75
commit 27dceadba1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
354 changed files with 1518 additions and 1755 deletions

View file

@ -0,0 +1,34 @@
angular.module('portainer.docker')
.factory('ConfigHelper', [function ConfigHelperFactory() {
'use strict';
return {
flattenConfig: function(config) {
if (config) {
return {
Id: config.ConfigID,
Name: config.ConfigName,
FileName: config.File.Name,
Uid: config.File.UID,
Gid: config.File.GID,
Mode: config.File.Mode
};
}
return {};
},
configConfig: function(config) {
if (config) {
return {
ConfigID: config.Id,
ConfigName: config.Name,
File: {
Name: config.FileName || config.Name,
UID: config.Uid || '0',
GID: config.Gid || '0',
Mode: config.Mode || 292
}
};
}
return {};
}
};
}]);

View file

@ -0,0 +1,62 @@
angular.module('portainer.docker')
.factory('ContainerHelper', [function ContainerHelperFactory() {
'use strict';
var helper = {};
helper.commandStringToArray = function(command) {
return splitargs(command);
};
helper.commandArrayToString = function(array) {
return array.map(function(elem) {
return '\'' + elem + '\'';
}).join(' ');
};
helper.configFromContainer = function(container) {
var config = container.Config;
// HostConfig
config.HostConfig = container.HostConfig;
// Name
config.name = container.Name.replace(/^\//g, '');
// Network
var mode = config.HostConfig.NetworkMode;
config.NetworkingConfig = {
'EndpointsConfig': {}
};
config.NetworkingConfig.EndpointsConfig = container.NetworkSettings.Networks;
if (mode.indexOf('container:') !== -1) {
delete config.Hostname;
delete config.ExposedPorts;
}
// Set volumes
var binds = [];
var volumes = {};
for (var v in container.Mounts) {
if ({}.hasOwnProperty.call(container.Mounts, v)) {
var mount = container.Mounts[v];
var volume = {
'type': mount.Type,
'name': mount.Name || mount.Source,
'containerPath': mount.Destination,
'readOnly': mount.RW === false
};
var name = mount.Name || mount.Source;
var containerPath = mount.Destination;
if (name && containerPath) {
var bind = name + ':' + containerPath;
volumes[containerPath] = {};
if (mount.RW === false) {
bind += ':ro';
}
binds.push(bind);
}
}
}
config.HostConfig.Binds = binds;
config.Volumes = volumes;
return config;
};
return helper;
}]);

View file

@ -0,0 +1,59 @@
angular.module('portainer.docker')
.factory('ImageHelper', [function ImageHelperFactory() {
'use strict';
var helper = {};
helper.extractImageAndRegistryFromRepository = function(repository) {
var slashCount = _.countBy(repository)['/'];
var registry = null;
var image = repository;
if (slashCount >= 1) {
// assume something/something[/...]
registry = repository.substr(0, repository.indexOf('/'));
// assume valid DNS name or IP (contains at least one '.')
if (_.countBy(registry)['.'] > 0) {
image = repository.substr(repository.indexOf('/') + 1);
} else {
registry = null;
}
}
return {
registry: registry,
image: image
};
};
function extractNameAndTag(imageName, registry) {
var imageNameAndTag = imageName.split(':');
var image = imageNameAndTag[0];
var tag = imageNameAndTag[1] ? imageNameAndTag[1] : 'latest';
if (registry) {
image = registry + '/' + imageNameAndTag[0];
}
return {
image: image,
tag: tag
};
}
helper.createImageConfigForCommit = function(imageName, registry) {
var imageAndTag = extractNameAndTag(imageName, registry);
return {
repo: imageAndTag.image,
tag: imageAndTag.tag
};
};
helper.createImageConfigForContainer = function (imageName, registry) {
var imageAndTag = extractNameAndTag(imageName, registry);
return {
fromImage: imageAndTag.image,
tag: imageAndTag.tag
};
};
return helper;
}]);

View file

@ -0,0 +1,36 @@
angular.module('portainer.docker')
.factory('InfoHelper', [function InfoHelperFactory() {
'use strict';
return {
determineEndpointMode: function(info) {
var mode = {
provider: '',
role: ''
};
if (_.startsWith(info.ServerVersion, 'swarm')) {
mode.provider = 'DOCKER_SWARM';
if (info.SystemStatus[0][1] === 'primary') {
mode.role = 'PRIMARY';
} else {
mode.role = 'REPLICA';
}
} else {
if (!info.Swarm || _.isEmpty(info.Swarm.NodeID)) {
if (info.ID === 'vSphere Integrated Containers') {
mode.provider = 'VMWARE_VIC';
} else {
mode.provider = 'DOCKER_STANDALONE';
}
} else {
mode.provider = 'DOCKER_SWARM_MODE';
if (info.Swarm.ControlAvailable) {
mode.role = 'MANAGER';
} else {
mode.role = 'WORKER';
}
}
}
return mode;
}
};
}]);

View file

@ -0,0 +1,23 @@
angular.module('portainer.docker')
.factory('LabelHelper', [function LabelHelperFactory() {
'use strict';
return {
fromLabelHashToKeyValue: function(labels) {
if (labels) {
return Object.keys(labels).map(function(key) {
return {key: key, value: labels[key], originalKey: key, originalValue: labels[key], added: true};
});
}
return [];
},
fromKeyValueToLabelHash: function(labelKV) {
var labels = {};
if (labelKV) {
labelKV.forEach(function(label) {
labels[label.key] = label.value;
});
}
return labels;
}
};
}]);

View file

@ -0,0 +1,24 @@
angular.module('portainer.docker')
.factory('NodeHelper', [function NodeHelperFactory() {
'use strict';
return {
nodeToConfig: function(node) {
return {
Name: node.Spec.Name,
Role: node.Spec.Role,
Labels: node.Spec.Labels,
Availability: node.Spec.Availability
};
},
getManagerIP: function(nodes) {
var managerIp;
for (var n in nodes) {
if (undefined === nodes[n].ManagerStatus || nodes[n].ManagerStatus.Reachability !== 'reachable') {
continue;
}
managerIp = nodes[n].ManagerStatus.Addr.split(':')[0];
}
return managerIp;
}
};
}]);

View file

@ -0,0 +1,34 @@
angular.module('portainer.docker')
.factory('SecretHelper', [function SecretHelperFactory() {
'use strict';
return {
flattenSecret: function(secret) {
if (secret) {
return {
Id: secret.SecretID,
Name: secret.SecretName,
FileName: secret.File.Name,
Uid: secret.File.UID,
Gid: secret.File.GID,
Mode: secret.File.Mode
};
}
return {};
},
secretConfig: function(secret) {
if (secret) {
return {
SecretID: secret.Id,
SecretName: secret.Name,
File: {
Name: secret.FileName,
UID: secret.Uid || '0',
GID: secret.Gid || '0',
Mode: secret.Mode || 444
}
};
}
return {};
}
};
}]);

View file

@ -0,0 +1,247 @@
angular.module('portainer.docker')
.factory('ServiceHelper', [function ServiceHelperFactory() {
'use strict';
var helper = {};
helper.associateTasksToService = function(service, tasks) {
service.Tasks = [];
var otherServicesTasks = [];
for (var i = 0; i < tasks.length; i++) {
var task = tasks[i];
if (task.ServiceId === service.Id) {
service.Tasks.push(task);
} else {
otherServicesTasks.push(task);
}
}
tasks = otherServicesTasks;
};
helper.serviceToConfig = function(service) {
return {
Name: service.Spec.Name,
Labels: service.Spec.Labels,
TaskTemplate: service.Spec.TaskTemplate,
Mode: service.Spec.Mode,
UpdateConfig: service.Spec.UpdateConfig,
Networks: service.Spec.Networks,
EndpointSpec: service.Spec.EndpointSpec
};
};
helper.translateKeyValueToPlacementPreferences = function(keyValuePreferences) {
if (keyValuePreferences) {
var preferences = [];
keyValuePreferences.forEach(function(preference) {
if (preference.strategy && preference.strategy !== '' && preference.value && preference.value !== '') {
switch (preference.strategy.toLowerCase()) {
case 'spread':
preferences.push({
'Spread': {
'SpreadDescriptor': preference.value
}
});
break;
}
}
});
return preferences;
}
return [];
};
helper.translateKeyValueToPlacementConstraints = function(keyValueConstraints) {
if (keyValueConstraints) {
var constraints = [];
keyValueConstraints.forEach(function(constraint) {
if (constraint.key && constraint.key !== '' && constraint.value && constraint.value !== '') {
constraints.push(constraint.key + constraint.operator + constraint.value);
}
});
return constraints;
}
return [];
};
helper.translateEnvironmentVariables = function(env) {
if (env) {
var variables = [];
env.forEach(function(variable) {
var idx = variable.indexOf('=');
var keyValue = [variable.slice(0, idx), variable.slice(idx + 1)];
var originalValue = (keyValue.length > 1) ? keyValue[1] : '';
variables.push({
key: keyValue[0],
value: originalValue,
originalKey: keyValue[0],
originalValue: originalValue,
added: true
});
});
return variables;
}
return [];
};
helper.translateEnvironmentVariablesToEnv = function(env) {
if (env) {
var variables = [];
env.forEach(function(variable) {
if (variable.key && variable.key !== '') {
variables.push(variable.key + '=' + variable.value);
}
});
return variables;
}
return [];
};
helper.translatePreferencesToKeyValue = function(preferences) {
if (preferences) {
var keyValuePreferences = [];
preferences.forEach(function(preference) {
if (preference.Spread) {
keyValuePreferences.push({
strategy: 'Spread',
value: preference.Spread.SpreadDescriptor
});
}
});
return keyValuePreferences;
}
return [];
};
helper.translateConstraintsToKeyValue = function(constraints) {
function getOperator(constraint) {
var indexEquals = constraint.indexOf('==');
if (indexEquals >= 0) {
return [indexEquals, '=='];
}
return [constraint.indexOf('!='), '!='];
}
if (constraints) {
var keyValueConstraints = [];
constraints.forEach(function(constraint) {
var operatorIndices = getOperator(constraint);
var key = constraint.slice(0, operatorIndices[0]);
var operator = operatorIndices[1];
var value = constraint.slice(operatorIndices[0] + 2);
keyValueConstraints.push({
key: key,
value: value,
operator: operator,
originalKey: key,
originalValue: value
});
});
return keyValueConstraints;
}
};
helper.translateHumanDurationToNanos = function(humanDuration) {
var nanos;
var regex = /^([0-9]+)(h|m|s|ms|us|ns)$/i;
var matches = humanDuration.match(regex);
if (matches !== null && matches.length === 3) {
var duration = parseInt(matches[1], 10);
var unit = matches[2];
// Moment.js cannot use micro or nanoseconds
switch (unit) {
case 'ns':
nanos = duration;
break;
case 'us':
nanos = duration * 1000;
break;
default:
nanos = moment.duration(duration, unit).asMilliseconds() * 1000000;
}
}
return nanos;
};
// Convert nanoseconds to the higher unit possible
// e.g 1840 nanoseconds = 1804ns
// e.g 300000000000 nanoseconds = 5m
// e.g 3510000000000 nanoseconds = 3510s
// e.g 3540000000000 nanoseconds = 59m
// e.g 3600000000000 nanoseconds = 1h
helper.translateNanosToHumanDuration = function(nanos) {
var humanDuration = '0s';
var conversionFromNano = {};
conversionFromNano['ns'] = 1;
conversionFromNano['us'] = conversionFromNano['ns'] * 1000;
conversionFromNano['ms'] = conversionFromNano['us'] * 1000;
conversionFromNano['s'] = conversionFromNano['ms'] * 1000;
conversionFromNano['m'] = conversionFromNano['s'] * 60;
conversionFromNano['h'] = conversionFromNano['m'] * 60;
Object.keys(conversionFromNano).forEach(function(unit) {
if ( nanos % conversionFromNano[unit] === 0 && (nanos / conversionFromNano[unit]) > 0) {
humanDuration = (nanos / conversionFromNano[unit]) + unit;
}
});
return humanDuration;
};
helper.translateLogDriverOptsToKeyValue = function(logOptions) {
var options = [];
if (logOptions) {
Object.keys(logOptions).forEach(function(key) {
options.push({
key: key,
value: logOptions[key],
originalKey: key,
originalValue: logOptions[key],
added: true
});
});
}
return options;
};
helper.translateKeyValueToLogDriverOpts = function(keyValueLogDriverOpts) {
var options = {};
if (keyValueLogDriverOpts) {
keyValueLogDriverOpts.forEach(function(option) {
if (option.key && option.key !== '' && option.value && option.value !== '') {
options[option.key] = option.value;
}
});
}
return options;
};
helper.translateHostsEntriesToHostnameIP = function(entries) {
var ipHostEntries = [];
if (entries) {
entries.forEach(function(entry) {
if (entry.indexOf(' ') && entry.split(' ').length === 2) {
var keyValue = entry.split(' ');
ipHostEntries.push({ hostname: keyValue[1], ip: keyValue[0]});
}
});
}
return ipHostEntries;
};
helper.translateHostnameIPToHostsEntries = function(entries) {
var ipHostEntries = [];
if (entries) {
entries.forEach(function(entry) {
if (entry.ip && entry.hostname) {
ipHostEntries.push(entry.ip + ' ' + entry.hostname);
}
});
}
return ipHostEntries;
};
return helper;
}]);

View file

@ -0,0 +1,30 @@
angular.module('portainer.docker')
.factory('VolumeHelper', [function VolumeHelperFactory() {
'use strict';
var helper = {};
helper.createDriverOptions = function(optionArray) {
var options = {};
optionArray.forEach(function (option) {
options[option.name] = option.value;
});
return options;
};
helper.isVolumeUsedByAService = function(volume, services) {
for (var i = 0; i < services.length; i++) {
var service = services[i];
var mounts = service.Mounts;
for (var j = 0; j < mounts.length; j++) {
var mount = mounts[j];
if (mount.Source === volume.Id) {
return true;
}
}
}
return false;
};
return helper;
}]);