2016-08-03 03:11:09 +00:00
|
|
|
angular.module('containerConsole', [])
|
2017-04-12 19:47:22 +00:00
|
|
|
.controller('ContainerConsoleController', ['$scope', '$stateParams', 'Settings', 'Container', 'Image', 'Exec', '$timeout', 'EndpointProvider', 'Notifications',
|
|
|
|
function ($scope, $stateParams, Settings, Container, Image, Exec, $timeout, EndpointProvider, Notifications) {
|
2016-08-03 03:11:09 +00:00
|
|
|
$scope.state = {};
|
2016-10-26 03:29:29 +00:00
|
|
|
$scope.state.loaded = false;
|
|
|
|
$scope.state.connected = false;
|
2016-08-03 03:11:09 +00:00
|
|
|
|
|
|
|
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;
|
2016-10-26 03:29:29 +00:00
|
|
|
if (d.message) {
|
2017-04-12 19:47:22 +00:00
|
|
|
Notifications.error("Error", d, 'Unable to retrieve container details');
|
2016-10-26 03:29:29 +00:00
|
|
|
$('#loadingViewSpinner').hide();
|
|
|
|
} else {
|
|
|
|
Image.get({id: d.Image}, function(imgData) {
|
|
|
|
$scope.imageOS = imgData.Os;
|
|
|
|
$scope.state.command = imgData.Os === 'windows' ? 'powershell' : 'bash';
|
|
|
|
$scope.state.loaded = true;
|
|
|
|
$('#loadingViewSpinner').hide();
|
|
|
|
}, function (e) {
|
2017-04-12 19:47:22 +00:00
|
|
|
Notifications.error("Failure", e, 'Unable to retrieve image details');
|
2016-10-26 03:29:29 +00:00
|
|
|
$('#loadingViewSpinner').hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, function (e) {
|
2017-04-12 19:47:22 +00:00
|
|
|
Notifications.error("Failure", e, 'Unable to retrieve container details');
|
2016-08-03 03:11:09 +00:00
|
|
|
$('#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();
|
2017-04-12 19:47:22 +00:00
|
|
|
Notifications.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);
|
2017-03-12 16:24:15 +00:00
|
|
|
var url = window.location.href.split('#')[0] + 'api/websocket/exec?id=' + execId + '&endpointId=' + EndpointProvider.endpointID();
|
2016-08-17 06:23:30 +00:00
|
|
|
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();
|
2017-04-12 19:47:22 +00:00
|
|
|
Notifications.error("Failure", e, 'Unable to start an exec instance');
|
2016-08-03 03:11:09 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.disconnect = function() {
|
2016-10-26 03:29:29 +00:00
|
|
|
$scope.state.connected = false;
|
2016-08-03 03:11:09 +00:00
|
|
|
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) {
|
2017-04-12 19:47:22 +00:00
|
|
|
Notifications.error('Error', {}, 'Unable to resize TTY');
|
2016-08-03 03:11:09 +00:00
|
|
|
}
|
2016-09-02 03:25:20 +00:00
|
|
|
}, function (e) {
|
2017-04-12 19:47:22 +00:00
|
|
|
Notifications.error("Failure", {}, 'Unable to resize TTY');
|
2016-08-03 03:11:09 +00:00
|
|
|
});
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function initTerm(url, height, width) {
|
|
|
|
socket = new WebSocket(url);
|
|
|
|
|
2016-10-26 03:29:29 +00:00
|
|
|
$scope.state.connected = true;
|
2016-08-03 03:11:09 +00:00
|
|
|
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) {
|
2016-10-26 03:29:29 +00:00
|
|
|
$scope.state.connected = false;
|
2016-08-03 03:11:09 +00:00
|
|
|
};
|
|
|
|
socket.onclose = function(evt) {
|
2016-10-26 03:29:29 +00:00
|
|
|
$scope.state.connected = false;
|
2016-08-03 03:11:09 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}]);
|