2022-10-20 14:33:54 +00:00
|
|
|
import { formatLogs } from '../helpers/logHelper';
|
2019-03-21 05:46:49 +00:00
|
|
|
import { ServiceViewModel } from '../models/service';
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
angular.module('portainer.docker').factory('ServiceService', [
|
|
|
|
'$q',
|
|
|
|
'Service',
|
2022-10-20 14:33:54 +00:00
|
|
|
function ServiceServiceFactory($q, Service) {
|
2020-04-10 21:54:53 +00:00
|
|
|
'use strict';
|
|
|
|
var service = {};
|
|
|
|
|
|
|
|
service.services = function (filters) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
Service.query({ filters: filters ? filters : {} })
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
var services = data.map(function (item) {
|
|
|
|
return new ServiceViewModel(item);
|
|
|
|
});
|
|
|
|
deferred.resolve(services);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve services', err: err });
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.service = function (id) {
|
|
|
|
var deferred = $q.defer();
|
2019-03-21 05:46:49 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
Service.get({ id: id })
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
var service = new ServiceViewModel(data);
|
|
|
|
deferred.resolve(service);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to retrieve service details', err: err });
|
|
|
|
});
|
2017-05-23 18:56:10 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
service.remove = function (service) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
Service.remove({ id: service.Id })
|
|
|
|
.$promise.then(function success(data) {
|
|
|
|
if (data.message) {
|
|
|
|
deferred.reject({ msg: data.message, err: data.message });
|
|
|
|
} else {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject({ msg: 'Unable to remove service', err: err });
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2017-09-14 06:04:59 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.update = function (serv, config, rollback) {
|
|
|
|
return service.service(serv.Id).then((data) => {
|
|
|
|
const params = {
|
|
|
|
id: serv.Id,
|
|
|
|
version: data.Version,
|
|
|
|
};
|
|
|
|
if (rollback) {
|
|
|
|
params.rollback = rollback;
|
|
|
|
}
|
|
|
|
return Service.update(params, config).$promise;
|
2017-09-14 06:04:59 +00:00
|
|
|
});
|
2018-02-28 06:19:28 +00:00
|
|
|
};
|
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
service.logs = function (id, stdout, stderr, timestamps, since, tail) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
var parameters = {
|
|
|
|
id: id,
|
|
|
|
stdout: stdout || 0,
|
|
|
|
stderr: stderr || 0,
|
|
|
|
timestamps: timestamps || 0,
|
|
|
|
since: since || 0,
|
|
|
|
tail: tail || 'all',
|
|
|
|
};
|
2018-05-04 07:45:05 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
Service.logs(parameters)
|
|
|
|
.$promise.then(function success(data) {
|
2022-10-20 14:33:54 +00:00
|
|
|
var logs = formatLogs(data.logs, { stripHeaders: true, withTimestamps: !!timestamps });
|
2020-04-10 21:54:53 +00:00
|
|
|
deferred.resolve(logs);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
deferred.reject(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
2018-02-28 06:19:28 +00:00
|
|
|
|
2020-04-10 21:54:53 +00:00
|
|
|
return service;
|
|
|
|
},
|
|
|
|
]);
|