2016-08-03 03:11:09 +00:00
|
|
|
angular.module('containerConsole', [])
|
2017-07-13 16:04:58 +00:00
|
|
|
.controller('ContainerConsoleController', ['$scope', '$stateParams', 'Container', 'Image', 'EndpointProvider', 'Notifications', 'ContainerHelper', 'ContainerService', 'ExecService',
|
|
|
|
function ($scope, $stateParams, Container, Image, EndpointProvider, Notifications, ContainerHelper, ContainerService, ExecService) {
|
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;
|
2017-07-10 07:10:10 +00:00
|
|
|
$scope.formValues = {};
|
2017-07-11 14:52:39 +00:00
|
|
|
|
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-05-23 18:56:10 +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;
|
2017-07-10 07:10:10 +00:00
|
|
|
$scope.formValues.command = imgData.Os === 'windows' ? 'powershell' : 'bash';
|
2016-10-26 03:29:29 +00:00
|
|
|
$scope.state.loaded = true;
|
|
|
|
$('#loadingViewSpinner').hide();
|
|
|
|
}, function (e) {
|
2017-05-23 18:56:10 +00:00
|
|
|
Notifications.error('Failure', e, 'Unable to retrieve image details');
|
2016-10-26 03:29:29 +00:00
|
|
|
$('#loadingViewSpinner').hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, function (e) {
|
2017-05-23 18:56:10 +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;
|
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 = {
|
|
|
|
id: $stateParams.id,
|
|
|
|
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;
|
|
|
|
var url = window.location.href.split('#')[0] + 'api/websocket/exec?id=' + execId + '&endpointId=' + EndpointProvider.endpointID();
|
|
|
|
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');
|
|
|
|
})
|
|
|
|
.finally(function final() {
|
2016-08-03 03:11:09 +00:00
|
|
|
$('#loadConsoleSpinner').hide();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$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;
|
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);
|
|
|
|
});
|
2017-07-11 14:52:39 +00:00
|
|
|
term.open(document.getElementById('terminal-container'), true);
|
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
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}]);
|