]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown/ifupdownbase.py
fixing: ifupdown2 (subprocesses) lives on after control-c + Parsing cmd string with...
[mirror_ifupdown2.git] / ifupdown / ifupdownbase.py
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
10 import logging
11 import subprocess
12 import re
13 import os
14 import rtnetlink_api as rtnetlink_api
15 import signal
16 import shlex
17
18 from iface import *
19 from ifupdown.utils import utils
20
21
22 class ifupdownBase(object):
23
24 def __init__(self):
25 modulename = self.__class__.__name__
26 self.logger = logging.getLogger('ifupdown.' + modulename)
27
28 def exec_command(self, cmd, cmdenv=None, nowait=False):
29 cmd_returncode = 0
30 cmdout = ''
31 try:
32 self.logger.info('executing ' + cmd)
33 if self.DRYRUN:
34 return cmdout
35 ch = subprocess.Popen(shlex.split(cmd),
36 stdout=subprocess.PIPE,
37 shell=False, env=cmdenv,
38 stderr=subprocess.STDOUT,
39 close_fds=True)
40 utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT)
41 cmdout = ch.communicate()[0]
42 cmd_returncode = ch.wait()
43 except OSError, e:
44 raise Exception('could not execute ' + cmd +
45 '(' + str(e) + ')')
46 finally:
47 utils.disable_subprocess_signal_forwarding(signal.SIGINT)
48 if cmd_returncode != 0:
49 raise Exception('error executing cmd \'%s\'' %cmd +
50 '\n(' + cmdout.strip('\n ') + ')')
51 return cmdout
52
53 def ignore_error(self, errmsg):
54 if (self.FORCE == True or re.search(r'exists', errmsg,
55 re.IGNORECASE | re.MULTILINE) is not None):
56 return True
57 return False
58
59 def log_warn(self, str):
60 if self.ignore_error(str) == False:
61 if self.logger.getEffectiveLevel() == logging.DEBUG:
62 traceback.print_stack()
63 self.logger.warn(str)
64 pass
65
66 def log_error(self, str):
67 if self.ignore_error(str) == False:
68 raise
69 #raise Exception(str)
70 else:
71 pass
72
73 def link_exists(self, ifacename):
74 return os.path.exists('/sys/class/net/%s' %ifacename)
75
76 def link_up(self, ifacename):
77 rtnetlink_api.rtnl_api.link_set(ifacename, "up")
78
79 def link_down(self, ifacename):
80 rtnetlink_api.rtnl_api.link_set(ifacename, "down")