2019-09-26 18:47:07 +00:00
|
|
|
import { Factory } from 'ember-cli-mirage';
|
2019-10-03 14:13:08 +00:00
|
|
|
import faker from 'nomad-ui/mirage/faker';
|
2017-09-19 14:47:10 +00:00
|
|
|
import { generateResources } from '../common';
|
2022-04-07 19:51:41 +00:00
|
|
|
import { dasherize } from '@ember/string';
|
2022-08-29 18:04:55 +00:00
|
|
|
import { pickOne } from '../utils';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2018-05-12 00:30:15 +00:00
|
|
|
const DRIVERS = ['docker', 'java', 'rkt', 'qemu', 'exec', 'raw_exec'];
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
export default Factory.extend({
|
2020-10-29 12:46:42 +00:00
|
|
|
createRecommendations: false,
|
|
|
|
|
2022-08-29 18:04:55 +00:00
|
|
|
withServices: false,
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
// Hidden property used to compute the Summary hash
|
|
|
|
groupNames: [],
|
|
|
|
|
2020-02-12 19:49:28 +00:00
|
|
|
// Set in the TaskGroup factory
|
|
|
|
volumeMounts: [],
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
JobID: '',
|
|
|
|
|
2022-04-07 19:51:41 +00:00
|
|
|
name: (id) => `task-${dasherize(faker.hacker.noun())}-${id}`,
|
2019-09-30 14:44:22 +00:00
|
|
|
driver: () => faker.helpers.randomize(DRIVERS),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-10-12 22:26:54 +00:00
|
|
|
originalResources: generateResources,
|
2022-04-07 19:51:41 +00:00
|
|
|
resources: function () {
|
2020-10-12 22:26:54 +00:00
|
|
|
// Generate resources the usual way, but transform to the old
|
|
|
|
// shape because that's what the job spec uses.
|
|
|
|
const resources = this.originalResources;
|
|
|
|
return {
|
|
|
|
CPU: resources.Cpu.CpuShares,
|
|
|
|
MemoryMB: resources.Memory.MemoryMB,
|
2021-04-28 20:38:14 +00:00
|
|
|
MemoryMaxMB: resources.Memory.MemoryMaxMB,
|
2020-10-12 22:26:54 +00:00
|
|
|
DiskMB: resources.Disk.DiskMB,
|
|
|
|
};
|
|
|
|
},
|
2020-04-30 13:15:19 +00:00
|
|
|
|
2022-04-07 19:51:41 +00:00
|
|
|
Lifecycle: (i) => {
|
2020-09-01 16:49:36 +00:00
|
|
|
const cycle = i % 6;
|
2020-04-30 13:15:19 +00:00
|
|
|
|
|
|
|
if (cycle === 0) {
|
|
|
|
return null;
|
|
|
|
} else if (cycle === 1) {
|
|
|
|
return { Hook: 'prestart', Sidecar: false };
|
2020-08-25 17:17:18 +00:00
|
|
|
} else if (cycle === 2) {
|
2020-04-30 13:15:19 +00:00
|
|
|
return { Hook: 'prestart', Sidecar: true };
|
2020-08-25 17:17:18 +00:00
|
|
|
} else if (cycle === 3) {
|
|
|
|
return { Hook: 'poststart', Sidecar: false };
|
|
|
|
} else if (cycle === 4) {
|
|
|
|
return { Hook: 'poststart', Sidecar: true };
|
2020-09-01 16:49:36 +00:00
|
|
|
} else if (cycle === 5) {
|
|
|
|
return { Hook: 'poststop' };
|
2020-04-30 13:15:19 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-29 12:46:42 +00:00
|
|
|
|
|
|
|
afterCreate(task, server) {
|
|
|
|
if (task.createRecommendations) {
|
|
|
|
const recommendations = [];
|
|
|
|
|
|
|
|
if (faker.random.number(10) >= 1) {
|
2022-04-07 19:51:41 +00:00
|
|
|
recommendations.push(
|
|
|
|
server.create('recommendation', { task, resource: 'CPU' })
|
|
|
|
);
|
2020-10-29 12:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (faker.random.number(10) >= 1) {
|
2022-04-07 19:51:41 +00:00
|
|
|
recommendations.push(
|
|
|
|
server.create('recommendation', { task, resource: 'MemoryMB' })
|
|
|
|
);
|
2020-10-29 12:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
task.save({ recommendationIds: recommendations.mapBy('id') });
|
|
|
|
}
|
2022-08-29 18:04:55 +00:00
|
|
|
|
|
|
|
if (task.withServices) {
|
|
|
|
const services = server.createList('service-fragment', 1, {
|
|
|
|
provider: 'nomad',
|
2022-09-07 14:23:39 +00:00
|
|
|
taskName: task.name,
|
2022-08-29 18:04:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
services.push(
|
|
|
|
server.create('service-fragment', {
|
|
|
|
provider: 'consul',
|
2022-09-07 14:23:39 +00:00
|
|
|
taskName: task.name,
|
2022-08-29 18:04:55 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
services.forEach((fragment) => {
|
|
|
|
server.createList('service', 5, {
|
|
|
|
serviceName: fragment.name,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
task.update({ services });
|
|
|
|
}
|
2020-10-29 12:46:42 +00:00
|
|
|
},
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|