]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/ifupdown/statemanager.py
83d6e52744394b5b1f1ab3c3a5eabb861057ae5c
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
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
):
20 """ pickle a list of iface objects """
22 with
open(filename
, 'w') as f
:
23 for obj
in list_of_objects
:
24 cPickle
.dump(obj
, f
, cPickle
.HIGHEST_PROTOCOL
)
29 def save_obj(cls
, f
, obj
):
30 """ pickle iface object """
32 cPickle
.dump(obj
, f
, cPickle
.HIGHEST_PROTOCOL
)
37 def load(cls
, filename
):
38 """ load picked iface object """
39 with
open(filename
, 'r') as f
:
41 try: yield cPickle
.load(f
)
42 except EOFError: break
46 """ state manager for managing ifupdown iface obj state
48 ifupdown2 has to maitain old objects for down operation on
49 interfaces. ie to down or delete old configuration.
51 This class uses pickle to store iface objects.
55 state_dir
= '/var/tmp/network/'
56 """directory where the state file is stored """
58 state_filename
= 'ifstatenew'
59 """name of the satefile """
62 """ Initializes statemanager internal state
64 which includes a dictionary of last pickled iface objects
66 self
.ifaceobjdict
= OrderedDict()
67 self
.logger
= logging
.getLogger('ifupdown.' +
68 self
.__class
__.__name
__)
69 if not os
.path
.exists(self
.state_dir
):
70 os
.mkdir(self
.state_dir
)
71 self
.state_file
= self
.state_dir
+ self
.state_filename
73 def save_ifaceobj(self
, ifaceobj
):
74 self
.ifaceobjdict
.setdefault(ifaceobj
.name
,
77 def read_saved_state(self
, filename
=None):
78 """This member function reads saved iface objects
81 filename (str): name of the state file
84 pickle_filename
= filename
85 if not pickle_filename
:
86 pickle_filename
= self
.state_file
87 if not os
.path
.exists(pickle_filename
):
89 for ifaceobj
in pickling
.load(pickle_filename
):
90 self
.save_ifaceobj(ifaceobj
)
92 def get_ifaceobjs(self
, ifacename
):
93 return self
.ifaceobjdict
.get(ifacename
)
95 def ifaceobj_sync(self
, ifaceobj
, op
):
96 """This member function sync's new obj state to old statemanager state
99 ifaceobj (object): new iface object
100 op (str): ifupdown operation
103 self
.logger
.debug('%s: statemanager sync state' %ifaceobj
.name
)
104 old_ifaceobjs
= self
.ifaceobjdict
.get(ifaceobj
.name
)
106 if not old_ifaceobjs
:
107 self
.ifaceobjdict
[ifaceobj
.name
] = [ifaceobj
]
109 # If it matches any of the object, return
110 if any(o
.compare(ifaceobj
) for o
in old_ifaceobjs
):
112 # If it does not match any of the objects, and if
113 # all objs in the list came from the pickled file,
114 # then reset the list and add this object as a fresh one,
115 # else append to the list
116 if old_ifaceobjs
[0].flags
& iface
._PICKLED
:
117 del self
.ifaceobjdict
[ifaceobj
.name
]
118 self
.ifaceobjdict
[ifaceobj
.name
] = [ifaceobj
]
120 self
.ifaceobjdict
[ifaceobj
.name
].append(ifaceobj
)
122 # If down of object successfull, delete object from state manager
123 if not old_ifaceobjs
:
125 if ifaceobj
.status
!= ifaceStatus
.SUCCESS
:
127 # If it matches any of the object, return
129 for o
in old_ifaceobjs
:
130 if o
.compare(ifaceobj
):
131 old_ifaceobjs
.pop(oidx
)
132 if not len(old_ifaceobjs
):
133 del self
.ifaceobjdict
[ifaceobj
.name
]
137 def save_state(self
):
138 """ saves state (ifaceobjects) to persistent state file """
141 with
open(self
.state_file
, 'w') as f
:
142 if not len(self
.ifaceobjdict
):
145 self
.logger
.debug('saving state ..')
146 for ifaceobjs
in self
.ifaceobjdict
.values():
147 [pickling
.save_obj(f
, i
) for i
in ifaceobjs
]
151 def dump_pretty(self
, ifacenames
, format
='native'):
153 ifacenames
= self
.ifaceobjdict
.keys()
155 ifaceobjs
= self
.get_ifaceobjs(i
)
158 for ifaceobj
in ifaceobjs
:
162 ifaceobj
.dump_pretty()
164 def dump(self
, ifacenames
=None):
165 self
.logger
.debug('statemanager iface state:')
168 ifaceobj
= self
.ifaces
.get(i
)
170 raise ifaceNotFoundError('ifname %s'
172 ifaceobj
.dump(self
.logger
)
174 for ifacename
, ifaceobjs
in self
.ifaceobjdict
.items():
175 [i
.dump(self
.logger
) for i
in ifaceobjs
]