2017-02-10 01:11:36 +00:00
|
|
|
angular.module('portainer.services')
|
2017-03-20 11:01:35 +00:00
|
|
|
.factory('ImageService', ['$q', 'Image', 'ImageHelper', function ImageServiceFactory($q, Image, ImageHelper) {
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.images = function() {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
Image.query({}).$promise
|
|
|
|
.then(function success(data) {
|
|
|
|
var images = data.map(function (item) {
|
|
|
|
return new ImageViewModel(item);
|
|
|
|
});
|
|
|
|
deferred.resolve(images);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve images', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.pullImage = function(image, registry) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
var imageConfiguration = ImageHelper.createImageConfigForContainer(image, registry);
|
2017-02-10 01:11:36 +00:00
|
|
|
Image.create(imageConfiguration).$promise
|
|
|
|
.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 });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-03-20 11:01:35 +00:00
|
|
|
|
2017-05-23 18:25:56 +00:00
|
|
|
service.pullTag = function(tag) {
|
|
|
|
var imageAndRegistry = ImageHelper.extractImageAndRegistryFromTag(tag);
|
|
|
|
return service.pullImage(imageAndRegistry.image, imageAndRegistry.registry);
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.pushImage = function(tag) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
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-02-10 01:11:36 +00:00
|
|
|
return service;
|
|
|
|
}]);
|