2017-08-07 13:50:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-08-08 11:52:32 +02:00
|
|
|
/* global moment */
|
2017-08-15 20:00:52 +02:00
|
|
|
/* global Terminal */
|
2017-08-08 11:52:32 +02:00
|
|
|
|
2017-08-17 09:30:31 +02:00
|
|
|
angular.module('Application').controller('DebugController', ['$scope', '$location', 'Client', function ($scope, $location, Client) {
|
2017-08-07 21:00:23 +02:00
|
|
|
Client.onReady(function () { if (!Client.getUserInfo().admin) $location.path('/'); });
|
|
|
|
|
|
2017-08-07 13:50:45 +02:00
|
|
|
$scope.config = Client.getConfig();
|
|
|
|
|
$scope.user = Client.getUserInfo();
|
|
|
|
|
|
2017-08-07 16:48:27 +02:00
|
|
|
$scope.logs = [];
|
2017-08-07 17:09:48 +02:00
|
|
|
$scope.selected = '';
|
|
|
|
|
$scope.activeEventSource = null;
|
2017-08-15 20:00:52 +02:00
|
|
|
$scope.terminal = null;
|
2017-08-17 09:30:31 +02:00
|
|
|
$scope.terminalSocket = null;
|
2017-08-08 10:07:33 +02:00
|
|
|
$scope.lines = 10;
|
2017-08-15 20:00:52 +02:00
|
|
|
$scope.terminalVisible = false;
|
2017-08-07 16:48:27 +02:00
|
|
|
|
|
|
|
|
function ab2str(buf) {
|
|
|
|
|
return String.fromCharCode.apply(null, new Uint16Array(buf));
|
|
|
|
|
}
|
2017-08-07 13:50:45 +02:00
|
|
|
|
|
|
|
|
$scope.populateLogTypes = function () {
|
2017-08-07 18:24:19 +02:00
|
|
|
$scope.logs.push({ name: 'System (All)', type: 'platform', value: 'all', url: Client.makeURL('/api/v1/cloudron/logs?units=all') });
|
|
|
|
|
$scope.logs.push({ name: 'Box', type: 'platform', value: 'box', url: Client.makeURL('/api/v1/cloudron/logs?units=box') });
|
|
|
|
|
$scope.logs.push({ name: 'Mail', type: 'platform', value: 'mail', url: Client.makeURL('/api/v1/cloudron/logs?units=mail') });
|
2017-08-07 13:50:45 +02:00
|
|
|
|
|
|
|
|
Client.getInstalledApps().forEach(function (app) {
|
2017-08-17 11:29:49 +02:00
|
|
|
$scope.logs.push({ name: app.fqdn + ' (' + app.manifest.title + ')', type: 'app', value: app.id, url: Client.makeURL('/api/v1/apps/' + app.id + '/logs'), addons: app.manifest.addons });
|
2017-08-07 13:50:45 +02:00
|
|
|
});
|
2017-08-07 17:09:48 +02:00
|
|
|
|
|
|
|
|
$scope.selected = $scope.logs[0];
|
2017-08-07 13:50:45 +02:00
|
|
|
};
|
|
|
|
|
|
2017-08-17 11:29:49 +02:00
|
|
|
$scope.usesAddon = function (addon) {
|
|
|
|
|
if (!$scope.selected || !$scope.selected.addons) return false;
|
|
|
|
|
return !!Object.keys($scope.selected.addons).find(function (a) { return a === addon; });
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-15 20:00:52 +02:00
|
|
|
function reset() {
|
2017-08-07 17:09:48 +02:00
|
|
|
// close the old event source so we wont receive any new logs
|
|
|
|
|
if ($scope.activeEventSource) {
|
|
|
|
|
$scope.activeEventSource.close();
|
|
|
|
|
$scope.activeEventSource = null;
|
2017-08-07 16:48:27 +02:00
|
|
|
}
|
2017-08-07 17:09:48 +02:00
|
|
|
|
2017-08-15 20:00:52 +02:00
|
|
|
var logViewer = $('.logs-and-term-container');
|
|
|
|
|
logViewer.empty();
|
|
|
|
|
|
|
|
|
|
if ($scope.terminal) {
|
|
|
|
|
$scope.terminal.destroy();
|
|
|
|
|
$scope.terminal = null;
|
|
|
|
|
}
|
2017-08-17 09:30:31 +02:00
|
|
|
|
|
|
|
|
if ($scope.terminalSocket) {
|
|
|
|
|
$scope.terminalSocket = null;
|
|
|
|
|
}
|
2017-08-15 20:00:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.showLogs = function () {
|
|
|
|
|
$scope.terminalVisible = false;
|
|
|
|
|
|
|
|
|
|
reset();
|
2017-08-07 17:09:48 +02:00
|
|
|
|
2017-08-17 09:33:00 +02:00
|
|
|
if (!$scope.selected) return;
|
|
|
|
|
|
2017-08-15 20:00:52 +02:00
|
|
|
var func = $scope.selected.type === 'platform' ? Client.getPlatformLogs : Client.getAppLogs;
|
|
|
|
|
func($scope.selected.value, true, $scope.lines, function handleLogs(error, result) {
|
|
|
|
|
if (error) return console.error(error);
|
2017-08-07 17:09:48 +02:00
|
|
|
|
|
|
|
|
$scope.activeEventSource = result;
|
|
|
|
|
result.onmessage = function handleMessage(message) {
|
|
|
|
|
var data;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
data = JSON.parse(message.data);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return console.error(e);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 18:58:15 +02:00
|
|
|
// check if we want to auto scroll (this is before the appending, as that skews the check)
|
2017-08-15 20:00:52 +02:00
|
|
|
var tmp = $('.logs-and-term-container');
|
2017-08-15 22:11:14 +02:00
|
|
|
var autoScroll = tmp[0].scrollTop > (tmp[0].scrollTopMax - 24);
|
2017-08-07 18:58:15 +02:00
|
|
|
|
2017-08-08 09:55:18 +02:00
|
|
|
var logLine = $('<div class="log-line">');
|
|
|
|
|
var timeString = moment.utc(data.realtimeTimestamp/1000).format('MMM DD HH:mm:ss');
|
|
|
|
|
logLine.html('<span class="time">' + timeString + ' </span>' + window.ansiToHTML(typeof data.message === 'string' ? data.message : ab2str(data.message)));
|
2017-08-15 20:00:52 +02:00
|
|
|
tmp.append(logLine);
|
2017-08-07 18:58:15 +02:00
|
|
|
|
2017-08-15 22:11:14 +02:00
|
|
|
if (autoScroll) tmp[0].lastChild.scrollIntoView({ behavior: 'instant', block: 'end' });
|
2017-08-07 17:09:48 +02:00
|
|
|
};
|
|
|
|
|
});
|
2017-08-15 20:00:52 +02:00
|
|
|
};
|
|
|
|
|
|
2017-08-17 09:30:31 +02:00
|
|
|
$scope.showTerminal = function (retry) {
|
2017-08-15 20:00:52 +02:00
|
|
|
$scope.terminalVisible = true;
|
|
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
|
2017-08-17 09:33:00 +02:00
|
|
|
if (!$scope.selected) return;
|
|
|
|
|
|
2017-08-17 09:30:31 +02:00
|
|
|
// we can only connect to apps here
|
|
|
|
|
if ($scope.selected.type !== 'app') {
|
|
|
|
|
var tmp = $('.logs-and-term-container');
|
|
|
|
|
var logLine = $('<div class="log-line">');
|
|
|
|
|
logLine.html('Terminal is only supported for app, not for ' + $scope.selected.name);
|
|
|
|
|
tmp.append(logLine);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 20:00:52 +02:00
|
|
|
$scope.terminal = new Terminal();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// websocket cannot use relative urls
|
2017-08-17 09:33:00 +02:00
|
|
|
var url = Client.apiOrigin.replace('https', 'wss') + '/api/v1/apps/' + $scope.selected.value + '/execws?tty=true&access_token=' + Client.getToken();
|
2017-08-17 09:30:31 +02:00
|
|
|
$scope.terminalSocket = new WebSocket(url);
|
|
|
|
|
$scope.terminal.attach($scope.terminalSocket);
|
2017-08-15 21:51:12 +02:00
|
|
|
|
2017-08-17 09:30:31 +02:00
|
|
|
$scope.terminalSocket.onclose = function () {
|
2017-08-15 21:51:12 +02:00
|
|
|
// retry in one second
|
2017-08-17 09:30:31 +02:00
|
|
|
setTimeout($scope.showTerminal.bind(null, true), 1000);
|
2017-08-15 21:51:12 +02:00
|
|
|
};
|
2017-08-15 20:00:52 +02:00
|
|
|
} catch (e) {
|
2017-08-15 21:51:12 +02:00
|
|
|
console.error(e);
|
2017-08-15 20:00:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.terminal.open(document.querySelector('.logs-and-term-container'));
|
|
|
|
|
$scope.terminal.fit();
|
2017-08-17 09:30:31 +02:00
|
|
|
|
|
|
|
|
if (retry) $scope.terminal.writeln('Reconnecting...');
|
|
|
|
|
else $scope.terminal.writeln('Connecting...');
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-17 21:42:05 +02:00
|
|
|
$scope.terminalInject = function (addon) {
|
2017-08-17 09:30:31 +02:00
|
|
|
if (!$scope.terminalSocket) return;
|
|
|
|
|
|
2017-08-17 21:42:05 +02:00
|
|
|
var cmd;
|
|
|
|
|
if (addon === 'mysql') cmd = 'mysql --user=${MYSQL_USERNAME} --password=${MYSQL_PASSWORD} --host=${MYSQL_HOST} ${MYSQL_DATABASE}';
|
|
|
|
|
else if (addon === 'postgresql') cmd = 'PGPASSWORD=${POSTGRESQL_PASSWORD} psql -h ${POSTGRESQL_HOST} -p ${POSTGRESQL_PORT} -U ${POSTGRESQL_USERNAME} -d ${POSTGRESQL_DATABASE}';
|
|
|
|
|
else if (addon === 'mongodb') cmd = 'mongo -u "${MONGODB_USERNAME}" -p "${MONGODB_PASSWORD}" ${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}';
|
|
|
|
|
else if (addon === 'redis') cmd = 'redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}"';
|
|
|
|
|
|
|
|
|
|
if (!cmd) return;
|
|
|
|
|
|
2017-08-18 20:31:07 +02:00
|
|
|
cmd += ' ';
|
2017-08-17 21:42:05 +02:00
|
|
|
|
|
|
|
|
$scope.terminalSocket.send(cmd);
|
2017-08-18 20:31:07 +02:00
|
|
|
$scope.terminal.focus();
|
2017-08-17 21:42:05 +02:00
|
|
|
}
|
2017-08-15 20:00:52 +02:00
|
|
|
|
|
|
|
|
$scope.$watch('selected', function (newVal) {
|
|
|
|
|
if (!newVal) return;
|
|
|
|
|
|
|
|
|
|
if ($scope.terminalVisible) $scope.showTerminal();
|
|
|
|
|
else $scope.showLogs();
|
2017-08-07 16:48:27 +02:00
|
|
|
});
|
|
|
|
|
|
2017-08-07 13:50:45 +02:00
|
|
|
Client.onReady($scope.populateLogTypes);
|
2017-08-08 11:52:32 +02:00
|
|
|
|
|
|
|
|
$scope.$on('$destroy', function () {
|
|
|
|
|
if ($scope.activeEventSource) {
|
|
|
|
|
$scope.activeEventSource.onmessage = function () {};
|
|
|
|
|
$scope.activeEventSource.close();
|
|
|
|
|
$scope.activeEventSource = null;
|
|
|
|
|
}
|
2017-08-15 20:00:52 +02:00
|
|
|
|
|
|
|
|
if ($scope.terminal) {
|
|
|
|
|
$scope.terminal.destroy();
|
|
|
|
|
}
|
2017-08-08 11:52:32 +02:00
|
|
|
});
|
2017-08-07 13:50:45 +02:00
|
|
|
}]);
|