def wait_for(template_id_or_desc, number=1): if "defined" in template_id_or_desc: command = """ echo -e "set head off;\\n set linesize 2000; \\n select 'result'||count(*) from task_info where status in (1,2) and template_id='{}';"| sqlplus -s omc/omc|grep result """.format(template_id_or_desc) else: command = """ echo -e "set head off;\\n select 'result'||count(*) from task_info INNER JOIN plan_info ON task_info.plan_id = plan_info.plan_id where task_info.status in (1,2) and plan_info.DESCRIPTION like '{}';"| sqlplus -s omc/omc """.format(template_id_or_desc) while 1: stdout, stderr = execute_command(command) response = stdout.replace('result', '') Debug.info('waitFor {}: {} < {}'.format(template_id_or_desc, response, number)) if response.isdigit(): num = int(response) if num >= number: Debug.info("Wait for {} 10s...".format(template_id_or_desc)) time.sleep(10) else: break else: Debug.error( "Wait for function failed: response is not digital: {}".format( response)) time.sleep(2)
def issue_token(self, user, cache_file, password=None): if not password: password = self._get_password(user) command = """ curl -v -k -X "POST" "https://{fqdn}:9527/restda/oauth2/token" -H "Content-Type:application/x-www-form-urlencoded" -d "grant_type=client_credentials" -u '{user_name}:{password}' """.format(fqdn=self.fqdn, user_name=user, password=password) stdout, stderr = execute_command(command) try: response = eval(stdout) Debug.info(response) except Exception as err: Debug.error("Command: {}, stdout: {}, stderr: {}".format( command, stdout, stderr)) Debug.error("Exception occur: {}".format(err)) raise Exception( "format token failed, error: {}: request: {},response: {}". format(err, command, stdout)) if "token_key" in response: token = response["token_key"] with open(cache_file, "ab") as f: f.write("{}\n".format( json.dumps( collections.OrderedDict( Token=token, TimeStamp=int(time.time())))).encode('utf-8')) return token elif "errorMessage" in response: raise Exception(response["errorMessage"]) else: raise Exception("token response unexpected: {}".format(stdout))
def upload_exception(self, error): send_json = [{"workerId": self.workerId, "status": CRASH, "errorReason": error}] try: self.output_queue.put(send_json) except Exception as err: # the QueueServer already exit. Debug.error("Worker upload exception failed: {}. {}".format(err, traceback.format_exc()))
def upload_status(self, status): send_json = [{"workerId": self.workerId, "status": status}] try: self.output_queue.put(send_json) except Exception as err: # the QueueServer already exit. Debug.error("Worker upload status failed: {}. {}".format(err, traceback.format_exc()))
def upload_task_status(self, error=None): status = {"workerId": self.workerId, "PassTask": self.PassTask, "FailTask": self.FailTask} if error: status.update({"Exception": error}) send_json = [status] try: self.output_queue.put(send_json) except Exception as err: # the QueueServer already exit. Debug.error("Worker upload task status failed: {}. {}".format(err, traceback.format_exc()))
def constant(filename): root_dir = dirname(dirname(abspath(__file__))) template_dir = join(root_dir, 'templates') template_path = join(template_dir, filename) if isfile(template_path): with open(template_path, 'rb') as f: content = f.read().decode('utf-8') return content else: error_message = 'Template file: {} is not exist.'.format( template_path) Debug.error(error_message) raise Exception(error_message)
def template(filename): if not filename.endswith(".json"): filename = "{}.json".format(filename) root_dir = dirname(dirname(abspath(__file__))) template_dir = join(root_dir, 'templates') template_path = join(template_dir, filename) if isfile(template_path): with open(template_path, 'rb') as f: content = f.read().decode('utf-8') return Template(content) else: error_message = 'Template file: {} is not exist.'.format( template_path) Debug.error(error_message) raise Exception(error_message)
def create_plan(self, token, json): command = """ curl -v -k -H "Authorization:Bearer {token}" -X "POST" "https://{fqdn}:9527/restda/api/v1/plans" -H "Content-Type:application/json" --data '{json}' """.format(token=token, fqdn=self.fqdn, json=json) stdout, stderr = execute_command(command) response = eval(stdout) if "status" in response: Debug.info("create plan request: {} response: {}".format( command, response)) assert response[ "status"] == "success", "Create A plan failed , because Response is {}".format( stdout) elif "errorMessage" in response: Debug.error("create plan request: {} response: {}".format( command, response)) raise Exception(response["errorMessage"]) else: Debug.error("create plan request: {} response: {}".format( command, response)) raise Exception(stdout)
def read_token(self, cache_file): start_time = time.time() while 1: if time.time() - start_time > self.read_token_timeout: raise Exception("read token timeout in {}".format( self.read_token_timeout)) if exists(cache_file): with open(cache_file, "rb") as f: lines = f.readlines() lines = list(map(lambda line: line.decode('utf-8'), lines)) if lines: last_line = lines[-1] try: token_map = eval(last_line) token = token_map["Token"] time_stamp = token_map["TimeStamp"] if time.time() - time_stamp < self.token_expiration: return token except Exception as err: Debug.error("Read Token failed, err: {}. {}".format( err, traceback.format_exc())) time.sleep(10) Debug.info('read token failed, next in 10 s')