2019-03-21 05:46:49 +00:00
|
|
|
import _ from 'lodash-es';
|
|
|
|
import { ImageViewModel } from '../models/image';
|
|
|
|
import { ImageDetailsViewModel } from "../models/imageDetails";
|
|
|
|
import { ImageLayerViewModel } from "../models/imageLayer";
|
|
|
|
|
2018-02-01 12:27:52 +00:00
|
|
|
angular.module('portainer.docker')
|
2018-07-26 13:09:48 +00:00
|
|
|
.factory('ImageService', ['$q', 'Image', 'ImageHelper', 'RegistryService', 'HttpRequestHelper', 'ContainerService', 'FileUploadService',
|
|
|
|
function ImageServiceFactory($q, Image, ImageHelper, RegistryService, HttpRequestHelper, ContainerService, FileUploadService) {
|
2017-02-10 01:11:36 +00:00
|
|
|
'use strict';
|
|
|
|
var service = {};
|
|
|
|
|
2017-03-20 11:01:35 +00:00
|
|
|
service.image = function(imageId) {
|
2017-02-10 01:11:36 +00:00
|
|
|
var deferred = $q.defer();
|
2017-03-20 11:01:35 +00:00
|
|
|
Image.get({id: imageId}).$promise
|
|
|
|
.then(function success(data) {
|
|
|
|
if (data.message) {
|
|
|
|
deferred.reject({ msg: data.message });
|
|
|
|
} else {
|
|
|
|
var image = new ImageDetailsViewModel(data);
|
|
|
|
deferred.resolve(image);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve image details', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-07-12 14:11:11 +00:00
|
|
|
service.images = function(withUsage) {
|
2017-03-20 11:01:35 +00:00
|
|
|
var deferred = $q.defer();
|
2017-07-12 14:11:11 +00:00
|
|
|
|
2017-07-11 07:56:28 +00:00
|
|
|
$q.all({
|
2017-08-24 05:53:34 +00:00
|
|
|
containers: withUsage ? ContainerService.containers(1) : [],
|
2017-07-11 07:56:28 +00:00
|
|
|
images: Image.query({}).$promise
|
|
|
|
})
|
2017-03-20 11:01:35 +00:00
|
|
|
.then(function success(data) {
|
2017-08-24 05:53:34 +00:00
|
|
|
var containers = data.containers;
|
2017-07-12 14:11:11 +00:00
|
|
|
|
2017-08-24 05:53:34 +00:00
|
|
|
var images = data.images.map(function(item) {
|
|
|
|
item.ContainerCount = 0;
|
|
|
|
for (var i = 0; i < containers.length; i++) {
|
|
|
|
var container = containers[i];
|
|
|
|
if (container.ImageID === item.Id) {
|
|
|
|
item.ContainerCount++;
|
|
|
|
}
|
|
|
|
}
|
2017-03-20 11:01:35 +00:00
|
|
|
return new ImageViewModel(item);
|
|
|
|
});
|
2017-08-24 05:53:34 +00:00
|
|
|
|
2017-03-20 11:01:35 +00:00
|
|
|
deferred.resolve(images);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve images', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-07-08 06:59:32 +00:00
|
|
|
service.history = function(imageId) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
Image.history({id: imageId}).$promise
|
|
|
|
.then(function success(data) {
|
|
|
|
if (data.message) {
|
|
|
|
deferred.reject({ msg: data.message });
|
|
|
|
} else {
|
|
|
|
var layers = [];
|
2018-03-14 00:27:06 +00:00
|
|
|
var order = data.length;
|
2017-07-08 06:59:32 +00:00
|
|
|
angular.forEach(data, function(imageLayer) {
|
2018-03-14 00:27:06 +00:00
|
|
|
layers.push(new ImageLayerViewModel(order, imageLayer));
|
|
|
|
order--;
|
2017-07-08 06:59:32 +00:00
|
|
|
});
|
|
|
|
deferred.resolve(layers);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve image details', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-06-20 11:00:32 +00:00
|
|
|
service.pushImage = function(tag, registry) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
var authenticationDetails = registry.Authentication ? RegistryService.encodedCredentials(registry) : '';
|
|
|
|
HttpRequestHelper.setRegistryAuthenticationHeader(authenticationDetails);
|
|
|
|
Image.push({tag: tag}).$promise
|
|
|
|
.then(function success(data) {
|
|
|
|
if (data[data.length - 1].error) {
|
|
|
|
deferred.reject({ msg: data[data.length - 1].error });
|
|
|
|
} else {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to push image tag', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-06-29 14:05:39 +00:00
|
|
|
function pullImageAndIgnoreErrors(imageConfiguration) {
|
2017-03-20 11:01:35 +00:00
|
|
|
var deferred = $q.defer();
|
2017-06-20 11:00:32 +00:00
|
|
|
|
2017-06-29 14:05:39 +00:00
|
|
|
Image.create({}, imageConfiguration).$promise
|
|
|
|
.finally(function final() {
|
|
|
|
deferred.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function pullImageAndAcknowledgeErrors(imageConfiguration) {
|
|
|
|
var deferred = $q.defer();
|
2017-06-20 11:00:32 +00:00
|
|
|
|
|
|
|
Image.create({}, imageConfiguration).$promise
|
2017-02-10 01:11:36 +00:00
|
|
|
.then(function success(data) {
|
2017-05-23 18:25:56 +00:00
|
|
|
var err = data.length > 0 && data[data.length - 1].hasOwnProperty('message');
|
2017-02-10 01:11:36 +00:00
|
|
|
if (err) {
|
|
|
|
var detail = data[data.length - 1];
|
2017-05-23 18:25:56 +00:00
|
|
|
deferred.reject({ msg: detail.message });
|
2017-02-10 01:11:36 +00:00
|
|
|
} else {
|
|
|
|
deferred.resolve(data);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to pull image', err: err });
|
|
|
|
});
|
2017-03-20 11:01:35 +00:00
|
|
|
|
2017-06-20 11:00:32 +00:00
|
|
|
return deferred.promise;
|
2017-06-29 14:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
service.pullImage = function(image, registry, ignoreErrors) {
|
|
|
|
var imageDetails = ImageHelper.extractImageAndRegistryFromRepository(image);
|
|
|
|
var imageConfiguration = ImageHelper.createImageConfigForContainer(imageDetails.image, registry.URL);
|
|
|
|
var authenticationDetails = registry.Authentication ? RegistryService.encodedCredentials(registry) : '';
|
|
|
|
HttpRequestHelper.setRegistryAuthenticationHeader(authenticationDetails);
|
|
|
|
|
|
|
|
if (ignoreErrors) {
|
|
|
|
return pullImageAndIgnoreErrors(imageConfiguration);
|
|
|
|
}
|
|
|
|
return pullImageAndAcknowledgeErrors(imageConfiguration);
|
2017-05-23 18:25:56 +00:00
|
|
|
};
|
|
|
|
|
2017-03-20 11:01:35 +00:00
|
|
|
service.tagImage = function(id, image, registry) {
|
|
|
|
var imageConfig = ImageHelper.createImageConfigForCommit(image, registry);
|
|
|
|
return Image.tag({id: id, tag: imageConfig.tag, repo: imageConfig.repo}).$promise;
|
|
|
|
};
|
|
|
|
|
2018-07-26 13:09:48 +00:00
|
|
|
service.downloadImages = function(images) {
|
|
|
|
var names = ImageHelper.getImagesNamesForDownload(images);
|
|
|
|
return Image.download(names).$promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.uploadImage = function(file) {
|
|
|
|
return FileUploadService.loadImages(file);
|
|
|
|
};
|
|
|
|
|
2017-03-20 11:01:35 +00:00
|
|
|
service.deleteImage = function(id, forceRemoval) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
Image.remove({id: id, force: forceRemoval}).$promise
|
|
|
|
.then(function success(data) {
|
|
|
|
if (data[0].message) {
|
|
|
|
deferred.reject({ msg: data[0].message });
|
|
|
|
} else {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to remove image', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-11-20 13:34:14 +00:00
|
|
|
service.getUniqueTagListFromImages = function (availableImages) {
|
|
|
|
return _.flatten(_.map(availableImages, function (image) {
|
|
|
|
_.remove(image.RepoTags, function (item) {
|
|
|
|
return item.indexOf('<none>') !== -1;
|
|
|
|
});
|
|
|
|
return image.RepoTags ? _.uniqWith(image.RepoTags, _.isEqual) : [];
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
return service;
|
|
|
|
}]);
|