2020-02-12 19:49:28 +00:00
|
|
|
import { Factory, trait } from 'ember-cli-mirage';
|
2019-10-03 14:13:08 +00:00
|
|
|
import faker from 'nomad-ui/mirage/faker';
|
2020-02-12 19:49:28 +00:00
|
|
|
import { provide } from '../utils';
|
2020-08-24 16:24:32 +00:00
|
|
|
import { generateResources } from '../common';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
const DISK_RESERVATIONS = [200, 500, 1000, 2000, 5000, 10000, 100000];
|
|
|
|
|
|
|
|
export default Factory.extend({
|
2018-04-12 17:47:12 +00:00
|
|
|
name: id => `${faker.hacker.noun().dasherize()}-g-${id}`,
|
2019-04-12 03:08:09 +00:00
|
|
|
count: () => faker.random.number({ min: 1, max: 2 }),
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
ephemeralDisk: () => ({
|
|
|
|
Sticky: faker.random.boolean(),
|
2019-09-26 18:47:07 +00:00
|
|
|
SizeMB: faker.helpers.randomize(DISK_RESERVATIONS),
|
2017-09-19 14:47:10 +00:00
|
|
|
Migrate: faker.random.boolean(),
|
|
|
|
}),
|
|
|
|
|
2020-02-12 19:49:28 +00:00
|
|
|
noHostVolumes: trait({
|
2020-02-13 23:47:01 +00:00
|
|
|
volumes: () => ({}),
|
2020-02-12 19:49:28 +00:00
|
|
|
}),
|
|
|
|
|
2020-06-18 01:07:53 +00:00
|
|
|
withScaling: faker.random.boolean,
|
|
|
|
|
2020-02-12 19:49:28 +00:00
|
|
|
volumes: makeHostVolumes(),
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
// Directive used to control whether or not allocations are automatically
|
|
|
|
// created.
|
|
|
|
createAllocations: true,
|
|
|
|
|
2018-05-04 20:08:30 +00:00
|
|
|
// Directived used to control whether or not the allocation should fail
|
|
|
|
// and reschedule, creating reschedule events.
|
|
|
|
withRescheduling: false,
|
|
|
|
|
2019-09-04 14:39:56 +00:00
|
|
|
// Directive used to control whether the task group should have services.
|
|
|
|
withServices: false,
|
|
|
|
|
2020-10-29 12:46:42 +00:00
|
|
|
// Directive used to control whether dynamic application sizing recommendations
|
|
|
|
// should be created.
|
|
|
|
createRecommendations: false,
|
|
|
|
|
2019-04-12 03:08:09 +00:00
|
|
|
// When true, only creates allocations
|
|
|
|
shallow: false,
|
|
|
|
|
2020-08-24 16:24:32 +00:00
|
|
|
// When set, passed into tasks to set resource values
|
|
|
|
resourceSpec: null,
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
afterCreate(group, server) {
|
2019-04-12 03:08:09 +00:00
|
|
|
let taskIds = [];
|
2020-02-12 19:49:28 +00:00
|
|
|
let volumes = Object.keys(group.volumes);
|
2019-04-12 03:08:09 +00:00
|
|
|
|
2020-06-18 01:07:53 +00:00
|
|
|
if (group.withScaling) {
|
|
|
|
group.update({
|
|
|
|
scaling: {
|
|
|
|
Min: 1,
|
|
|
|
Max: 5,
|
|
|
|
Policy: faker.random.boolean() && {
|
|
|
|
EvaluationInterval: '10s',
|
|
|
|
Cooldown: '2m',
|
|
|
|
Check: {
|
|
|
|
avg_conn: {
|
|
|
|
Source: 'prometheus',
|
|
|
|
Query:
|
|
|
|
'scalar(avg((haproxy_server_current_sessions{backend="http_back"}) and (haproxy_server_up{backend="http_back"} == 1)))',
|
|
|
|
Strategy: {
|
|
|
|
'target-value': {
|
|
|
|
target: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-12 03:08:09 +00:00
|
|
|
if (!group.shallow) {
|
2020-08-24 16:24:32 +00:00
|
|
|
const resources =
|
|
|
|
group.resourceSpec && divide(group.count, parseResourceSpec(group.resourceSpec));
|
|
|
|
const tasks = provide(group.count, (_, idx) => {
|
2020-02-13 23:47:01 +00:00
|
|
|
const mounts = faker.helpers
|
|
|
|
.shuffle(volumes)
|
|
|
|
.slice(0, faker.random.number({ min: 1, max: 3 }));
|
2020-08-24 16:24:32 +00:00
|
|
|
|
|
|
|
const maybeResources = {};
|
|
|
|
if (resources) {
|
2020-10-13 14:54:07 +00:00
|
|
|
maybeResources.originalResources = generateResources(resources[idx]);
|
2020-08-24 16:24:32 +00:00
|
|
|
}
|
2020-02-12 19:49:28 +00:00
|
|
|
return server.create('task', {
|
|
|
|
taskGroup: group,
|
2020-08-24 16:24:32 +00:00
|
|
|
...maybeResources,
|
2020-02-12 19:49:28 +00:00
|
|
|
volumeMounts: mounts.map(mount => ({
|
|
|
|
Volume: mount,
|
|
|
|
Destination: `/${faker.internet.userName()}/${faker.internet.domainWord()}/${faker.internet.color()}`,
|
|
|
|
PropagationMode: '',
|
|
|
|
ReadOnly: faker.random.boolean(),
|
|
|
|
})),
|
2020-10-29 12:46:42 +00:00
|
|
|
createRecommendations: group.createRecommendations,
|
2020-02-12 19:49:28 +00:00
|
|
|
});
|
2019-04-12 03:08:09 +00:00
|
|
|
});
|
|
|
|
taskIds = tasks.mapBy('id');
|
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
group.update({
|
2019-04-12 03:08:09 +00:00
|
|
|
taskIds: taskIds,
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (group.createAllocations) {
|
|
|
|
Array(group.count)
|
|
|
|
.fill(null)
|
|
|
|
.forEach((_, i) => {
|
2018-05-04 20:08:30 +00:00
|
|
|
const props = {
|
2017-09-19 14:47:10 +00:00
|
|
|
jobId: group.job.id,
|
2018-02-16 02:55:59 +00:00
|
|
|
namespace: group.job.namespace,
|
2017-09-19 14:47:10 +00:00
|
|
|
taskGroup: group.name,
|
|
|
|
name: `${group.name}.[${i}]`,
|
2018-05-04 20:08:30 +00:00
|
|
|
rescheduleSuccess: group.withRescheduling ? faker.random.boolean() : null,
|
|
|
|
rescheduleAttempts: group.withRescheduling
|
|
|
|
? faker.random.number({ min: 1, max: 5 })
|
|
|
|
: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (group.withRescheduling) {
|
|
|
|
server.create('allocation', 'rescheduled', props);
|
|
|
|
} else {
|
|
|
|
server.create('allocation', props);
|
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
});
|
|
|
|
}
|
2019-09-04 14:39:56 +00:00
|
|
|
|
|
|
|
if (group.withServices) {
|
|
|
|
Array(faker.random.number({ min: 1, max: 3 }))
|
|
|
|
.fill(null)
|
|
|
|
.forEach(() => {
|
|
|
|
server.create('service', {
|
2020-08-28 00:58:24 +00:00
|
|
|
taskGroup: group,
|
2019-09-04 14:39:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
},
|
|
|
|
});
|
2020-02-12 19:49:28 +00:00
|
|
|
|
|
|
|
function makeHostVolumes() {
|
|
|
|
const generate = () => ({
|
|
|
|
Name: faker.internet.domainWord(),
|
|
|
|
Type: 'host',
|
2020-02-13 23:47:01 +00:00
|
|
|
Source: faker.internet.domainWord(),
|
2020-02-12 19:49:28 +00:00
|
|
|
ReadOnly: faker.random.boolean(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const volumes = provide(faker.random.number({ min: 1, max: 5 }), generate);
|
|
|
|
return volumes.reduce((hash, volume) => {
|
|
|
|
hash[volume.Name] = volume;
|
|
|
|
return hash;
|
|
|
|
}, {});
|
|
|
|
}
|
2020-08-24 16:24:32 +00:00
|
|
|
|
|
|
|
function parseResourceSpec(spec) {
|
|
|
|
const mapping = {
|
|
|
|
M: 'MemoryMB',
|
|
|
|
C: 'CPU',
|
|
|
|
D: 'DiskMB',
|
|
|
|
I: 'IOPS',
|
|
|
|
};
|
|
|
|
|
|
|
|
const terms = spec.split(',').map(t => {
|
|
|
|
const [k, v] = t
|
|
|
|
.trim()
|
|
|
|
.split(':')
|
|
|
|
.map(kv => kv.trim());
|
|
|
|
return [k, +v];
|
|
|
|
});
|
|
|
|
|
|
|
|
return terms.reduce((hash, term) => {
|
|
|
|
hash[mapping[term[0]]] = term[1];
|
|
|
|
return hash;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split a single resources object into N resource objects where
|
|
|
|
// the sum of each property of the new resources objects equals
|
|
|
|
// the original resources properties
|
|
|
|
// ex: divide(2, { Mem: 400, Cpu: 250 }) -> [{ Mem: 80, Cpu: 50 }, { Mem: 320, Cpu: 200 }]
|
|
|
|
function divide(count, resources) {
|
|
|
|
const wheel = roulette(1, count);
|
|
|
|
|
|
|
|
const ret = provide(count, (_, idx) => {
|
|
|
|
return Object.keys(resources).reduce((hash, key) => {
|
|
|
|
hash[key] = Math.round(resources[key] * wheel[idx]);
|
|
|
|
return hash;
|
|
|
|
}, {});
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Roulette splits a number into N divisions
|
|
|
|
// Variance is a value between 0 and 1 that determines how much each division in
|
|
|
|
// size. At 0 each division is even, at 1, it's entirely random but the sum of all
|
|
|
|
// divisions is guaranteed to equal the total value.
|
|
|
|
function roulette(total, divisions, variance = 0.8) {
|
|
|
|
let roulette = new Array(divisions).fill(total / divisions);
|
|
|
|
roulette.forEach((v, i) => {
|
|
|
|
if (i === roulette.length - 1) return;
|
|
|
|
roulette.splice(i, 2, ...rngDistribute(roulette[i], roulette[i + 1], variance));
|
|
|
|
});
|
|
|
|
return roulette;
|
|
|
|
}
|
|
|
|
|
|
|
|
function rngDistribute(a, b, variance = 0.8) {
|
|
|
|
const move = a * faker.random.number({ min: 0, max: variance, precision: 0.01 });
|
|
|
|
return [a - move, b + move];
|
|
|
|
}
|