def list(self, deployment_id): params = {'deployment_id': deployment_id} self.score.response = Http.get(self.score.url + '/executions', headers=self.score.get_headers(), params=params, verify=self.score.verify, logger=self.logger) if self.score.response.status_code == requests.codes.ok: return json.loads(self.score.response.content) else: Log.error(self.logger, 'list executions returned %s' % self.score.response.status_code)
def block_until_completed(self, task): progress = task.get_Progress() status = task.get_status() rnd = 0 while status != "success": if status == "error": error = task.get_Error() Log.error(self.logger, "task error, major=%s, minor=%s, message=%s" % (error.get_majorErrorCode(), error.get_minorErrorCode(), error.get_message())) return False else: # some task doesn't not report progress if progress: pass else: rnd += 1 time.sleep(1) self.response = Http.get(task.get_href(), headers=self.vcloud_session.get_vcloud_headers(), verify=self.verify, logger=self.logger) if self.response.status_code == requests.codes.ok: task = taskType.parseString(self.response.content, True) progress = task.get_Progress() status = task.get_status() else: Log.error(self.logger, "can't get task") return False return True
def display_progress(self, task, cmd_proc=None, headers=None): progress = task.get_Progress() status = task.get_status() rnd = 0 response = None while status != "success": if status == "error": error = task.get_Error() sys.stdout.write('\r' + ' ' * 120 + '\r') sys.stdout.flush() self.print_error(CommonUtils.convertPythonObjToStr( error, name="Error"), cmd_proc=cmd_proc) return None else: # some task doesn't not report progress if progress: sys.stdout.write("\rprogress : [" + "*" * int(progress) + " " * (100 - int(progress - 1)) + "] " + str(progress) + " %") else: sys.stdout.write("\rprogress : ") if rnd % 4 == 0: sys.stdout.write( "[" + "*" * 25 + " " * 75 + "]") elif rnd % 4 == 1: sys.stdout.write( "[" + " " * 25 + "*" * 25 + " " * 50 + "]") elif rnd % 4 == 2: sys.stdout.write( "[" + " " * 50 + "*" * 25 + " " * 25 + "]") elif rnd % 4 == 3: sys.stdout.write( "[" + " " * 75 + "*" * 25 + "]") rnd += 1 sys.stdout.flush() time.sleep(1) response = Http.get(task.get_href(), headers=headers, verify=cmd_proc.verify, logger=cmd_proc.logger) if response.status_code == requests.codes.ok: task = parseString(response.content, True) progress = task.get_Progress() status = task.get_status() else: Log.error(cmd_proc.logger, "can't get task") return sys.stdout.write("\r" + " " * 120) sys.stdout.flush() if response is not None: if cmd_proc is not None and cmd_proc.json_output: sys.stdout.write("\r" + self.task_to_json(response.content) + '\n') else: sys.stdout.write("\r" + self.task_to_table(response.content) + '\n') sys.stdout.flush()
def execute(self, operation, http, body=None, targetVM=None): """ Execute an operation against a VM as an Asychronous Task. :param operation: (str): The command to execute :param http: (str): The http operation. :param body: (str, optional): a body for the http request :param targetVM: (str, optional): The name of the VM that will be the target of the request. :return: (TaskType or Bool) a :class:`pyvcloud.schema.vcd.v1_5.schemas.admin.vCloudEntities.TaskType` object that can be used to monitor the request. \n Or False if the request failed, error and debug level messages are logged. """ vApp = targetVM if targetVM else self.me link = filter(lambda link: link.get_rel() == operation, vApp.get_Link()) if not link: Log.error(self.logger, "link not found; rel=%s" % operation) Log.debug( self.logger, "vApp href=%s, name=%s" % (vApp.get_href(), vApp.get_name())) return False else: if http == "post": headers = self.headers if body and body.startswith('<DeployVAppParams '): headers[ 'Content-type'] = 'application/vnd.vmware.vcloud.deployVAppParams+xml' elif body and body.startswith('<UndeployVAppParams '): headers[ 'Content-type'] = 'application/vnd.vmware.vcloud.undeployVAppParams+xml' elif body and body.startswith('<CreateSnapshotParams '): headers[ 'Content-type'] = 'application/vnd.vmware.vcloud.createSnapshotParams+xml' self.response = Http.post(link[0].get_href(), data=body, headers=headers, verify=self.verify, logger=self.logger) elif http == "put": self.response = Http.put(link[0].get_href(), data=body, headers=self.headers, verify=self.verify, logger=self.logger) else: self.response = Http.delete(link[0].get_href(), headers=self.headers, verify=self.verify, logger=self.logger) if self.response.status_code == requests.codes.accepted: return taskType.parseString(self.response.content, True) else: Log.debug( self.logger, "failed; response status=%d, content=%s" % (self.response.status_code, self.response.text)) return False
def customize_on_next_poweron(self): vm = self._get_vms()[0] link = filter(lambda link: link.get_rel() == "customizeAtNextPowerOn", vm.get_Link()) if link: self.response = Http.post(link[0].get_href(), data=None, headers=self.headers, logger=self.logger) if self.response.status_code == requests.codes.no_content: return True Log.error(self.logger, "link not found") return False
def list(self, deployment_id): params = {'deployment_id': deployment_id} self.score.response = Http.get(self.score.url + '/executions', headers=self.score.get_headers(), params=params, verify=self.score.verify, logger=self.logger) if self.score.response.status_code != requests.codes.ok: Log.error( self.logger, 'list executions returned %s' % self.score.response.status_code) raise exceptions.from_response(self.score.response) return json.loads(self.score.response.content)
def re_login(self): if self.vca is None or \ (self.vca.token is None and self.password is None): return False result = False try: Log.debug( self.logger, 'about to re-login with ' + 'host=%s type=%s token=%s org=%s' % (self.vca.host, self.vca.service_type, self.vca.token, self.vca.org)) org_url = None if self.vca.vcloud_session is None else \ self.vca.vcloud_session.org_url result = self.vca.login(token=self.vca.token, org=self.vca.org, org_url=org_url) if result: Log.debug(self.logger, 'vca.login with token successful') self.re_login_vcloud_session() else: Log.debug( self.logger, 'vca.login with token failed %s' % self.vca.response.content) raise Exception('login with token failed') except Exception as e: Log.error(self.logger, str(e)) tb = traceback.format_exc() Log.error(self.logger, tb) if self.password is not None and len(self.password) > 0: try: Log.debug(self.logger, 'about to re-login with password') result = self.vca.login(password=self.password, org=self.vca.org) if result: Log.debug(self.logger, 'about to re-login vcloud_session') self.re_login_vcloud_session() Log.debug(self.logger, 'after re-login vcloud_session') self.save_config(self.profile, self.profile_file) except Exception: return False return result
def re_login(self): if self.vca is None or \ (self.vca.token is None and self.password is None): return False result = False try: Log.debug(self.logger, 'about to re-login with ' + 'host=%s type=%s token=%s org=%s' % (self.vca.host, self.vca.service_type, self.vca.token, self.vca.org)) org_url = None if self.vca.vcloud_session is None else \ self.vca.vcloud_session.org_url result = self.vca.login(token=self.vca.token, org=self.vca.org, org_url=org_url) if result: Log.debug(self.logger, 'vca.login with token successful') self.re_login_vcloud_session() else: Log.debug(self.logger, 'vca.login with token failed %s' % self.vca.response.content) raise Exception('login with token failed') except Exception as e: Log.error(self.logger, str(e)) tb = traceback.format_exc() Log.error(self.logger, tb) if self.password is not None and len(self.password) > 0: try: Log.debug(self.logger, 'about to re-login with password') result = self.vca.login(password=self.password, org=self.vca.org) if result: Log.debug(self.logger, 'about to re-login vcloud_session') self.re_login_vcloud_session() Log.debug(self.logger, 'after re-login vcloud_session') self.save_config(self.profile, self.profile_file) except Exception: return False return result
def customize_on_next_poweron(self): """ Force the guest OS customization script to be run for the first VM in the vApp. A customization script must have been previously associated with the VM using the pyvcloud customize_guest_os method or using the vCD console The VMware tools must be installed in the Guest OS. :return: (bool) True if the request was accepted, False otherwise. If False an error level log message is generated. """ vm = self._get_vms()[0] link = filter(lambda link: link.get_rel() == "customizeAtNextPowerOn", vm.get_Link()) if link: self.response = Http.post(link[0].get_href(), data=None, headers=self.headers, logger=self.logger) if self.response.status_code == requests.codes.no_content: return True Log.error(self.logger, "link not found") return False
def execute(self, operation, http, body=None, targetVM=None): """ Execute an operation against a VM as an Asychronous Task. :param operation: (str): The command to execute :param http: (str): The http operation. :param body: (str, optional): a body for the http request :param targetVM: (str, optional): The name of the VM that will be the target of the request. :return: (TaskType or Bool) a :class:`pyvcloud.schema.vcd.v1_5.schemas.admin.vCloudEntities.TaskType` object that can be used to monitor the request. \n Or False if the request failed, error and debug level messages are logged. """ vApp = targetVM if targetVM else self.me link = filter(lambda link: link.get_rel() == operation, vApp.get_Link()) if not link: Log.error(self.logger, "link not found; rel=%s" % operation) Log.debug(self.logger, "vApp href=%s, name=%s" % (vApp.get_href(), vApp.get_name())) return False else: if http == "post": headers = self.headers if body and body.startswith('<DeployVAppParams '): headers['Content-type'] = 'application/vnd.vmware.vcloud.deployVAppParams+xml' elif body and body.startswith('<UndeployVAppParams '): headers['Content-type'] = 'application/vnd.vmware.vcloud.undeployVAppParams+xml' elif body and body.startswith('<CreateSnapshotParams '): headers['Content-type'] = 'application/vnd.vmware.vcloud.createSnapshotParams+xml' self.response = Http.post(link[0].get_href(), data=body, headers=headers, verify=self.verify, logger=self.logger) elif http == "put": self.response = Http.put(link[0].get_href(), data=body, headers=self.headers, verify=self.verify, logger=self.logger) else: self.response = Http.delete(link[0].get_href(), headers=self.headers, verify=self.verify, logger=self.logger) if self.response.status_code == requests.codes.accepted: return taskType.parseString(self.response.content, True) else: Log.debug(self.logger, "failed; response status=%d, content=%s" % (self.response.status_code, self.response.text)) return False
def execute(self, operation, http, body=None, targetVM=None): vApp = targetVM if targetVM else self.me link = filter(lambda link: link.get_rel() == operation, vApp.get_Link()) if not link: Log.error(self.logger, "link not found; rel=%s" % operation) Log.debug(self.logger, "vApp href=%s, name=%s" % (vApp.get_href(), vApp.get_name())) return False else: if http == "post": headers = self.headers if body and body.startswith('<DeployVAppParams '): headers['Content-type'] = 'application/vnd.vmware.vcloud.deployVAppParams+xml' elif body and body.startswith('<UndeployVAppParams '): headers['Content-type'] = 'application/vnd.vmware.vcloud.undeployVAppParams+xml' self.response = Http.post(link[0].get_href(), data = body, headers=headers, verify=self.verify, logger=self.logger) elif http == "put": self.response = Http.put(link[0].get_href(), data = body, headers=self.headers, verify=self.verify, logger=self.logger) else: self.response = Http.delete(link[0].get_href(), headers=self.headers, verify=self.verify, logger=self.logger) if self.response.status_code == requests.codes.accepted: return taskType.parseString(self.response.content, True) else: Log.debug(self.logger, "failed; response status=%d, content=%s" % (self.response.status_code, response.text)) return False