def parse(self, text): args = shlex.split(text) if len(args) == 0: return cmd = args[0] if cmd in self.cmds_internal: self.cmds_internal[cmd](args[1:]) print '' return if self.default_dc is not None: text = '-dcid ' + self.default_dc + ' ' + text args = shlex.split(text) for c in self.cmds_api: if c.replace('-', '').replace('@', '').lower() == cmd.replace('-', '').replace('@', '').lower(): if c == 'deleteDataCenter' and self.default_dc is not None: print 'Data center ' + self.default_dc + ' is in use. You may not perform data center deletion operations. Type \'use\' to reset and try again\n' return args.insert(0, 'dummy') # equivalent of argv[0] argsParser = pb.argsparser.ArgsParser() argsParser.readUserArgs(args) requestedOp = argsParser.getRequestedOperation() if requestedOp[0] == '@': helper = pb.helper.Helper() pb.argsparser.ArgsParser.operations[requestedOp]['lambda'](helper) return if not argsParser.isAuthenticated(): print 'Missing authentication' return formatter = pb.formatter.Formatter() if argsParser.baseArgs['s']: formatter.shortFormat() api = pb.api.API(argsParser.baseArgs['u'], argsParser.baseArgs['p'], debug = argsParser.baseArgs['debug']) if pb.errorhandler.last_error() != 0: return pb.argsparser.ArgsParser.operations[requestedOp]['lambda'](formatter, api, argsParser.opArgs) if pb.errorhandler.last_error() != 0: return while self.wait and self.default_dc is not None: if self.default_dc_state(api) == 'AVAILABLE' or pb.errorhandler.last_error != 0: break sys.stdout.write('.') sys.stdout.flush() time.sleep(1) print '-' return
print(sys.argv) pb.errorhandler.ArgsError("Unknown operation: " + argsParser.baseArgs["op"]) # @ operations don't require pb api nor authentication if requestedOp[0] == "@": helper = pb.helper.Helper() pb.argsparser.ArgsParser.operations[requestedOp]["api"](helper) sys.exit(0) # perform regular (non-@) operation if not argsParser.isAuthenticated(): pb.errorhandler.ArgsError("Authentication error: Missing authentication") formatter = pb.formatter.Formatter() if argsParser.baseArgs["s"]: formatter.shortFormat() formatter.batch = argsParser.baseArgs["batch"] try: api = pb.api.API(argsParser.baseArgs["u"], argsParser.baseArgs["p"], debug=argsParser.baseArgs["debug"]) apiResult = pb.argsparser.ArgsParser.operations[requestedOp]["api"](api, argsParser.opArgs) pb.argsparser.ArgsParser.operations[requestedOp]["out"](formatter, apiResult) if (not argsParser.baseArgs["s"]) and (not formatter.batch): print("") print("Request ID: %s" % (str(api.requestId) if api.requestId is not None else "(none)")) except Exception as e: print("Error: %s" % str(e)) sys.exit(3) # soap fault
def run_command(self, cmd, args): if cmd == 'deletedatacenter': if self.default_dc is not None: self.out('Data center ' + self.default_dc + ' is in use. You may not perform any data center deletion operations. Type \'use\' to reset and try again\n') return args.insert(0, 'pbapi-internal-placeholder-text') # equivalent of argv[0] argsParser = pb.argsparser.ArgsParser() pb.errorhandler.initializing += 1 argsParser.readUserArgs(sys.argv) pb.errorhandler.last_error() pb.errorhandler.initializing -= 1 argsParser.readUserArgs(args) requestedOp = argsParser.getRequestedOperation() if requestedOp == '': # argsParser is telling us to ignore this command # this hack will be removed once we replace errorhandler with exceptions return if requestedOp is None: raise Exception('Invalid operation') if requestedOp[0] == '@': helper = pb.helper.Helper() pb.argsparser.ArgsParser.operations[requestedOp]['api'](helper) return if not argsParser.isAuthenticated(): raise Exception('Authentication error: Missing authentication') formatter = pb.formatter.Formatter() if argsParser.baseArgs['s']: formatter.shortFormat() formatter.batch = argsParser.baseArgs["batch"] try: api = pb.api.API(argsParser.baseArgs['u'], argsParser.baseArgs['p'], debug = argsParser.baseArgs['debug']) except Exception as ex: self.out(str(ex)); return if cmd == 'deletedatacenter': # check if datacenter is empty first (as per PB API 1.2 specs, clients should check if dc is empty before deleting) apiResult = pb.argsparser.ArgsParser.operations['getDataCenter']['api'](api, argsParser.opArgs) if ('servers' in apiResult and len(apiResult['servers']) > 0) or ('storages' in apiResult and len(apiResult['storages']) > 0) or ('loadBalancers' in apiResult and len(apiResult['loadBalancers']) > 0): ans = raw_input('The data center is not empty! Do you want to continue? [y/N] ') if ans[0:1].lower() != 'y': self.out('Operation cancelled') return apiResult = pb.argsparser.ArgsParser.operations[requestedOp]['api'](api, argsParser.opArgs) try: pb.argsparser.ArgsParser.operations[requestedOp]['out'](formatter, apiResult) except Exception as ex: raise Exception('Internal error while printing response: ' + str(ex)) return self.waitOnce = argsParser.baseArgs["wait"] if ("wait" in argsParser.baseArgs and 'dcid' in argsParser.opArgs) else None while (self.wait and (self.default_dc is not None) and self.waitOnce is None) or (self.waitOnce is not None and self.waitOnce): try: if (self.waitOnce is not None) and self.waitOnce: try: if api.getDataCenterState(argsParser.opArgs['dcid']) == 'AVAILABLE': break except: # data center may no longer be available in case of deleteDataCenter command break else: if api.getDataCenterState(self.default_dc) == 'AVAILABLE': break except Exception as ex: self.out(str(ex)) break sys.stdout.write('.') sys.stdout.flush() time.sleep(1) self.waitOnce = None if (self.wait): self.out('\n') # reload datacenters list if cmd in ['createdatacenter', 'deletedatacenter']: try: self.parse('get-all-datacenters') except Exception as ex: raise Exception('Failed to get list of data centers: ' + str(ex))