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 */
|
2021-07-14 09:15:21 +00:00
|
|
|
constructor($async, $scope, ImageHelper, RegistryService, EndpointService, ImageService, Notifications) {
|
2019-11-27 22:36:39 +00:00
|
|
|
this.$async = $async;
|
|
|
|
this.$scope = $scope;
|
|
|
|
this.ImageHelper = ImageHelper;
|
|
|
|
this.RegistryService = RegistryService;
|
2021-07-14 09:15:21 +00:00
|
|
|
this.EndpointService = EndpointService;
|
2019-11-27 22:36:39 +00:00
|
|
|
this.ImageService = ImageService;
|
|
|
|
this.Notifications = Notifications;
|
|
|
|
|
|
|
|
this.onRegistryChange = this.onRegistryChange.bind(this);
|
2021-10-14 12:42:10 +00:00
|
|
|
this.onImageChange = this.onImageChange.bind(this);
|
2019-11-27 22:36:39 +00:00
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
this.registries = [];
|
|
|
|
this.images = [];
|
|
|
|
this.defaultRegistry = new DockerHubViewModel();
|
|
|
|
|
2019-11-27 22:36:39 +00:00
|
|
|
this.$scope.$watch(() => this.model.Registry, this.onRegistryChange);
|
2021-10-14 12:42:10 +00:00
|
|
|
this.$scope.$watch(() => this.model.Image, this.onImageChange);
|
2019-11-27 22:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isKnownRegistry(registry) {
|
2022-08-02 02:39:53 +00:00
|
|
|
return registry && !(registry instanceof DockerHubViewModel) && registry.URL;
|
2019-11-27 22:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getRegistryURL(registry) {
|
2022-08-02 02:39:53 +00:00
|
|
|
if (!registry) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-27 22:36:39 +00:00
|
|
|
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 {
|
2021-07-14 09:15:21 +00:00
|
|
|
const registries = _.filter(this.registries, (reg) => this.isKnownRegistry(reg));
|
2019-11-27 22:36:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-24 18:27:32 +00:00
|
|
|
isDockerHubRegistry() {
|
2022-08-02 02:39:53 +00:00
|
|
|
return this.model.UseRegistry && this.model.Registry && (this.model.Registry.Type === RegistryTypes.DOCKERHUB || this.model.Registry.Type === RegistryTypes.ANONYMOUS);
|
2021-03-24 18:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onRegistryChange() {
|
2019-11-27 22:36:39 +00:00
|
|
|
this.prepareAutocomplete();
|
2022-08-02 02:39:53 +00:00
|
|
|
if (this.model.Registry && this.model.Registry.Type === RegistryTypes.GITLAB && this.model.Image) {
|
2019-11-27 22:36:39 +00:00
|
|
|
this.model.Image = _.replace(this.model.Image, this.model.Registry.Gitlab.ProjectPath, '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 12:42:10 +00:00
|
|
|
async onImageChange() {
|
|
|
|
if (!this.isDockerHubRegistry()) {
|
|
|
|
this.setValidity(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:36:39 +00:00
|
|
|
displayedRegistryURL() {
|
2022-08-02 02:39:53 +00:00
|
|
|
return (this.model.Registry && this.getRegistryURL(this.model.Registry)) || 'docker.io';
|
2019-11-27 22:36:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 09:15:21 +00:00
|
|
|
async reloadRegistries() {
|
|
|
|
return this.$async(async () => {
|
|
|
|
try {
|
2021-10-19 01:54:53 +00:00
|
|
|
let showDefaultRegistry = false;
|
|
|
|
this.registries = await this.EndpointService.registries(this.endpoint.Id, this.namespace);
|
|
|
|
|
2021-11-29 22:03:08 +00:00
|
|
|
// Sort the registries by Name
|
|
|
|
this.registries.sort((a, b) => a.Name.localeCompare(b.Name));
|
|
|
|
|
2021-10-19 01:54:53 +00:00
|
|
|
// hide default(anonymous) dockerhub registry if user has an authenticated one
|
|
|
|
if (!this.registries.some((registry) => registry.Type === RegistryTypes.DOCKERHUB)) {
|
|
|
|
showDefaultRegistry = true;
|
2021-11-29 22:03:08 +00:00
|
|
|
// Add dockerhub on top
|
|
|
|
this.registries.splice(0, 0, this.defaultRegistry);
|
2021-10-19 01:54:53 +00:00
|
|
|
}
|
2021-07-14 09:15:21 +00:00
|
|
|
|
2022-08-02 02:39:53 +00:00
|
|
|
const id = this.model.Registry && this.model.Registry.Id;
|
2021-07-14 09:15:21 +00:00
|
|
|
const registry = _.find(this.registries, { Id: id });
|
|
|
|
if (!registry) {
|
2021-10-19 01:54:53 +00:00
|
|
|
this.model.Registry = showDefaultRegistry ? this.defaultRegistry : this.registries[0];
|
2021-07-14 09:15:21 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to retrieve registries');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadImages() {
|
|
|
|
return this.$async(async () => {
|
|
|
|
try {
|
|
|
|
if (!this.autoComplete) {
|
|
|
|
this.images = [];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const images = await this.ImageService.images();
|
|
|
|
this.images = this.ImageService.getUniqueTagListFromImages(images);
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to retrieve images');
|
2017-08-13 10:17:41 +00:00
|
|
|
}
|
2021-07-14 09:15:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$onChanges({ namespace, endpoint }) {
|
|
|
|
if ((namespace || endpoint) && this.endpoint.Id) {
|
|
|
|
this.reloadRegistries();
|
2019-11-27 22:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
2021-07-14 09:15:21 +00:00
|
|
|
return this.$async(async () => {
|
|
|
|
await this.loadImages();
|
|
|
|
});
|
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);
|