def _translate_pkg_to_id(self, pkgtype, pkg): uai_logger.info("Start download {0} package info".format(pkgtype)) api_op = GetUAITrainEnvPkgAPIOp(self.pub_key, self.pri_key, pkgtype, self.project_id, self.region, self.zone) succ, result = api_op.call_api() if succ is False: raise RuntimeError("Error get {0} info from server".format(pkgtype)) for avpkg in result['PkgSet']: if pkgtype == 'OS' or pkgtype == 'Python' or pkgtype == 'AIFrame': versionsplit = pkg.rfind('-') if versionsplit > 0: if avpkg["PkgName"] == pkg[:versionsplit] and ( avpkg["PkgVersion"] == "" or avpkg["PkgVersion"] == pkg[versionsplit + 1:]): return avpkg["PkgId"] elif versionsplit < 0: if avpkg["PkgName"] == pkg: return avpkg["PkgId"] else: if avpkg["PkgName"] == pkg: return avpkg["PkgId"] uai_logger.error("Some {0} package is not supported: {1}".format(pkgtype, pkg)) raise RuntimeError("Some {0} package is not supported: {1}".format(pkgtype, pkg)) return None
def _delete_request(self): private_key = self.conf_params['private_key'] task_id = self.delete_params['TaskId'] self.delete_params['Signature'] = _verfy_ac(private_key, self.delete_params) uai_logger.debug('==' * 10) uai_logger.info( "Start to make delete task {0} request".format(task_id)) r = requests.get(self.delete_url, params=self.delete_params) if r.status_code != requests.codes.ok: uai_logger.debug('================================') uai_logger.warning('Delete Error, PLS check your connection') sys.exit(0) json_data = json.loads(r.text) r_info = {} for k, v in json_data.items(): if isinstance(k, unicode): k = k.encode('utf-8') if isinstance(v, unicode): v = v.encode('utf-8') r_info[k] = v if check_retcode(r_info) != NORMAL_CONDITION: uai_logger.error('Delete Error: {0}'.format(r_info['Message'])) sys.exit(0) else: uai_logger.info('Delete task id {0} successful'.format(task_id))
def cmd_run(self, params): super(UaiServiceCheckBaseImgExistOp, self).cmd_run(params) envOp = GetUAIAvailableEnvPkgOp(public_key=self.public_key, private_key=self.private_key, project_id=self.project_id, region=self.region, zone=self.zone) succ, self.rsp = envOp.call_api() if self.rsp["RetCode"] != 0: uai_logger.error("Fail: [checkBase][getEnv] {0}".format(self.rsp)) raise RuntimeError("Fail: [checkBase][getEnv] {0}".format( self.rsp)) self.os_version = self._translate_pkg_to_id( 'os', self.params['os'].split(','))[0] self.python_version = self._translate_pkg_to_id( 'python_v', self.params['python_v'].split(','))[0] self.ai_frame_version = self._translate_pkg_to_id( 'ai_arch_v', self.params['ai_arch_v'].split(','))[0] checkOp = CheckUAIBaseImgExistOp( public_key=self.public_key, private_key=self.private_key, project_id=self.project_id, region=self.region, zone=self.zone, os_version=self.os_version, python_version=self.python_version, ai_frame_version=self.ai_frame_version) succ, rsp = checkOp.call_api() return succ, rsp
def _translate_pkg_to_id(self, pkgtype, pkglist): resultlist = [] uai_logger.info( "Start translate {0} package to their id, packages: {1}".format( pkgtype, pkglist)) for avpkg in self.rsp['PkgSet']: for pkg in pkglist: if pkgtype == 'os' or pkgtype == 'python_v' or pkgtype == 'ai_arch_v': versionsplit = pkg.rfind('-') if versionsplit >= 0: if avpkg["PkgName"] == pkg[:versionsplit] and ( avpkg["PkgVersion"] == "" or avpkg["PkgVersion"] == pkg[versionsplit + 1:]): pkglist.remove(pkg) resultlist.append(avpkg["PkgId"]) elif versionsplit < 0: if avpkg["PkgName"] == pkg: pkglist.remove(pkg) resultlist.append(avpkg["PkgId"]) else: if avpkg["PkgName"] == pkg: pkglist.remove(pkg) resultlist.append(avpkg["PkgId"]) if len(pkglist) != 0: uai_logger.error("Some {0} package is not supported: {1}".format( pkgtype, pkglist)) raise RuntimeError("Some {0} package is not supported: {1}".format( pkgtype, pkglist)) uai_logger.info( "End translate {0} package to their id, result: {1}".format( pkgtype, resultlist)) return resultlist
def cmd_run(self, args): if self._parse_args(args) == False: return False bill_op = GetUAITrainBillInfoOp(pub_key=self.pub_key, priv_key=self.pri_key, beg_time=self.begin_time, end_time=self.end_time, offset=self.offset, limit=self.limit, project_id=self.project_id, region=self.region, zone=self.zone) succ, resp = bill_op.call_api() if succ is False: uai_logger.error("Error call list bill info") return False print('Total job num: {0}, Total exec time: {1} s, Total price: {2}'. format(resp['TotalCount'], resp['TotalExecuteTime'], float(resp['TotalPrice']) / 100)) result = resp['DataSet'] for job_bill in result: self._format_billinfo(job_bill) return True
def _check_request(self): max_poll_steps = MAX_POLL_STEPS uai_logger.debug('=' * 10 + 'Start poll the state of deploy' + '=' * 10) uai_logger.debug('the url to check the state of deploy: {0}'.format( self.check_url)) self.check_params['Signature'] = _verfy_ac( self.request_params['PrivateKey'], self.check_params) for step in range(max_poll_steps): uai_logger.debug( 'the step {0} to poll the state of the deploy'.format(step)) r = requests.get(self.check_url, params=self.check_params) uai_logger.debug('the result of step {0}'.format(step)) if r.status_code != requests.codes.ok: uai_logger.warning('================================') uai_logger.warning('Check Error, PLS check your connection') break json_data = json.loads(r.text) r_info = {} for k, v in json_data.items(): if isinstance(k, unicode): k = k.encode('utf-8') if isinstance(v, unicode): v = v.encode('utf-8') r_info[k] = v if check_retcode(r_info) != NORMAL_CONDITION: uai_logger.warning('Deploy Error: {0}'.format( r_info['Message'])) break else: if r_info['Status'] == 'SUCCESS': uai_logger.info( 'Building Success, the domain of your deploy application is {0}' .format(r_info['Domain'])) self.id_file.write('URL: {0}\n'.format(r_info['Domain'])) break elif r_info['Status'] == 'BUILDING': uai_logger.info('Building Image Now') time.sleep(5) continue elif r_info['Status'] == 'PUSHIMAGE': uai_logger.info('Pushing Image Now') time.sleep(5) continue elif r_info['Status'] == 'PAASDEPLOY': uai_logger.info('Creating App On PAAS Now') time.sleep(5) continue elif r_info['Status'] == 'FAILED': uai_logger.error('Error Message: {0} '.format( r_info['Message'])) break
def _format_request_param(self): self._format_account_param() if self.conf_params['commands'] == 'checkbase': self._format_checkbase_param() self.cmd_url = UCLOUD_API_URL if self.conf_params['commands'] == 'create': self._format_create_param() self.cmd_url = UCLOUD_API_URL else: uai_logger.error("Command is not valid: {0} ".format( self.conf_params['commands'])) raise RuntimeError("Command is not valid: {0} ".format( self.conf_params['commands']))
def check_retcode(ret_info): ret_code, ret_message = get_info(ret_info) if ret_code == 0: return NORMAL_CONDITION elif ret_code in range(-1, 2000): uai_logger.warning("Something wrong with API Gateway.") try: common[ret_code](ret_message) except KeyError: uai_logger.error("The RetCode is not included in COMMON RetCode.") except Exception, e: uai_logger.exception(e) if "Time Out" in str(e): return RETRY_CONDITION else: return FATAL_CONDITION
def _cmd_common_request(self): if 'Signature' in self.cmd_params: self.cmd_params.pop('Signature') self.cmd_params['Signature'] = _verfy_ac(self.private_key, self.cmd_params) uai_logger.info("Call http request: {0} ".format(get_request(self.cmd_url, params=self.cmd_params))) r = requests.get(self.cmd_url, params=self.cmd_params) print(r.text) self.rsp = json.loads(r.text, encoding="utf-8") if self.rsp["RetCode"] != 0: uai_logger.error("{0} Fail: [{1}]{2}".format(self.cmd_params["Action"], self.rsp["RetCode"], self.rsp["Message"].encode('utf-8'))) return False else: del self.rsp['Action'] uai_logger.info("{0} Success: {1}".format(self.cmd_params["Action"], get_response(self.rsp, 0))) return True
def _format_request_param(self): self._format_account_param() if self.conf_params['commands'] == 'create': self._format_create_param() self.cmd_url = UCLOUD_API_URL elif self.conf_params['commands'] == 'listservice': self._format_listservice_param() self.cmd_url = UCLOUD_API_URL print(self.cmd_params) elif self.conf_params['commands'] == 'listversion': self._format_listversion_param() self.cmd_url = UCLOUD_API_URL elif self.conf_params['commands'] == 'delete': self._format_delete_param() self.cmd_url = UCLOUD_API_URL elif self.conf_params['commands'] == 'stop': self._format_stop_param() self.cmd_url = UCLOUD_API_URL elif self.conf_params['commands'] == 'start': self._format_start_param() self.cmd_url = UCLOUD_API_URL elif self.conf_params['commands'] == 'modifyname': self._format_modifyname_param() self.cmd_url = UCLOUD_API_URL elif self.conf_params['commands'] == 'modifyweight': self._format_modifyweight_param() self.cmd_url = UCLOUD_API_URL elif self.conf_params['commands'] == 'availableenv': self._format_availableenv_param() self.cmd_url = UCLOUD_API_URL elif self.conf_params['commands'] == 'checkbase': self._format_checkbase_param() self.cmd_url = UCLOUD_API_URL # elif self.conf_params['commands'] == 'translate': # self._format_translate_param() elif self.conf_params['commands'] == 'deploy': self._format_deploy_param() self.cmd_url = UCLOUD_API_URL elif self.conf_params['commands'] == 'checkprogress': self._format_checkprogress_param() self.cmd_url = UCLOUD_API_URL else: uai_logger.error("Command is not valid: {0} ".format( self.conf_params['commands'])) raise RuntimeError("Command is not valid: {0} ".format( self.conf_params['commands']))
def _translate_pkg_to_id(self, pkgtype, pkglist): if not os.path.exists(pkgtype): raise RuntimeError( "{0} file doesn't found, please download from github " "and put it under the same directory as deploy tool".format( pkgtype)) # uai_logger.info("Start download {0} package info".format(pkgtype)) # self.conf_params['pkg_type'] = pkgtype # self._format_availableenv_param() # self.cmd_url = UCLOUD_API_URL # self._cmd_writefile_package(pkgtype) resultlist = [] uai_logger.info( "Start translate {0} package to their id, packages: {1}".format( pkgtype, pkglist)) for avpkg in json.load(open(pkgtype), 'utf-8'): for pkg in pkglist: if pkgtype == 'os' or pkgtype == 'language' or pkgtype == 'ai_arch': versionsplit = pkg.rfind('-') if versionsplit >= 0: if avpkg["PkgName"] == pkg[:versionsplit] and ( avpkg["PkgVersion"] == "" or avpkg["PkgVersion"] == pkg[versionsplit + 1:]): pkglist.remove(pkg) resultlist.append(avpkg["PkgId"]) elif versionsplit < 0: if avpkg["PkgName"] == pkg: pkglist.remove(pkg) resultlist.append(avpkg["PkgId"]) else: if avpkg["PkgName"] == pkg: pkglist.remove(pkg) resultlist.append(avpkg["PkgId"]) if len(pkglist) != 0: uai_logger.error("Some {0} package is not supported: {1}".format( pkgtype, pkglist)) raise RuntimeError("Some {0} package is not supported: {1}".format( pkgtype, pkglist)) uai_logger.info( "End translate {0} package to their id, result: {1}".format( pkgtype, resultlist)) return resultlist
def _cmd_writefile_package(self, filepath): if ('Signature' in self.cmd_params) is True: self.cmd_params.pop('Signature') self.cmd_params['Signature'] = _verfy_ac( self.conf_params['private_key'], self.cmd_params) uai_logger.info("Call http request: {0} ".format( get_request(self.cmd_url, params=self.cmd_params))) r = requests.get(self.cmd_url, params=self.cmd_params) rsp = json.loads(r.text, encoding='utf-8') if rsp["RetCode"] != 0: uai_logger.error("{0} Fail: [{1}]{2}".format( self.cmd_params["Action"], rsp["RetCode"], rsp["Message"].encode('utf-8'))) raise RuntimeError("{0} Fail: [{1}]{2}".format( self.cmd_params["Action"], rsp["RetCode"], rsp["Message"].encode('utf-8'))) else: with open(filepath, 'w') as f: json.dump(rsp["PkgSet"], f)
def _cmd_common_request(self): if ('Signature' in self.cmd_params) is True: self.cmd_params.pop('Signature') self.cmd_params['Signature'] = _verfy_ac(self.priv_key, self.cmd_params) uai_logger.info("Signature: {0}".format(self.cmd_params['Signature'])) uai_logger.info(self.cmd_params) uai_logger.info("Call http request: {0} ".format( get_request(self.cmd_url, params=self.cmd_params))) r = requests.get(self.cmd_url, params=self.cmd_params) rsp = json.loads(r.text, encoding='utf-8') if rsp['RetCode'] != 0: uai_logger.error("{0} Fail: [{1}]{2}".format( self.cmd_params[PARAM_ACTION], rsp['RetCode'], rsp['Message'].encode('utf-8'))) return False, rsp else: del rsp[PARAM_ACTION] #uai_logger.info("{0} Success: {1}".format(self.cmd_params[PARAM_ACTION], get_response(rsp, 0))) return True, rsp
def cmd_run(self, args): if self._parse_args(args) == False: return False create_op = GetUAITrainJobListOp(pub_key=self.pub_key, priv_key=self.pri_key, job_id=self.job_id, offset=self.offset, limit=self.limit, project_id=self.project_id, region=self.region, zone=self.zone) succ, resp = create_op.call_api() if succ is False: uai_logger.error("Error call list train jobs") return False result = resp['DataSet'] for job in result: self._format_jobinfo(job)
def cmd_run(self, args): if self._parse_args(args) == False: return False create_op = GetUAITrainJobListOp( pub_key=self.pub_key, priv_key=self.pri_key, job_id=self.job_id, project_id=self.project_id, region=self.region, zone=self.zone) succ, resp = create_op.call_api() if succ is False: uai_logger.error("Error call list train jobs") return False if resp['TotalCount'] == 0: uai_logger.error("can't get the config of this job id: {0}, please check your job id".format(self.job_id)) return False job = resp['DataSet'][0] self._format_jobinfo(job) return True
def _check_job_running(self): job_op = GetUAITrainJobListApiOp(pub_key=self.pub_key, priv_key=self.pri_key, job_id=self.job_id, project_id=self.project_id, region=self.region, zone=self.zone) succ, resp = job_op.call_api() if succ is False: uai_logger.error("Error call list train jobs") return False if resp['TotalCount'] == 0: uai_logger.error( "can't get the config of this job id: {0}, please check your job id" .format(self.job_id)) return False status = resp['DataSet'][0]['Status'] if status == 'Doing': return True else: return False
def _check_args(self, params): print(params) if params["ResourceId"] == "" or type(params["ResourceId"]) != str: uai_logger.error("resource_id shoud be <str> and is not nil.") return False if type(params["BeginTime"]) != int: uai_logger.error("beg_time shoud be <int>.") return False if type(params["EndTime"]) != int: uai_logger.error("end_time shoud be <int>.") return False if params["BeginTime"] > params["EndTime"]: uai_logger.error( "end_time should be greater than beg_time. end_time: {0}, beg_time: {1}" .format(params["EndTime"], params["BeginTime"])) return False return True
def _check_args(self, params): if params["ServiceID"] == "" or type(params["ServiceID"]) != str: uai_logger.error("ServiceID should be <str> and is not nil.") return False if params["MetricID"] == "" or type(params["MetricID"]) != int: uai_logger.error("MetricID should be <int> and is not nil.") return False if params["MaxNode"] == "" or type(params["MaxNode"]) != int: uai_logger.error("MaxNode should be <int> and is not nil.") return False if params["MinNode"] == "" or type(params["MinNode"]) != int: uai_logger.error("MinNode should be <int> and is not nil.") return False if params["UpThreshold"] == "": uai_logger.error("UpThreshold should be <float> and is not nil.") return False if params["DownThreshold"] == "": uai_logger.error("DownThreshold should be <float> and is not nil.") return False if params["EnableStandby"] == "" or type( params["EnableStandby"]) != bool: uai_logger.error("EnableStandby should be <bool> and is not nil.") return False params['EnableStandby'] = 'true' if params[ "EnableStandby"] is True else 'false' return True
return ret_code, ret_message def check_retcode(ret_info): ret_code, ret_message = get_info(ret_info) if ret_code == 0: return NORMAL_CONDITION elif ret_code in range(-1, 2000): uai_logger.warning("Something wrong with API Gateway.") try: common[ret_code](ret_message) except KeyError: uai_logger.error("The RetCode is not included in COMMON RetCode.") except Exception, e: uai_logger.exception(e) if "Time Out" in str(e): return RETRY_CONDITION else: return FATAL_CONDITION else: uai_logger.error("Unknown Error Region.") return FATAL_CONDITION def assert_check(ret_info): ret_code, ret_message = get_info(ret_info) if ret_code == 0: uai_logger.debug("Pass API RetCode Check") else: common[ret_code](ret_message)
def _check_args(self, params): if params["ServiceID"] == "" or type(params["ServiceID"]) != str: uai_logger.error("ServiceID should be <str> and is not nil.") return False return True