2018-08-21 19:07:45 +00:00
|
|
|
import Component from '@ember/component';
|
2018-08-21 23:43:46 +00:00
|
|
|
import { assert } from '@ember/debug';
|
2018-08-21 19:07:45 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2022-03-08 17:28:36 +00:00
|
|
|
import { computed, action } from '@ember/object';
|
2018-08-21 19:07:45 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error';
|
|
|
|
import localStorageProperty from 'nomad-ui/utils/properties/local-storage';
|
2022-03-08 17:28:36 +00:00
|
|
|
import { attributeBindings } from '@ember-decorators/component';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
2022-03-08 17:28:36 +00:00
|
|
|
@attributeBindings('data-test-job-editor')
|
2020-06-10 13:49:16 +00:00
|
|
|
export default class JobEditor extends Component {
|
|
|
|
@service store;
|
|
|
|
@service config;
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
'data-test-job-editor' = true;
|
2018-08-23 00:36:04 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
job = null;
|
|
|
|
onSubmit() {}
|
2018-08-21 23:43:46 +00:00
|
|
|
|
2021-02-17 21:01:44 +00:00
|
|
|
@computed('_context')
|
2020-06-10 13:49:16 +00:00
|
|
|
get context() {
|
|
|
|
return this._context;
|
|
|
|
}
|
2018-08-21 23:43:46 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
set context(value) {
|
|
|
|
const allowedValues = ['new', 'edit'];
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert(
|
|
|
|
`context must be one of: ${allowedValues.join(', ')}`,
|
|
|
|
allowedValues.includes(value)
|
|
|
|
);
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
this.set('_context', value);
|
|
|
|
}
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2022-03-08 17:28:36 +00:00
|
|
|
@action updateCode(value) {
|
|
|
|
if (!this.job.isDestroying && !this.job.isDestroyed) {
|
|
|
|
this.job.set('_newDefinition', value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
_context = null;
|
|
|
|
parseError = null;
|
|
|
|
planError = null;
|
|
|
|
runError = null;
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
planOutput = null;
|
|
|
|
|
|
|
|
@localStorageProperty('nomadMessageJobPlan', true) showPlanMessage;
|
|
|
|
|
|
|
|
@computed('planOutput')
|
|
|
|
get stage() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.planOutput ? 'plan' : 'editor';
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@(task(function* () {
|
2018-08-21 19:07:45 +00:00
|
|
|
this.reset();
|
|
|
|
|
|
|
|
try {
|
2019-03-26 07:46:44 +00:00
|
|
|
yield this.job.parse();
|
2018-08-21 19:07:45 +00:00
|
|
|
} catch (err) {
|
2022-07-11 16:33:17 +00:00
|
|
|
const error =
|
|
|
|
messageFromAdapterError(err, 'parse jobs') || 'Could not parse input';
|
2018-08-21 19:07:45 +00:00
|
|
|
this.set('parseError', error);
|
2018-08-21 23:41:05 +00:00
|
|
|
this.scrollToError();
|
2018-08-21 19:07:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-03-26 07:46:44 +00:00
|
|
|
const plan = yield this.job.plan();
|
2018-08-21 19:07:45 +00:00
|
|
|
this.set('planOutput', plan);
|
|
|
|
} catch (err) {
|
2022-07-11 16:33:17 +00:00
|
|
|
const error =
|
|
|
|
messageFromAdapterError(err, 'plan jobs') || 'Could not plan job';
|
2018-08-21 19:07:45 +00:00
|
|
|
this.set('planError', error);
|
2018-08-21 23:41:05 +00:00
|
|
|
this.scrollToError();
|
2018-08-21 19:07:45 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}).drop())
|
|
|
|
plan;
|
2018-08-21 19:07:45 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2018-08-21 19:07:45 +00:00
|
|
|
try {
|
2019-03-26 07:46:44 +00:00
|
|
|
if (this.context === 'new') {
|
|
|
|
yield this.job.run();
|
2018-08-21 23:41:05 +00:00
|
|
|
} else {
|
2019-03-26 07:46:44 +00:00
|
|
|
yield this.job.update();
|
2018-08-21 23:41:05 +00:00
|
|
|
}
|
2018-08-21 19:07:45 +00:00
|
|
|
|
|
|
|
const id = this.get('job.plainId');
|
|
|
|
const namespace = this.get('job.namespace.name') || 'default';
|
|
|
|
|
|
|
|
this.reset();
|
|
|
|
|
|
|
|
// Treat the job as ephemeral and only provide ID parts.
|
2019-03-26 07:46:44 +00:00
|
|
|
this.onSubmit(id, namespace);
|
2018-08-21 19:07:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
const error = messageFromAdapterError(err) || 'Could not submit job';
|
|
|
|
this.set('runError', error);
|
2018-08-21 23:41:05 +00:00
|
|
|
this.set('planOutput', null);
|
|
|
|
this.scrollToError();
|
2018-08-21 19:07:45 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
submit;
|
2018-08-21 19:07:45 +00:00
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.set('planOutput', null);
|
|
|
|
this.set('planError', null);
|
|
|
|
this.set('parseError', null);
|
2018-08-21 23:41:05 +00:00
|
|
|
this.set('runError', null);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-08-21 23:41:05 +00:00
|
|
|
|
|
|
|
scrollToError() {
|
|
|
|
if (!this.get('config.isTest')) {
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2022-11-02 14:34:10 +00:00
|
|
|
|
|
|
|
@action uploadJobSpec(event) {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = () => {
|
|
|
|
this.updateCode(reader.result);
|
|
|
|
};
|
|
|
|
|
|
|
|
const [file] = event.target.files;
|
|
|
|
reader.readAsText(file);
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|