2018-02-01 12:27:52 +00:00
|
|
|
angular.module('portainer.docker')
|
2018-08-18 08:31:01 +00:00
|
|
|
.controller('ContainerConsoleController', ['$scope', '$transition$', 'ContainerService', 'ImageService', 'EndpointProvider', 'Notifications', 'ContainerHelper', 'ExecService', 'HttpRequestHelper', 'LocalStorage', 'CONSOLE_COMMANDS_LABEL_PREFIX',
|
|
|
|
function ($scope, $transition$, ContainerService, ImageService, EndpointProvider, Notifications, ContainerHelper, ExecService, HttpRequestHelper, LocalStorage, CONSOLE_COMMANDS_LABEL_PREFIX) {
|
2018-05-06 07:15:57 +00:00
|
|
|
var socket, term;
|
|
|
|
|
2017-11-12 19:27:28 +00:00
|
|
|
$scope.state = {
|
|
|
|
loaded: false,
|
|
|
|
connected: false
|
|
|
|
};
|
|
|
|
|
2017-07-10 07:10:10 +00:00
|
|
|
$scope.formValues = {};
|
2018-08-18 08:31:01 +00:00
|
|
|
$scope.containerCommands = [];
|
2017-07-11 14:52:39 +00:00
|
|
|
|
2016-08-03 03:11:09 +00:00
|
|
|
// Ensure the socket is closed before leaving the view
|
2018-08-22 15:33:06 +00:00
|
|
|
$scope.$on('$stateChangeStart', function () {
|
2016-10-01 08:44:23 +00:00
|
|
|
if (socket && socket !== null) {
|
2016-08-03 03:11:09 +00:00
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$scope.connect = function() {
|
2017-08-04 06:02:26 +00:00
|
|
|
var termWidth = Math.floor(($('#terminal-container').width() - 20) / 8.39);
|
2016-08-03 03:11:09 +00:00
|
|
|
var termHeight = 30;
|
2017-07-11 14:52:39 +00:00
|
|
|
var command = $scope.formValues.isCustomCommand ?
|
2017-07-10 07:10:10 +00:00
|
|
|
$scope.formValues.customCommand : $scope.formValues.command;
|
2016-08-03 03:11:09 +00:00
|
|
|
var execConfig = {
|
2017-09-21 14:00:53 +00:00
|
|
|
id: $transition$.params().id,
|
2016-08-03 03:11:09 +00:00
|
|
|
AttachStdin: true,
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
Tty: true,
|
2017-07-10 07:10:10 +00:00
|
|
|
User: $scope.formValues.user,
|
|
|
|
Cmd: ContainerHelper.commandStringToArray(command)
|
2016-08-03 03:11:09 +00:00
|
|
|
};
|
|
|
|
|
2017-07-13 16:04:58 +00:00
|
|
|
var execId;
|
|
|
|
ContainerService.createExec(execConfig)
|
|
|
|
.then(function success(data) {
|
|
|
|
execId = data.Id;
|
2018-06-18 09:56:31 +00:00
|
|
|
var jwtToken = LocalStorage.getJWT();
|
|
|
|
var url = window.location.href.split('#')[0] + 'api/websocket/exec?id=' + execId + '&endpointId=' + EndpointProvider.endpointID() + '&token=' + jwtToken;
|
2018-05-06 07:15:57 +00:00
|
|
|
if ($transition$.params().nodeName) {
|
|
|
|
url += '&nodeName=' + $transition$.params().nodeName;
|
|
|
|
}
|
2017-07-13 16:04:58 +00:00
|
|
|
if (url.indexOf('https') > -1) {
|
|
|
|
url = url.replace('https://', 'wss://');
|
2016-09-02 03:25:20 +00:00
|
|
|
} else {
|
2017-07-13 16:04:58 +00:00
|
|
|
url = url.replace('http://', 'ws://');
|
2016-08-03 03:11:09 +00:00
|
|
|
}
|
2017-07-13 16:04:58 +00:00
|
|
|
initTerm(url, termHeight, termWidth);
|
|
|
|
return ExecService.resizeTTY(execId, termHeight, termWidth, 2000);
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to exec into container');
|
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 initTerm(url, height, width) {
|
|
|
|
socket = new WebSocket(url);
|
|
|
|
|
2016-10-26 03:29:29 +00:00
|
|
|
$scope.state.connected = true;
|
2018-08-22 15:33:06 +00:00
|
|
|
socket.onopen = function() {
|
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);
|
|
|
|
});
|
2018-03-06 07:40:02 +00:00
|
|
|
term.open(document.getElementById('terminal-container'));
|
|
|
|
term.focus();
|
2016-10-07 04:19:25 +00:00
|
|
|
term.resize(width, height);
|
|
|
|
term.setOption('cursorBlink', true);
|
2017-08-04 06:02:26 +00:00
|
|
|
term.fit();
|
|
|
|
|
|
|
|
window.onresize = function() {
|
|
|
|
term.fit();
|
|
|
|
};
|
2016-08-03 03:11:09 +00:00
|
|
|
|
|
|
|
socket.onmessage = function (e) {
|
|
|
|
term.write(e.data);
|
|
|
|
};
|
2018-08-22 15:33:06 +00:00
|
|
|
socket.onerror = function () {
|
2016-10-26 03:29:29 +00:00
|
|
|
$scope.state.connected = false;
|
2016-08-03 03:11:09 +00:00
|
|
|
};
|
2018-08-22 15:33:06 +00:00
|
|
|
socket.onclose = function() {
|
2016-10-26 03:29:29 +00:00
|
|
|
$scope.state.connected = false;
|
2016-08-03 03:11:09 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
|
|
|
|
function initView() {
|
|
|
|
HttpRequestHelper.setPortainerAgentTargetHeader($transition$.params().nodeName);
|
|
|
|
ContainerService.container($transition$.params().id)
|
|
|
|
.then(function success(data) {
|
|
|
|
var container = data;
|
|
|
|
$scope.container = container;
|
|
|
|
return ImageService.image(container.Image);
|
|
|
|
})
|
|
|
|
.then(function success(data) {
|
|
|
|
var image = data;
|
2018-08-18 08:31:01 +00:00
|
|
|
var containerLabels = $scope.container.Config.Labels;
|
2018-05-06 07:15:57 +00:00
|
|
|
$scope.imageOS = image.Os;
|
|
|
|
$scope.formValues.command = image.Os === 'windows' ? 'powershell' : 'bash';
|
2018-08-18 08:31:01 +00:00
|
|
|
$scope.containerCommands = Object.keys(containerLabels)
|
|
|
|
.filter(function(label) {
|
|
|
|
return label.indexOf(CONSOLE_COMMANDS_LABEL_PREFIX) === 0;
|
|
|
|
})
|
|
|
|
.map(function(label) {
|
|
|
|
return {title: label.replace(CONSOLE_COMMANDS_LABEL_PREFIX, ''), command: containerLabels[label]};
|
|
|
|
});
|
2018-05-06 07:15:57 +00:00
|
|
|
$scope.state.loaded = true;
|
|
|
|
})
|
|
|
|
.catch(function error(err) {
|
|
|
|
Notifications.error('Error', err, 'Unable to retrieve container details');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
initView();
|
2016-08-03 03:11:09 +00:00
|
|
|
}]);
|