]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/ifupdown/statemanager.py
3 # Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
7 # interface state manager
15 from ifupdown2
.ifupdown
.iface
import *
17 import ifupdown2
.ifupdown
.exceptions
as exceptions
19 from ifupdown
.iface
import *
21 import ifupdown
.exceptions
as exceptions
25 """ class with helper methods for pickling/unpickling iface objects """
28 def save(cls
, filename
, list_of_objects
):
29 """ pickle a list of iface objects """
31 with
open(filename
, 'w') as f
:
32 for obj
in list_of_objects
:
33 cPickle
.dump(obj
, f
, cPickle
.HIGHEST_PROTOCOL
)
38 def save_obj(cls
, f
, obj
):
39 """ pickle iface object """
41 cPickle
.dump(obj
, f
, cPickle
.HIGHEST_PROTOCOL
)
46 def load(cls
, filename
):
47 """ load picked iface object """
48 with
open(filename
, 'r') as f
:
50 try: yield cPickle
.load(f
)
51 except EOFError: break
55 """ state manager for managing ifupdown iface obj state
57 ifupdown2 has to maitain old objects for down operation on
58 interfaces. ie to down or delete old configuration.
60 This class uses pickle to store iface objects.
64 state_dir
= '/var/tmp/network/'
65 """directory where the state file is stored """
67 state_filename
= 'ifstatenew'
68 """name of the satefile """
70 state_rundir
= '/run/network/'
71 """name of the state run dir """
73 state_runlockfile
= 'ifstatelock'
74 """name of the state run lock file """
77 """ Initializes statemanager internal state
79 which includes a dictionary of last pickled iface objects
81 self
.ifaceobjdict
= OrderedDict()
82 self
.logger
= logging
.getLogger('ifupdown.' +
83 self
.__class
__.__name
__)
84 if not os
.path
.exists(self
.state_dir
):
85 os
.mkdir(self
.state_dir
)
86 if not os
.path
.exists(self
.state_rundir
):
87 os
.mkdir(self
.state_rundir
)
88 self
.state_file
= self
.state_dir
+ self
.state_filename
90 def save_ifaceobj(self
, ifaceobj
):
91 self
.ifaceobjdict
.setdefault(ifaceobj
.name
,
94 def read_saved_state(self
, filename
=None):
95 """This member function reads saved iface objects
98 filename (str): name of the state file
101 pickle_filename
= filename
102 if not pickle_filename
:
103 pickle_filename
= self
.state_file
104 if not os
.path
.exists(pickle_filename
):
106 for ifaceobj
in pickling
.load(pickle_filename
):
107 self
.save_ifaceobj(ifaceobj
)
109 def get_ifaceobjs(self
, ifacename
):
110 return self
.ifaceobjdict
.get(ifacename
)
112 def ifaceobj_sync(self
, ifaceobj
, op
):
113 """This member function sync's new obj state to old statemanager state
116 ifaceobj (object): new iface object
117 op (str): ifupdown operation
120 self
.logger
.debug('%s: statemanager sync state %s'
121 %(ifaceobj
.name
, op
))
122 old_ifaceobjs
= self
.ifaceobjdict
.get(ifaceobj
.name
)
124 if not old_ifaceobjs
:
125 self
.ifaceobjdict
[ifaceobj
.name
] = [ifaceobj
]
127 # If it matches any of the object, return
128 if any(o
.compare(ifaceobj
) for o
in old_ifaceobjs
):
130 # If it does not match any of the objects, and if
131 # all objs in the list came from the pickled file,
132 # then reset the list and add this object as a fresh one,
133 # else append to the list
134 if old_ifaceobjs
[0].flags
& iface
._PICKLED
:
135 del self
.ifaceobjdict
[ifaceobj
.name
]
136 self
.ifaceobjdict
[ifaceobj
.name
] = [ifaceobj
]
138 self
.ifaceobjdict
[ifaceobj
.name
].append(ifaceobj
)
140 # If down of object successfull, delete object from state manager
141 if not old_ifaceobjs
:
143 if ifaceobj
.status
!= ifaceStatus
.SUCCESS
:
145 # If it matches any of the object, return
147 for o
in old_ifaceobjs
:
148 if o
.compare(ifaceobj
):
149 old_ifaceobjs
.pop(oidx
)
150 if not len(old_ifaceobjs
):
151 del self
.ifaceobjdict
[ifaceobj
.name
]
155 def save_state(self
):
156 """ saves state (ifaceobjects) to persistent state file """
159 with
open(self
.state_file
, 'w') as f
:
160 if not len(self
.ifaceobjdict
):
163 self
.logger
.debug('saving state ..')
164 for ifaceobjs
in self
.ifaceobjdict
.values():
165 [pickling
.save_obj(f
, i
) for i
in ifaceobjs
]
166 open('%s/%s' %(self
.state_rundir
, self
.state_runlockfile
), 'w').close()
170 def dump_pretty(self
, ifacenames
, format
='native'):
172 ifacenames
= self
.ifaceobjdict
.keys()
174 ifaceobjs
= self
.get_ifaceobjs(i
)
177 for ifaceobj
in ifaceobjs
:
181 ifaceobj
.dump_pretty()
183 def dump(self
, ifacenames
=None):
184 self
.logger
.debug('statemanager iface state:')
187 ifaceobj
= self
.ifaces
.get(i
)
189 raise exceptions
.ifaceNotFoundError('ifname %s'
191 ifaceobj
.dump(self
.logger
)
193 for ifacename
, ifaceobjs
in self
.ifaceobjdict
.items():
194 [i
.dump(self
.logger
) for i
in ifaceobjs
]
196 statemanager_api
= stateManager()
199 global statemanager_api
200 statemanager_api
= stateManager()