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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
"""
A Cobbler repesentation of an IP network.
Copyright 2009, Red Hat, Inc
John Eckersberg <jeckersb@redhat.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
"""
import utils
import item
from cexceptions import *
from utils import _, _IP, _CIDR
class Network(item.Item):
TYPE_NAME = _("network")
COLLECTION_TYPE = "network"
def make_clone(self):
ds = self.to_datastruct()
cloned = Network(self.config)
cloned.from_datastruct(ds)
return cloned
def clear(self,is_subobject=False):
self.name = None
self.cidr = None
self.address = None
self.gateway = None
self.broadcast = None
self.nameservers = []
self.reserved = []
self.used_addresses = []
self.free_addresses = []
self.comment = ""
def from_datastruct(self,seed_data):
self.name = self.load_item(seed_data, 'name')
self.cidr = _CIDR(self.load_item(seed_data, 'cidr'))
self.address = _IP(self.load_item(seed_data, 'address', self.cidr[0]))
self.gateway = _IP(self.load_item(seed_data, 'gateway', self.cidr[-2]))
self.broadcast = _IP(self.load_item(seed_data, 'broadcast', self.cidr[-1]))
self.nameservers = [_IP(i) for i in self.load_item(seed_data, 'nameservers', [])]
self.reserved = [_CIDR(c) for c in self.load_item(seed_data, 'reserved', [])]
self.used_addresses = self.load_item(seed_data, 'used_addresses', [])
self.free_addresses = [_CIDR(c) for c in self.load_item(seed_data, 'free_addresses', [])]
self.comment = self.load_item(seed_data, 'comment', '')
return self
def set_cidr(self, cidr):
if self.cidr == None:
self.free_addresses = [_CIDR(cidr)]
self.cidr = _CIDR(cidr)
def set_address(self, address):
if self.address != None:
self._add_to_free(address)
self.address = _IP(address)
self._remove_from_free(self.address)
def set_gateway(self, gateway):
if self.gateway != None:
self._add_to_free(gateway)
self.gateway = _IP(gateway)
self._remove_from_free(self.gateway)
def set_broadcast(self, broadcast):
if self.broadcast != None:
self._add_to_free(broadcast)
self.broadcast = _IP(broadcast)
self._remove_from_free(self.broadcast)
def set_nameservers(self, nameservers):
old = self.nameservers
nameservers = [_IP(s.strip()) for s in nameservers.split(',')]
if old != None:
for ns in old:
if ns not in nameservers:
self._add_to_free(ns)
for ns in nameservers:
if ns not in old:
self._remove_from_free(ns)
self.nameservers = nameservers
def set_reserved(self, reserved):
pass
def get_assigned_address(self, system, intf):
"""
Get the address in the network assigned to an interface of a system.
"""
for item in self.used_addresses:
if item['system'] == system and item['intf'] == intf:
return str(item['ip'])
return None
def subscribe_system(self, system, intf, ip=None):
"""
Join a system to the network. If ip is passed in, try to
claim that specific address, otherwise just grab the first
free address.
"""
if not ip:
if self.free_address_count() == 0:
raise CX(_("Network %s has no free addresses" % self.cidr))
ip = self.free_addresses[0][0]
self._allocate_address(system, intf, ip)
def unsubscribe_system(self, system, intf):
"""
Remove a system from the network. Allocate it's address back
into the free pool.
"""
addr = self.get_assigned_address(system, intf)
if not addr:
raise CX(_("Attempting to unsubscribe %s:%s from %s, but not subscribed" % (system, intf, self.name)))
self._remove_from_used(addr)
self._add_to_free(addr)
def _addr_available(self, addr):
"""
Is addr free in the network?
"""
for cidr in self.free_addresses:
if addr in cidr:
return True
return False
def _add_to_free(self, addr, compact=True):
"""
Add addr to the list of free addresses. If compact is True,
then take the list of CIDRs in free_addresses and compact it.
"""
addr = _IP(addr).cidr()
self.free_addresses.append(addr)
if compact:
self.free_addreses = self._compact(self.free_addresses)
def _remove_from_free(self, addr):
"""
Take addr off of the list of free addresses
"""
self.free_addresses = self._subtract_and_flatten(self.free_addresses, [addr])
self.free_addresses.sort()
def _add_to_used(self, used_dict):
"""
Take used_dict and append it onto used_addresses. Make sure
no duplicates make it in.
"""
if self.used_addresses != []:
# should really throw an error if it's already there
# probably a sign something has gone wrong elsewhere
for i in [foo['ip'] for foo in self.used_addresses]:
if used_dict['ip'] == i:
raise CX(_("Trying to add %s to used_addresses but is already there!" % i))
self.used_addresses.append(used_dict)
def _remove_from_used(self, addr):
"""
Take addr off of the list of used addresses
"""
for d in self.used_addresses:
if d['ip'] == addr:
index = self.used_addresses.index(d)
del(self.used_addresses[index])
def _allocate_address(self, system, intf, addr):
"""
Try to allocate addr to system on interface intf.
"""
if not self._addr_available(addr):
raise CX(_("Address %s is not available for allocation" % addr))
self._remove_from_free(addr)
self._add_to_used({'ip': addr, 'system': system, 'intf': intf})
def _subtract_and_flatten(self, cidr_list, remove_list):
"""
For each item I in remove_list, find the cidr C in cidr_list
that contains I. Perform the subtraction C - I which returns
a new minimal cidr list not containing I. Replace C with this
result, flattened out so we don't get multiple levels of
lists.
"""
for item in remove_list:
for i in range(len(cidr_list)):
if item in cidr_list[i]:
cidr_list += cidr_list[i] - item
del(cidr_list[i])
break
return cidr_list
def _compact(self, cidr_list):
"""
Compacts a list of CIDR objects down to a minimal-length list L
such that the set of IP addresses contained in L is the same as
the original.
For example:
[10.0.0.0/32, 10.0.0.1/32, 10.0.0.2/32, 10.0.0.3/32]
becomes
[10.0.0.0/30]
"""
if len(cidr_list) <= 1:
return cidr_list
did_compact = False
skip_next = False
compacted = []
for i in range(1, len(cidr_list)):
cur = cidr_list[i]
prev = cidr_list[i-1]
if skip_next:
skip_next = False
continue
last = prev[-1]
last += 1
last = last.cidr()
if last == cur[0].cidr() and prev.size() == cur.size():
compacted.append(CIDR('%s/%d' % (str(prev[0]), prev.prefixlen - 1)))
did_compact = True
skip_next = True
if did_compact:
return compact(compacted)
else:
return cidr_list
def used_address_count(self):
return len(self.used_addresses)
def free_address_count(self):
total = 0
for item in self.free_addresses:
total += len(item)
return total
def is_valid(self):
"""
A network is valid if:
* it has a name and a CIDR
* it does not overlap another network
"""
if self.name is None:
raise CX(_("name is required"))
if self.cidr is None:
raise CX(_("cidr is required"))
for other in self.config.networks():
if other.name == self.name:
continue
if self.cidr in other.cidr or other.cidr in self.cidr:
raise CX(_("cidr %s overlaps with network %s (%s)" % (self.cidr, other.name, other.cidr)))
return True
def to_datastruct(self):
def convert_used_addresses(l):
"""
used_addresses is a bit more involved...
"""
stringified = []
for item in l:
item['ip'] = str(item['ip'])
stringified.append(item)
return stringified
return {
'name' : self.name,
'cidr' : str(self.cidr),
'address' : str(self.address),
'gateway' : str(self.gateway),
'broadcast' : str(self.broadcast),
'nameservers' : [str(i) for i in self.nameservers],
'reserved' : [str(i) for i in self.reserved],
'used_addresses' : convert_used_addresses(self.used_addresses),
'free_addresses' : [str(i) for i in self.free_addresses],
'comment' : self.comment
}
def printable(self):
buf = _("network : %s\n") % self.name
buf = buf + _("CIDR : %s\n") % self.cidr
buf = buf + _("gateway : %s\n") % self.gateway
buf = buf + _("network address : %s\n") % self.address
buf = buf + _("broadcast : %s\n") % self.broadcast
buf = buf + _("nameservers : %s\n") % [str(i) for i in self.nameservers]
buf = buf + _("reserved : %s\n") % [str(i) for i in self.reserved]
buf = buf + _("free addresses : %s\n") % self.free_address_count()
buf = buf + _("used addresses : %s\n") % self.used_address_count()
buf = buf + _("comment : %s\n") % self.comment
return buf
def get_parent(self):
"""
currently the Cobbler object space does not support subobjects of this object
as it is conceptually not useful.
"""
return None
def remote_methods(self):
return {
'name' : self.set_name,
'cidr' : self.set_cidr,
'address' : self.set_address,
'gateway' : self.set_gateway,
'broadcast' : self.set_broadcast,
'nameservers' : self.set_nameservers,
'reserved' : self.set_reserved,
'used_addresses' : self.set_used_addresses,
'free_addresses' : self.set_free_addresses,
'comment' : self.set_comment
}
|