2020-03-24 23:22:16 +00:00
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
|
import Controller from '@ember/controller';
|
2020-04-06 18:52:42 +00:00
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
|
import { alias, mapBy, sort, uniq } from '@ember/object/computed';
|
2020-03-24 23:22:16 +00:00
|
|
|
|
import escapeTaskName from 'nomad-ui/utils/escape-task-name';
|
|
|
|
|
import ExecCommandEditorXtermAdapter from 'nomad-ui/utils/classes/exec-command-editor-xterm-adapter';
|
|
|
|
|
import ExecSocketXtermAdapter from 'nomad-ui/utils/classes/exec-socket-xterm-adapter';
|
2020-04-01 13:08:42 +00:00
|
|
|
|
import localStorageProperty from 'nomad-ui/utils/properties/local-storage';
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
|
|
|
|
const ANSI_UI_GRAY_400 = '\x1b[38;2;142;150;163m';
|
|
|
|
|
const ANSI_WHITE = '\x1b[0m';
|
|
|
|
|
|
|
|
|
|
export default Controller.extend({
|
|
|
|
|
sockets: service(),
|
|
|
|
|
system: service(),
|
2020-04-03 15:22:22 +00:00
|
|
|
|
token: service(),
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
|
|
|
|
queryParams: ['allocation'],
|
|
|
|
|
|
2020-04-01 13:08:42 +00:00
|
|
|
|
command: localStorageProperty('nomadExecCommand', '/bin/bash'),
|
2020-03-24 23:22:16 +00:00
|
|
|
|
socketOpen: false,
|
|
|
|
|
|
2020-04-06 18:52:42 +00:00
|
|
|
|
pendingAndRunningAllocations: computed('model.allocations.@each.clientStatus', function() {
|
|
|
|
|
return this.model.allocations.filter(
|
|
|
|
|
allocation => allocation.clientStatus === 'pending' || allocation.clientStatus === 'running'
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
pendingAndRunningTaskGroups: mapBy('pendingAndRunningAllocations', 'taskGroup'),
|
|
|
|
|
uniquePendingAndRunningTaskGroups: uniq('pendingAndRunningTaskGroups'),
|
|
|
|
|
|
|
|
|
|
taskGroupSorting: Object.freeze(['name']),
|
|
|
|
|
sortedTaskGroups: sort('uniquePendingAndRunningTaskGroups', 'taskGroupSorting'),
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
2020-05-26 14:56:25 +00:00
|
|
|
|
setUpTerminal(Terminal) {
|
2020-03-24 23:22:16 +00:00
|
|
|
|
this.terminal = new Terminal({ fontFamily: 'monospace', fontWeight: '400' });
|
|
|
|
|
window.execTerminal = this.terminal; // Issue to improve: https://github.com/hashicorp/nomad/issues/7457
|
|
|
|
|
|
|
|
|
|
this.terminal.write(ANSI_UI_GRAY_400);
|
|
|
|
|
this.terminal.writeln('Select a task to start your session.');
|
|
|
|
|
},
|
|
|
|
|
|
2020-04-06 18:52:42 +00:00
|
|
|
|
allocations: alias('model.allocations'),
|
|
|
|
|
|
|
|
|
|
taskState: computed(
|
2020-06-09 21:03:28 +00:00
|
|
|
|
'allocations.{[],@each.isActive}',
|
2020-04-06 18:52:42 +00:00
|
|
|
|
'allocationShortId',
|
|
|
|
|
'taskName',
|
|
|
|
|
'taskGroupName',
|
|
|
|
|
'allocation',
|
2020-06-09 21:03:28 +00:00
|
|
|
|
'allocation.states.@each.{name,isRunning}',
|
2020-04-06 18:52:42 +00:00
|
|
|
|
function() {
|
|
|
|
|
if (!this.allocations) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let allocation;
|
|
|
|
|
|
|
|
|
|
if (this.allocationShortId) {
|
|
|
|
|
allocation = this.allocations.findBy('shortId', this.allocationShortId);
|
|
|
|
|
} else {
|
|
|
|
|
allocation = this.allocations.find(allocation =>
|
|
|
|
|
allocation.states
|
|
|
|
|
.filterBy('isActive')
|
|
|
|
|
.mapBy('name')
|
|
|
|
|
.includes(this.taskName)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (allocation) {
|
|
|
|
|
return allocation.states.find(state => state.name === this.taskName);
|
|
|
|
|
}
|
2020-06-09 21:03:28 +00:00
|
|
|
|
|
|
|
|
|
return;
|
2020-04-06 18:52:42 +00:00
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
|
2020-03-24 23:22:16 +00:00
|
|
|
|
actions: {
|
2020-04-06 18:52:42 +00:00
|
|
|
|
setTaskProperties({ allocationShortId, taskName, taskGroupName }) {
|
|
|
|
|
this.setProperties({
|
|
|
|
|
allocationShortId,
|
|
|
|
|
taskName,
|
|
|
|
|
taskGroupName,
|
|
|
|
|
});
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
2020-04-06 19:08:22 +00:00
|
|
|
|
if (this.taskState) {
|
|
|
|
|
this.terminal.write(ANSI_UI_GRAY_400);
|
|
|
|
|
this.terminal.writeln('');
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
2020-04-06 19:08:22 +00:00
|
|
|
|
if (!allocationShortId) {
|
|
|
|
|
this.terminal.writeln(
|
|
|
|
|
'Multiple instances of this task are running. The allocation below was selected by random draw.'
|
|
|
|
|
);
|
|
|
|
|
this.terminal.writeln('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.terminal.writeln('Customize your command, then hit ‘return’ to run.');
|
2020-03-24 23:22:16 +00:00
|
|
|
|
this.terminal.writeln('');
|
2020-04-06 19:08:22 +00:00
|
|
|
|
this.terminal.write(
|
|
|
|
|
`$ nomad alloc exec -i -t -task ${escapeTaskName(taskName)} ${
|
|
|
|
|
this.taskState.allocation.shortId
|
|
|
|
|
} `
|
|
|
|
|
);
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
2020-04-06 19:08:22 +00:00
|
|
|
|
this.terminal.write(ANSI_WHITE);
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
2020-04-06 19:08:22 +00:00
|
|
|
|
this.terminal.write(this.command);
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
2020-04-06 19:08:22 +00:00
|
|
|
|
if (this.commandEditorAdapter) {
|
|
|
|
|
this.commandEditorAdapter.destroy();
|
|
|
|
|
}
|
2020-03-24 23:22:16 +00:00
|
|
|
|
|
2020-04-06 19:08:22 +00:00
|
|
|
|
this.commandEditorAdapter = new ExecCommandEditorXtermAdapter(
|
|
|
|
|
this.terminal,
|
|
|
|
|
this.openAndConnectSocket.bind(this),
|
|
|
|
|
this.command
|
|
|
|
|
);
|
2020-03-24 23:22:16 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
openAndConnectSocket(command) {
|
2020-04-06 19:08:22 +00:00
|
|
|
|
if (this.taskState) {
|
|
|
|
|
this.set('socketOpen', true);
|
|
|
|
|
this.set('command', command);
|
|
|
|
|
this.socket = this.sockets.getTaskStateSocket(this.taskState, command);
|
|
|
|
|
|
|
|
|
|
new ExecSocketXtermAdapter(this.terminal, this.socket, this.token.secret);
|
|
|
|
|
} else {
|
|
|
|
|
this.terminal.writeln(`Failed to open a socket because task ${this.taskName} is not active.`);
|
|
|
|
|
}
|
2020-03-24 23:22:16 +00:00
|
|
|
|
},
|
|
|
|
|
});
|