]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdownaddons/utilsbase.py
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
11 from ifupdown
.iface
import *
19 def wrap(*args
, **kwargs
):
20 started_at
= time
.time()
21 result
= func(*args
, **kwargs
)
23 print (time
.time() - started_at
)
27 class utilsBase(object):
28 """ Base class for ifupdown addon utilities """
30 def __init__(self
, *args
, **kargs
):
31 modulename
= self
.__class
__.__name
__
32 self
.logger
= logging
.getLogger('ifupdown.' + modulename
)
33 self
.FORCE
= kargs
.get('force', False)
34 self
.DRYRUN
= kargs
.get('dryrun', False)
35 self
.NOWAIT
= kargs
.get('nowait', False)
36 self
.PERFMODE
= kargs
.get('perfmode', False)
37 self
.CACHE
= kargs
.get('cache', False)
39 def exec_commandl(self
, cmdl
, cmdenv
=None):
40 """ Executes command """
45 self
.logger
.info('executing ' + ' '.join(cmdl
))
48 ch
= subprocess
.Popen(cmdl
,
49 stdout
=subprocess
.PIPE
,
50 shell
=False, env
=cmdenv
,
51 stderr
=subprocess
.STDOUT
,
53 cmdout
= ch
.communicate()[0]
54 cmd_returncode
= ch
.wait()
56 raise Exception('failed to execute cmd \'%s\' (%s)'
57 %(' '.join(cmdl
), str(e
)))
58 if cmd_returncode
!= 0:
59 raise Exception('failed to execute cmd \'%s\''
60 %' '.join(cmdl
) + '(' + cmdout
.strip('\n ') + ')')
63 def exec_command(self
, cmd
, cmdenv
=None):
64 """ Executes command given as string in the argument cmd """
66 return self
.exec_commandl(cmd
.split(), cmdenv
)
68 def exec_command_talk_stdin(self
, cmd
, stdinbuf
):
69 """ Executes command and writes to stdin of the process """
73 self
.logger
.info('executing %s [%s]' %(cmd
, stdinbuf
))
76 ch
= subprocess
.Popen(cmd
.split(),
77 stdout
=subprocess
.PIPE
,
78 stdin
=subprocess
.PIPE
,
80 stderr
=subprocess
.STDOUT
,
82 cmdout
= ch
.communicate(input=stdinbuf
)[0]
83 cmd_returncode
= ch
.wait()
85 raise Exception('failed to execute cmd \'%s\' (%s)'
87 if cmd_returncode
!= 0:
88 raise Exception('failed to execute cmd \'%s [%s]\''
89 %(cmd
, stdinbuf
) + '(' + cmdout
.strip('\n ') + ')')
92 def subprocess_check_output(self
, cmdl
):
93 self
.logger
.info('executing ' + ' '.join(cmdl
))
97 return subprocess
.check_output(cmdl
, stderr
=subprocess
.STDOUT
)
99 raise Exception('failed to execute cmd \'%s\' (%s)'
100 %(' '.join(cmdl
), e
.output
))
102 def subprocess_check_call(self
, cmdl
):
103 """ subprocess check_call implementation using popen
105 Uses popen because it needs the close_fds argument
110 self
.logger
.info('executing ' + ' '.join(cmdl
))
113 ch
= subprocess
.Popen(cmdl
,
118 cmd_returncode
= ch
.wait()
120 raise Exception('failed to execute cmd \'%s\' (%s)'
121 %(' '.join(cmdl
), str(e
)))
122 if cmd_returncode
!= 0:
123 raise Exception('failed to execute cmd \'%s\''
127 def write_file(self
, filename
, strexpr
):
129 self
.logger
.info('writing \'%s\'' %strexpr
+
130 ' to file %s' %filename
)
133 with
open(filename
, 'w') as f
:
136 self
.logger
.warn('error writing to file %s'
137 %filename
+ '(' + str(e
) + ')')
141 def read_file(self
, filename
):
143 self
.logger
.debug('reading \'%s\'' %filename
)
144 with
open(filename
, 'r') as f
:
150 def read_file_oneline(self
, filename
):
152 self
.logger
.debug('reading \'%s\'' %filename
)
153 with
open(filename
, 'r') as f
:
154 return f
.readline().strip('\n')
159 def sysctl_set(self
, variable
, value
):
160 self
.exec_command('sysctl %s=' %variable
+ '%s' %value
)
162 def sysctl_get(self
, variable
):
163 return self
.exec_command('sysctl %s' %variable
).split('=')[1].strip()