open-nomad/ui/app/controllers/jobs/run.js

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-08-15 00:29:51 +00:00
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
2018-08-15 00:29:51 +00:00
import { computed } from '@ember/object';
import { task } from 'ember-concurrency';
2018-08-15 22:18:38 +00:00
import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error';
import localStorageProperty from 'nomad-ui/utils/properties/local-storage';
2018-08-15 00:29:51 +00:00
export default Controller.extend({
store: service(),
2018-08-15 22:18:38 +00:00
parseError: null,
planError: null,
runError: null,
planOutput: null,
2018-08-15 22:18:38 +00:00
showPlanMessage: localStorageProperty('nomadMessageJobPlan', true),
showEditorMessage: localStorageProperty('nomadMessageJobEditor', true),
2018-08-15 00:29:51 +00:00
stage: computed('planOutput', function() {
return this.get('planOutput') ? 'plan' : 'editor';
}),
plan: task(function*() {
2018-08-15 22:18:38 +00:00
this.reset();
2018-08-15 00:29:51 +00:00
try {
yield this.get('model').parse();
} catch (err) {
2018-08-15 22:18:38 +00:00
const error = messageFromAdapterError(err) || 'Could not parse input';
this.set('parseError', error);
return;
2018-08-15 00:29:51 +00:00
}
try {
yield this.get('model').plan();
const plan = this.get('store').peekRecord('job-plan', this.get('model.id'));
this.set('planOutput', plan);
2018-08-15 00:29:51 +00:00
} catch (err) {
2018-08-15 22:18:38 +00:00
const error = messageFromAdapterError(err) || 'Could not plan job';
this.set('planError', error);
2018-08-15 00:29:51 +00:00
}
}).drop(),
2018-08-15 01:26:26 +00:00
submit: task(function*() {
try {
yield this.get('model').run();
2018-08-15 22:18:38 +00:00
2018-08-15 01:26:26 +00:00
const id = this.get('model.plainId');
const namespace = this.get('model.namespace.name') || 'default';
2018-08-15 22:18:38 +00:00
this.reset();
2018-08-15 01:26:26 +00:00
// navigate to the new job page
this.transitionToRoute('jobs.job', id, {
queryParams: { jobNamespace: namespace },
});
} catch (err) {
2018-08-15 22:18:38 +00:00
const error = messageFromAdapterError(err) || 'Could not submit job';
this.set('runError', error);
2018-08-15 01:26:26 +00:00
}
}),
2018-08-15 00:29:51 +00:00
2018-08-15 22:18:38 +00:00
reset() {
2018-08-15 00:29:51 +00:00
this.set('planOutput', null);
this.set('planError', null);
this.set('parseError', null);
},
});