]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - addons/vrrpd.py
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 ifupdown
.utils
import utils
12 from ifupdownaddons
.modulebase
import moduleBase
13 from ifupdownaddons
.iproute2
import iproute2
14 import ifupdown
.ifupdownflags
as ifupdownflags
20 except ImportError, e
:
21 raise ImportError (str(e
) + "- required module not found")
23 class vrrpd(moduleBase
):
24 """ ifupdown2 addon module to configure vrrpd attributes """
26 _modinfo
= {'mhelp' : 'ethtool configuration module for interfaces',
29 {'help' : 'vrrp instance id',
30 'validrange' : ['1', '4096'],
31 'example' : ['vrrp-id 1']},
33 {'help': 'set vrrp priority',
34 'validrange' : ['0', '255'],
35 'example' : ['vrrp-priority 20']},
37 {'help': 'set vrrp virtual ip',
38 'validvals' : ['<ipv4>', ],
39 'example' : ['vrrp-virtual-ip 10.0.1.254']}}}
41 def __init__(self
, *args
, **kargs
):
42 moduleBase
.__init
__(self
, *args
, **kargs
)
45 def _check_if_process_is_running(self
, cmdname
, cmdline
):
49 cmdl
= ['/bin/pidof', cmdname
]
50 pidstr
= utils
.exec_commandl(cmdl
, stderr
=None).strip('\n')
60 tmpcmdline
= cmdline
.replace(' ', '')
62 pcmdline
= self
.read_file_oneline('/proc/%s/cmdline' %pid
)
63 pcmdline
= re
.sub(r
'\\(.)', r
'\1', pcmdline
)
64 self
.logger
.info('(%s)' %(pcmdline))
65 self
.logger
.info('(%s)' %(tmpcmdline))
66 self
.logger
.info('(%d) (%d)' %(len(pcmdline
), len(tmpcmdline
)))
67 if pcmdline
and pcmdline
== tmpcmdline
:
68 targetpids
.append(pid
)
73 def _up(self
, ifaceobj
):
74 """ up vrrpd -n -D -i $IFACE -v 1 -p 20 10.0.1.254
75 up ifplugd -i $IFACE -b -f -u0 -d1 -I -p -q """
77 if (not ifupdownflags
.flags
.DRYRUN
and
78 not os
.path
.exists('/sys/class/net/%s' %ifaceobj
.name
)):
82 attrval
= ifaceobj
.get_attr_value_first('vrrp-id')
84 cmd
+= ' -v %s' %attrval
87 attrval
= ifaceobj
.get_attr_value_first('vrrp-priority')
89 cmd
+= ' -p %s' %attrval
91 self
.logger
.warn('%s: incomplete vrrp parameters ' %ifaceobj
.name
,
92 '(priority not found)')
93 attrval
= ifaceobj
.get_attr_value_first('vrrp-virtual-ip')
97 self
.logger
.warn('%s: incomplete vrrp arguments ' %ifaceobj
.name
,
98 '(virtual ip not found)')
100 cmd
= '/usr/sbin/vrrpd -n -D -i %s %s' %(ifaceobj
.name
, cmd
)
101 utils
.exec_command(cmd
)
103 cmd
= '/usr/sbin/ifplugd -i %s -b -f -u0 -d1 -I -p -q' %ifaceobj
.name
104 if self
._check
_if
_process
_is
_running
('/usr/sbin/ifplugd', cmd
):
105 self
.logger
.info('%s: ifplugd already running' %ifaceobj
.name
)
107 utils
.exec_command(cmd
)
109 def _kill_pid_from_file(self
, pidfilename
):
110 if os
.path
.exists(pidfilename
):
111 pid
= self
.read_file_oneline(pidfilename
)
112 if os
.path
.exists('/proc/%s' %pid
):
113 os
.kill(int(pid
), signal
.SIGTERM
)
115 def _down(self
, ifaceobj
):
116 """ down ifplugd -k -i $IFACE
117 down kill $(cat /var/run/vrrpd_$IFACE_*.pid) """
118 attrval
= ifaceobj
.get_attr_value_first('vrrp-id')
122 utils
.exec_command('/usr/sbin/ifplugd -k -i %s' % ifaceobj
.name
)
124 self
.logger
.debug('%s: ifplugd down error (%s)'
125 %(ifaceobj
.name
, str(e
)))
128 for pidfile
in glob
.glob('/var/run/vrrpd_%s_*.pid' %ifaceobj
.name
):
130 self
._kill
_pid
_from
_file
(pidfile
)
132 self
.logger
.debug('%s: vrrpd down error (%s)'
133 %(ifaceobj
.name
, str(e
)))
136 def _query_check(self
, ifaceobj
, ifaceobjcurr
):
141 _run_ops
= {'post-up' : _up
,
145 """ returns list of ops supported by this module """
146 return self
._run
_ops
.keys()
148 def run(self
, ifaceobj
, operation
, query_ifaceobj
=None, **extra_args
):
149 """ run ethtool configuration on the interface object passed as
153 **ifaceobj** (object): iface object
155 **operation** (str): any of 'post-up', 'query-checkcurr',
158 **query_ifaceobj** (object): query check ifaceobject. This is only
159 valid when op is 'query-checkcurr'. It is an object same as
160 ifaceobj, but contains running attribute values and its config
161 status. The modules can use it to return queried running state
162 of interfaces. status is success if the running state is same
163 as user required state in ifaceobj. error otherwise.
165 op_handler
= self
._run
_ops
.get(operation
)
168 if operation
== 'query-checkcurr':
169 op_handler(self
, ifaceobj
, query_ifaceobj
)
171 op_handler(self
, ifaceobj
)