]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - pkg/iface.py
3 # Copyright 2013. Cumulus Networks, Inc.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
10 """ifupdown2 network interface object
12 It closely resembles the 'iface' object in /etc/network/interfaces
13 file. But can be extended to include any other network interface format
16 The module contains the following public classes:
18 - ifaceState -- enumerates iface object state
20 - ifaceStatus -- enumerates iface object status (success/error)
22 - ifaceJsonEncoder -- Json encoder for the iface object
24 - iface -- network in terface object class
28 from collections
import OrderedDict
32 _tickmark
= ' (' + u
'\u2713'.encode('utf8') + ')'
33 _crossmark
= ' (' + u
'\u2717'.encode('utf8') + ')'
36 """Enumerates iface status """
44 def to_str(cls
, state
):
45 if state
== cls
.UNKNOWN
:
47 elif state
== cls
.SUCCESS
:
49 elif state
== cls
.ERROR
:
51 elif state
== cls
.NOTFOUND
:
55 def from_str(cls
, state_str
):
56 if state_str
== 'unknown':
58 elif state_str
== 'success':
60 elif state_str
== 'error':
64 """Enumerates iface state """
80 def to_str(cls
, state
):
81 if state
== cls
.UNKNOWN
:
83 elif state
== cls
.NEW
:
85 elif state
== cls
.PRE_UP
:
89 elif state
== cls
.POST_UP
:
91 elif state
== cls
.PRE_DOWN
:
93 elif state
== cls
.DOWN
:
95 elif state
== cls
.POST_DOWN
:
97 elif state
== cls
.QUERY_CHECKCURR
:
98 return 'query-checkcurr'
99 elif state
== cls
.QUERY_RUNNING
:
100 return 'query-running'
103 def from_str(cls
, state_str
):
104 if state_str
== 'unknown':
106 elif state_str
== 'new':
108 elif state_str
== 'pre-up':
110 elif state_str
== 'up':
112 elif state_str
== 'post-up':
114 elif state_str
== 'pre-down':
116 elif state_str
== 'down':
118 elif state_str
== 'post-down':
120 elif state_str
== 'query-checkcurr':
121 return cls
.QUERY_CHECKCURR
122 elif state_str
== 'query-running':
123 return cls
.QUERY_RUNNING
125 class ifaceJsonEncoder(json
.JSONEncoder
):
126 def default(self
, o
):
129 for k
, v
in o
.config
.items():
135 return OrderedDict({'name' : o
.name
,
136 'addr_method' : o
.addr_method
,
137 'addr_family' : o
.addr_family
,
139 'config' : retconfig
})
142 """ ifupdown2 interface object class
145 name Name of the interface
146 addr_family Address family eg, inet, inet6. Can be None to indicate both address families
147 addr_method Address method eg, static, manual or None for static
149 config dictionary of config lines for this interface
150 state Configuration state of an interface as defined by
152 status Configuration status of an interface as defined by
154 flags Internal flags used by iface processing
155 priv_flags private flags owned by module using this class
156 refcnt reference count, indicating number of interfaces
157 dependent on this iface
158 lowerifaces list of interface names lower to this interface or
159 this interface depends on
160 upperifaces list of interface names upper to this interface or
161 the interfaces that depend on this interface
162 auto True if interface belongs to the auto class
163 classes List of classes the interface belongs to
164 env shell environment the interface needs during execution
165 raw_config raw interface config from file
168 # flag to indicate that the object was created from pickled state
175 self
.addr_family
= None
176 self
.addr_method
= None
177 self
.config
= OrderedDict()
178 self
._config
_status
= {}
179 self
.state
= ifaceState
.NEW
180 self
.status
= ifaceStatus
.UNKNOWN
182 self
.priv_flags
= 0x0
184 self
.lowerifaces
= None
185 self
.upperifaces
= None
190 self
.linkstate
= None
192 def inc_refcnt(self
):
195 def dec_refcnt(self
):
198 def is_config_present(self
):
199 addr_method
= self
.addr_method
201 if (addr_method
.find('dhcp') != -1 or
202 addr_method
.find('dhcp6') != -1):
209 def set_class(self
, classname
):
210 """ Appends a class to the list """
211 self
.classes
.append(classname
)
213 def set_state_n_status(self
, state
, status
):
217 def set_flag(self
, flag
):
220 def clear_flag(self
, flag
):
223 def add_to_upperifaces(self
, upperifacename
):
225 if upperifacename
not in self
.upperifaces
:
226 self
.upperifaces
.append(upperifacename
)
228 self
.upperifaces
= [upperifacename
]
230 def get_attr_value(self
, attr_name
):
231 return self
.config
.get(attr_name
)
233 def get_attr_value_first(self
, attr_name
):
234 attr_value_list
= self
.config
.get(attr_name
)
236 return attr_value_list
[0]
239 def get_attr_value_n(self
, attr_name
, attr_index
):
240 attr_value_list
= self
.config
.get(attr_name
)
243 return attr_value_list
[attr_index
]
254 def generate_env(self
):
257 env
['IFACE'] = self
.name
258 for attr
, attr_value
in config
.items():
259 attr_env_name
= 'IF_%s' %attr
.upper()
260 env
[attr_env_name
] = attr_value
[0]
264 def update_config(self
, attr_name
, attr_value
):
265 self
.config
.setdefault(attr_name
, []).append(attr_value
)
267 def update_config_dict(self
, attrdict
):
268 self
.config
.update(attrdict
)
270 def update_config_with_status(self
, attr_name
, attr_value
, attr_status
=0):
274 self
.config
.setdefault(attr_name
, []).append(attr_value
)
275 self
._config
_status
.setdefault(attr_name
, []).append(attr_status
)
277 # set global iface state
279 self
.status
= ifaceStatus
.ERROR
280 elif self
.status
!= ifaceStatus
.ERROR
:
281 # Not already error, mark success
282 self
.status
= ifaceStatus
.SUCCESS
284 def get_config_attr_status(self
, attr_name
, idx
=0):
285 return self
._config
_status
.get(attr_name
, [])[idx
]
287 def get_config_attr_status_str(self
, attr_name
, idx
=0):
288 ret
= self
.get_config_attr_status(attr_name
, idx
)
294 def compare(self
, dstiface
):
295 """ Compares two objects
297 Returns True if object self is same as dstiface and False otherwise """
299 if self
.name
!= dstiface
.name
: return False
300 if self
.addr_family
!= dstiface
.addr_family
: return False
301 if self
.addr_method
!= dstiface
.addr_method
: return False
302 if self
.auto
!= dstiface
.auto
: return False
303 if self
.classes
!= dstiface
.classes
: return False
304 if any(True for k
in self
.config
if k
not in dstiface
.config
):
306 if any(True for k
,v
in self
.config
.items()
307 if v
!= dstiface
.config
.get(k
)): return False
310 def __getstate__(self
):
311 odict
= self
.__dict
__.copy()
314 del odict
['lowerifaces']
315 del odict
['upperifaces']
317 del odict
['_config_status']
319 del odict
['priv_flags']
320 del odict
['raw_config']
321 del odict
['linkstate']
325 def __setstate__(self
, dict):
326 self
.__dict
__.update(dict)
327 self
._config
_status
= {}
328 self
.state
= ifaceState
.NEW
329 self
.status
= ifaceStatus
.UNKNOWN
332 self
.lowerifaces
= None
333 self
.upperifaces
= None
334 self
.linkstate
= None
338 self
.flags |
= self
._PICKLED
340 def dump_raw(self
, logger
):
343 print 'auto %s' %self
.name
344 print (self
.raw_config
[0])
345 for i
in range(1, len(self
.raw_config
)):
346 print(indent
+ self
.raw_config
[i
])
348 def dump(self
, logger
):
350 logger
.info(self
.name
+ ' : {')
351 logger
.info(indent
+ 'family: %s' %self
.addr_family
)
352 logger
.info(indent
+ 'method: %s' %self
.addr_method
)
353 logger
.info(indent
+ 'flags: %x' %self
.flags
)
354 logger
.info(indent
+ 'state: %s'
355 %ifaceState
.to_str(self
.state
))
356 logger
.info(indent
+ 'status: %s'
357 %ifaceStatus
.to_str(self
.status
))
358 logger
.info(indent
+ 'refcnt: %d' %self
.refcnt
)
361 logger
.info(indent
+ 'lowerdevs: %s' %str
(d
))
363 logger
.info(indent
+ 'lowerdevs: None')
365 logger
.info(indent
+ 'config: ')
368 logger
.info(indent
+ indent
+ str(config
))
371 def dump_pretty(self
, with_status
=False):
375 outbuf
+= 'auto %s\n' %self
.name
376 outbuf
+= 'iface %s' %self
.name
378 outbuf
+= ' %s' %self
.addr_family
380 outbuf
+= ' %s' %self
.addr_method
384 for cname
, cvaluelist
in config
.items():
386 for cv
in cvaluelist
:
388 outbuf
+= indent
+ '%s %s %s\n' %(cname
, cv
,
389 self
.get_config_attr_status_str(cname
, idx
))
391 outbuf
+= indent
+ '%s %s\n' %(cname
, cv
)
395 def dump_json(self
, with_status
=False):
396 print json
.dumps(self
, cls
=ifaceJsonEncoder
, indent
=4)