-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathcloudmonkey.py
More file actions
963 lines (861 loc) · 37.3 KB
/
cloudmonkey.py
File metadata and controls
963 lines (861 loc) · 37.3 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
try:
import argcomplete
import argparse
import atexit
import cmd
import csv
import copy
import json
import logging
import os
import shlex
import sys
import time
import types
import jmespath
from cachemaker import loadcache, savecache, monkeycache, splitverbsubject
from config import __version__, __description__, __projecturl__
from config import display_types
from config import read_config, write_config, config_file, default_profile
from dicttoxml import dicttoxml
from optparse import OptionParser
from prettytable import PrettyTable
from printer import monkeyprint
from requester import monkeyrequest
from requester import login
from requester import logout
from urlparse import urlparse
from xml.dom.minidom import parseString
except ImportError, e:
print("Import error in %s : %s" % (__name__, e))
import sys
sys.exit()
try:
from precache import apicache
except ImportError:
apicache = {'count': 0, 'verbs': [], 'asyncapis': []}
normal_readline = True
# Fix terminal env before importing readline
# Without it, char ESC[?1034h gets printed in output
# There is not TERM variable in some environment such as Docker.
if 'TERM' not in os.environ or os.environ['TERM'].startswith('xterm'):
os.environ['TERM'] = 'vt100'
try:
import readline
except ImportError, e:
print("Module readline not found, autocompletions will fail", e)
else:
import rlcompleter
readline_doc = getattr(readline, '__doc__', '')
if readline_doc is not None and 'libedit' in readline_doc:
readline.parse_and_bind("bind ^I rl_complete")
readline.parse_and_bind("bind ^R em-inc-search-prev")
normal_readline = False
else:
readline.parse_and_bind("tab: complete")
log_fmt = '%(asctime)s - %(filename)s:%(lineno)s - [%(levelname)s] %(message)s'
logger = logging.getLogger(__name__)
class CloudMonkeyShell(cmd.Cmd, object):
intro = ("☁ Apache CloudStack 🐵 (legacy) cloudmonkey " + __version__ + ".\n" +
"Please switch to latest Go-based version https://github.com/apache/cloudstack-cloudmonkey/releases.\n" +
"For usage see https://github.com/apache/cloudstack-cloudmonkey/wiki.\n" +
"Type help or ? to list commands.\n")
ruler = "="
config_options = []
profile_names = []
verbs = []
interpreterMode = False
error_on_last_command = False
param_cache = {}
prompt = "🐵 > "
protocol = "http"
host = "localhost"
port = "8080"
path = "/client/api"
hook_count = 0
def __init__(self, pname, cfile):
self.default_apis = self.completenames('')
self.program_name = pname
self.config_file = cfile
self.config_options = read_config(self.get_attr, self.set_attr,
self.config_file)
self.loadcache()
self.init_credential_store()
logging.basicConfig(filename=self.log_file,
level=logging.DEBUG, format=log_fmt)
logger.debug("Loaded config fields:\n%s" % map(lambda x: "%s=%s" %
(x, getattr(self, x)),
self.config_options))
cmd.Cmd.__init__(self)
try:
if os.path.exists(self.history_file):
readline.read_history_file(self.history_file)
except IOError, e:
logger.debug(u"Error: Unable to read history. " + unicode(e))
atexit.register(readline.write_history_file, self.history_file)
def init_credential_store(self):
self.credentials = {'apikey': self.apikey, 'secretkey': self.secretkey,
'domain': self.domain, 'username': self.username,
'password': self.password,
'signatureversion': self.signatureversion}
parsed_url = urlparse(self.url)
self.protocol = "http" if not parsed_url.scheme else parsed_url.scheme
self.host = parsed_url.netloc
self.port = "8080" if not parsed_url.port else parsed_url.port
self.path = parsed_url.path
self.set_prompt()
def get_prompt(self):
return self.prompt.split(") ")[-1]
def set_prompt(self):
self.prompt = "(%s) %s" % (self.profile, self.get_prompt())
def get_attr(self, field):
return getattr(self, field)
def set_attr(self, field, value):
return setattr(self, field, value)
def emptyline(self):
pass
def cmdloop(self, intro=None):
self.interpreterMode = True
print(self.intro)
print "Using management server profile:", self.profile, "\n"
while True:
try:
super(CloudMonkeyShell, self).cmdloop(intro="")
except KeyboardInterrupt:
print("^C")
def precmd(self, line):
self.hook_count -= 1
if self.hook_count <= 0:
self.hook_count = 0
readline.set_startup_hook()
return line
def loadcache(self):
if os.path.exists(self.cache_file):
self.apicache = loadcache(self.cache_file)
else:
self.apicache = apicache
if 'verbs' in self.apicache:
self.verbs = self.apicache['verbs']
for verb in self.verbs:
handler_name = "do_" + str(verb)
handler_doc = str("%ss resources" % verb.capitalize())
if hasattr(self, handler_name) and getattr(self, handler_name).__doc__ == handler_doc:
continue
def add_grammar(verb):
default_handler = None
if self.default_apis.__contains__(verb):
default_handler = getattr(self, handler_name)
def grammar_closure(self, args):
if not args:
if default_handler:
default_handler(args)
return
args = args.decode("utf-8")
if self.pipe_runner(u"{0} {1}".format(verb, args)):
return
if ' --help' in args or ' -h' in args:
self.do_help("%s %s" % (verb, args))
return
try:
args_partition = args.partition(" ")
cmd = self.apicache[verb][args_partition[0]]['name']
args = args_partition[2]
except KeyError, e:
if default_handler:
default_handler(args)
else:
self.monkeyprint("Error: invalid %s api arg " % verb,
str(e))
return
self.default(u"{0} {1}".format(cmd, args))
return grammar_closure
grammar_handler = add_grammar(verb)
grammar_handler.__doc__ = handler_doc
grammar_handler.__name__ = handler_name
setattr(self.__class__, grammar_handler.__name__, grammar_handler)
def monkeyprint(self, *args):
output = u""
try:
for arg in args:
if isinstance(type(arg), types.NoneType) or not arg:
continue
if not (isinstance(arg, str) or isinstance(arg, unicode)):
arg = unicode(arg)
output += arg
except Exception, e:
print(str(e))
output = output.encode("utf-8")
if self.color == 'true':
monkeyprint(output)
else:
if output.startswith("Error"):
sys.stderr.write(output + "\n")
sys.stderr.flush()
else:
print output
def print_result(self, result, result_filter=[], result_query=''):
if not result or len(result) == 0:
return
filtered_result = copy.deepcopy(result)
if result_filter and isinstance(result_filter, list) \
and len(result_filter) > 0:
tfilter = {} # temp var to hold a dict of the filters
tresult = filtered_result # dupe the result to filter
if result_filter:
for res in result_filter:
tfilter[res] = 1
for okey, oval in result.iteritems():
if isinstance(oval, dict):
for tkey in oval:
if tkey not in tfilter:
try:
del(tresult[okey][oval][tkey])
except:
pass
elif isinstance(oval, list):
for x in range(len(oval)):
if isinstance(oval[x], dict):
for tkey in oval[x]:
if tkey not in tfilter:
try:
del(tresult[okey][x][tkey])
except:
pass
else:
try:
del(tresult[okey][x])
except:
pass
filtered_result = tresult
def print_result_json(result):
self.monkeyprint(json.dumps(result,
sort_keys=True,
indent=2,
ensure_ascii=False,
separators=(',', ': ')))
def print_result_jmespath(result, result_query):
try:
expression = jmespath.compile(result_query)
except Exception as e:
raise ValueError("Bad value for --query: %s \n\n %s" % (result_query, str(e)))
try:
self.monkeyprint(json.dumps(expression.search(result),
sort_keys=True,
indent=2,
ensure_ascii=False,
separators=(',', ': ')))
except Exception as e:
raise ValueError("Bad formatted JSON for JMESPATH: %s \n\n %s" % (result, str(e)))
def print_result_xml(result):
custom_root = "CloudStack-%s" % self.profile.replace(" ", "_")
xml = dicttoxml(result, attr_type=False, custom_root=custom_root)
self.monkeyprint(parseString(xml).toprettyxml())
def print_result_csv(result):
if "count" in result:
result.pop("count")
if len(result.keys()) == 1:
item = result[result.keys()[0]]
if isinstance(item, list):
result = item
elif isinstance(item, dict):
result = [item]
if isinstance(result, list) and len(result) > 0:
if isinstance(result[0], dict):
keys = result[0].keys()
writer = csv.DictWriter(sys.stdout, keys)
print ','.join(keys)
for item in result:
row = {}
for k in keys:
if k not in item:
row[k] = None
else:
if type(item[k]) is unicode:
row[k] = item[k].encode('utf8')
else:
row[k] = item[k]
writer.writerow(row)
elif isinstance(result, dict):
keys = result.keys()
writer = csv.DictWriter(sys.stdout, keys)
print ','.join(keys)
writer.writerow(result)
def print_result_tabular(result):
def print_table(printer, toprow):
if printer:
self.monkeyprint(printer.get_string())
return PrettyTable(toprow)
printer = None
toprow = []
if not result:
return
toprow = set(reduce(lambda x, y: x + y, map(lambda x: x.keys(),
filter(lambda x: isinstance(x, dict), result))))
printer = print_table(printer, toprow)
for node in result:
if not node:
continue
for key in toprow:
if key not in node:
node[key] = ''
row = map(lambda x: node[x], toprow)
if printer and row:
printer.add_row(row)
print_table(printer, toprow)
def print_result_as_dict(result):
for key in sorted(result.keys(), key=lambda x:
x not in ['id', 'count', 'name'] and x):
if isinstance(result[key], list):
self.monkeyprint(key + ":")
print_result_as_list(result[key])
elif isinstance(result[key], dict):
self.monkeyprint(key + ":")
print_result_as_dict(result[key])
else:
value = unicode(result[key])
self.monkeyprint(key, " = ", value)
def print_result_as_list(result):
for idx, node in enumerate(result):
if isinstance(node, dict):
if self.display == 'table':
print_result_tabular(result)
break
print_result_as_dict(node)
elif isinstance(node, list):
print_result_as_list(node)
else:
self.monkeyprint(filtered_result)
if result and node and (idx+1) < len(result):
self.monkeyprint(self.ruler * 80)
if self.display == "json":
print_result_json(filtered_result)
return
if self.display == "jmespath":
print_result_jmespath(filtered_result, result_query)
return
if self.display == "xml":
print_result_xml(filtered_result)
return
if self.display == "csv":
print_result_csv(filtered_result)
return
if isinstance(filtered_result, dict):
print_result_as_dict(filtered_result)
elif isinstance(filtered_result, list):
print_result_as_list(filtered_result)
else:
self.monkeyprint(filtered_result)
def make_request(self, command, args={}, isasync=False):
self.error_on_last_command = False
response, error = monkeyrequest(command, args, isasync,
self.asyncblock, logger,
self.url, self.credentials,
self.timeout, self.expires,
self.verifysslcert == 'true',
self.signatureversion)
if error:
self.monkeyprint(u"Error {0}".format(error))
self.error_on_last_command = True
return response
def update_param_cache(self, api, result={}):
if not api:
return
logger.debug("Updating param cache for %s API" % api)
responsekey = filter(lambda x: 'response' in x, result.keys())[0]
result = result[responsekey]
options = []
uuids = []
for key in result.keys():
if isinstance(result[key], list):
for element in result[key]:
if 'id' in element.keys():
uuid = unicode(element['id'])
name = ""
keyspace = ["name", "displayname",
"username", "description"]
for name_key in keyspace:
if name_key in element.keys():
name = element[name_key]
break
options.append((uuid, name,))
uuids.append(uuid)
self.param_cache[api] = {}
self.param_cache[api]["ts"] = int(time.time())
self.param_cache[api]["options"] = sorted(options)
return sorted(uuids)
def default(self, args):
try:
args = args.strip()
args.decode("utf-8")
except UnicodeError, ignore:
args = args.encode("utf-8")
if self.pipe_runner(args):
return
apiname = args.partition(' ')[0]
verb, subject = splitverbsubject(apiname)
lexp = shlex.shlex(args)
lexp.whitespace = " "
lexp.whitespace_split = True
lexp.posix = True
args = []
while True:
try:
next_val = lexp.next()
if not next_val:
break
next_val = next_val.decode("utf-8")
args.append(next_val.replace(u'\x00', u''))
except ValueError, err:
self.monkeyprint("Command parsing error: ", err)
return
args_dict = dict(map(lambda x: [x.partition("=")[0],
x.partition("=")[2]],
args[1:])[x] for x in range(len(args) - 1))
field_query = []
if 'query' in args_dict:
field_query = args_dict.pop('query')
field_filter = []
if 'filter' in args_dict:
field_filter = filter(lambda x: x.strip() != '',
args_dict.pop('filter').split(','))
field_filter = list(set(field_filter))
missing = []
if verb in self.apicache and subject in self.apicache[verb]:
missing = filter(lambda x: x not in [key.split('[')[0].lower()
for key in args_dict],
self.apicache[verb][subject]['requiredparams'])
if len(missing) > 0:
self.monkeyprint("Missing arguments: ", ' '.join(missing))
return
isasync = False
if 'asyncapis' in self.apicache:
if apiname.decode("utf-8") in self.apicache["asyncapis"]:
isasync = True
result = self.make_request(apiname, args_dict, isasync)
if not result or not isinstance(result, dict):
if isinstance(result, unicode):
result = result.decode("utf-8")
logger.debug("Invalid command result: %s" % result)
return
try:
responsekeys = filter(lambda x: 'response' in x, result.keys())
for responsekey in responsekeys:
self.print_result(result[responsekey], field_filter, field_query)
if apiname.startswith("list") and "id" not in args_dict:
self.update_param_cache(apiname, result)
except Exception as e:
self.monkeyprint("Error on parsing and printing ", e)
def completedefault(self, text, line, begidx, endidx):
partitions = line[:endidx].partition(" ")
verb = partitions[0].strip()
rline = partitions[2].lstrip().partition(" ")
subject = rline[0]
separator = rline[1]
params = rline[2].lstrip()
if verb not in self.verbs:
return []
autocompletions = []
search_string = ""
if separator != " ": # Complete verb subjects
autocompletions = map(lambda x: x + " ",
self.apicache[verb].keys())
search_string = subject
else: # Complete subject params
autocompletions = map(lambda x: x + "=",
map(lambda x: x['name'],
self.apicache[verb][subject]['params']))
search_string = text
if self.paramcompletion == 'true':
param = line[:endidx].split(" ")[-1]
idx = param.find("=")
value = param[idx + 1:]
param = param[:idx]
if param == "filter":
response_params = self.apicache[verb][subject]["response"]
used = filter(lambda x: x.strip() != "",
value.split(",")[:-1])
unused = map(lambda x: x['name'] + ",", filter(lambda x:
"name" in x and x["name"] not in used,
response_params))
last_value = value.split(",")[-1]
if last_value:
unused = filter(lambda x: x.startswith(last_value),
unused)
suffix = ",".join(used)
if suffix:
suffix += ","
global normal_readline
if normal_readline:
return filter(lambda x: x.startswith(last_value),
map(lambda x: x, unused))
else: # OSX fix
return filter(lambda x: x.startswith(value),
map(lambda x: suffix + x, unused))
elif len(value) < 36 and idx != -1:
api = None
logger.debug("[Paramcompl] For %s %s %s=" % (verb, subject,
param))
if "id" in param:
logger.debug("[Paramcompl] Using 'list' heuristics")
if param == "id" or param == "ids":
entity = subject
else:
entity = param.replace("id", "")
apis = []
for resource in self.apicache["list"]:
if resource.startswith(entity):
api = self.apicache["list"][resource]['name']
if (entity + "s") == resource.lower():
break
apis.append(api)
api = None
if len(apis) > 0 and not api:
logger.debug("[Paramcompl] APIs: %s" % apis)
api = min(apis, key=len)
logger.debug("[Paramcompl] Possible API: %s" % api)
if not api:
logger.debug("[Paramcompl] Using relative approx")
params = self.apicache[verb][subject]['params']
arg = filter(lambda x: x['name'] == param, params)[0]
if "type" in arg and arg["type"] == "boolean":
return filter(lambda x: x.startswith(value),
["true ", "false "])
related = arg['related']
apis = filter(lambda x: 'list' in x, related)
logger.debug("[Paramcompl] Related APIs: %s" % apis)
if len(apis) > 0:
api = apis[0]
else:
if param == "account":
api = "listAccounts"
else:
return
uuids = []
cache_burst_ts = int(time.time()) - 900
logger.debug("Trying paramcompletion using API: %s" % api)
if api in self.param_cache.keys() and \
len(self.param_cache[api]["options"]) > 0 and \
self.param_cache[api]["ts"] > cache_burst_ts:
for option in self.param_cache[api]["options"]:
uuid = option[0]
if uuid.startswith(value):
uuids.append(uuid)
else:
api_args = {'listall': 'true', 'templatefilter': 'all'}
response = self.make_request(api, args=api_args)
if not response:
return
uuids = self.update_param_cache(api, response)
if len(uuids) > 1:
print
options = sorted(self.param_cache[api]["options"],
key=lambda x: x[1])
for option in options:
uuid = option[0]
name = option[1]
if uuid.startswith(value):
print uuid, name
autocompletions = map(lambda x: x + " ", uuids)
search_string = value
if subject != "" and line.split(" ")[-1].find('=') == -1:
autocompletions.append("filter=")
return [s for s in autocompletions if s.startswith(search_string)]
def do_sync(self, args):
"""
Asks cloudmonkey to discovery and sync apis available on user specified
CloudStack host server which has the API discovery plugin, on failure
it rollbacks last datastore or api precached datastore.
"""
response = self.make_request("listApis")
if not response:
monkeyprint("Failed to sync apis, please check your config?")
monkeyprint("Note: `sync` requires api discovery service enabled" +
" on the CloudStack management server")
return
self.apicache = monkeycache(response)
savecache(self.apicache, self.cache_file)
monkeyprint("%s APIs discovered and cached" % self.apicache["count"])
self.loadcache()
def do_api(self, args):
"""
Make raw api calls. Syntax: api <apiName> <args>=<values>.
Example:
api listAccount listall=true
"""
if len(args) > 0:
return self.default(args)
else:
self.monkeyprint("Please use a valid syntax")
def do_set(self, args):
"""
Set config for cloudmonkey. For example, options can be:
url, auth, log_file, history_file
You may also edit your ~/.cloudmonkey_config instead of using set.
Example:
set url http://localhost:8080/client/api
set prompt 🐵 cloudmonkey>
set log_file /var/log/cloudmonkey.log
"""
args = args.strip().partition(" ")
key, value = (args[0].strip(), args[2].strip())
if not key:
return
allowed_blank_keys = ["username", "password", "apikey", "secretkey",
"domain"]
if key not in allowed_blank_keys and not value:
print "Blank value of %s is not allowed" % key
return
self.prompt = self.get_prompt()
setattr(self, key, value)
if key in ['host', 'port', 'path', 'protocol']:
key = 'url'
self.url = "%s://%s:%s%s" % (self.protocol, self.host,
self.port, self.path)
print "This option has been deprecated, please set 'url' instead"
print "This server url will be used:", self.url
write_config(self.get_attr, self.config_file)
read_config(self.get_attr, self.set_attr, self.config_file)
self.init_credential_store()
if key.strip() == 'profile' and self.interpreterMode:
print "\nLoaded server profile '%s' with options:" % value
for option in default_profile.keys():
value = self.get_attr(option)
if option in ["password", "apikey", "secretkey"] and value:
value = value[:2] + "XXX" + value[4:6] + "YYY...(hidden)"
print " %s = %s" % (option, value)
print
def complete_set(self, text, line, begidx, endidx):
mline = line.partition(" ")[2].lstrip().partition(" ")
option = mline[0].strip()
separator = mline[1]
value = mline[2].lstrip()
if separator == "":
return [s for s in self.config_options if s.startswith(option)] + self.completedefault(text, line, begidx, endidx)
elif option == "profile":
return [s for s in self.profile_names if s.startswith(value)]
elif option == "display":
return [s for s in display_types
if s.startswith(value)]
elif option in ["asyncblock", "color", "paramcompletion",
"verifysslcert"]:
return [s for s in ["true", "false"] if s.startswith(value)]
elif "set" in self.apicache["verbs"]:
return self.completedefault(text, line, begidx, endidx)
return []
def do_login(self, args):
"""
Login using stored credentials. Starts a session to be reused for
subsequent api calls
"""
try:
session, sessionkey = login(self.url, self.username, self.password)
self.credentials['session'] = session
self.credentials['sessionkey'] = sessionkey
except Exception, e:
self.monkeyprint("Error: Login failed to the server: ", unicode(e))
def do_logout(self, args):
"""
Logout of session started with login with username and password
"""
try:
logout(self.url, self.credentials.get('session'))
except Exception, e:
pass
self.credentials['session'] = None
self.credentials['sessionkey'] = None
def pipe_runner(self, args):
if args.find(" |") > -1:
pname = self.program_name
if '.py' in pname:
pname = "python " + pname
if isinstance(args, str):
self.do_shell("{0} {1}".format(pname, args))
else:
self.do_shell(u"{0} {1}".format(pname, args))
return True
return False
def do_shell(self, args):
"""
Execute shell commands using shell <command> or !<command>
Example:
!ls
shell ls
!for((i=0; i<10; i++)); do cloudmonkey create user account=admin \
email=test@test.tt firstname=user$i lastname=user$i \
password=password username=user$i; done
"""
if args.isdigit():
self.do_history("!" + args)
return
if isinstance(args, str):
os.system(args)
else:
os.system(args.encode("utf-8"))
def do_history(self, args):
"""
Prints cloudmonkey history
"""
if self.pipe_runner("history " + args):
return
startIdx = 1
endIdx = readline.get_current_history_length()
numLen = len(str(endIdx))
historyArg = args.split(' ')[0]
if historyArg.isdigit():
startIdx = endIdx - long(historyArg)
if startIdx < 1:
startIdx = 1
elif historyArg == "clear" or historyArg == "c":
readline.clear_history()
print "CloudMonkey history cleared"
return
elif len(historyArg) > 1 and historyArg[0] == "!" and historyArg[1:].isdigit():
command = readline.get_history_item(long(historyArg[1:]))
readline.set_startup_hook(lambda: readline.insert_text(command))
self.hook_count = 1
return
for idx in xrange(startIdx, endIdx):
self.monkeyprint("%s %s" % (str(idx).rjust(numLen),
readline.get_history_item(idx)))
def do_help(self, args):
"""
Show help docs for various topics
Example:
help list
help list users
?list
?list users
"""
fields = args.partition(" ")
if fields[2] == "":
cmd.Cmd.do_help(self, args)
else:
verb = fields[0]
subject = fields[2].partition(" ")[0]
if subject in self.apicache[verb]:
api = self.apicache[verb][subject]
helpdoc = "(%s) %s" % (api['name'], api['description'])
if api['isasync']:
helpdoc += "\nThis API is asynchronous."
required = api['requiredparams']
if len(required) > 0:
helpdoc += "\nRequired params are %s" % ' '.join(required)
helpdoc += "\nParameters\n" + "=" * 10
for param in api['params']:
helpdoc += "\n%s = (%s) %s" % (
param['name'], param['type'],
param['description'])
self.monkeyprint(helpdoc)
else:
self.monkeyprint("Error: no such api (%s) on %s" %
(subject, verb))
def complete_help(self, text, line, begidx, endidx):
fields = line.partition(" ")
subfields = fields[2].partition(" ")
if subfields[1] != " ":
return cmd.Cmd.complete_help(self, text, line, begidx, endidx)
else:
line = fields[2]
text = subfields[2]
return self.completedefault(text, line, begidx, endidx)
def do_EOF(self, args):
"""
Quit on Ctrl+d or EOF
"""
self.do_logout(None)
sys.exit()
def do_exit(self, args):
"""
Quit CloudMonkey CLI
"""
return self.do_quit(args)
def do_quit(self, args):
"""
Quit CloudMonkey CLI
"""
self.monkeyprint("Bye!")
return self.do_EOF(args)
def main():
parser = argparse.ArgumentParser(usage="cloudmonkey [options] [commands]",
description=__description__,
epilog="Try cloudmonkey [help|?]")
parser.add_argument("-v", "--version", action="version",
default=argparse.SUPPRESS,
version="cloudmonkey %s" % __version__,
help="show CloudMonkey's version and exit")
parser.add_argument("-c", "--config-file",
dest="configFile", default=config_file,
help="config file for cloudmonkey", metavar="FILE")
parser.add_argument("-b", "--block-async", action="store_true",
help="block and poll result on async API calls")
parser.add_argument("-n", "--noblock-async", action="store_true",
help="do not block on async API calls")
parser.add_argument("-d", "--display-type",
dest="displayType", default=None,
help="output displays: json, xml, table or default",
choices=tuple(display_types))
parser.add_argument("-p", "--profile",
dest="serverProfile", default=None,
help="server profile to load")
parser.add_argument("commands", nargs=argparse.REMAINDER,
help="API commands")
argcomplete.autocomplete(parser)
args = parser.parse_args()
shell = CloudMonkeyShell(sys.argv[0], args.configFile)
if args.displayType and args.displayType in display_types:
shell.set_attr("display", args.displayType)
if args.noblock_async:
shell.set_attr("asyncblock", "false")
if args.block_async:
shell.set_attr("asyncblock", "true")
if args.serverProfile and args.serverProfile.strip() != '':
shell.do_set("profile %s" % args.serverProfile)
if len(args.commands) > 0:
shell.set_attr("color", "false")
commands = []
for command in args.commands:
split_command = command.split("=", 1)
if len(split_command) > 1:
key = split_command[0]
value = split_command[1]
if " " not in value or \
(value.startswith("\"") and value.endswith("\"")) or \
(value.startswith("\'") and value.endswith("\'")):
commands.append(command)
else:
commands.append("%s=\"%s\"" % (key, value))
else:
commands.append(command)
shell.onecmd(" ".join(commands))
if shell.error_on_last_command:
sys.exit(1)
else:
shell.cmdloop()
try:
sys.stdout.close()
except:
pass
try:
sys.stderr.close()
except:
pass
if __name__ == "__main__":
main()