2019-03-21 05:46:49 +00:00
|
|
|
import _ from 'lodash-es';
|
2020-08-05 10:23:19 +00:00
|
|
|
import { RegistryTypes } from '@/portainer/models/registryTypes';
|
2019-03-21 05:46:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').factory('ImageHelper', [
|
|
|
|
function ImageHelperFactory() {
|
|
|
|
'use strict';
|
2017-05-23 18:25:56 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
var helper = {};
|
2017-05-23 18:25:56 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
helper.isValidTag = isValidTag;
|
|
|
|
helper.createImageConfigForContainer = createImageConfigForContainer;
|
|
|
|
helper.getImagesNamesForDownload = getImagesNamesForDownload;
|
|
|
|
helper.removeDigestFromRepository = removeDigestFromRepository;
|
|
|
|
helper.imageContainsURL = imageContainsURL;
|
2019-10-14 13:45:09 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function isValidTag(tag) {
|
|
|
|
return tag.match(/^(?![\.\-])([a-zA-Z0-9\_\.\-])+$/g);
|
|
|
|
}
|
2019-10-14 13:45:09 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function getImagesNamesForDownload(images) {
|
|
|
|
var names = images.map(function (image) {
|
|
|
|
return image.RepoTags[0] !== '<none>:<none>' ? image.RepoTags[0] : image.Id;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
names: names,
|
|
|
|
};
|
|
|
|
}
|
2019-11-27 22:36:39 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {PorImageRegistryModel} registry
|
|
|
|
*/
|
|
|
|
function createImageConfigForContainer(registry) {
|
|
|
|
const data = {
|
|
|
|
fromImage: '',
|
|
|
|
};
|
|
|
|
let fullImageName = '';
|
2018-07-26 13:09:48 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
if (registry.UseRegistry) {
|
|
|
|
if (registry.Registry.Type === RegistryTypes.GITLAB) {
|
|
|
|
const slash = _.startsWith(registry.Image, ':') ? '' : '/';
|
|
|
|
fullImageName = registry.Registry.URL + '/' + registry.Registry.Gitlab.ProjectPath + slash + registry.Image;
|
2021-03-24 22:50:34 +00:00
|
|
|
} else if (registry.Registry.Type === RegistryTypes.QUAY) {
|
2021-03-12 23:47:35 +00:00
|
|
|
const name = registry.Registry.Quay.UseOrganisation ? registry.Registry.Quay.OrganisationName : registry.Registry.Username;
|
|
|
|
const url = registry.Registry.URL ? registry.Registry.URL + '/' : '';
|
|
|
|
fullImageName = url + name + '/' + registry.Image;
|
2020-04-10 21:54:53 +00:00
|
|
|
} else {
|
|
|
|
const url = registry.Registry.URL ? registry.Registry.URL + '/' : '';
|
|
|
|
fullImageName = url + registry.Image;
|
|
|
|
}
|
|
|
|
if (!_.includes(registry.Image, ':')) {
|
|
|
|
fullImageName += ':latest';
|
|
|
|
}
|
2019-11-27 22:36:39 +00:00
|
|
|
} else {
|
2020-04-10 21:54:53 +00:00
|
|
|
fullImageName = registry.Image;
|
2019-11-27 22:36:39 +00:00
|
|
|
}
|
2017-05-23 18:25:56 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
data.fromImage = fullImageName;
|
|
|
|
return data;
|
|
|
|
}
|
2017-05-23 18:25:56 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function imageContainsURL(image) {
|
|
|
|
const split = _.split(image, '/');
|
|
|
|
const url = split[0];
|
|
|
|
if (split.length > 1) {
|
|
|
|
return _.includes(url, '.') || _.includes(url, ':');
|
|
|
|
}
|
|
|
|
return false;
|
2019-11-27 22:36:39 +00:00
|
|
|
}
|
2017-05-23 18:25:56 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
function removeDigestFromRepository(repository) {
|
|
|
|
return repository.split('@sha')[0];
|
|
|
|
}
|
2018-06-20 13:53:58 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return helper;
|
|
|
|
},
|
|
|
|
]);
|