]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - pkg/statemanager.py
3 # Copyright 2013. Cumulus Networks, Inc.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
7 # interface state manager
10 from collections
import OrderedDict
16 """ class with helper methods for pickling/unpickling iface objects """
19 def save(cls
, filename
, list_of_objects
):
21 with
open(filename
, 'w') as f
:
22 for obj
in list_of_objects
:
23 cPickle
.dump(obj
, f
, cPickle
.HIGHEST_PROTOCOL
)
28 def save_obj(cls
, f
, obj
):
30 cPickle
.dump(obj
, f
, cPickle
.HIGHEST_PROTOCOL
)
35 def load(cls
, filename
):
36 with
open(filename
, 'r') as f
:
38 try: yield cPickle
.load(f
)
39 except EOFError: break
43 """ state manager for managing ifupdown iface obj state """
45 state_dir
= '/var/tmp/network/'
46 state_filename
= 'ifstatenew'
49 self
.ifaceobjdict
= OrderedDict()
50 self
.logger
= logging
.getLogger('ifupdown.' +
51 self
.__class
__.__name
__)
52 if not os
.path
.exists(self
.state_dir
):
53 os
.mkdir(self
.state_dir
)
54 self
.state_file
= self
.state_dir
+ self
.state_filename
56 def save_ifaceobj(self
, ifaceobj
):
57 self
.ifaceobjdict
.setdefault(ifaceobj
.name
,
60 def read_saved_state(self
, filename
=None):
61 pickle_filename
= filename
62 if not pickle_filename
:
63 pickle_filename
= self
.state_file
64 if not os
.path
.exists(pickle_filename
):
66 for ifaceobj
in pickling
.load(pickle_filename
):
67 self
.save_ifaceobj(ifaceobj
)
69 def get_ifaceobjs(self
, ifacename
):
70 return self
.ifaceobjdict
.get(ifacename
)
72 def ifaceobj_sync(self
, ifaceobj
):
73 self
.logger
.debug('%s: statemanager sync state' %ifaceobj
.name
)
74 old_ifaceobjs
= self
.ifaceobjdict
.get(ifaceobj
.name
)
76 self
.ifaceobjdict
[ifaceobj
.name
] = [ifaceobj
]
78 # If it matches any of the object, return
79 if any(o
.compare(ifaceobj
) for o
in old_ifaceobjs
):
81 # If it does not match any of the objects, and if
82 # all objs in the list came from the pickled file,
83 # then reset the list and add this object as a fresh one,
84 # else append to the list
85 if old_ifaceobjs
[0].flags
& iface
._PICKLED
:
86 del self
.ifaceobjdict
[ifaceobj
.name
]
87 self
.ifaceobjdict
[ifaceobj
.name
] = [ifaceobj
]
89 self
.ifaceobjdict
[ifaceobj
.name
].append(ifaceobj
)
93 with
open(self
.state_file
, 'w') as f
:
94 for ifaceobjs
in self
.ifaceobjdict
.values():
95 [pickling
.save_obj(f
, i
) for i
in ifaceobjs
]
99 def dump_pretty(self
, ifacenames
, format
='native'):
101 ifacenames
= self
.ifaceobjdict
.keys()
103 ifaceobjs
= self
.get_ifaceobjs(i
)
106 for ifaceobj
in ifaceobjs
:
110 ifaceobj
.dump_pretty()
112 def dump(self
, ifacenames
=None):
113 self
.logger
.debug('statemanager iface state:')
116 ifaceobj
= self
.ifaces
.get(i
)
118 raise ifaceNotFoundError('ifname %s'
120 ifaceobj
.dump(self
.logger
)
122 for ifacename
, ifaceobjs
in self
.ifaceobjdict
.items():
123 [i
.dump(self
.logger
) for i
in ifaceobjs
]