]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/addons/vrrpd.py
addons: bridge: warn users if bridge attributes are used under non-bridge stanza
[mirror_ifupdown2.git] / ifupdown2 / addons / vrrpd.py
1 #!/usr/bin/python
2 #
3 # Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
5 #
6
7 import os
8 import re
9 import glob
10 import signal
11
12 try:
13 import ifupdown2.ifupdown.ifupdownflags as ifupdownflags
14
15 from ifupdown2.ifupdownaddons.modulebase import moduleBase
16
17 from ifupdown2.ifupdown.iface import *
18 from ifupdown2.ifupdown.utils import utils
19 except ImportError:
20 import ifupdown.ifupdownflags as ifupdownflags
21
22 from ifupdownaddons.modulebase import moduleBase
23
24 from ifupdown.iface import *
25 from ifupdown.utils import utils
26
27
28 class vrrpd(moduleBase):
29 """ ifupdown2 addon module to configure vrrpd attributes """
30
31 _modinfo = {'mhelp' : 'ethtool configuration module for interfaces',
32 'attrs': {
33 'vrrp-id' :
34 {'help' : 'vrrp instance id',
35 'validrange' : ['1', '4096'],
36 'example' : ['vrrp-id 1']},
37 'vrrp-priority' :
38 {'help': 'set vrrp priority',
39 'validrange' : ['0', '255'],
40 'example' : ['vrrp-priority 20']},
41 'vrrp-virtual-ip' :
42 {'help': 'set vrrp virtual ip',
43 'validvals' : ['<ipv4>', ],
44 'example' : ['vrrp-virtual-ip 10.0.1.254']}}}
45
46 def __init__(self, *args, **kargs):
47 moduleBase.__init__(self, *args, **kargs)
48
49 def _check_if_process_is_running(self, cmdname, cmdline):
50 targetpids = []
51 pidstr = ''
52 try:
53 cmdl = [utils.pidof_cmd, cmdname]
54 pidstr = utils.exec_commandl(cmdl, stderr=None).strip('\n')
55 except:
56 pass
57 if not pidstr:
58 return []
59
60 pids = pidstr.split()
61 if not pids:
62 return targetpids
63 for pid in pids:
64 tmpcmdline = cmdline.replace(' ', '')
65 try:
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)
73 except:
74 pass
75 return targetpids
76
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 """
80
81 if (not ifupdownflags.flags.DRYRUN and
82 not os.path.exists('/sys/class/net/%s' %ifaceobj.name)):
83 return
84
85 cmd = ''
86 attrval = ifaceobj.get_attr_value_first('vrrp-id')
87 if attrval:
88 cmd += ' -v %s' %attrval
89 else:
90 return
91 attrval = ifaceobj.get_attr_value_first('vrrp-priority')
92 if attrval:
93 cmd += ' -p %s' %attrval
94 else:
95 self.logger.warn('%s: incomplete vrrp parameters ' %ifaceobj.name,
96 '(priority not found)')
97 attrval = ifaceobj.get_attr_value_first('vrrp-virtual-ip')
98 if attrval:
99 cmd += ' %s' %attrval
100 else:
101 self.logger.warn('%s: incomplete vrrp arguments ' %ifaceobj.name,
102 '(virtual ip not found)')
103 return
104 cmd = ('%s -n -D -i %s %s' %
105 (utils.vrrpd_cmd, ifaceobj.name, cmd))
106 utils.exec_command(cmd)
107
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)
112 return
113 utils.exec_command(cmd)
114
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)
120
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')
125 if not attrval:
126 return
127 try:
128 utils.exec_command('%s -k -i %s' %
129 (utils.ifplugd_cmd, ifaceobj.name))
130 except Exception, e:
131 self.logger.debug('%s: ifplugd down error (%s)'
132 %(ifaceobj.name, str(e)))
133 pass
134
135 for pidfile in glob.glob('/var/run/vrrpd_%s_*.pid' %ifaceobj.name):
136 try:
137 self._kill_pid_from_file(pidfile)
138 except Exception, e:
139 self.logger.debug('%s: vrrpd down error (%s)'
140 %(ifaceobj.name, str(e)))
141 pass
142
143 def _query_check(self, ifaceobj, ifaceobjcurr):
144 # XXX
145 return
146
147
148 _run_ops = {'post-up' : _up,
149 'pre-down' : _down}
150
151 def get_ops(self):
152 """ returns list of ops supported by this module """
153 return self._run_ops.keys()
154
155 def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
156 """ run ethtool configuration on the interface object passed as
157 argument
158
159 Args:
160 **ifaceobj** (object): iface object
161
162 **operation** (str): any of 'post-up', 'query-checkcurr',
163 'query-running'
164 Kwargs:
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.
171 """
172 op_handler = self._run_ops.get(operation)
173 if not op_handler:
174 return
175 if operation == 'query-checkcurr':
176 op_handler(self, ifaceobj, query_ifaceobj)
177 else:
178 op_handler(self, ifaceobj)