2016-08-03 03:11:09 +00:00
|
|
|
angular.module('containerConsole', [])
|
2016-09-02 03:25:20 +00:00
|
|
|
.controller('ContainerConsoleController', ['$scope', '$stateParams', 'Settings', 'Container', 'Exec', '$timeout', 'Messages',
|
|
|
|
function ($scope, $stateParams, Settings, Container, Exec, $timeout, Messages) {
|
2016-08-03 03:11:09 +00:00
|
|
|
$scope.state = {};
|
|
|
|
$scope.state.command = "bash";
|
|
|
|
$scope.connected = false;
|
|
|
|
|
|
|
|
var socket, term;
|
|
|
|
|
|
|
|
// Ensure the socket is closed before leaving the view
|
|
|
|
$scope.$on('$stateChangeStart', function (event, next, current) {
|
2016-10-01 08:44:23 +00:00
|
|
|
if (socket && socket !== null) {
|
2016-08-03 03:11:09 +00:00
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Container.get({id: $stateParams.id}, function(d) {
|
|
|
|
$scope.container = d;
|
|
|
|
$('#loadingViewSpinner').hide();
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.connect = function() {
|
|
|
|
$('#loadConsoleSpinner').show();
|
|
|
|
var termWidth = Math.round($('#terminal-container').width() / 8.2);
|
|
|
|
var termHeight = 30;
|
|
|
|
var execConfig = {
|
|
|
|
id: $stateParams.id,
|
|
|
|
AttachStdin: true,
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: $scope.state.command.replace(" ", ",").split(",")
|
|
|
|
};
|
|
|
|
|
|
|
|
Container.exec(execConfig, function(d) {
|
2016-09-02 03:25:20 +00:00
|
|
|
if (d.message) {
|
|
|
|
$('#loadConsoleSpinner').hide();
|
2016-09-02 05:40:03 +00:00
|
|
|
Messages.error("Error", {}, d.message);
|
2016-09-02 03:25:20 +00:00
|
|
|
} else {
|
2016-08-03 03:11:09 +00:00
|
|
|
var execId = d.Id;
|
|
|
|
resizeTTY(execId, termHeight, termWidth);
|
2016-08-17 06:23:30 +00:00
|
|
|
var url = window.location.href.split('#')[0] + 'ws/exec?id=' + execId;
|
|
|
|
if (url.indexOf('https') > -1) {
|
|
|
|
url = url.replace('https://', 'wss://');
|
|
|
|
} else {
|
|
|
|
url = url.replace('http://', 'ws://');
|
|
|
|
}
|
2016-08-03 03:11:09 +00:00
|
|
|
initTerm(url, termHeight, termWidth);
|
|
|
|
}
|
|
|
|
}, function (e) {
|
|
|
|
$('#loadConsoleSpinner').hide();
|
2016-09-02 05:40:03 +00:00
|
|
|
Messages.error("Failure", e, 'Unable to start an exec instance');
|
2016-08-03 03:11:09 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.disconnect = function() {
|
|
|
|
$scope.connected = false;
|
|
|
|
if (socket !== null) {
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
if (term !== null) {
|
|
|
|
term.destroy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function resizeTTY(execId, height, width) {
|
|
|
|
$timeout(function() {
|
|
|
|
Exec.resize({id: execId, height: height, width: width}, function (d) {
|
2016-09-02 03:25:20 +00:00
|
|
|
if (d.message) {
|
2016-09-02 05:40:03 +00:00
|
|
|
Messages.error('Error', {}, 'Unable to resize TTY');
|
2016-08-03 03:11:09 +00:00
|
|
|
}
|
2016-09-02 03:25:20 +00:00
|
|
|
}, function (e) {
|
2016-09-02 05:40:03 +00:00
|
|
|
Messages.error("Failure", {}, 'Unable to resize TTY');
|
2016-08-03 03:11:09 +00:00
|
|
|
});
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function initTerm(url, height, width) {
|
|
|
|
socket = new WebSocket(url);
|
|
|
|
|
|
|
|
$scope.connected = true;
|
|
|
|
socket.onopen = function(evt) {
|
|
|
|
$('#loadConsoleSpinner').hide();
|
2016-10-07 04:19:25 +00:00
|
|
|
term = new Terminal();
|
2016-08-03 03:11:09 +00:00
|
|
|
|
|
|
|
term.on('data', function (data) {
|
|
|
|
socket.send(data);
|
|
|
|
});
|
|
|
|
term.open(document.getElementById('terminal-container'));
|
2016-10-07 04:19:25 +00:00
|
|
|
term.resize(width, height);
|
|
|
|
term.setOption('cursorBlink', true);
|
2016-08-03 03:11:09 +00:00
|
|
|
|
|
|
|
socket.onmessage = function (e) {
|
|
|
|
term.write(e.data);
|
|
|
|
};
|
|
|
|
socket.onerror = function (error) {
|
|
|
|
$scope.connected = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
socket.onclose = function(evt) {
|
|
|
|
$scope.connected = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}]);
|