2019-11-27 22:36:39 +00:00
|
|
|
import angular from 'angular';
|
2019-03-21 05:46:49 +00:00
|
|
|
import _ from 'lodash-es';
|
2019-11-27 22:36:39 +00:00
|
|
|
import { DockerHubViewModel } from 'Portainer/models/dockerhub';
|
2020-08-05 10:23:19 +00:00
|
|
|
import { RegistryTypes } from '@/portainer/models/registryTypes';
|
2019-03-21 05:46:49 +00:00
|
|
|
|
2019-11-27 22:36:39 +00:00
|
|
|
class porImageRegistryController {
|
|
|
|
/* @ngInject */
|
|
|
|
constructor($async, $scope, ImageHelper, RegistryService, DockerHubService, ImageService, Notifications) {
|
|
|
|
this.$async = $async;
|
|
|
|
this.$scope = $scope;
|
|
|
|
this.ImageHelper = ImageHelper;
|
|
|
|
this.RegistryService = RegistryService;
|
|
|
|
this.DockerHubService = DockerHubService;
|
|
|
|
this.ImageService = ImageService;
|
|
|
|
this.Notifications = Notifications;
|
|
|
|
|
|
|
|
this.onInit = this.onInit.bind(this);
|
|
|
|
this.onRegistryChange = this.onRegistryChange.bind(this);
|
|
|
|
|
|
|
|
this.$scope.$watch(() => this.model.Registry, this.onRegistryChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
isKnownRegistry(registry) {
|
|
|
|
return !(registry instanceof DockerHubViewModel) && registry.URL;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRegistryURL(registry) {
|
|
|
|
let url = registry.URL;
|
|
|
|
if (registry.Type === RegistryTypes.GITLAB) {
|
|
|
|
url = registry.URL + '/' + registry.Gitlab.ProjectPath;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareAutocomplete() {
|
|
|
|
let images = [];
|
|
|
|
const registry = this.model.Registry;
|
|
|
|
if (this.isKnownRegistry(registry)) {
|
|
|
|
const url = this.getRegistryURL(registry);
|
|
|
|
const registryImages = _.filter(this.images, (image) => _.includes(image, url));
|
2020-04-10 21:54:53 +00:00
|
|
|
images = _.map(registryImages, (image) => _.replace(image, new RegExp(url + '/?'), ''));
|
2019-11-27 22:36:39 +00:00
|
|
|
} else {
|
|
|
|
const registries = _.filter(this.availableRegistries, (reg) => this.isKnownRegistry(reg));
|
|
|
|
const registryImages = _.flatMap(registries, (registry) => _.filter(this.images, (image) => _.includes(image, registry.URL)));
|
|
|
|
const imagesWithoutKnown = _.difference(this.images, registryImages);
|
|
|
|
images = _.filter(imagesWithoutKnown, (image) => !this.ImageHelper.imageContainsURL(image));
|
|
|
|
}
|
|
|
|
this.availableImages = images;
|
|
|
|
}
|
|
|
|
|
|
|
|
onRegistryChange() {
|
|
|
|
this.prepareAutocomplete();
|
|
|
|
if (this.model.Registry.Type === RegistryTypes.GITLAB && this.model.Image) {
|
|
|
|
this.model.Image = _.replace(this.model.Image, this.model.Registry.Gitlab.ProjectPath, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
displayedRegistryURL() {
|
|
|
|
return this.getRegistryURL(this.model.Registry) || 'docker.io';
|
|
|
|
}
|
|
|
|
|
|
|
|
async onInit() {
|
|
|
|
try {
|
|
|
|
const [registries, dockerhub, images] = await Promise.all([
|
|
|
|
this.RegistryService.registries(),
|
|
|
|
this.DockerHubService.dockerhub(),
|
2020-04-10 21:54:53 +00:00
|
|
|
this.autoComplete ? this.ImageService.images() : [],
|
2019-11-27 22:36:39 +00:00
|
|
|
]);
|
|
|
|
this.images = this.ImageService.getUniqueTagListFromImages(images);
|
|
|
|
this.availableRegistries = _.concat(dockerhub, registries);
|
|
|
|
|
|
|
|
const id = this.model.Registry.Id;
|
|
|
|
if (!id) {
|
|
|
|
this.model.Registry = dockerhub;
|
2018-07-03 18:31:02 +00:00
|
|
|
} else {
|
2020-04-10 21:54:53 +00:00
|
|
|
this.model.Registry = _.find(this.availableRegistries, { Id: id });
|
2017-08-13 10:17:41 +00:00
|
|
|
}
|
2019-11-27 22:36:39 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to retrieve registries');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
|
|
|
return this.$async(this.onInit);
|
2018-07-03 18:31:02 +00:00
|
|
|
}
|
2019-11-27 22:36:39 +00:00
|
|
|
}
|
2017-06-20 11:00:32 +00:00
|
|
|
|
2019-11-27 22:36:39 +00:00
|
|
|
export default porImageRegistryController;
|
|
|
|
angular.module('portainer.docker').controller('porImageRegistryController', porImageRegistryController);
|