-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansible.js
50 lines (40 loc) · 1.5 KB
/
ansible.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const yaml = require('js-yaml');
const fs = require('fs');
module.exports = function (payload) {
const modeName = 'mode' in payload ? payload.mode.toLowerCase() : 'ansible';
let doc = yaml.load(fs.readFileSync('./ansible/template.yaml', 'utf-8'));
const mode = require('./modes/' + modeName);
let phoneTypes = [];
const extensions = {}
for (const pos in payload.phones) {
const phone = payload.phones[pos];
if (phoneTypes.indexOf(payload.phones[pos].type) === -1) {
phoneTypes.push(payload.phones[pos].type);
}
if (!('extension' in phone)) {
console.error(`${phone.name} has no extension!`);
return;
} else {
extensions[phone.name] = phone.extension;
}
}
for (const type of phoneTypes) {
doc.all.children[type] = {};
doc.all.children[type].hosts = {};
}
doc.all.children["phones"] = {}
doc.all.children["phones"].hosts = {}
for (const pos in payload.phones) {
const phone = payload.phones[pos];
if (phone.mac === 'Unknown') {
console.error('Phone with no MAC - skipping.')
continue;
}
if (!(phone.name in extensions)) {
console.error("Missing " + phone.name)
continue;
}
doc.all.children["phones"].hosts['SEP' + phone.mac.toUpperCase()] = mode.getHostConfig(payload, extensions, pos, phone);
}
fs.writeFileSync(`./ansible/${modeName}.yaml`, yaml.dump(doc));
}