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-11 15:32:17 +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') });
|
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-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-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: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:30:31 +02:00
|
|
|
var url = Client.apiOrigin.replace('https', 'wss') + '/api/v1/apps/' + $scope.selected.value + '/execws?tty=true';
|
|
|
|
|
$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...');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.terminalInjectMysql = function () {
|
|
|
|
|
if (!$scope.terminalSocket) return;
|
|
|
|
|
|
|
|
|
|
$scope.terminalSocket.send('mysql --user=${MYSQL_USERNAME} --password=${MYSQL_PASSWORD} --host=${MYSQL_HOST} ${MYSQL_DATABASE}\n');
|
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
|
|
|
}]);
|