-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·99 lines (83 loc) · 3.22 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var request = require('request'),
fs = require('fs'),
cheerio = require('cheerio'),
process = require('process'),
fs = require('fs'),
inventory = require('./inventory.js'),
async = require('async'),
headless = require ('./headless.js');
config = require('./config.json');
var username = config.username,
password = config.password,
config_directory = config['generated_config_directory'];
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
console.log(reason.stack);
});
// We'll write all of our generated configs out to this folder
if (!fs.existsSync(config_directory)) fs.mkdirSync(config_directory);
function getModelsToUpdateFromCommandLine(inventoryList) {
// remove `node app.js` from arvg
args = process.argv.slice(2).map((arg) => arg.toLowerCase());
if (args.length == 0) {
console.log("No switches given. List individual switch IDs as arguments, or just 'all'")
process.exit(1);
}
if (args.length == 1 && args[0].toLowerCase() == "all") {
return inventoryList;
}
return inventoryList.filter((inv) => {
return args.indexOf(inv['id'].toLowerCase()) !== -1
})
}
var theTemplater = require('./templater.js');
var templater = new theTemplater('switch-config-template.underscore', {snmppassword: password});
function getTemplateForDevice(device) {
var path = `${config_directory}/${device['id']}.txt`;
var template = templater.getTemplateFor(device);
fs.writeFileSync(path, template);
return path;
}
function getListOfFunctionsToRun(inventoryList) {
return inventoryList.map((inv) => function(callback) {
processInventoryItem(inv, callback);
})
}
// Process an individual switch, including config generation and uploading
function processInventoryItem(inv, callback) {
// TODO:
// * Generate template file
// * Get the base address for this switch model
// * Create Chromium session
// * Upload config file
console.log(`processing ${inv['id']}`);
let confPath = getTemplateForDevice(inv);
var baseAddressLocal= `http://${inv['ip']}/`;
var options = {
// UNCOMMENT THIS LINE TO RE-ENABLE CONFIG THINGS
configPath: confPath
}
var h = new headless(baseAddressLocal, username, password, options, function() {
//h.close(); // this closes the chrome window upon completion
callback(null);
});
}
function generateDhcpReservations(inv, callback) {
leases = inv.filter(function(i) { return !!i['id'] && !!i['ip'] && !!i['mac']})
console.log(leases);
console.log("Generating DHCP static leases file");
var dhcpTempl = new theTemplater('dhcp-leases.conf.underscore', {});
var conf = dhcpTempl.getTemplateFor({"switches": inv});
fs.writeFileSync("dhcp-leases.conf", conf);
}
// Get list of all inventory items, and trigger processing on them
inventory(config['google_spreadsheet_id'], (err, inventoryList) => {
if (err) {
console.log(err)
}
generateDhcpReservations(inventoryList);
const targets = getModelsToUpdateFromCommandLine(inventoryList);
async.parallelLimit(getListOfFunctionsToRun(targets), 10, (err, res) => {
console.log(`Done with err: ${err} and result ${res} `)
})
})