]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown/ifupdownbase.py
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
7 # base object for various ifupdown objects
14 import rtnetlink_api
as rtnetlink_api
19 from ifupdown
.utils
import utils
22 class ifupdownBase(object):
25 modulename
= self
.__class
__.__name
__
26 self
.logger
= logging
.getLogger('ifupdown.' + modulename
)
28 def exec_command(self
, cmd
, cmdenv
=None, nowait
=False):
32 self
.logger
.info('executing ' + cmd
)
35 ch
= subprocess
.Popen(shlex
.split(cmd
),
36 stdout
=subprocess
.PIPE
,
37 shell
=False, env
=cmdenv
,
38 stderr
=subprocess
.STDOUT
,
40 utils
.enable_subprocess_signal_forwarding(ch
, signal
.SIGINT
)
41 cmdout
= ch
.communicate()[0]
42 cmd_returncode
= ch
.wait()
44 raise Exception('could not execute ' + cmd
+
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 ') + ')')
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):
59 def log_warn(self
, str):
60 if self
.ignore_error(str) == False:
61 if self
.logger
.getEffectiveLevel() == logging
.DEBUG
:
62 traceback
.print_stack()
66 def log_error(self
, str):
67 if self
.ignore_error(str) == False:
73 def link_exists(self
, ifacename
):
74 return os
.path
.exists('/sys/class/net/%s' %ifacename
)
76 def link_up(self
, ifacename
):
77 rtnetlink_api
.rtnl_api
.link_set(ifacename
, "up")
79 def link_down(self
, ifacename
):
80 rtnetlink_api
.rtnl_api
.link_set(ifacename
, "down")