2018-01-22 13:01:38 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
/* global angular */
|
2018-01-22 13:01:38 -08:00
|
|
|
/* global moment */
|
2019-09-05 22:22:42 +02:00
|
|
|
/* global $ */
|
2018-01-22 13:01:38 -08:00
|
|
|
|
|
|
|
|
// create main application module
|
|
|
|
|
var app = angular.module('Application', ['angular-md5', 'ui-notification']);
|
|
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
app.controller('LogsController', ['$scope', 'Client', function ($scope, Client) {
|
2018-01-22 13:01:38 -08:00
|
|
|
var search = decodeURIComponent(window.location.search).slice(1).split('&').map(function (item) { return item.split('='); }).reduce(function (o, k) { o[k[0]] = k[1]; return o; }, {});
|
|
|
|
|
|
|
|
|
|
$scope.initialized = false;
|
|
|
|
|
$scope.client = Client;
|
|
|
|
|
$scope.selected = '';
|
|
|
|
|
$scope.activeEventSource = null;
|
2018-04-13 13:13:51 +02:00
|
|
|
$scope.lines = 100;
|
2018-01-22 13:01:38 -08:00
|
|
|
$scope.selectedAppInfo = null;
|
2018-12-08 21:45:49 -08:00
|
|
|
$scope.selectedTaskInfo = null;
|
2018-01-22 13:01:38 -08:00
|
|
|
|
|
|
|
|
function ab2str(buf) {
|
|
|
|
|
return String.fromCharCode.apply(null, new Uint16Array(buf));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.clear = function () {
|
|
|
|
|
var logViewer = $('.logs-container');
|
|
|
|
|
logViewer.empty();
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-08 19:49:52 -08:00
|
|
|
// https://github.com/janl/mustache.js/blob/master/mustache.js#L60
|
|
|
|
|
var entityMap = {
|
|
|
|
|
'&': '&',
|
|
|
|
|
'<': '<',
|
|
|
|
|
'>': '>',
|
|
|
|
|
'"': '"',
|
|
|
|
|
"'": ''',
|
|
|
|
|
'/': '/',
|
|
|
|
|
'`': '`',
|
|
|
|
|
'=': '='
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function escapeHtml(string) {
|
|
|
|
|
return String(string).replace(/[&<>"'`=\/]/g, function fromEntityMap (s) {
|
2019-01-08 20:46:39 -08:00
|
|
|
return entityMap[s];
|
2019-01-08 19:49:52 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 13:01:38 -08:00
|
|
|
function showLogs() {
|
|
|
|
|
if (!$scope.selected) return;
|
|
|
|
|
|
2018-11-26 14:49:50 +01:00
|
|
|
var func;
|
|
|
|
|
if ($scope.selected.type === 'platform') func = Client.getPlatformLogs;
|
2018-12-08 21:45:49 -08:00
|
|
|
else if ($scope.selected.type === 'service') func = Client.getServiceLogs;
|
|
|
|
|
else if ($scope.selected.type === 'task') func = Client.getTaskLogs;
|
|
|
|
|
else if ($scope.selected.type === 'app') func = Client.getAppLogs;
|
2018-11-26 14:49:50 +01:00
|
|
|
|
2019-01-08 20:46:39 -08:00
|
|
|
func($scope.selected.value, true /* follow */, $scope.lines, function handleLogs(error, result) {
|
2018-01-22 13:01:38 -08:00
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.activeEventSource = result;
|
|
|
|
|
result.onmessage = function handleMessage(message) {
|
|
|
|
|
var data;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
data = JSON.parse(message.data);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return console.error(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if we want to auto scroll (this is before the appending, as that skews the check)
|
|
|
|
|
var tmp = $('.logs-container');
|
2018-04-13 13:12:30 +02:00
|
|
|
var autoScroll = tmp[0].scrollTop > (tmp[0].scrollHeight - tmp.innerHeight() - 24);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
|
|
|
|
var logLine = $('<div class="log-line">');
|
2020-02-10 13:54:44 -08:00
|
|
|
// realtimeTimestamp is 0 if line is blank or some parse error
|
|
|
|
|
var timeString = data.realtimeTimestamp ? moment(data.realtimeTimestamp/1000).format('MMM DD HH:mm:ss') : '';
|
2019-01-08 19:49:52 -08:00
|
|
|
logLine.html('<span class="time">' + timeString + ' </span>' + window.ansiToHTML(escapeHtml(typeof data.message === 'string' ? data.message : ab2str(data.message))));
|
2018-01-22 13:01:38 -08:00
|
|
|
tmp.append(logLine);
|
|
|
|
|
|
|
|
|
|
if (autoScroll) tmp[0].lastChild.scrollIntoView({ behavior: 'instant', block: 'end' });
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 21:45:49 -08:00
|
|
|
function select(ids, callback) {
|
|
|
|
|
if (ids.id) {
|
|
|
|
|
var BUILT_IN_LOGS = [
|
|
|
|
|
{ name: 'Box', type: 'platform', value: 'box', url: Client.makeURL('/api/v1/cloudron/logs/box') },
|
2019-08-27 14:54:39 +02:00
|
|
|
{ name: 'Graphite', type: 'service', value: 'graphite', url: Client.makeURL('/api/v1/services/graphite/logs') },
|
2018-12-08 21:45:49 -08:00
|
|
|
{ name: 'MongoDB', type: 'service', value: 'mongodb', url: Client.makeURL('/api/v1/services/mongodb/logs') },
|
|
|
|
|
{ name: 'MySQL', type: 'service', value: 'mysql', url: Client.makeURL('/api/v1/services/mysql/logs') },
|
|
|
|
|
{ name: 'PostgreSQL', type: 'service', value: 'postgresql', url: Client.makeURL('/api/v1/services/postgresql/logs') },
|
|
|
|
|
{ name: 'Mail', type: 'service', value: 'mail', url: Client.makeURL('/api/v1/services/mail/logs') },
|
|
|
|
|
{ name: 'Docker', type: 'service', value: 'docker', url: Client.makeURL('/api/v1/services/docker/logs') },
|
2020-06-15 17:30:32 +02:00
|
|
|
{ name: 'Nginx', type: 'service', value: 'nginx', url: Client.makeURL('/api/v1/services/nginx/logs') },
|
2019-04-05 11:01:47 -07:00
|
|
|
{ name: 'Unbound', type: 'service', value: 'unbound', url: Client.makeURL('/api/v1/services/unbound/logs') },
|
|
|
|
|
{ name: 'SFTP', type: 'service', value: 'sftp', url: Client.makeURL('/api/v1/services/sftp/logs') },
|
2020-03-30 18:43:43 +02:00
|
|
|
{ name: 'TURN/STUN', type: 'service', value: 'turn', url: Client.makeURL('/api/v1/services/turn/logs') },
|
2018-12-08 21:45:49 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$scope.selected = BUILT_IN_LOGS.find(function (e) { return e.value === ids.id; });
|
2019-03-01 15:45:55 -08:00
|
|
|
callback();
|
|
|
|
|
} else if (ids.crashId) {
|
|
|
|
|
$scope.selected = {
|
|
|
|
|
type: 'platform',
|
|
|
|
|
value: 'crash-' + ids.crashId,
|
|
|
|
|
name: 'Crash',
|
|
|
|
|
url: Client.makeURL('/api/v1/cloudron/logs/crash-' + ids.crashId)
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-25 18:51:32 -07:00
|
|
|
callback();
|
2018-12-08 21:45:49 -08:00
|
|
|
} else if (ids.appId) {
|
|
|
|
|
Client.getApp(ids.appId, function (error, app) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
$scope.selectedAppInfo = app;
|
|
|
|
|
|
|
|
|
|
$scope.selected = {
|
|
|
|
|
type: 'app',
|
|
|
|
|
value: app.id,
|
|
|
|
|
name: app.fqdn + ' (' + app.manifest.title + ')',
|
|
|
|
|
url: Client.makeURL('/api/v1/apps/' + app.id + '/logs'),
|
|
|
|
|
addons: app.manifest.addons
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
} else if (ids.taskId) {
|
|
|
|
|
Client.getTask(ids.taskId, function (error, task) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
$scope.selectedTaskInfo = task;
|
|
|
|
|
|
|
|
|
|
$scope.selected = {
|
|
|
|
|
type: 'task',
|
|
|
|
|
value: task.id,
|
|
|
|
|
name: task.type,
|
|
|
|
|
url: Client.makeURL('/api/v1/tasks/' + task.id + '/logs')
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-06-25 18:51:32 -07:00
|
|
|
}
|
|
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
function init() {
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
Client.getStatus(function (error, status) {
|
|
|
|
|
if (error) return Client.initError(error, init);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
if (!status.activated) {
|
|
|
|
|
console.log('Not activated yet, redirecting', status);
|
|
|
|
|
window.location.href = '/';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-05-07 14:33:35 -07:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
// check version and force reload if needed
|
|
|
|
|
if (!localStorage.version) {
|
|
|
|
|
localStorage.version = status.version;
|
|
|
|
|
} else if (localStorage.version !== status.version) {
|
|
|
|
|
localStorage.version = status.version;
|
|
|
|
|
window.location.reload(true);
|
|
|
|
|
}
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
console.log('Running log version ', localStorage.version);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
// get user profile as the first thing. this populates the "scope" and affects subsequent API calls
|
|
|
|
|
Client.refreshUserInfo(function (error) {
|
|
|
|
|
if (error) return Client.initError(error, init);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
Client.refreshConfig(function (error) {
|
|
|
|
|
if (error) return Client.initError(error, init);
|
2018-05-07 14:33:35 -07:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
select({ id: search.id, taskId: search.taskId, appId: search.appId, crashId: search.crashId }, function (error) {
|
|
|
|
|
if (error) return Client.initError(error, init);
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
// now mark the Client to be ready
|
|
|
|
|
Client.setReady();
|
2018-01-22 13:01:38 -08:00
|
|
|
|
2019-09-05 22:22:42 +02:00
|
|
|
$scope.initialized = true;
|
|
|
|
|
|
|
|
|
|
showLogs();
|
|
|
|
|
});
|
2018-05-07 14:33:35 -07:00
|
|
|
});
|
2018-01-22 13:01:38 -08:00
|
|
|
});
|
|
|
|
|
});
|
2019-09-05 22:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init();
|
2018-01-22 13:01:38 -08:00
|
|
|
}]);
|