-
Notifications
You must be signed in to change notification settings - Fork 2
/
amf.py
301 lines (271 loc) · 11.8 KB
/
amf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/python
'''
##################################################################################
# License: MIT
# Copyright 2018 Agile Data Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE.
##################################################################################
'''
import os, sys, json
import subprocess
import traceback
class amf_print:
'''Utiity class for printing json or non json output '''
def __init__(self, svc):
self.svc = svc
self.uiflag = '-u' in sys.argv # ui flag to write json
self.verbose = '-v' in sys.argv # make output more verbose
def error(self, data):
self.write('ERROR', data)
def info(self, data):
self.write('INFO', data)
def write(self, level, data):
if self.uiflag:
# use json when ui flag is set
if self.verbose or level != 'INFO':
vals = {'LEVEL':level, 'SVC': self.svc.upper(), 'DATA': data}
print(json.dumps(vals))
else:
if type(data) is list:
# put >>> in front of listed items
for line in data:
print('>>>\t', line)
else:
if level:
# print level if specified
print(level+':\t', data)
else:
print(data)
class amfservice:
'''
Implement a subclass of pyservice to create a module to manage a specific application.
Name the module after the application name. For example: sfg.py, seas.py, etc.
'''
def __init__(self, service, home):
self.home = home
self.service = service
self.verbose = '-v' in sys.argv
self.printer = amf_print(service)
def run(self, command, printflag=True, noWait=False):
self.outbuf = [] # buffer for stdout
self.errbuf = [] # buffer for stderr
# launch process
if noWait:
process = subprocess.Popen(command, shell=True, stdout=None, stderr=None)
process.wait()
return process.returncode
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# save output in buffer for possible search later
for line in process.stdout:
line = line.strip()
if printflag and self.verbose:
self.printer.info(line)
self.outbuf.append(line)
for line in process.stderr:
line = line.strip()
if printflag and self.verbose:
self.printer.error(line)
self.errbuf.append(line)
# wait for process to end
process.wait()
# printflag set, print out buffers to help identify problem
if printflag and process.returncode != 0:
self.printer.error('%s failed, return code: %d' % (command, process.returncode))
if not self.verbose:
self.printer.info('dumping standard output...')
self.printer.write('STDOUT', self.outbuf)
self.printer.info('dumping standard error...')
self.printer.write('STDERR', self.errbuf)
return process.returncode
class amf:
'''Help page for AMF v0.1
Usage: AMF <command> <service> (options)
Where required 'command' is one of the following:
help - will display this information
list - list services and service commands
Where required 'service' is either a valid service name or 'all'
When 'all' is specified, the command will apply to all services.
The following optional parameters are supported:
-v verbose
-u support ui interactions, returns output in json.
Optional parameters must not contain a space after the hyamfen '-', the options
may be anywhere in the command line. Here are some examples. Options may not
be combined. Some of the options like -u and all are not implemented yet.
Examples of correct formats:
AMF start all -v
AMF start -v all
AMF -v start all
AMF -v -u start all
Examples of incorrect formats:
AMF start all -vu
AMF start - v all
examples of usage:
'AMF list all' will list all services
'AMF list sfg' will list all commands supported for sfg
'AMF start sfg' will start sfg
'AMF start all' will start all services supporting the 'start' command
'AMF start sfg -v' will start sfg with verbose output
'AMF restart sfg' will restart sfg
'AMF status sfg' will show status for sfg.
notes:
Commands and services are not case sensitive.
'''
GET_HELP = 'type "AMF help" for help'
def get_parms(self):
self.printer = amf_print('main')
command = None
service = 'all'
self.svclist = {}
# skip first parm containing program name
for arg in sys.argv[1:]:
# skip flags starting with -
if arg.startswith('-'):
continue
else:
# expect service name, then command name
# flags with - can be anywhere on command line
if not command:
command = arg.lower()
else:
service = arg.lower()
if command and service:
searchparm = 'AMF_'
if service != 'all':
searchparm += service.upper() + '_'
# find all modules configured in .profile
# configured using AMF_HOME where ?? is module name
# in upper case
for parm in os.environ.keys():
if not parm.startswith(searchparm):
continue
svc = parm[4:parm.rindex('_')].lower()
if svc != '':
self.svclist[svc] = os.environ.get(parm)
return command, service
def do_list(self, service):
status = 0
# for each service home found, list supported commands for service
if service == 'all':
self.printer.info('listing all services')
# for each module, load module, get attributes
for svc in self.svclist.keys():
self.printer.info('service [%s]' % svc)
try:
# import module
module = __import__('amf_svc_'+svc)
except ImportError:
print(traceback.format_exc())
self.printer.error('list failed, module not installed [%s.py]' % svc)
status = 1
continue
# get class definition
myclass = getattr(module, svc)
# get public class methods
clist = []
self.printer.info('service description')
# get instance of class
clazz = myclass('amf_svc_'+svc, self.svclist[svc])
# get class doc string for printing dpc strings
self.format(clist, clazz)
# print class info
self.printer.info(clist)
clist = []
self.printer.info('service commands...')
# get class methods for printing dpc strings
methods = myclass.__dict__
mlist = methods.keys()
mlist.sort()
for method in mlist:
# add info for each method
self.format(clist, clazz, method)
self.printer.info(clist)
# this just to suppress warning that myclazz not used
myclass = clazz
return status
def format(self, clist, clazz, method=None):
if method:
# if method not special method
if not method.startswith('_'):
# get doc string for the method
strval = eval('clazz.'+method+'.__doc__')
lines = strval.split('\n')
clist.append(method + '\t- %s' % lines[0])
if len(lines) > 1:
for line in lines[1:]:
if line.strip():
clist.append('\t %s' % line.strip())
else:
# get doc string for the class
for line in clazz.__doc__.split('\n'):
if line.strip():
clist.append('%s' % line.strip())
def do_command(self, command, service):
status = 0
# svclist is a list of local services supported
# e.g. SFG,SEAS
for svc in self.svclist.keys():
self.printer.info('processing command - %s %s' % (svc, command))
try:
# import service module
module = __import__('amf_svc_'+svc)
except ImportError:
self.printer.error('%s failed, module not installed [%s.py]' % (command, svc))
status = 1
continue
# get class definition
myclass = getattr(module, svc)
# create instance of the class
clazz = myclass(svc, self.svclist[svc])
# perform specified operation on the class
# get public class methods
methods = myclass.__dict__
# check if method is supported
method = methods.get(command)
if not method:
self.printer.error('%s service does not support command [%s]' % (svc, command))
status = 1
else:
# else run class method
if eval('clazz.' + command + '()'):
status = 1
# the following to avoid warnings for unused clazz variable
module = clazz
return status
def run(self):
status = 1
command, service = self.get_parms()
if not command:
self.printer.error('no command specified')
self.printer.write(None, self.GET_HELP)
elif command == 'help':
self.printer.write('', self.__doc__)
status = 0
elif len(self.svclist) == 0:
if service == 'all':
self.printer.error('no services configured in the environment')
else:
self.printer.error('service not configured [%s]' % service)
self.printer.error(self.GET_HELP)
elif not command:
self.printer.error('command not specified')
self.printer.write('', self.GET_HELP)
elif not service:
self.printer.error('service not specified')
self.printer.write(None, self.GET_HELP)
elif command == 'list':
status = self.do_list(service)
else:
status = self.do_command(command, service)
if command != 'help':
text = 'FAILED'
if status == 0:
text = 'SUCCESS'
self.printer.write('STATUS', text)
return status
if __name__ == '__main__':
amf = amf()
status = amf.run()
sys.exit(status)