-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.js
168 lines (157 loc) · 4.73 KB
/
Inventory.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var when = require('when');
var nodefn = require('when/node');
var sqlite3 = require('sqlite3');
function Inventory(path){
this.path = path;
this.db = null;
}
/**
* Open a database connection.
*/
Inventory.prototype.open = function open(){
var self = this;
return when.promise(function(resolve, reject){
self.db = new sqlite3.Database(self.path, function(error){
if(error){
reject(error);
}
else{
resolve();
}
});
});
};
/**
* Get an array of all host entries, whether declared or actual.
*/
Inventory.prototype.getHosts = function getHosts(){
var db = this.db;
var query = 'SELECT mac, lease_ip, lease_hostname, mapping_ip, mapping_hostname, GROUP_CONCAT(group_members."group") groups FROM' +
' (SELECT leases.mac mac, leases.ip lease_ip, leases.hostname lease_hostname, mappings.ip mapping_ip, mappings.hostname mapping_hostname FROM leases LEFT OUTER JOIN mappings ON leases.mac = mappings.mac' +
' UNION SELECT mappings.mac mac, leases.ip lease_ip, leases.hostname lease_hostname, mappings.ip mapping_ip, mappings.hostname mapping_hostname FROM mappings LEFT OUTER JOIN leases ON mappings.mac = leases.mac' +
' ) hosts LEFT OUTER JOIN group_members ON hosts.mapping_hostname = group_members.hostname GROUP BY mac, group_members.hostname, hosts.lease_ip, hosts.mapping_hostname, hosts.mapping_ip';
return nodefn.call(db.all.bind(db), query, []);
};
/**
* Add a lease entry.
*/
Inventory.prototype.addLease = function addLease(mac, ip, hostname){
var db = this.db;
var query = 'INSERT INTO leases (mac, ip, hostname) VALUES (?, ?, ?)';
return nodefn.call(db.run.bind(db), query, [
String(mac),
String(ip),
String(hostname)
]);
};
/**
* Add a static mapping so that the host may be assigned a defined IP and host name next time it asks for a lease.
*/
Inventory.prototype.addStaticMapping = function addStaticMapping(mac, ip, hostname, enabled, comment){
var db = this.db;
var query = 'INSERT INTO mappings (mac, ip, hostname, enabled, comment) VALUES (?, ?, ?, ?, ?)';
return nodefn.call(db.run.bind(db), query, [
String(mac),
String(ip),
String(hostname),
Boolean(enabled),
String(comment)
]);
};
/**
* Update a static mapping by MAC address.
*/
Inventory.prototype.updateStaticMapping = function updateStaticMapping(mac, ip, hostname, enabled, comment){
var db = this.db;
var query = 'UPDATE mappings SET ip = ?, hostname = ?, enabled = ?, comment = ? WHERE mac = ?';
return when.promise(function(resolve, reject){
db.run(query, [
String(ip),
String(hostname),
Boolean(enabled),
String(comment),
String(mac)
], function(error){
if(error){
return void reject(error);
}
if(this.changes < 1){
return void reject(new Error('Could not update static mapping - no matching entry found for MAC address: ' + mac));
}
resolve();
});
});
};
/**
* Delete a static mapping by MAC address.
*/
Inventory.prototype.deleteStaticMapping = function deleteStaticMapping(mac){
var db = this.db;
var query = 'DELETE FROM mappings WHERE mac = ?';
return when.promise(function(resolve, reject){
db.run(query, [
String(mac)
], function(error){
if(error){
return void reject(error);
}
if(this.changes < 1){
return void reject(new Error('Could not delete static mapping - no matching entry found for MAC address: ' + mac));
}
resolve();
});
});
};
/**
* Delete a lease.
*/
Inventory.prototype.deleteLease = function deleteLease(mac){
var db = this.db;
var query = 'DELETE FROM leases WHERE mac = ?';
return when.promise(function(resolve, reject){
db.run(query, [
String(mac)
], function verifyDeletedCount(error){
if(error){
return void reject(error);
}
if(this.changes < 1){
return void reject(new Error('Could not remove lease - no matching entry found for MAC address: ' + mac));
}
resolve();
});
});
};
/**
* Assign a hostname to a group.
*/
Inventory.prototype.addGroupMember = function addGroupMember(group, hostname){
var db = this.db;
var query = 'INSERT INTO group_members ("group", hostname) VALUES (?, ?)';
return nodefn.call(db.run.bind(db), query, [
String(group),
String(hostname)
]);
};
/**
* Remove a hostname from a group.
*/
Inventory.prototype.deleteGroupMember = function deleteGroupMember(group, hostname){
var db = this.db;
var query = 'DELETE FROM group_members WHERE "group" = ? AND hostname = ?';
return when.promise(function(resolve, reject){
db.run(query, [
String(group),
String(hostname)
], function verifyDeletedCount(error){
if(error){
return void reject(error);
}
if(this.changes < 1){
return void reject(new Error('Could not remove group member - no matching entry found for group/hostname: ' + group + '/' + hostname));
}
resolve();
});
});
};
module.exports.Inventory = Inventory;