2018-02-01 12:27:52 +00:00
|
|
|
angular.module('portainer.docker')
|
2018-05-04 07:45:05 +00:00
|
|
|
.factory('ContainerService', ['$q', 'Container', 'ResourceControlService', 'LogHelper',
|
|
|
|
function ContainerServiceFactory($q, Container, ResourceControlService, LogHelper) {
|
2017-02-10 01:11:36 +00:00
|
|
|
'use strict';
|
|
|
|
var service = {};
|
|
|
|
|
2017-09-09 16:49:21 +00:00
|
|
|
service.container = function(id) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
Container.get({ id: id }).$promise
|
|
|
|
.then(function success(data) {
|
|
|
|
var container = new ContainerDetailsViewModel(data);
|
|
|
|
deferred.resolve(container);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve container information', err: err });
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-10-15 17:24:40 +00:00
|
|
|
service.containers = function(all, filters) {
|
2017-02-10 01:11:36 +00:00
|
|
|
var deferred = $q.defer();
|
2017-10-17 08:12:16 +00:00
|
|
|
Container.query({ all : all, filters: filters }).$promise
|
2017-02-10 01:11:36 +00:00
|
|
|
.then(function success(data) {
|
2017-10-15 17:24:40 +00:00
|
|
|
var containers = data.map(function (item) {
|
|
|
|
return new ContainerViewModel(item);
|
|
|
|
});
|
2017-06-01 08:14:55 +00:00
|
|
|
deferred.resolve(containers);
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
2017-09-09 16:49:21 +00:00
|
|
|
deferred.reject({ msg: 'Unable to retrieve containers', err: err });
|
2017-02-10 01:11:36 +00:00
|
|
|
});
|
2017-10-15 17:24:40 +00:00
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
service.startContainer = function(id) {
|
|
|
|
return Container.start({ id: id }, {}).$promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.stopContainer = function(id) {
|
|
|
|
return Container.stop({ id: id }, {}).$promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.restartContainer = function(id) {
|
|
|
|
return Container.restart({ id: id }, {}).$promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.killContainer = function(id) {
|
|
|
|
return Container.kill({ id: id }, {}).$promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.pauseContainer = function(id) {
|
|
|
|
return Container.pause({ id: id }, {}).$promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.resumeContainer = function(id) {
|
|
|
|
return Container.unpause({ id: id }, {}).$promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.renameContainer = function(id, newContainerName) {
|
|
|
|
return Container.rename({id: id, name: newContainerName }, {}).$promise;
|
|
|
|
};
|
|
|
|
|
2018-08-16 09:31:00 +00:00
|
|
|
service.updateRestartPolicy = updateRestartPolicy;
|
|
|
|
|
|
|
|
function updateRestartPolicy(id, restartPolicy, maximumRetryCounts) {
|
|
|
|
return Container.update({ id: id },
|
|
|
|
{ RestartPolicy: { Name: restartPolicy, MaximumRetryCount: maximumRetryCounts } }
|
|
|
|
).$promise;
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
service.createContainer = function(configuration) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
Container.create(configuration).$promise
|
|
|
|
.then(function success(data) {
|
2018-07-23 05:01:03 +00:00
|
|
|
deferred.resolve(data);
|
2017-02-10 01:11:36 +00:00
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to create container', err: err });
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.createAndStartContainer = function(configuration) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
var containerID;
|
|
|
|
service.createContainer(configuration)
|
|
|
|
.then(function success(data) {
|
|
|
|
containerID = data.Id;
|
|
|
|
return service.startContainer(containerID);
|
|
|
|
})
|
|
|
|
.then(function success() {
|
|
|
|
deferred.resolve({ Id: containerID });
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject(err);
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-05-23 18:56:10 +00:00
|
|
|
|
|
|
|
service.remove = function(container, removeVolumes) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
Container.remove({ id: container.Id, v: (removeVolumes) ? 1 : 0, force: true }).$promise
|
2017-05-23 18:56:10 +00:00
|
|
|
.then(function success(data) {
|
|
|
|
if (data.message) {
|
|
|
|
deferred.reject({ msg: data.message, err: data.message });
|
|
|
|
}
|
|
|
|
if (container.ResourceControl && container.ResourceControl.Type === 1) {
|
|
|
|
return ResourceControlService.deleteResourceControl(container.ResourceControl.Id);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function success() {
|
|
|
|
deferred.resolve();
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to remove container', err: err });
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2017-07-13 16:04:58 +00:00
|
|
|
service.createExec = function(execConfig) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
Container.exec({}, execConfig).$promise
|
2017-07-13 16:04:58 +00:00
|
|
|
.then(function success(data) {
|
|
|
|
if (data.message) {
|
|
|
|
deferred.reject({ msg: data.message, err: data.message });
|
|
|
|
} else {
|
|
|
|
deferred.resolve(data);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2018-10-29 04:49:35 +00:00
|
|
|
service.logs = function(id, stdout, stderr, timestamps, since, tail, stripHeaders) {
|
2018-05-04 07:45:05 +00:00
|
|
|
var deferred = $q.defer();
|
|
|
|
|
2018-02-28 06:19:28 +00:00
|
|
|
var parameters = {
|
|
|
|
id: id,
|
|
|
|
stdout: stdout || 0,
|
|
|
|
stderr: stderr || 0,
|
|
|
|
timestamps: timestamps || 0,
|
2018-10-29 04:49:35 +00:00
|
|
|
since: since || 0,
|
2018-02-28 06:19:28 +00:00
|
|
|
tail: tail || 'all'
|
|
|
|
};
|
|
|
|
|
2018-05-04 07:45:05 +00:00
|
|
|
Container.logs(parameters).$promise
|
|
|
|
.then(function success(data) {
|
|
|
|
var logs = LogHelper.formatLogs(data.logs, stripHeaders);
|
|
|
|
deferred.resolve(logs);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
2018-02-28 06:19:28 +00:00
|
|
|
};
|
|
|
|
|
2017-09-09 16:49:21 +00:00
|
|
|
service.containerStats = function(id) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
Container.stats({ id: id }).$promise
|
2017-09-09 16:49:21 +00:00
|
|
|
.then(function success(data) {
|
|
|
|
var containerStats = new ContainerStatsViewModel(data);
|
|
|
|
deferred.resolve(containerStats);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.containerTop = function(id) {
|
2018-05-06 07:15:57 +00:00
|
|
|
return Container.top({ id: id }).$promise;
|
2017-10-17 06:56:40 +00:00
|
|
|
};
|
2017-10-17 08:12:16 +00:00
|
|
|
|
2017-10-17 06:56:40 +00:00
|
|
|
service.inspect = function(id) {
|
2018-05-06 07:15:57 +00:00
|
|
|
return Container.inspect({ id: id }).$promise;
|
2017-09-09 16:49:21 +00:00
|
|
|
};
|
|
|
|
|
2018-10-28 06:06:50 +00:00
|
|
|
service.prune = function(filters) {
|
|
|
|
return Container.prune({ filters: filters }).$promise;
|
|
|
|
};
|
|
|
|
|
2017-02-10 01:11:36 +00:00
|
|
|
return service;
|
|
|
|
}]);
|