2018-11-15 19:59:24 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-01-22 10:54:03 +01:00
|
|
|
/* global angular:false */
|
|
|
|
|
/* global $:false */
|
2018-11-16 17:21:57 +01:00
|
|
|
|
2018-11-26 08:59:04 +01:00
|
|
|
angular.module('Application').controller('SystemController', ['$scope', '$location', '$interval', 'Client', function ($scope, $location, $interval, Client) {
|
2018-11-15 19:59:24 +01:00
|
|
|
Client.onReady(function () { if (!Client.getUserInfo().admin) $location.path('/'); });
|
|
|
|
|
|
2018-11-20 16:53:42 +01:00
|
|
|
$scope.config = Client.getConfig();
|
2018-11-15 19:59:24 +01:00
|
|
|
$scope.ready = false;
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.services = [];
|
2018-11-26 09:24:58 +01:00
|
|
|
$scope.isRebootRequired = false;
|
2018-11-15 19:59:24 +01:00
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
function refresh(serviceName, callback) {
|
2018-11-21 18:17:53 +01:00
|
|
|
callback = callback || function () {};
|
|
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
Client.getService(serviceName, function (error, result) {
|
2018-11-21 18:17:53 +01:00
|
|
|
if (error) Client.error(error);
|
2018-11-15 19:59:24 +01:00
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
var service = $scope.services.find(function (s) { return s.name === serviceName; });
|
|
|
|
|
if (!service) $scope.services.push(result);
|
|
|
|
|
|
|
|
|
|
service.status = result.status;
|
|
|
|
|
service.config = result.config;
|
|
|
|
|
service.memoryUsed = result.memoryUsed;
|
|
|
|
|
service.memoryPercent = result.memoryPercent;
|
|
|
|
|
|
|
|
|
|
callback(null, service);
|
2018-11-15 19:59:24 +01:00
|
|
|
});
|
2018-11-16 17:21:57 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-20 14:47:39 +01:00
|
|
|
function refreshAll() {
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.services.forEach(function (s) {
|
|
|
|
|
refresh(s.name);
|
2018-11-20 14:47:39 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 09:24:58 +01:00
|
|
|
$scope.reboot = {
|
|
|
|
|
busy: false,
|
|
|
|
|
|
|
|
|
|
show: function () {
|
|
|
|
|
$scope.reboot.busy = false;
|
|
|
|
|
|
|
|
|
|
$('#rebootModal').modal('show');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
submit: function () {
|
|
|
|
|
$scope.reboot.busy = true;
|
|
|
|
|
|
|
|
|
|
Client.reboot(function (error) {
|
|
|
|
|
$scope.reboot.busy = false;
|
|
|
|
|
if (error) return Client.error(error);
|
|
|
|
|
|
|
|
|
|
$('#rebootModal').modal('hide');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.restartService = function (serviceName) {
|
|
|
|
|
function waitForActive(serviceName) {
|
|
|
|
|
refresh(serviceName, function (error, result) {
|
|
|
|
|
if (result.status === 'active') return;
|
2018-11-21 18:17:53 +01:00
|
|
|
|
2019-04-05 10:46:35 -07:00
|
|
|
setTimeout(function () { waitForActive(serviceName); }, 3000);
|
2018-11-21 18:17:53 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-05 10:46:35 -07:00
|
|
|
$scope.services.find(function (s) { return s.name === serviceName; }).status = 'starting';
|
|
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
Client.restartService(serviceName, function (error) {
|
2018-11-16 17:21:57 +01:00
|
|
|
if (error) return Client.error(error);
|
|
|
|
|
|
2019-04-05 10:46:35 -07:00
|
|
|
// show "busy" indicator for 3 seconds to show some ui activity
|
|
|
|
|
setTimeout(function () { waitForActive(serviceName); }, 3000);
|
2018-11-16 17:21:57 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigure = {
|
2018-11-20 16:53:42 +01:00
|
|
|
error: null,
|
|
|
|
|
busy: false,
|
2018-12-10 11:35:53 +01:00
|
|
|
service: null,
|
2018-11-20 16:53:42 +01:00
|
|
|
|
|
|
|
|
// form model
|
|
|
|
|
memoryLimit: 0,
|
|
|
|
|
memoryTicks: [],
|
|
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
show: function (service) {
|
|
|
|
|
$scope.serviceConfigure.reset();
|
2018-11-20 16:53:42 +01:00
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigure.service = service;
|
|
|
|
|
$scope.serviceConfigure.memoryLimit = service.config.memory;
|
2018-11-20 16:53:42 +01:00
|
|
|
|
|
|
|
|
// TODO improve those
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigure.memoryTicks = [];
|
2018-11-20 16:53:42 +01:00
|
|
|
|
|
|
|
|
// create ticks starting from manifest memory limit. the memory limit here is currently split into ram+swap (and thus *2 below)
|
|
|
|
|
// TODO: the *2 will overallocate since 4GB is max swap that cloudron itself allocates
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigure.memoryTicks = [];
|
2018-11-20 16:53:42 +01:00
|
|
|
var npow2 = Math.pow(2, Math.ceil(Math.log($scope.config.memory)/Math.log(2)));
|
|
|
|
|
for (var i = 256; i <= (npow2*2/1024/1024); i *= 2) {
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigure.memoryTicks.push(i * 1024 * 1024);
|
2018-11-20 16:53:42 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
$('#serviceConfigureModal').modal('show');
|
2018-11-20 16:53:42 +01:00
|
|
|
},
|
|
|
|
|
|
2018-11-21 17:06:01 +01:00
|
|
|
submit: function (memoryLimit) {
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigure.busy = true;
|
|
|
|
|
$scope.serviceConfigure.error = null;
|
2018-11-20 16:53:42 +01:00
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
Client.configureService($scope.serviceConfigure.service.name, memoryLimit, function (error) {
|
|
|
|
|
$scope.serviceConfigure.busy = false;
|
2018-11-20 16:53:42 +01:00
|
|
|
if (error) {
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigure.error = error.message;
|
2018-11-20 16:53:42 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
refresh($scope.serviceConfigure.service.name);
|
2018-11-21 15:57:26 +01:00
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
$('#serviceConfigureModal').modal('hide');
|
|
|
|
|
$scope.serviceConfigure.reset();
|
2018-11-20 16:53:42 +01:00
|
|
|
});
|
2018-11-21 18:17:53 +01:00
|
|
|
},
|
2018-11-20 16:53:42 +01:00
|
|
|
|
|
|
|
|
reset: function () {
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigure.busy = false;
|
|
|
|
|
$scope.serviceConfigure.error = null;
|
|
|
|
|
$scope.serviceConfigure.service = null;
|
2018-11-20 16:53:42 +01:00
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigure.memoryLimit = 0;
|
|
|
|
|
$scope.serviceConfigure.memoryTicks = [];
|
2018-11-20 16:53:42 +01:00
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.serviceConfigureForm.$setPristine();
|
|
|
|
|
$scope.serviceConfigureForm.$setUntouched();
|
2018-11-20 16:53:42 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-18 20:01:53 +01:00
|
|
|
Client.onReady(function () {
|
2018-11-26 09:24:58 +01:00
|
|
|
Client.isRebootRequired(function (error, result) {
|
|
|
|
|
if (error) console.error(error);
|
2018-11-18 20:01:53 +01:00
|
|
|
|
2018-11-26 09:24:58 +01:00
|
|
|
$scope.isRebootRequired = !!result;
|
|
|
|
|
|
2018-12-02 18:07:11 -08:00
|
|
|
Client.getServices(function (error, result) {
|
2018-11-26 09:24:58 +01:00
|
|
|
if (error) return Client.error(error);
|
2018-11-18 20:01:53 +01:00
|
|
|
|
2018-12-10 11:35:53 +01:00
|
|
|
$scope.services = result.map(function (serviceName) { return { name: serviceName }; });
|
2018-11-20 14:47:39 +01:00
|
|
|
|
2018-11-26 09:24:58 +01:00
|
|
|
// just kick off the status fetching
|
|
|
|
|
refreshAll();
|
|
|
|
|
|
|
|
|
|
$scope.ready = true;
|
|
|
|
|
});
|
2018-11-18 20:01:53 +01:00
|
|
|
});
|
|
|
|
|
});
|
2018-11-15 19:59:24 +01:00
|
|
|
}]);
|