]>
Commit | Line | Data |
---|---|---|
f82758bf RP |
1 | #!/usr/bin/python |
2 | # | |
3 | # Copyright 2014 Cumulus Networks, Inc. All rights reserved. | |
4 | # Author: Roopa Prabhu, roopa@cumulusnetworks.com | |
5 | # | |
6 | ||
7 | import logging | |
8 | import subprocess | |
9 | import re | |
10 | import io | |
11 | from ifupdown.iface import * | |
12 | from cache import * | |
13 | ||
14 | #import timeit | |
15 | import time | |
16 | import logging | |
17 | ||
18 | def profile(func): | |
19 | def wrap(*args, **kwargs): | |
20 | started_at = time.time() | |
21 | result = func(*args, **kwargs) | |
22 | print str(func) | |
23 | print (time.time() - started_at) | |
24 | return result | |
25 | return wrap | |
26 | ||
27 | class utilsBase(object): | |
28 | """ Base class for ifupdown addon utilities """ | |
29 | ||
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) | |
38 | ||
39 | def exec_commandl(self, cmdl, cmdenv=None): | |
40 | """ Executes command """ | |
41 | ||
42 | cmd_returncode = 0 | |
43 | cmdout = '' | |
44 | try: | |
45 | self.logger.info('executing ' + ' '.join(cmdl)) | |
46 | if self.DRYRUN: | |
47 | return cmdout | |
48 | ch = subprocess.Popen(cmdl, | |
49 | stdout=subprocess.PIPE, | |
50 | shell=False, env=cmdenv, | |
51 | stderr=subprocess.STDOUT, | |
52 | close_fds=True) | |
53 | cmdout = ch.communicate()[0] | |
54 | cmd_returncode = ch.wait() | |
55 | except OSError, e: | |
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 ') + ')') | |
61 | return cmdout | |
62 | ||
63 | def exec_command(self, cmd, cmdenv=None): | |
64 | """ Executes command given as string in the argument cmd """ | |
65 | ||
66 | return self.exec_commandl(cmd.split(), cmdenv) | |
67 | ||
68 | def exec_command_talk_stdin(self, cmd, stdinbuf): | |
69 | """ Executes command and writes to stdin of the process """ | |
70 | cmd_returncode = 0 | |
71 | cmdout = '' | |
72 | try: | |
73 | self.logger.info('executing %s [%s]' %(cmd, stdinbuf)) | |
74 | if self.DRYRUN: | |
75 | return cmdout | |
76 | ch = subprocess.Popen(cmd.split(), | |
77 | stdout=subprocess.PIPE, | |
78 | stdin=subprocess.PIPE, | |
79 | shell=False, | |
80 | stderr=subprocess.STDOUT, | |
81 | close_fds=True) | |
82 | cmdout = ch.communicate(input=stdinbuf)[0] | |
83 | cmd_returncode = ch.wait() | |
84 | except OSError, e: | |
85 | raise Exception('failed to execute cmd \'%s\' (%s)' | |
86 | %(cmd, str(e))) | |
87 | if cmd_returncode != 0: | |
88 | raise Exception('failed to execute cmd \'%s [%s]\'' | |
89 | %(cmd, stdinbuf) + '(' + cmdout.strip('\n ') + ')') | |
90 | return cmdout | |
91 | ||
92 | def subprocess_check_output(self, cmdl): | |
93 | self.logger.info('executing ' + ' '.join(cmdl)) | |
94 | if self.DRYRUN: | |
95 | return | |
96 | try: | |
97 | return subprocess.check_output(cmdl, stderr=subprocess.STDOUT) | |
98 | except Exception, e: | |
99 | raise Exception('failed to execute cmd \'%s\' (%s)' | |
100 | %(' '.join(cmdl), e.output)) | |
101 | ||
102 | def subprocess_check_call(self, cmdl): | |
103 | """ subprocess check_call implementation using popen | |
104 | ||
105 | Uses popen because it needs the close_fds argument | |
106 | """ | |
107 | ||
108 | cmd_returncode = 0 | |
109 | try: | |
110 | self.logger.info('executing ' + ' '.join(cmdl)) | |
111 | if self.DRYRUN: | |
112 | return | |
113 | ch = subprocess.Popen(cmdl, | |
114 | stdout=None, | |
115 | shell=False, | |
116 | stderr=None, | |
117 | close_fds=True) | |
118 | cmd_returncode = ch.wait() | |
119 | except Exception, e: | |
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\'' | |
124 | %' '.join(cmdl)) | |
125 | return | |
126 | ||
127 | def write_file(self, filename, strexpr): | |
128 | try: | |
129 | self.logger.info('writing \'%s\'' %strexpr + | |
130 | ' to file %s' %filename) | |
131 | if self.DRYRUN: | |
132 | return 0 | |
133 | with open(filename, 'w') as f: | |
134 | f.write(strexpr) | |
135 | except IOError, e: | |
136 | self.logger.warn('error writing to file %s' | |
137 | %filename + '(' + str(e) + ')') | |
138 | return -1 | |
139 | return 0 | |
140 | ||
141 | def read_file(self, filename): | |
142 | try: | |
143 | self.logger.debug('reading \'%s\'' %filename) | |
144 | with open(filename, 'r') as f: | |
145 | return f.readlines() | |
146 | except: | |
147 | return None | |
148 | return None | |
149 | ||
150 | def read_file_oneline(self, filename): | |
151 | try: | |
152 | self.logger.debug('reading \'%s\'' %filename) | |
153 | with open(filename, 'r') as f: | |
154 | return f.readline().strip('\n') | |
155 | except: | |
156 | return None | |
157 | return None | |
158 | ||
159 | def sysctl_set(self, variable, value): | |
160 | self.exec_command('sysctl %s=' %variable + '%s' %value) | |
161 | ||
162 | def sysctl_get(self, variable): | |
163 | return self.exec_command('sysctl %s' %variable).split('=')[1].strip() |