]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/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
15 import rtnetlink_api
as rtnetlink_api
17 class ifupdownBase(object):
20 modulename
= self
.__class
__.__name
__
21 self
.logger
= logging
.getLogger('ifupdown.' + modulename
)
23 def exec_command(self
, cmd
, cmdenv
=None, nowait
=False):
27 self
.logger
.info('Executing ' + cmd
)
30 ch
= subprocess
.Popen(cmd
.split(),
31 stdout
=subprocess
.PIPE
,
32 shell
=False, env
=cmdenv
,
33 stderr
=subprocess
.STDOUT
,
35 cmdout
= ch
.communicate()[0]
36 cmd_returncode
= ch
.wait()
38 raise Exception('could not execute ' + cmd
+
40 if cmd_returncode
!= 0:
41 raise Exception('error executing cmd \'%s\'' %cmd
+
42 '\n(' + cmdout
.strip('\n ') + ')')
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):
51 def log_warn(self
, str):
52 if self
.ignore_error(str) == False:
53 if self
.logger
.getEffectiveLevel() == logging
.DEBUG
:
54 traceback
.print_stack()
58 def log_error(self
, str):
59 if self
.ignore_error(str) == False:
65 def link_exists(self
, ifacename
):
66 return os
.path
.exists('/sys/class/net/%s' %ifacename
)
68 def link_up(self
, ifacename
):
69 rtnetlink_api
.rtnl_api
.link_set(ifacename
, "up")
71 def link_down(self
, ifacename
):
72 rtnetlink_api
.rtnl_api
.link_set(ifacename
, "down")