]> git.proxmox.com Git - mirror_ifupdown2.git/blame - addons/vrrpd.py
attribute syntax check using validvals/validrange and keywords
[mirror_ifupdown2.git] / addons / vrrpd.py
CommitLineData
830c91d7
RP
1#!/usr/bin/python
2#
3# Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4# Author: Roopa Prabhu, roopa@cumulusnetworks.com
5#
6
7try:
8 from ipaddr import IPNetwork
9 from sets import Set
10 from ifupdown.iface import *
a193d8d1 11 from ifupdown.utils import utils
830c91d7
RP
12 from ifupdownaddons.modulebase import moduleBase
13 from ifupdownaddons.iproute2 import iproute2
fc5e1735 14 import ifupdown.ifupdownflags as ifupdownflags
830c91d7
RP
15 import os
16 import glob
17 import logging
18 import signal
830c91d7
RP
19 import re
20except ImportError, e:
21 raise ImportError (str(e) + "- required module not found")
22
23class vrrpd(moduleBase):
24 """ ifupdown2 addon module to configure vrrpd attributes """
25
26 _modinfo = {'mhelp' : 'ethtool configuration module for interfaces',
27 'attrs': {
28 'vrrp-id' :
29 {'help' : 'vrrp instance id',
c6370b56 30 'validrange' : ['1', '4096'],
830c91d7
RP
31 'example' : ['vrrp-id 1']},
32 'vrrp-priority' :
33 {'help': 'set vrrp priority',
c6370b56 34 'validrange' : ['0', '255'],
830c91d7
RP
35 'example' : ['vrrp-priority 20']},
36 'vrrp-virtual-ip' :
37 {'help': 'set vrrp virtual ip',
482b2fab 38 'validvals' : ['<ipv4>', ],
830c91d7
RP
39 'example' : ['vrrp-virtual-ip 10.0.1.254']}}}
40
41 def __init__(self, *args, **kargs):
42 moduleBase.__init__(self, *args, **kargs)
43 self.ipcmd = None
44
45 def _check_if_process_is_running(self, cmdname, cmdline):
46 targetpids = []
47 pidstr = ''
48 try:
a193d8d1
JF
49 cmdl = ['/bin/pidof', cmdname]
50 pidstr = utils.exec_commandl(cmdl, stderr=None).strip('\n')
830c91d7
RP
51 except:
52 pass
53 if not pidstr:
54 return []
55
56 pids = pidstr.split()
57 if not pids:
58 return targetpids
59 for pid in pids:
60 tmpcmdline = cmdline.replace(' ', '')
61 try:
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)
69 except:
70 pass
830c91d7
RP
71 return targetpids
72
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 """
76
fc5e1735 77 if (not ifupdownflags.flags.DRYRUN and
830c91d7
RP
78 not os.path.exists('/sys/class/net/%s' %ifaceobj.name)):
79 return
80
81 cmd = ''
82 attrval = ifaceobj.get_attr_value_first('vrrp-id')
83 if attrval:
84 cmd += ' -v %s' %attrval
85 else:
86 return
87 attrval = ifaceobj.get_attr_value_first('vrrp-priority')
88 if attrval:
89 cmd += ' -p %s' %attrval
90 else:
91 self.logger.warn('%s: incomplete vrrp parameters ' %ifaceobj.name,
92 '(priority not found)')
93 attrval = ifaceobj.get_attr_value_first('vrrp-virtual-ip')
94 if attrval:
95 cmd += ' %s' %attrval
96 else:
97 self.logger.warn('%s: incomplete vrrp arguments ' %ifaceobj.name,
98 '(virtual ip not found)')
99 return
100 cmd = '/usr/sbin/vrrpd -n -D -i %s %s' %(ifaceobj.name, cmd)
a193d8d1 101 utils.exec_command(cmd)
830c91d7
RP
102
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)
106 return
a193d8d1 107 utils.exec_command(cmd)
830c91d7
RP
108
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)
114
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')
119 if not attrval:
120 return
121 try:
a193d8d1 122 utils.exec_command('/usr/sbin/ifplugd -k -i %s' % ifaceobj.name)
830c91d7
RP
123 except Exception, e:
124 self.logger.debug('%s: ifplugd down error (%s)'
125 %(ifaceobj.name, str(e)))
126 pass
127
128 for pidfile in glob.glob('/var/run/vrrpd_%s_*.pid' %ifaceobj.name):
129 try:
130 self._kill_pid_from_file(pidfile)
131 except Exception, e:
132 self.logger.debug('%s: vrrpd down error (%s)'
133 %(ifaceobj.name, str(e)))
134 pass
135
136 def _query_check(self, ifaceobj, ifaceobjcurr):
137 # XXX
138 return
139
140
141 _run_ops = {'post-up' : _up,
142 'pre-down' : _down}
143
144 def get_ops(self):
145 """ returns list of ops supported by this module """
146 return self._run_ops.keys()
147
148 def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
149 """ run ethtool configuration on the interface object passed as
150 argument
151
152 Args:
153 **ifaceobj** (object): iface object
154
155 **operation** (str): any of 'post-up', 'query-checkcurr',
156 'query-running'
157 Kwargs:
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.
164 """
165 op_handler = self._run_ops.get(operation)
166 if not op_handler:
167 return
168 if operation == 'query-checkcurr':
169 op_handler(self, ifaceobj, query_ifaceobj)
170 else:
171 op_handler(self, ifaceobj)