2019-06-27 16:21:00 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import Component from '@ember/component';
|
2020-06-10 13:49:16 +00:00
|
|
|
import { action, computed } from '@ember/object';
|
|
|
|
import { equal, gt } from '@ember/object/computed';
|
2019-06-27 16:21:00 +00:00
|
|
|
import RSVP from 'rsvp';
|
2019-07-02 22:37:17 +00:00
|
|
|
import Log from 'nomad-ui/utils/classes/log';
|
2019-06-27 16:21:00 +00:00
|
|
|
import timeout from 'nomad-ui/utils/timeout';
|
2020-06-10 13:49:16 +00:00
|
|
|
import { classNames } from '@ember-decorators/component';
|
|
|
|
import classic from 'ember-classic-decorator';
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
@classNames('boxed-section', 'task-log')
|
|
|
|
export default class File extends Component {
|
|
|
|
@service token;
|
2020-07-22 20:46:38 +00:00
|
|
|
@service system;
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
'data-test-file-viewer' = true;
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
allocation = null;
|
|
|
|
taskState = null;
|
|
|
|
file = null;
|
|
|
|
stat = null; // { Name, IsDir, Size, FileMode, ModTime, ContentType }
|
2019-06-27 16:21:00 +00:00
|
|
|
|
|
|
|
// When true, request logs from the server agent
|
2020-06-10 13:49:16 +00:00
|
|
|
useServer = false;
|
2019-06-27 16:21:00 +00:00
|
|
|
|
|
|
|
// When true, logs cannot be fetched from either the client or the server
|
2020-06-10 13:49:16 +00:00
|
|
|
noConnection = false;
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
clientTimeout = 1000;
|
|
|
|
serverTimeout = 5000;
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
mode = 'head';
|
2019-07-02 20:01:07 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('stat.ContentType')
|
|
|
|
get fileComponent() {
|
2019-07-26 05:44:29 +00:00
|
|
|
const contentType = this.stat.ContentType || '';
|
2019-07-20 19:00:20 +00:00
|
|
|
|
|
|
|
if (contentType.startsWith('image/')) {
|
|
|
|
return 'image';
|
|
|
|
} else if (contentType.startsWith('text/') || contentType.startsWith('application/json')) {
|
|
|
|
return 'stream';
|
|
|
|
} else {
|
|
|
|
return 'unknown';
|
2019-06-27 16:21:00 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@gt('stat.Size', 50000) isLarge;
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@equal('fileComponent', 'unknown') fileTypeIsUnknown;
|
|
|
|
@equal('fileComponent', 'stream') isStreamable;
|
|
|
|
isStreaming = false;
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('allocation.id', 'taskState.name', 'file')
|
|
|
|
get catUrl() {
|
2020-06-01 19:03:56 +00:00
|
|
|
const taskUrlPrefix = this.taskState ? `${this.taskState.name}/` : '';
|
2020-06-01 13:15:59 +00:00
|
|
|
const encodedPath = encodeURIComponent(`${taskUrlPrefix}${this.file}`);
|
2020-07-22 20:46:38 +00:00
|
|
|
let apiPath = `/v1/client/fs/cat/${this.allocation.id}?path=${encodedPath}`;
|
|
|
|
if (this.system.shouldIncludeRegion) {
|
|
|
|
apiPath += `®ion=${this.system.activeRegion}`;
|
|
|
|
}
|
|
|
|
return apiPath;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('isLarge', 'mode')
|
|
|
|
get fetchMode() {
|
2019-07-20 19:00:20 +00:00
|
|
|
if (this.mode === 'streaming') {
|
|
|
|
return 'stream';
|
|
|
|
}
|
|
|
|
|
2019-07-02 20:01:07 +00:00
|
|
|
if (!this.isLarge) {
|
|
|
|
return 'cat';
|
2019-07-02 22:37:17 +00:00
|
|
|
} else if (this.mode === 'head' || this.mode === 'tail') {
|
2019-07-02 20:01:07 +00:00
|
|
|
return 'readat';
|
|
|
|
}
|
2020-06-09 21:03:28 +00:00
|
|
|
|
2020-06-10 14:07:16 +00:00
|
|
|
return undefined;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('allocation.{id,node.httpAddr}', 'fetchMode', 'useServer')
|
|
|
|
get fileUrl() {
|
2020-06-09 21:03:28 +00:00
|
|
|
const address = this.get('allocation.node.httpAddr');
|
|
|
|
const url = `/v1/client/fs/${this.fetchMode}/${this.allocation.id}`;
|
|
|
|
return this.useServer ? url : `//${address}${url}`;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-07-02 20:01:07 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('taskState.name', 'file', 'mode')
|
|
|
|
get fileParams() {
|
2019-08-12 20:51:38 +00:00
|
|
|
// The Log class handles encoding query params
|
2020-06-01 19:03:56 +00:00
|
|
|
const taskUrlPrefix = this.taskState ? `${this.taskState.name}/` : '';
|
2020-06-01 13:15:59 +00:00
|
|
|
const path = `${taskUrlPrefix}${this.file}`;
|
2019-07-02 20:01:07 +00:00
|
|
|
|
|
|
|
switch (this.mode) {
|
|
|
|
case 'head':
|
|
|
|
return { path, offset: 0, limit: 50000 };
|
|
|
|
case 'tail':
|
2019-07-02 22:37:17 +00:00
|
|
|
return { path, offset: this.stat.Size - 50000, limit: 50000 };
|
|
|
|
case 'streaming':
|
2019-07-02 20:01:07 +00:00
|
|
|
return { path, offset: 50000, origin: 'end' };
|
|
|
|
default:
|
|
|
|
return { path };
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@computed('fileUrl', 'fileParams', 'mode')
|
|
|
|
get logger() {
|
2019-07-02 22:37:17 +00:00
|
|
|
// The cat and readat APIs are in plainText while the stream API is always encoded.
|
|
|
|
const plainText = this.mode === 'head' || this.mode === 'tail';
|
|
|
|
|
2019-07-02 20:01:07 +00:00
|
|
|
// If the file request can't settle in one second, the client
|
2019-06-27 16:21:00 +00:00
|
|
|
// must be unavailable and the server should be used instead
|
|
|
|
const timing = this.useServer ? this.serverTimeout : this.clientTimeout;
|
2019-07-02 22:37:17 +00:00
|
|
|
const logFetch = url =>
|
2019-06-27 16:21:00 +00:00
|
|
|
RSVP.race([this.token.authorizedRequest(url), timeout(timing)]).then(
|
2019-08-08 00:35:15 +00:00
|
|
|
response => {
|
|
|
|
if (!response || !response.ok) {
|
|
|
|
this.nextErrorState(response);
|
2019-06-27 16:21:00 +00:00
|
|
|
}
|
2019-08-08 00:35:15 +00:00
|
|
|
return response;
|
|
|
|
},
|
|
|
|
error => this.nextErrorState(error)
|
2019-06-27 16:21:00 +00:00
|
|
|
);
|
2019-07-02 22:37:17 +00:00
|
|
|
|
|
|
|
return Log.create({
|
|
|
|
logFetch,
|
|
|
|
plainText,
|
|
|
|
params: this.fileParams,
|
|
|
|
url: this.fileUrl,
|
|
|
|
});
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 16:21:00 +00:00
|
|
|
|
2019-08-08 00:35:15 +00:00
|
|
|
nextErrorState(error) {
|
|
|
|
if (this.useServer) {
|
|
|
|
this.set('noConnection', true);
|
|
|
|
} else {
|
|
|
|
this.send('failoverToServer');
|
|
|
|
}
|
|
|
|
throw error;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleStream() {
|
|
|
|
this.set('mode', 'streaming');
|
|
|
|
this.toggleProperty('isStreaming');
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
gotoHead() {
|
|
|
|
this.set('mode', 'head');
|
|
|
|
this.set('isStreaming', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
gotoTail() {
|
|
|
|
this.set('mode', 'tail');
|
|
|
|
this.set('isStreaming', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
failoverToServer() {
|
|
|
|
this.set('useServer', true);
|
|
|
|
}
|
|
|
|
}
|