]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/addons/vrrpd.py
3 # Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
13 import ifupdown2
.ifupdown
.ifupdownflags
as ifupdownflags
15 from ifupdown2
.ifupdownaddons
.modulebase
import moduleBase
17 from ifupdown2
.ifupdown
.iface
import *
18 from ifupdown2
.ifupdown
.utils
import utils
20 import ifupdown
.ifupdownflags
as ifupdownflags
22 from ifupdownaddons
.modulebase
import moduleBase
24 from ifupdown
.iface
import *
25 from ifupdown
.utils
import utils
28 class vrrpd(moduleBase
):
29 """ ifupdown2 addon module to configure vrrpd attributes """
31 _modinfo
= {'mhelp' : 'ethtool configuration module for interfaces',
34 {'help' : 'vrrp instance id',
35 'validrange' : ['1', '4096'],
36 'example' : ['vrrp-id 1']},
38 {'help': 'set vrrp priority',
39 'validrange' : ['0', '255'],
40 'example' : ['vrrp-priority 20']},
42 {'help': 'set vrrp virtual ip',
43 'validvals' : ['<ipv4>', ],
44 'example' : ['vrrp-virtual-ip 10.0.1.254']}}}
46 def __init__(self
, *args
, **kargs
):
47 moduleBase
.__init
__(self
, *args
, **kargs
)
49 def _check_if_process_is_running(self
, cmdname
, cmdline
):
53 cmdl
= [utils
.pidof_cmd
, cmdname
]
54 pidstr
= utils
.exec_commandl(cmdl
, stderr
=None).strip('\n')
64 tmpcmdline
= cmdline
.replace(' ', '')
66 pcmdline
= self
.read_file_oneline('/proc/%s/cmdline' %pid
)
67 pcmdline
= re
.sub(r
'\\(.)', r
'\1', pcmdline
)
68 self
.logger
.info('(%s)' %(pcmdline))
69 self
.logger
.info('(%s)' %(tmpcmdline))
70 self
.logger
.info('(%d) (%d)' %(len(pcmdline
), len(tmpcmdline
)))
71 if pcmdline
and pcmdline
== tmpcmdline
:
72 targetpids
.append(pid
)
77 def _up(self
, ifaceobj
):
78 """ up vrrpd -n -D -i $IFACE -v 1 -p 20 10.0.1.254
79 up ifplugd -i $IFACE -b -f -u0 -d1 -I -p -q """
81 if (not ifupdownflags
.flags
.DRYRUN
and
82 not os
.path
.exists('/sys/class/net/%s' %ifaceobj
.name
)):
86 attrval
= ifaceobj
.get_attr_value_first('vrrp-id')
88 cmd
+= ' -v %s' %attrval
91 attrval
= ifaceobj
.get_attr_value_first('vrrp-priority')
93 cmd
+= ' -p %s' %attrval
95 self
.logger
.warn('%s: incomplete vrrp parameters ' %ifaceobj
.name
,
96 '(priority not found)')
97 attrval
= ifaceobj
.get_attr_value_first('vrrp-virtual-ip')
101 self
.logger
.warn('%s: incomplete vrrp arguments ' %ifaceobj
.name
,
102 '(virtual ip not found)')
104 cmd
= ('%s -n -D -i %s %s' %
105 (utils
.vrrpd_cmd
, ifaceobj
.name
, cmd
))
106 utils
.exec_command(cmd
)
108 cmd
= ('%s -i %s -b -f -u0 -d1 -I -p -q' %
109 (utils
.ifplugd_cmd
, ifaceobj
.name
))
110 if self
._check
_if
_process
_is
_running
(utils
.ifplugd_cmd
, cmd
):
111 self
.logger
.info('%s: ifplugd already running' %ifaceobj
.name
)
113 utils
.exec_command(cmd
)
115 def _kill_pid_from_file(self
, pidfilename
):
116 if os
.path
.exists(pidfilename
):
117 pid
= self
.read_file_oneline(pidfilename
)
118 if os
.path
.exists('/proc/%s' %pid
):
119 os
.kill(int(pid
), signal
.SIGTERM
)
121 def _down(self
, ifaceobj
):
122 """ down ifplugd -k -i $IFACE
123 down kill $(cat /var/run/vrrpd_$IFACE_*.pid) """
124 attrval
= ifaceobj
.get_attr_value_first('vrrp-id')
128 utils
.exec_command('%s -k -i %s' %
129 (utils
.ifplugd_cmd
, ifaceobj
.name
))
131 self
.logger
.debug('%s: ifplugd down error (%s)'
132 %(ifaceobj
.name
, str(e
)))
135 for pidfile
in glob
.glob('/var/run/vrrpd_%s_*.pid' %ifaceobj
.name
):
137 self
._kill
_pid
_from
_file
(pidfile
)
139 self
.logger
.debug('%s: vrrpd down error (%s)'
140 %(ifaceobj
.name
, str(e
)))
143 def _query_check(self
, ifaceobj
, ifaceobjcurr
):
148 _run_ops
= {'post-up' : _up
,
152 """ returns list of ops supported by this module """
153 return self
._run
_ops
.keys()
155 def run(self
, ifaceobj
, operation
, query_ifaceobj
=None, **extra_args
):
156 """ run ethtool configuration on the interface object passed as
160 **ifaceobj** (object): iface object
162 **operation** (str): any of 'post-up', 'query-checkcurr',
165 **query_ifaceobj** (object): query check ifaceobject. This is only
166 valid when op is 'query-checkcurr'. It is an object same as
167 ifaceobj, but contains running attribute values and its config
168 status. The modules can use it to return queried running state
169 of interfaces. status is success if the running state is same
170 as user required state in ifaceobj. error otherwise.
172 op_handler
= self
._run
_ops
.get(operation
)
175 if operation
== 'query-checkcurr':
176 op_handler(self
, ifaceobj
, query_ifaceobj
)
178 op_handler(self
, ifaceobj
)