2016-09-04 02:50:37 +00:00
|
|
|
angular.module('portainer.helpers', [])
|
2017-01-22 23:14:34 +00:00
|
|
|
.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)) {
|
|
|
|
mode.provider = "DOCKER_STANDALONE";
|
|
|
|
} else {
|
|
|
|
mode.provider = "DOCKER_SWARM_MODE";
|
|
|
|
if (info.Swarm.ControlAvailable) {
|
|
|
|
mode.role = "MANAGER";
|
|
|
|
} else {
|
|
|
|
mode.role = "WORKER";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
};
|
2017-01-25 21:12:04 +00:00
|
|
|
}])
|
|
|
|
.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;
|
|
|
|
}
|
|
|
|
};
|
2017-01-22 23:14:34 +00:00
|
|
|
}])
|
2016-08-31 06:03:41 +00:00
|
|
|
.factory('ImageHelper', [function ImageHelperFactory() {
|
|
|
|
'use strict';
|
|
|
|
return {
|
2016-12-13 20:33:24 +00:00
|
|
|
createImageConfigForCommit: function(imageName, registry) {
|
2016-08-31 06:03:41 +00:00
|
|
|
var imageNameAndTag = imageName.split(':');
|
|
|
|
var image = imageNameAndTag[0];
|
|
|
|
if (registry) {
|
|
|
|
image = registry + '/' + imageNameAndTag[0];
|
|
|
|
}
|
|
|
|
var imageConfig = {
|
|
|
|
repo: image,
|
|
|
|
tag: imageNameAndTag[1] ? imageNameAndTag[1] : 'latest'
|
|
|
|
};
|
|
|
|
return imageConfig;
|
2016-12-13 20:33:24 +00:00
|
|
|
},
|
|
|
|
createImageConfigForContainer: function (imageName, registry) {
|
|
|
|
var imageNameAndTag = imageName.split(':');
|
|
|
|
var image = imageNameAndTag[0];
|
|
|
|
if (registry) {
|
|
|
|
image = registry + '/' + imageNameAndTag[0];
|
|
|
|
}
|
|
|
|
var imageConfig = {
|
|
|
|
fromImage: image,
|
|
|
|
tag: imageNameAndTag[1] ? imageNameAndTag[1] : 'latest'
|
|
|
|
};
|
|
|
|
return imageConfig;
|
2016-08-31 06:03:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}])
|
|
|
|
.factory('ContainerHelper', [function ContainerHelperFactory() {
|
|
|
|
'use strict';
|
|
|
|
return {
|
|
|
|
hideContainers: function(containers, containersToHideLabels) {
|
|
|
|
return containers.filter(function (container) {
|
|
|
|
var filterContainer = false;
|
|
|
|
containersToHideLabels.forEach(function(label, index) {
|
|
|
|
if (_.has(container.Labels, label.name) &&
|
|
|
|
container.Labels[label.name] === label.value) {
|
|
|
|
filterContainer = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!filterContainer) {
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
});
|
2016-09-04 03:06:33 +00:00
|
|
|
}
|
2016-08-31 06:03:41 +00:00
|
|
|
};
|
2016-09-23 04:54:58 +00:00
|
|
|
}])
|
|
|
|
.factory('ServiceHelper', [function ServiceHelperFactory() {
|
|
|
|
'use strict';
|
|
|
|
return {
|
|
|
|
serviceToConfig: function(service) {
|
|
|
|
return {
|
|
|
|
Name: service.Spec.Name,
|
2016-11-25 06:21:06 +00:00
|
|
|
Labels: service.Spec.Labels,
|
2016-09-23 04:54:58 +00:00
|
|
|
TaskTemplate: service.Spec.TaskTemplate,
|
|
|
|
Mode: service.Spec.Mode,
|
2016-11-25 06:21:06 +00:00
|
|
|
UpdateConfig: service.Spec.UpdateConfig,
|
2016-09-23 04:54:58 +00:00
|
|
|
Networks: service.Spec.Networks,
|
|
|
|
EndpointSpec: service.Spec.EndpointSpec
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2016-10-20 03:43:09 +00:00
|
|
|
}])
|
2017-01-25 21:12:04 +00:00
|
|
|
.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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}])
|
2016-10-20 03:43:09 +00:00
|
|
|
.factory('TemplateHelper', [function TemplateHelperFactory() {
|
|
|
|
'use strict';
|
|
|
|
return {
|
|
|
|
getPortBindings: function(ports) {
|
|
|
|
var bindings = [];
|
|
|
|
ports.forEach(function (port) {
|
|
|
|
var portAndProtocol = _.split(port, '/');
|
|
|
|
var binding = {
|
|
|
|
containerPort: portAndProtocol[0],
|
|
|
|
protocol: portAndProtocol[1]
|
|
|
|
};
|
|
|
|
bindings.push(binding);
|
|
|
|
});
|
|
|
|
return bindings;
|
|
|
|
},
|
2016-10-27 06:55:44 +00:00
|
|
|
//Not used atm, may prove useful later
|
2016-10-20 03:43:09 +00:00
|
|
|
getVolumeBindings: function(volumes) {
|
|
|
|
var bindings = [];
|
|
|
|
volumes.forEach(function (volume) {
|
|
|
|
bindings.push({ containerPath: volume });
|
|
|
|
});
|
|
|
|
return bindings;
|
|
|
|
},
|
2016-10-27 06:55:44 +00:00
|
|
|
//Not used atm, may prove useful later
|
2016-10-20 03:43:09 +00:00
|
|
|
getEnvBindings: function(env) {
|
|
|
|
var bindings = [];
|
|
|
|
env.forEach(function (envvar) {
|
|
|
|
var binding = {
|
|
|
|
name: envvar.name
|
|
|
|
};
|
|
|
|
if (envvar.set) {
|
|
|
|
binding.value = envvar.set;
|
|
|
|
}
|
|
|
|
bindings.push(binding);
|
|
|
|
});
|
|
|
|
return bindings;
|
|
|
|
}
|
|
|
|
};
|
2016-10-27 06:55:44 +00:00
|
|
|
}]);
|