]> git.proxmox.com Git - mirror_ifupdown2.git/blame - ifupdown2/ifupdown/ifupdownbase.py
Merge 'vlan filtering bridge + vxlan + mlag + vrr' support from internal
[mirror_ifupdown2.git] / ifupdown2 / ifupdown / ifupdownbase.py
CommitLineData
2c8c4ce7
RP
1#!/usr/bin/python
2#
3# Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4# Author: Roopa Prabhu, roopa@cumulusnetworks.com
5#
6# ifupdownBase --
7# base object for various ifupdown objects
8#
9
10import logging
11import subprocess
12import re
13import os
14from iface import *
f82758bf 15import rtnetlink_api as rtnetlink_api
2c8c4ce7
RP
16
17class ifupdownBase(object):
18
19 def __init__(self):
20 modulename = self.__class__.__name__
21 self.logger = logging.getLogger('ifupdown.' + modulename)
22
23 def exec_command(self, cmd, cmdenv=None, nowait=False):
24 cmd_returncode = 0
25 cmdout = ''
26 try:
27 self.logger.info('Executing ' + cmd)
28 if self.DRYRUN:
29 return cmdout
30 ch = subprocess.Popen(cmd.split(),
31 stdout=subprocess.PIPE,
32 shell=False, env=cmdenv,
33 stderr=subprocess.STDOUT,
34 close_fds=True)
35 cmdout = ch.communicate()[0]
36 cmd_returncode = ch.wait()
37 except OSError, e:
38 raise Exception('could not execute ' + cmd +
39 '(' + str(e) + ')')
40 if cmd_returncode != 0:
41 raise Exception('error executing cmd \'%s\'' %cmd +
42 '\n(' + cmdout.strip('\n ') + ')')
43 return cmdout
44
45 def ignore_error(self, errmsg):
46 if (self.FORCE == True or re.search(r'exists', errmsg,
47 re.IGNORECASE | re.MULTILINE) is not None):
48 return True
49 return False
50
51 def log_warn(self, str):
52 if self.ignore_error(str) == False:
53 if self.logger.getEffectiveLevel() == logging.DEBUG:
54 traceback.print_stack()
55 self.logger.warn(str)
56 pass
57
58 def log_error(self, str):
59 if self.ignore_error(str) == False:
60 raise
61 #raise Exception(str)
62 else:
63 pass
64
65 def link_exists(self, ifacename):
66 return os.path.exists('/sys/class/net/%s' %ifacename)
67
68 def link_up(self, ifacename):
f82758bf 69 rtnetlink_api.rtnl_api.link_set(ifacename, "up")
2c8c4ce7
RP
70
71 def link_down(self, ifacename):
f82758bf 72 rtnetlink_api.rtnl_api.link_set(ifacename, "down")