示例#1
0
	def default_dc_state(self, api):
		return api.getDataCenterState(self.default_dc);
示例#2
0
	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))