]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/addons/vrrpd.py
97911411d3c228bc53fe0452da60113f1b06cb29
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
8 from ipaddr
import IPNetwork
10 from ifupdown
.iface
import *
11 from ifupdownaddons
.modulebase
import moduleBase
12 from ifupdownaddons
.iproute2
import iproute2
19 except ImportError, e
:
20 raise ImportError (str(e
) + "- required module not found")
22 class vrrpd(moduleBase
):
23 """ ifupdown2 addon module to configure vrrpd attributes """
25 _modinfo
= {'mhelp' : 'ethtool configuration module for interfaces',
28 {'help' : 'vrrp instance id',
29 'example' : ['vrrp-id 1']},
31 {'help': 'set vrrp priority',
32 'example' : ['vrrp-priority 20']},
34 {'help': 'set vrrp virtual ip',
35 'example' : ['vrrp-virtual-ip 10.0.1.254']}}}
37 def __init__(self
, *args
, **kargs
):
38 moduleBase
.__init
__(self
, *args
, **kargs
)
41 def _check_if_process_is_running(self
, cmdname
, cmdline
):
45 pidstr
= subprocess
.check_output(['/bin/pidof',
46 '%s' %cmdname
]).strip('\n')
56 tmpcmdline
= cmdline
.replace(' ', '')
58 pcmdline
= self
.read_file_oneline('/proc/%s/cmdline' %pid
)
59 pcmdline
= re
.sub(r
'\\(.)', r
'\1', pcmdline
)
60 self
.logger
.info('(%s)' %(pcmdline))
61 self
.logger
.info('(%s)' %(tmpcmdline))
62 self
.logger
.info('(%d) (%d)' %(len(pcmdline
), len(tmpcmdline
)))
63 if pcmdline
and pcmdline
== tmpcmdline
:
64 targetpids
.append(pid
)
69 def _up(self
, ifaceobj
):
70 """ up vrrpd -n -D -i $IFACE -v 1 -p 20 10.0.1.254
71 up ifplugd -i $IFACE -b -f -u0 -d1 -I -p -q """
73 if (not self
.DRYRUN
and
74 not os
.path
.exists('/sys/class/net/%s' %ifaceobj
.name
)):
78 attrval
= ifaceobj
.get_attr_value_first('vrrp-id')
80 cmd
+= ' -v %s' %attrval
83 attrval
= ifaceobj
.get_attr_value_first('vrrp-priority')
85 cmd
+= ' -p %s' %attrval
87 self
.logger
.warn('%s: incomplete vrrp parameters ' %ifaceobj
.name
,
88 '(priority not found)')
89 attrval
= ifaceobj
.get_attr_value_first('vrrp-virtual-ip')
93 self
.logger
.warn('%s: incomplete vrrp arguments ' %ifaceobj
.name
,
94 '(virtual ip not found)')
96 cmd
= '/usr/sbin/vrrpd -n -D -i %s %s' %(ifaceobj
.name
, cmd
)
97 self
.exec_command(cmd
)
99 cmd
= '/usr/sbin/ifplugd -i %s -b -f -u0 -d1 -I -p -q' %ifaceobj
.name
100 if self
._check
_if
_process
_is
_running
('/usr/sbin/ifplugd', cmd
):
101 self
.logger
.info('%s: ifplugd already running' %ifaceobj
.name
)
103 self
.exec_command(cmd
)
105 def _kill_pid_from_file(self
, pidfilename
):
106 if os
.path
.exists(pidfilename
):
107 pid
= self
.read_file_oneline(pidfilename
)
108 if os
.path
.exists('/proc/%s' %pid
):
109 os
.kill(int(pid
), signal
.SIGTERM
)
111 def _down(self
, ifaceobj
):
112 """ down ifplugd -k -i $IFACE
113 down kill $(cat /var/run/vrrpd_$IFACE_*.pid) """
114 attrval
= ifaceobj
.get_attr_value_first('vrrp-id')
118 self
.exec_command('/usr/sbin/ifplugd -k -i %s' %ifaceobj
.name
)
120 self
.logger
.debug('%s: ifplugd down error (%s)'
121 %(ifaceobj
.name
, str(e
)))
124 for pidfile
in glob
.glob('/var/run/vrrpd_%s_*.pid' %ifaceobj
.name
):
126 self
._kill
_pid
_from
_file
(pidfile
)
128 self
.logger
.debug('%s: vrrpd down error (%s)'
129 %(ifaceobj
.name
, str(e
)))
132 def _query_check(self
, ifaceobj
, ifaceobjcurr
):
137 _run_ops
= {'post-up' : _up
,
141 """ returns list of ops supported by this module """
142 return self
._run
_ops
.keys()
144 def run(self
, ifaceobj
, operation
, query_ifaceobj
=None, **extra_args
):
145 """ run ethtool configuration on the interface object passed as
149 **ifaceobj** (object): iface object
151 **operation** (str): any of 'post-up', 'query-checkcurr',
154 **query_ifaceobj** (object): query check ifaceobject. This is only
155 valid when op is 'query-checkcurr'. It is an object same as
156 ifaceobj, but contains running attribute values and its config
157 status. The modules can use it to return queried running state
158 of interfaces. status is success if the running state is same
159 as user required state in ifaceobj. error otherwise.
161 op_handler
= self
._run
_ops
.get(operation
)
164 if operation
== 'query-checkcurr':
165 op_handler(self
, ifaceobj
, query_ifaceobj
)
167 op_handler(self
, ifaceobj
)