Files
cloudron-box/webadmin/src/views/debug.js

406 lines
13 KiB
JavaScript
Raw Normal View History

2017-08-07 13:50:45 +02:00
'use strict';
/* global moment */
/* global Terminal */
2017-08-17 09:30:31 +02:00
angular.module('Application').controller('DebugController', ['$scope', '$location', 'Client', function ($scope, $location, Client) {
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();
$scope.terminalVisible = true;
2017-08-07 16:48:27 +02:00
$scope.logs = [];
2017-08-07 17:09:48 +02:00
$scope.selected = '';
$scope.activeEventSource = null;
$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-19 12:29:00 -07:00
$scope.restartAppBusy = false;
2017-09-05 23:11:04 +02:00
$scope.appBusy = false;
$scope.selectedAppInfo = null;
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.downloadFile = {
error: '',
filePath: '',
busy: false,
downloadUrl: function () {
if (!$scope.downloadFile.filePath) return '';
var filePath = $scope.downloadFile.filePath.replace(/\/*\//g, '/');
return Client.apiOrigin + '/api/v1/apps/' + $scope.selected.value + '/download?file=' + filePath + '&access_token=' + Client.getToken();
},
show: function () {
$scope.downloadFile.busy = false;
$scope.downloadFile.error = '';
$scope.downloadFile.filePath = '';
$('#downloadFileModal').modal('show');
},
submit: function () {
$scope.downloadFile.busy = true;
Client.checkDownloadableFile($scope.selected.value, $scope.downloadFile.filePath, function (error) {
$scope.downloadFile.busy = false;
if (error) {
$scope.downloadFile.error = 'The requested file does not exist.';
return;
}
// we have to click the link to make the browser do the download
// don't know how to prevent the browsers
$('#fileDownloadLink')[0].click();
$('#downloadFileModal').modal('hide');
});
}
};
2017-08-20 19:32:00 +02:00
$scope.uploadProgress = {
busy: false,
total: 0,
current: 0,
show: function () {
$scope.uploadProgress.total = 0;
$scope.uploadProgress.current = 0;
$('#uploadProgressModal').modal('show');
},
hide: function () {
$('#uploadProgressModal').modal('hide');
}
};
$scope.uploadFile = function () {
var fileUpload = document.querySelector('#fileUpload');
fileUpload.onchange = function (e) {
if (e.target.files.length === 0) return;
2017-08-20 19:32:00 +02:00
$scope.uploadProgress.busy = true;
$scope.uploadProgress.show();
Client.uploadFile($scope.selected.value, e.target.files[0], function progress(e) {
$scope.uploadProgress.total = e.total;
$scope.uploadProgress.current = e.loaded;
}, function (error) {
if (error) console.error(error);
$scope.uploadProgress.busy = false;
$scope.uploadProgress.hide();
});
};
fileUpload.click();
};
2017-08-07 13:50:45 +02:00
$scope.populateLogTypes = function () {
$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) {
$scope.logs.push({
type: 'app',
value: app.id,
name: app.fqdn + ' (' + app.manifest.title + ')',
2017-08-19 12:52:48 -07:00
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
};
$scope.usesAddon = function (addon) {
if (!$scope.selected || !$scope.selected.addons) return false;
return !!Object.keys($scope.selected.addons).find(function (a) { return a === addon; });
};
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-09-05 23:11:04 +02:00
var logViewer = $('#logsAndTerminalContainer');
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;
}
$scope.selectedAppInfo = null;
}
2017-08-19 12:29:00 -07:00
$scope.restartApp = function () {
$scope.restartAppBusy = true;
var appId = $scope.selected.value;
function waitUntilStopped(callback) {
Client.refreshInstalledApps(function (error) {
if (error) return callback(error);
Client.getApp(appId, function (error, result) {
if (error) return callback(error);
if (result.runState === 'stopped') return callback();
setTimeout(waitUntilStopped.bind(null, callback), 2000);
});
});
}
Client.stopApp(appId, function (error) {
if (error) return console.error('Failed to stop app.', error);
waitUntilStopped(function (error) {
if (error) return console.error('Failed to get app status.', error);
Client.startApp(appId, function (error) {
if (error) console.error('Failed to start app.', error);
$scope.restartAppBusy = false;
});
});
});
};
$scope.repairApp = function () {
$('#repairAppModal').modal('show');
};
$scope.repairAppBegin = function () {
2017-09-05 23:11:04 +02:00
$scope.appBusy = true;
2017-09-05 23:11:04 +02:00
Client.debugApp($scope.selected.value, true, function (error) {
if (error) return console.error(error);
2017-09-05 23:11:04 +02:00
Client.refreshInstalledApps(function (error) {
if (error) console.error(error);
$('#repairAppModal').modal('hide');
});
});
};
$scope.repairAppDone = function () {
2017-09-05 23:11:04 +02:00
$scope.appBusy = true;
2017-09-05 23:11:04 +02:00
Client.debugApp($scope.selected.value, false, function (error) {
if (error) return console.error(error);
2017-09-05 23:11:04 +02:00
Client.refreshInstalledApps(function (error) {
if (error) console.error(error);
});
});
};
$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;
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-09-05 23:11:04 +02:00
var tmp = $('#logsAndTerminalContainer');
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)));
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-17 09:30:31 +02:00
$scope.showTerminal = function (retry) {
$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') {
2017-09-05 23:11:04 +02:00
var tmp = $('#logsAndTerminalContainer');
2017-08-17 09:30:31 +02:00
var logLine = $('<div class="log-line">');
logLine.html('Terminal is only supported for apps, not for ' + $scope.selected.name);
2017-08-17 09:30:31 +02:00
tmp.append(logLine);
return;
}
// fetch current app state
2017-09-05 23:11:04 +02:00
Client.refreshInstalledApps(function (error) {
if (error) console.error(error);
$scope.terminal = new Terminal();
$scope.terminal.open(document.querySelector('#logsAndTerminalContainer'));
$scope.terminal.fit();
try {
// websocket cannot use relative urls
var url = Client.apiOrigin.replace('https', 'wss') + '/api/v1/apps/' + $scope.selected.value + '/execws?tty=true&rows=' + $scope.terminal.rows + '&columns=' + $scope.terminal.cols + '&access_token=' + Client.getToken();
$scope.terminalSocket = new WebSocket(url);
$scope.terminal.attach($scope.terminalSocket);
$scope.terminalSocket.onclose = function () {
// retry in one second only if terminal view is still selected
$scope.terminalReconnectTimeout = setTimeout(function () {
// if the scope was already destroyed, do not reconnect
if ($scope.$$destroyed) return;
if ($scope.terminalVisible) $scope.showTerminal(true);
}, 1000);
};
// Let the browser handle paste
$scope.terminal.attachCustomKeyEventHandler(function (e) {
if (e.key === 'v' && (e.ctrlKey || e.metaKey)) return false;
});
} catch (e) {
console.error(e);
}
2017-09-05 23:11:04 +02:00
if (retry) $scope.terminal.writeln('Reconnecting...');
else $scope.terminal.writeln('Connecting...');
});
2017-08-17 09:30:31 +02:00
};
$scope.terminalInject = function (addon) {
2017-08-17 09:30:31 +02:00
if (!$scope.terminalSocket) return;
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;
cmd += ' ';
$scope.terminalSocket.send(cmd);
$scope.terminal.focus();
}
$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-09-05 23:11:04 +02:00
Client.onApps(function () {
if ($scope.$$destroyed) return;
if ($scope.selected.type !== 'app') return $scope.appBusy = false;
var appId = $scope.selected.value;
Client.getApp(appId, function (error, result) {
if (error) return console.error(error);
// we expect this to be called _after_ a reconfigure was issued
if (result.installationState === 'pending_configure') {
$scope.appBusy = true;
} else if (result.installationState === 'installed') {
$scope.appBusy = false;
}
$scope.selectedAppInfo = result;
});
});
$scope.$on('$destroy', function () {
if ($scope.activeEventSource) {
$scope.activeEventSource.onmessage = function () {};
$scope.activeEventSource.close();
$scope.activeEventSource = null;
}
if ($scope.terminal) {
$scope.terminal.destroy();
}
});
// terminal right click handling
$scope.terminalClear = function () {
if (!$scope.terminal) return;
$scope.terminal.clear();
$scope.terminal.focus();
};
$scope.terminalCopy = function () {
if (!$scope.terminal) return;
// execCommand('copy') would copy any selection from the page, so do this only if terminal has a selection
if (!$scope.terminal.getSelection()) return;
document.execCommand('copy');
$scope.terminal.focus();
};
$('.contextMenuBackdrop').on('click', function (e) {
$('#terminalContextMenu').hide();
$('.contextMenuBackdrop').hide();
2017-08-22 16:24:26 +02:00
$scope.terminal.focus();
});
2017-09-05 23:11:04 +02:00
$('#logsAndTerminalContainer').on('contextmenu', function (e) {
if (!$scope.terminal) return true;
e.preventDefault();
$('.contextMenuBackdrop').show();
$('#terminalContextMenu').css({
display: 'block',
left: e.pageX,
top: e.pageY
});
return false;
});
// setup all the dialog focus handling
['downloadFileModal'].forEach(function (id) {
$('#' + id).on('shown.bs.modal', function () {
$(this).find("[autofocus]:first").focus();
});
});
2017-08-07 13:50:45 +02:00
}]);