def log_results(self, start_time): DpopRoom.log_results(self, start_time) log.info("rooms affected :" + str(self.monitored_area.get_room_who_need_intervention()), self.monitored_area.id, c.RESULTS)
def set_container_logs_and_errors(self, containers, results, job): for container in containers: image_name = container.attrs["Config"]["Image"] container_id = \ f'{image_name}_{container.short_id}' run_logs = container.logs(timestamps=True).decode() results.json_results_from_logs = self.get_json_out(run_logs) log.log('CONTAINER', f'{container_id} logs begin \n' + ('-' * 80)) log.log('CONTAINER', run_logs) log.log('CONTAINER', f'{container_id} logs end \n' + ('-' * 80)) log_url = self.upload_logs( run_logs, filename=f'{image_name}_job-{job.id}.txt') exit_code = container.attrs['State']['ExitCode'] if exit_code != 0: results.errors[container_id] = f'Container failed with' \ f' exit code {exit_code}' log.error(f'Container {container_id} failed with {exit_code}' f' for job {box2json(job)}, logs: {log_url}') elif container.status == 'dead': results.errors[container_id] = f'Container died, please retry.' log.error(f'Container {container_id} died' f' for job {box2json(job)}, logs: {log_url}') log.info(f'Uploaded logs for {container_id} to {log_url}') results.logs[container_id] = log_url
def do_one_iteration(self): log.info("Start", c.SERVER, c.INFO) results = "" self.is_running = True for agent in self.agents: self.mqtt_manager.publish_on_msg_to(agent.id) root = self.choose_root() for agent in self.agents: self.mqtt_manager.publish_elected_root_msg_to(agent.id, root) received_index = self.get_values() sorted_priorities = self.get_result_by_priority(received_index) for agent_id, priority in sorted_priorities: results += "Room " + str(agent_id) + \ " need intervention in " + str(c.DIMENSION[received_index[agent_id]]) + \ " minutes. PRIORITY : " + str(priority) + " " self.old_results_index[agent_id] = received_index[agent_id] log.info(results, c.SERVER, c.RESULTS) self.is_running = False
def __init__(self, is_on_gcp=False): self.is_on_gcp = is_on_gcp self.last_update_check_time = None if not self.is_on_gcp: log.info( 'Not pulling latest on non-gcp machines, assuming you are ' 'in dev')
def assign_jobs(self) -> Tuple[BoxList, List]: new_jobs = BoxList() exceptions = [] for job in self.jobs_db.where('status', '==', JOB_STATUS_CREATED): try: if self.should_start_job(job): log.info(f'Assigning job ' f'{box2json(job)}...') self.assign_job(job) new_jobs.append(job) except Exception as e: # Could have been a network failure, so just try again. # More granular exceptions should be handled before this # which can set the job to not run again # if that's what's called for. log.exception(f'Exception triggering eval for job {job}, ' f'will try again shortly.') exceptions.append(e) # TODO: Check for failed / crashed instance once per minute # TODO: Stop instances if they have been idle for longer than timeout # TODO: Cap total instances # TODO: Cap instances per bot owner, using first part of docker tag # TODO: Delete instances over threshold of stopped+started return new_jobs, exceptions
def do_value_propagation(self, join_matrix, util_matrix, matrix_dimensions_order): log.info("Value Start", self.dfs_structure.monitored_area.id, c.INFO) values = dict() if util_matrix is None: util_matrix = numpy.zeros(c.DIMENSION_SIZE, int) if not self.dfs_structure.is_root: self.mqtt_manager.publish_util_msg_to( self.dfs_structure.parent_id, json.dumps({ c.VARS: matrix_dimensions_order, c.DATA: util_matrix.tolist() })) values = self.get_values_from_parents() # Find best v index = self.get_index_of_best_value_with(values, matrix_dimensions_order, join_matrix) self.dfs_structure.monitored_area.current_v = c.DIMENSION[index] values[str(self.dfs_structure.monitored_area.id)] = index for child in self.dfs_structure.children_id: self.mqtt_manager.publish_value_msg_to(child, json.dumps(values)) if self.dfs_structure.is_leaf(): self.mqtt_manager.publish_value_msg_to_server(json.dumps(values))
def confirm_evaluation(self, job) -> bool: if in_test(): status = JOB_STATUS_CREATED ret = True elif dbox(job).confirmed: log.info(f'Job already confirmed ' f'{box2json(job)}') status = JOB_STATUS_CREATED ret = True else: url = f'{job.botleague_liaison_host}/confirm' json = {'eval_key': job.eval_spec.eval_key} log.info(f'Confirming eval {json} at {url}...') confirmation = requests.post(url, json=json) if 400 <= confirmation.status_code < 500: status = JOB_STATUS_DENIED_CONFIRMATION log.error('Botleague denied confirmation of job, skipping') ret = False elif not confirmation.ok: status = JOB_STATUS_CREATED log.error('Unable to confirm job with botleague liaison, ' 'will try again shortly') ret = False else: status = JOB_STATUS_CREATED log.success(f'Confirmed eval job ' f'{box2json(job)} at {url}') ret = True job.status = status job.confirmed = ret self.save_job(job) return ret
def reduce(): result = dbox(problem_ci) # Refetch all bots in case scores came in after initial request for bot_eval_key in problem_ci.bot_eval_keys: bot_eval = db.get(get_eval_db_key(bot_eval_key)) past_bot_scores = get_past_bot_scores(bot_eval) bot_eval_no_eval_key = deepcopy(bot_eval) del bot_eval_no_eval_key['eval_key'] log.info(f'Checking confidence interval for bot_eval ' f'{box2json(bot_eval)}\n' f'past scores: {box2json(past_bot_scores)}') if bot_eval.results.errors: result.error = str(bot_eval.results.errors) log.error(result.error + ': bot details ' \ f'{box2json(bot_eval_no_eval_key)}') return result in_interval, interval_info = score_within_confidence_interval( bot_eval, past_bot_scores) if not in_interval: result.error = f'Score for bot {bot_eval.results.score}' \ f' not within confidence interval ' \ f'{interval_info.low} to {interval_info.high}, ' \ f'mean: {interval_info.mean} ' \ f'problem CI failed' log.error(result.error + ': bot details ' \ f'{box2json(bot_eval_no_eval_key)}') return result else: log.success('Score for bot within confidence interval, ' 'problem CI successful!') return result
def request_eval(endpoint: str, eval_data: Box) -> PrResponse: try: if 'REPLACE_PROBLEM_HOST' in os.environ: endpoint = os.environ['REPLACE_PROBLEM_HOST'] + \ endpoint[endpoint.find('/eval'):] # TODO: Don't pass everything through to endpoint - i.e. cleanse serializable_data = json.loads( eval_data.to_json(default=str, sort_keys=True)) endpoint_resp = requests.post(endpoint, json=serializable_data, timeout=10) except requests.exceptions.Timeout: ret = EvalErrorPrResponse('Endpoint %s took too long to respond' % endpoint) else: # Yay, we did not timeout! if endpoint_resp.status_code != 200: ret = EvalErrorPrResponse( 'Endpoint %s failed with HTTP %r, response body was %s' % (endpoint, endpoint_resp.status_code, endpoint_resp.content)) log.error(ret.msg) else: ret = EvalStartedPrResponse( 'Started evaluation at %s' % endpoint, eval_data) # Now wait for a /confirm and /results request with the eval_key log.info(f'Request eval resp: {ret.msg}') return ret
def run(self): while self.running: data, addr = self.listener.recvfrom(4096) http_pareser = HttpParser() http_pareser.execute(data, len(data)) headers = http_pareser.get_headers() try: if headers['NTS'] == 'ssdp:alive' and headers[ 'NT'] == 'urn:zenterio-net:service:X-CTC_RemotePairing:1': stb = STB(uuid=headers['USN'][5:41], location=headers['LOCATION'], nt=headers['NT']) self.mutex.acquire(1) for x in self.stbs: if x.uuid == stb.uuid: break else: self.stbs.append(stb) log.info('-------------------------------------------') log.info("New STB detected!") log.info("UUID: " + stb.uuid) log.info("Location: " + stb.location) log.info("NT: " + stb.nt) self.mutex.release() except: pass
def data_process(): file_list = [] file_dir = "/var/lib/platform-check/" cmd_date = "date '+%Y-%m-%d'" date = shell.shell_cmd(cmd_date)[0].split()[0] info = "*"+date+"*" ##################### # file_list ###################### cmd_search = 'find %s -name %s' %(file_dir,info) search_result = shell.shell_cmd(cmd_search) for x in xrange(len(search_result)): se = search_result[x].split()[0] file_list.append(se) ################### # json_list ######################## global json_list json_list = [] for p in file_list: fp = json.load(open(p)) json_list.append(fp) ######################### # platform_info_list ######################### platform_info_list = [] for j in json_list: for n in j.keys(): js = j[n] platform_info_list.append(js) #print platform_info_list logging.info(platform_info_list) return platform_info_list
def on_message(self, client, obj, msg): str_msg = str(msg.payload.decode('utf-8')) if MessageTypes.is_on(str_msg): log.info("Iteration " + str(self.nb_iterations), self.monitored_area.id, c.INFO) if self.nb_iterations > 0: self.monitored_area.previous_v = self.monitored_area.current_v self.monitored_area.increment_time( int((datetime.now() - self.start_time).total_seconds() / 60)) self.start_time = datetime.now() log.info(self.monitored_area.to_json_format(), self.monitored_area.id, c.STATE) self.initialize_metrics() self.nb_iterations += 1 self.dpop_launch(self.monitored_area) elif MessageTypes.CHILD.value in str_msg or MessageTypes.PSEUDO.value in str_msg: client.child_msgs.append(str_msg) elif MessageTypes.UTIL.value in str_msg: client.util_msgs.append(str_msg) elif MessageTypes.VALUES.value in str_msg: client.value_msgs.append(str_msg) else: client.list_msgs_waiting.append(str_msg) super().on_message(client, obj, msg)
def pull_latest(check_first=False, remote_branch='production'): ret = False repo = git.Repo(ROOT_DIR) if check_first: origin = [r for r in repo.remotes if r.name == 'origin'][0] # Fetch all origin changes origin.update() head = repo.head.commit.hexsha prod = [b for b in origin.refs if b.name == f'origin/{remote_branch}'][0] prod_head = prod.commit.hexsha should_pull = head != prod_head else: should_pull = True if should_pull: pull_result = repo.git.pull('origin', remote_branch) if pull_result != 'Already up to date.': ret = True log.info(pull_result) return ret
def save_problem_ci_results(ci_error, db, error, eval_data, gist, problem_ci, results, should_merge): if not should_merge: # If problem_ci fails, don't save to aggregate bot scores collection if ci_error: log.error('Problem CI failed, not saving to bots ' 'official scores as this is likely an issue ' 'with the new version of the problem.') problem_ci.status = PROBLEM_CI_STATUS_FAILED problem_ci.error = ci_error update_pr_status_problem_ci(ci_error, problem_ci, eval_data) else: log.info('Problem CI not yet finished') else: # Aggregate data from bot evals now that they're done gists = BoxList() for bot_eval_key in problem_ci.bot_eval_keys: bot_eval = db.get(get_eval_db_key(bot_eval_key)) save_to_bot_scores( bot_eval, bot_eval.eval_key, Box(score=bot_eval.results.score, eval_key=bot_eval.eval_key)) gists.append(bot_eval.gist) problem_ci.gists = gists update_pr_status_problem_ci(error, problem_ci, eval_data) problem_ci.status = PROBLEM_CI_STATUS_PASSED db.set(problem_ci.id, problem_ci)
def log_results(self, start_time): Dpop.log_results(self, start_time) log.info( "v rooms :" + str([ tuple([room.id, room.current_v]) for room in self.monitored_area.rooms ]), self.monitored_area.id, c.RESULTS)
def handle_problem_ci_status(request): start = time.time() body, error = handle_problem_ci_status_request(request) resp = Response(json=body.to_dict()) if error: resp.status_code = error.http_status_code log.info(f'Problem CI status request took {time.time() - start} seconds') return resp
def roboy_say(text: str): #return #text = "A. " + text log.info("ROBOYSAY: " + text) #text = text.replace("'", "\\'") out_string = subprocess.check_output( ["./roboy_interface.sh", "speech_synthesis", text]).decode("ascii") print(out_string)
def handle_confirm(request): start = time.time() body, error = handle_confirm_request(request) resp = Response(json=body.to_dict()) if error: resp.status_code = error.http_status_code log.info(f'Confirm request took {time.time() - start} seconds') return resp
def semaphore_requested(self) -> Union[bool, str]: status = self.db.get(STATUS) if status == RUNNING + self.id: return False else: log.info('Semaphore changed to %s, stopping' % status) if not status.startswith(REQUESTED) and status != STOPPED: log.error('Unexpected semaphore status %s' % status) return status
def process_updates(updates): for update in updates: if update.type == "focus" and update.data["val"] == True: roboy_interface.roboy_say("I used to be an adventurer like you.") #roboy_interface.roboy_say("Firtina pellar inleeler yeaneethen yuksellejeck") telegramapp.the_game.dm.set_data(update.type, update.data) log.info("UPDATE: {name}:{data}".format(name = update.type, data = json.dumps(update.data))) return "ok"
def get_deviceName(self): values = os.popen(self.get_device).readlines() dev = values[1].split()[0] if len(values)-2 == 1: print dev log.info('手机设备为:'+dev) return dev else: log.warn('暂未获取到手机设备') print 'No device found'
def roboy_detect_speech(): log.info("ROBOYLISTENING") #text = text.replace("'", "\\'") out_string = subprocess.check_output( ["./roboy_interface.sh speech_recognition"], shell=True).decode("ascii") print(out_string, file=sys.stderr) if "Service call failed" in out_string: return None return out_string
def get_deviceName(self): values = os.popen(self.get_device).readlines() dev = values[1].split()[0] if len(values) - 2 == 1: print(dev) log.info("手机设备为:" + dev) return dev else: log.warn("暂未获取到手机设备") print("No device found")
def get_deviceName(self): values = os.popen(self.get_device).readlines() dev = values[1].split()[0] if len(values) - 2 == 1: print dev log.info('手机设备为:' + dev) return dev else: log.warn('暂未获取到手机设备') print 'No device found'
def run_all(test_module): log.info('Running all tests') num = 0 for attr in dir(test_module): if attr.startswith('test_'): num += 1 log.info('Running ' + attr) getattr(test_module, attr)() log.success(f'Test: {attr} ran successfully') return num
def getData(): filename = 'data.hy' file = os.path.join(__PATH__, filename) log.info("Reading %s ...", file) try: data = h5py.File(file, 'r') except: raise IOError( 'Dataset not found. Please make sure the dataset was downloaded.') log.info("Reading Done: %s", file) return data
def main(): test_module = test if len(sys.argv) > 1: test_case = sys.argv[1] log.info('Running ' + test_case) getattr(test_module, test_case)() num = 1 log.success(f'{test_case} ran successfully!') else: num = run_all(test_module) log.success(f'{num} tests ran successfully!')
def get_platformVersion(self): values = os.popen(self.get_Version).readlines() if values != '': Version=values[0].split('=')[1] print Version log.info('手机版本号为:'+Version) return Version.strip() else: log.warn('暂未获取到手机设备') print 'No device found'
def on_message(self, client, obj, msg): data_to_avg = [self.client.avg_msg_size, len(msg.payload)] self.client.avg_msg_size = (sum(data_to_avg)) / float(len(data_to_avg)) self.client.nb_msg_exchanged_total += 1 self.client.nb_msg_exchanged_current += 1 log.info(str(msg.payload.decode('utf-8')), msg.topic, c.INFO) gc.collect()
def get_platformVersion(self): values = os.popen(self.get_Version).readlines() if values != "": Version = values[0].split("=")[1] print(Version) log.info("手机版本号为:" + Version) return Version.strip() else: log.warn("暂未获取到手机设备") print("No device found")
def handle_results(request): final_results, error, gist = handle_results_request(request) resp_box = Box(results=final_results, error=error, gist=gist) resp = Response(json=resp_box.to_dict()) if error: resp.status_code = error.http_status_code log.error(f'Error handling results {error} - results:' f' {resp_box.to_json(indent=2)}') else: log.info(f'Results response {resp_box.to_json(indent=2)}') return resp
def get_platformVersion(self): values = os.popen(self.get_Version).readlines() if values != '': Version = values[0].split('=')[1] print Version log.info('手机版本号为:' + Version) return Version.strip() else: log.warn('暂未获取到手机设备') print 'No device found'
def mydriver(): desired_caps = { 'platformName':platformName,'deviceName':deviceName, 'platformVersion':platformVersion, 'appPackage':appPackage,'appActivity':appActivity, 'unicodeKeyboard':True,'resetKeyboard':True,'noReset':True, 'newCommandTimeout':180 } try: driver = webdriver.Remote('http://127.0.0.1:%s/wd/hub'%appium_port,desired_caps) time.sleep(4) log.info('获取driver成功') return driver except WebDriverException: print 'No driver'
def volume_list(): try: cmd = "nova volume-list" cmd_result = shell.shell_cmd(cmd) with open(list_dir+list_file_volume,'w') as f: f.writelines(cmd_result) if cmd_result: logging.info("%s %s" %(tenant_name,cmd_result)) else: logging.warning("%s:Instace information is empty,please check the relevant configuration" %tenant_name) except IOError: logging.error("Tenant:%s %s" % (tenant_name)) raise return cmd_result
def start_appium(self): """ 启动appium p:appium port bp:bootstrap port :return: 返回appium端口参数 """ aport = random.randint(4700, 4900) bpport = random.randint(4700, 4900) self.__start_driver(aport, bpport) log.info( 'start appium :p %s bp %s device:%s' % (aport, bpport, self.device)) time.sleep(10) return aport
def volume_json(): volume_json_key = [] volume_key_cmd = "nova volume-list|grep in-use |awk '{print $2}'" volume_json_key_result = shell.shell_cmd(volume_key_cmd) for k in xrange(len(volume_json_key_result)): jk = volume_json_key_result[k].split()[0] volume_json_key.append(jk) volume_json = [] for js in xrange(len(volume_json_key)): volume_value_cmd = "nova volume-show %s |grep device |awk '{print $4,$5,$6,$7,$12,$13}'" %volume_json_key[js] reuslt = shell.shell_cmd(volume_value_cmd) reuslt_ev = eval(reuslt[0].split('\n')[0])[0] volume_json.append(reuslt_ev) with open(list_dir+list_file_volume_json,'w') as fp: json.dump(volume_json,fp) logging.info(volume_json) return volume_json
def rest_team_port(port): user = '******' ip = '192.168.205.3' passwd='1haoche@151111!' # logging in switch child = expect.spawn('telnet %s' %ip) index = child.expect('User:'******'user:'******'user:%s' %user) child.expect('Password:'******'passwd:',passwd # rest port status child.expect('>') child.sendline('en') # child.expect('#') child.sendline('conf t') # child.expect('\(config\)#') child.sendline('interface port-channel %s' %port) # child.expect('\(config-if-Po%s\)#' %port) child.sendline('shut') child.expect('\(config-if-Po%s\)#' %port) child.sendline('no shut') print 'port:',port logging.info('port:%s' %port) # child.expect('\(config-if-Po%s\)#' %port) child.sendline('do show interface port-channel') print child.before except: traceback.print_exc() logging.error("login failed....") finally: child.close(force=True) logging.info("login end .......")
def inner(args): temp = func(args) file_name=path+io_generate_name() if temp == '4k': try: print "生成文件为:%s 大小为:4k" % file_name logging.info("生成文件为:%s 大小为:4k" % file_name) with open(file_name+'_4k','wb') as f: f.write(os.urandom(1024*4)) except IOError: print "写入4k文件失败" logging.error("写入4k文件失败") return 4 elif temp == '1m': try: print "生成文件为:%s 大小为:1m" % file_name logging.info("生成文件为:%s 大小为:1m" % file_name) with open(file_name+'_1m','wb') as f: f.write(os.urandom(1024*1024)) except IOError: print "写入1m文件失败" logging.error( "写入1m文件失败") return 1000 elif temp == '10m': try: print "生成文件为:%s 大小为:10m" % file_name logging.info("生成文件为:%s 大小为:10m" % file_name) with open(file_name+'_10m','wb') as f: f.write(os.urandom(1024*1024*10)) except IOError: print "写入10m文件失败" logging.error("写入10m文件失败") return 10000 else: print "请输入写入的文件大小...<4k|1m|10m>" logging.warning("请输入写入的文件大小...<4k|1m|10m>") return temp
def tenant_do_work(self,tenant_name,cmd): #tenant_name = 'kycloudprod' list_dir = '/var/lib/platform-manager/' cmd_date = "date '+%Y-%m-%d'" date = shell.shell_cmd(cmd_date)[0].split()[0] global list_file list_file='list_'+tenant_name+'_'+date if not os.path.exists(list_dir): os.makedirs(list_dir,0o755) #os.chdir(list_dir) #if not os.path.exists(list_file): #os.mknod(list_file) os.chdir(home_path) #TENANT_NAME_LIST = env_set_func(tenant_name) logging.info(os.environ.data) ############## if os.path.exists(list_file): list_file_backup = list_dir+list_file+'.bak' backup_cmd = "cp -f %s %s" %(list_dir+list_file,list_file_backup) shell.shell_cmd(backup_cmd) print "This is backup file" %list_file_backup ##################### # cmd_floatingip_list ###################### cmd_floatingip_list = "neutron floatingip-list" ####################### # cmd_floatingip_associate ############################ floatingip_relation = [] cmd_fixed = "cat %s|grep -v id |awk '{print $4}'|grep -v ^$" %(list_dir+list_file) cmd_float = "cat %s |grep -v id |awk '{print $6}'|grep -v ^$" %(list_dir+list_file) cmd_fixed_result = shell.shell_cmd(cmd_fixed) cmd_float_result = shell.shell_cmd(cmd_float) for n in xrange(len(cmd_fixed_result)): p = tuple((cmd_fixed_result[n].split()[0],cmd_float_result[n].split()[0])) floatingip_relation.append(p) print "This is floatingip_relation: %s" % floatingip_relation ####################################### fixed = {} fixed_keys = [] fixed_keys_cmd = "neutron floatingip-list |grep -v id|awk '{print $4}'|grep -v ^$" fixed_keys_result = shell.shell_cmd(fixed_keys_cmd) for fixed_k in xrange(len(fixed_keys_result)): fdk = fixed_keys_result[fixed_k].split()[0] fixed_keys.append(fdk) fixed_value = [] fixed_value_cmd = "neutron floatingip-list |grep -v id|awk '{print $8}'|grep -v ^$" fixed_value_result = shell.shell_cmd(fixed_value_cmd) for fixed_v in xrange(len(fixed_value_result)): fdv = fixed_value_result[fixed_v].split()[0] fixed_value.append(fdv) float = {} float_keys = [] float_keys_cmd = "neutron floatingip-list |grep -v id|awk '{print $6}'|grep -v ^$" float_keys_result = shell.shell_cmd(float_keys_cmd) for float_k in xrange(len(float_keys_result)): fok = float_keys_result[float_k].split()[0] float_keys.append(fok) float_value = [] float_value_cmd = "neutron floatingip-list |grep -v id|awk '{print $2}'|grep -v ^$" float_value_result = shell.shell_cmd(float_value_cmd) for float_v in xrange(len(float_value_result)): fov = float_value_result[float_v].split()[0] float_value.append(fov) for n1 in xrange(2): fixed[fixed_keys[n1]] = fixed_value[n1] for n2 in xrange(2): float[float_keys[n2]] = float_value[n2] ###################################### cmd_floatingip_associate = [] for f,p in floatingip_relation: floatingip = fixed.get(f) port = float.get(p) cmd_lop = "neutron floatingip-associate %s %s" % (floatingip,port) cmd_floatingip_associate.append(cmd_lop) #logging.info("this is floatingip: %s" % floatingip_id) #print "this is floatingip_id: %s" % floatingip_id #logging.info("this is port_id: %s" % port_id) #print "this is port_id: %s" % port_id content_list = [] cmd_name = { "list":cmd_floatingip_list, "associate":cmd_floatingip_associate } print cmd_name print cmd cmd_list = [] cmd_result = cmd_name.get(cmd) if isinstance(cmd_result,str): cmd_list.insert(0,cmd_result) if isinstance(cmd_result,list): cmd_list = cmd_result print "This is cmd_list: %s" % cmd_list for cmd_l in cmd_list: print "this is cmd_l:%s" % cmd_l content = shell.shell_cmd(cmd_l) #content = os.system('nova list') #f = open(list_dir+list_file,'a') f = open(list_dir+list_file,'w') f.writelines(content) f.close() logging.info(content) content_list.append(content) print "This is content: %s" % content return content_list
def collector(self,tenant_name): #tenant_name = 'kycloudprod' list_dir = '/var/lib/platform-check/' cmd_date = "date '+%Y-%m-%d'" date = shell.shell_cmd(cmd_date)[0].split()[0] check_report_json='check_'+tenant_name+'_'+date+'.json' if not os.path.exists(list_dir): os.makedirs(list_dir,0o755) #os.chdir(list_dir) os.chdir(home_path) #TENANT_NAME_LIST = env_set_func(tenant_name) logging.info(os.environ.data) ############################# # collect_id ########################### collect_id = [] cmd_id = "nova list |grep -v ^+ |grep -v ID|awk '{print $2}'" id_result = shell.shell_cmd(cmd_id) for id in xrange(len(id_result)): c_id = id_result[id].split()[0] collect_id.append(c_id) ##################################### # collect_list ############################ collect_list = {} collect_tenant = tenant_name for x in collect_id: ######################## # collect_ip ###################### cmd_ip = "nova list |grep %s |awk -F',' '{print $1}'|cut -d = -f2" %x ip_result =shell.shell_cmd(cmd_ip) collect_ip = ip_result[0].split()[0] ####################### # collect_status ################# cmd_status = "nova list |grep %s |awk '{print $6}'" %x status_result = shell.shell_cmd(cmd_status) collect_status = status_result[0].split()[0] ####################### # collect_host ####################### cmd_host = "nova list |grep %s |awk '{print $4}'" %x host_result = shell.shell_cmd(cmd_host) collect_host = host_result[0].split()[0] ####################### # collect_floatingip #################### cmd_floatingip = "nova list |grep %s |awk '{print $13}'" %x floatingip_result = shell.shell_cmd(cmd_floatingip) collect_floatingip = floatingip_result[0].split()[0] ##################### # collect_list ###################### collect_list[x] = { "ip":collect_ip, "status":collect_status, "hostname":collect_host, "tenant":collect_tenant, "floatingip":collect_floatingip } f = open(list_dir+check_report_json,'w') json.dump(collect_list,f) f.close() logging.info(collect_list)
def tenant_do_work(self,tenant_name,cmd): #tenant_name = 'kycloudprod' list_dir = '/var/lib/platform-manager/' cmd_date = "date '+%Y-%m-%d'" date = shell.shell_cmd(cmd_date)[0].split()[0] list_file_nova='list_'+tenant_name+'_'+date list_file_volume ='list_'+tenant_name+'_volume_'+date #list_file_volume_flag =list_file_volume+'_flag' list_file_volume_json = list_file_volume+'.json' #list_file_volume_back = list_file_volume+'.bak' if not os.path.exists(list_dir): os.makedirs(list_dir,0o755) #os.chdir(list_dir) os.chdir(home_path) #TENANT_NAME_LIST = env_set_func(tenant_name) logging.info(os.environ.data) cc = "nova list" c = shell.shell_cmd(cc) #print "QQQQQQQQ:%s" % c ##################### # instance_list ###################### def instance_list(): try: cmd = "nova list" cmd_result = shell.shell_cmd(cmd) with open(list_dir+list_file_nova,'w') as f: f.writelines(cmd_result) if cmd_result: logging.info("%s %s" %(tenant_name,cmd_result)) else: logging.warning("%s:Instace information is empty,please check the relevant configuration" %tenant_name) except IOError: logging.error("Tenant:%s %s" % tenant_name) raise return cmd_result #################### # volume_json #################### def volume_json(): volume_json_key = [] volume_key_cmd = "nova volume-list|grep in-use |awk '{print $2}'" volume_json_key_result = shell.shell_cmd(volume_key_cmd) for k in xrange(len(volume_json_key_result)): jk = volume_json_key_result[k].split()[0] volume_json_key.append(jk) volume_json = [] for js in xrange(len(volume_json_key)): volume_value_cmd = "nova volume-show %s |grep device |awk '{print $4,$5,$6,$7,$12,$13}'" %volume_json_key[js] reuslt = shell.shell_cmd(volume_value_cmd) reuslt_ev = eval(reuslt[0].split('\n')[0])[0] volume_json.append(reuslt_ev) with open(list_dir+list_file_volume_json,'w') as fp: json.dump(volume_json,fp) logging.info(volume_json) return volume_json ####################### # volume_list ############################ def volume_list(): try: cmd = "nova volume-list" cmd_result = shell.shell_cmd(cmd) with open(list_dir+list_file_volume,'w') as f: f.writelines(cmd_result) if cmd_result: logging.info("%s %s" %(tenant_name,cmd_result)) else: logging.warning("%s:Instace information is empty,please check the relevant configuration" %tenant_name) except IOError: logging.error("Tenant:%s %s" % (tenant_name)) raise return cmd_result
def tenant_do_work(self,tenant_name,cmd): #tenant_name = 'kycloudprod' list_dir = '/var/lib/platform-manager/' cmd_date = "date '+%Y-%m-%d'" date = shell.shell_cmd(cmd_date)[0].split()[0] list_file_nova='list_'+tenant_name+'_'+date list_file_volume ='list_'+tenant_name+'_volume' list_file_volume_flag =list_file_volume+'_flag' list_file_volume_json = list_file_volume+'.json' list_file_volume_back = list_file_volume+'.bak' if not os.path.exists(list_dir): os.makedirs(list_dir,0o755) #os.chdir(list_dir) os.chdir(home_path) #TENANT_NAME_LIST = env_set_func(tenant_name) logging.info(os.environ.data) ##################### # cmd_list ###################### cmd_list = "nova list" ####################### # cmd_volume_list ############################ cmd_volume_list = "nova volume-list" ###################### # cmd_stop ####################### ############################ cmd_stop = [] if cmd == "stop": server_running_id = [] server_cmd = "nova list |grep Running|awk '{print $2}'" cmd_result = shell.shell_cmd(server_cmd) # for i in xrange(len(cmd_result)): lo = cmd_result[i].split()[0] server_running_id.append(lo) # # for server_stop in server_running_id: cmd_lop = "nova stop %s" %server_stop cmd_stop.append(cmd_lop) logging.info("this is Server_runing_id: %s" % server_running_id) # print "this is Server_runing_id: %s" % server_running_id ########################################## # cmd_start ################################# cmd_start = [] if cmd == "start": server_shutdown_id = [] server_stop_cmd = " nova list|grep Shutdown |awk '{print $2}'" cmd_result_stop = shell.shell_cmd(server_stop_cmd) # for j in xrange(len(cmd_result_stop)): po = cmd_result_stop[j].split()[0] server_shutdown_id.append(po) ## for server_start in server_shutdown_id: cmd_pop = "nova start %s" %server_start cmd_start.append(cmd_pop) # print "this is Server_shutdown_id: %s" % server_shutdown_id #################################### # cmd_volume_attach ####################################### cmd_volume_attach = [] if cmd == "volume_attach" : start_list = [] #print "This is json_load:%s" %(list_dir+list_file_volume_json) f = open(list_dir+list_file_volume_json) json_load =json.load(f,encoding="utf-8") f.close() print "This is json_load:%s" %json_load for j in xrange(len(json_load)): cc = json_load[j] device = str(cc['device']) server_id = str(cc['server_id']) volume_id = str(cc['id']) ff = tuple((server_id,volume_id,device)) start_list.append(ff) for server,volume,device_path in start_list: cmd_v = "nova volume-attach %s %s %s" % (server,volume,device_path) cmd_volume_attach.append(cmd_v) # print "This is server_volume_id: %s" % server_volume_id logging.info("This is start_list: %s" % start_list) ################################## # cmd_volume_detach ########################### cmd_volume_detach = [] if cmd == "volume_detach": server_volume_detach_id = [] volume_detach_id = [] cmd_detach_server = "nova volume-list |grep in-use|awk '{print $(NF-1)}'" cmd_result_server = shell.shell_cmd(cmd_detach_server) for de in xrange(len(cmd_result_server)): sd = cmd_result_server[de].split()[0] server_volume_detach_id.append(sd) cmd_detach_volume = "nova volume-list |grep in-use|awk '{print $2}'" cmd_result_volume = shell.shell_cmd(cmd_detach_volume) for dv in xrange(len(cmd_result_volume)): vd = cmd_result_volume[dv].split()[0] volume_detach_id.append(vd) stop_list = [] for e in xrange(len(server_volume_detach_id)): fd = tuple((server_volume_detach_id[e],volume_detach_id[e])) stop_list.append(fd) for server_volume_01,volume_01 in stop_list: cmd_d = "nova volume-detach %s %s" % (server_volume_01,volume_01) cmd_volume_detach.append(cmd_d) #print "This is volume_detach_id:%s" % volume_detach_id #print "This is server_volume_detach_id:%s" %server_volume_detach_id ############################################## ############################# # volume_json ############################ if cmd == "stop" : volume_json_key = [] #volume_key_cmd = "grep in-use %s |awk '{print $2}'" %(list_dir+list_file_volume) volume_key_cmd = "nova volume-list|grep in-use |awk '{print $2}'" volume_json_key_result = shell.shell_cmd(volume_key_cmd) for k in xrange(len(volume_json_key_result)): jk = volume_json_key_result[k].split()[0] volume_json_key.append(jk) volume_json = [] for js in xrange(len(volume_json_key)): volume_value_cmd = "nova volume-show %s |grep device |awk '{print $4,$5,$6,$7,$12,$13}'" %volume_json_key[js] reuslt = shell.shell_cmd(volume_value_cmd) reuslt_ev = eval(reuslt[0].split('\n')[0])[0] volume_json.append(reuslt_ev) # json fp = open(list_dir+list_file_volume_json,'w') json.dump(volume_json,fp) fp.close() logging.info(volume_json) ############################################## content_list = [] cmd_name = { "list":cmd_list, "stop":cmd_stop, "volume_list":cmd_volume_list, "start":cmd_start, "volume_attach":cmd_volume_attach, "volume_detach":cmd_volume_detach } print "This is cmdname : %s" % cmd_name logging.debug("This is cmd_name:%s" %cmd_name) print cmd cmd_list = [] cmd_result = cmd_name.get(cmd) if isinstance(cmd_result,str): cmd_list.insert(0,cmd_result) if isinstance(cmd_result,list): cmd_list = cmd_result #print "This is cmd_list: %s" % cmd_list logging.debug("This is cmd_list: %s" % cmd_list) #################### in_use_check_id = [] in_use_check_server_cmd = "nova volume-list|grep in-use|awk '{print $(NF-1)}'" in_use_check_result = shell.shell_cmd(in_use_check_server_cmd) for in_use in xrange(len(in_use_check_result)): iu = in_use_check_result[in_use].split()[0] in_use_check_id.append(iu) for cmd_l in cmd_laist: #print "this is cmd_l:%s" % cmd_l logging.debug("this is cmd_l:%s" % cmd_l) if cmd_l == "nova volume-list": content = shell.shell_cmd(cmd_l) #content = os.system('nova list') back_cmd = "cp -f %s %s" %(list_dir+list_file_volume,list_dir+list_file_volume_back) shell.shell_cmd(back_cmd) f = open(list_dir+list_file_volume,'w') f.writelines(content) f.close() logging.info(content) content_list.append(content) else: if "nova start" in cmd_l: server_id = cmd_l.split()[2] if server_id in in_use_check_id: content = shell.shell_cmd(cmd_l) #content = os.system('nova list') f = open(list_dir+list_file_nova,'a') f.writelines(content) f.close() logging.info(content) content_list.append(content) else: content = shell.shell_cmd(cmd_l) #content = os.system('nova list') f = open(list_dir+list_file_nova,'a') f.writelines(content) f.close() logging.info(content) content_list.append(content) #print "This is content: %s" % content time.sleep(10) print "\033[1;31;40m Tenant: %s perform the %s action ,please later.....\033[0m" % (tenant_name,cmd) ############################## # check_result_instance and check_result_volume ################################## # check_result_instance cmd_check = "nova list |awk '{print $6}'|grep -v Status|grep -v ^$" check_cmd_result = shell.shell_cmd(cmd_check) check_result = [] for c in xrange(len(check_cmd_result)): check = check_cmd_result[c].split()[0] check_result.append(check) num = len(check_result) ###########################3 # check_result_volume cmd_check_volume = "nova volume-list|grep -v Status|awk '{print $4}'|grep -v ^$" check_volume_result = shell.shell_cmd(cmd_check) check_volume_result = [] for v in xrange(len(check_volume_result)): check_v = check_cmd_result[v].split()[0] check_volume_result.append(check_v) num_volume = len(check_volume_result) #print "I'am herer" if cmd == "stop" or cmd == "volume_detach": if num == check_result.count("SHUTOFF") and num_volume == check_volume_result.count("available"): logging.info("%s %s" %(check_result,check_volume_result)) logging.info("Tenant: %s All instance stop successfully!" %tenant_name) logging.info("Tenant: %s All volume deattch successfully!" %tenant_name) print "\033[1;31;40m Tenant: %s all instance stop successfully!\033[0m" % tenant_name print "\033[1;31;40m Tenant: %s all volume deattch successfully!\033[0m" % tenant_name elif num == check_result.count("SHUTOFF"): logging.info(check_result) logging.info("Tenant: %s All instance stop successfully!" %tenant_name) print "\033[1;31;40m Tenant: %s all instance stop successfully!\033[0m" % tenant_name elif 0 == check_result.count("SHUTOFF"): logging.info(check_result) logging.critical("Tenant: %s All instance stop failure!" %tenant_name) elif num != check_result.count("SHUTOFF"): logging.info(check_result) logging.error("Tenant:%s All your stoped operating withou success!" %tenant_name) elif num_volume == check_volume_result.count("available"): logging.info(check_volume_result) logging.info("Tenant: %s All volume deattch successfully!" %tenant_name) print "\033[1;31;40m Tenant: %s all volume deattch successfully!\033[0m" % tenant_name elif 0 == check_volume_result.count("available"): logging.info(check_volume_result) logging.critical("Tenant: %s All volume deattch failure!" %tenant_name) elif num != check_result.count("SHUTOFF") or num_volume != check_volume_result.count("available"): logging.info("There are info %s,%s" %(check_volume_result,check_result)) logging.error("Tenant:%s All your stoped operating withou success!" %tenant_name) if cmd == "start": ####################################### if num == check_result.count("ACTIVE"): logging.info(check_result) logging.info("Tenant:%s All instance started successfully!" %tenant_name) print "\033[1;31;40m Tenant:%s All instance started successfully!\033[0m" %tenant_name elif 0 == check_result.count("ACTIVE"): logging.info(check_result) logging.critical("Tenant: %s All instance start failure!" %tenant_name) elif num != check_result.count("ACTIVE"): logging.info(check_result) logging.error("Tenant:%s All your started operating withou success!" %tenant_name) if cmd == "volume_attach": check_volume_name_old = [] cmd_volume_name_old ="grep in-use %s |awk '{print $2}'" %(list_file_volume) name_old_result = shell.shell_cmd(cmd_volume_name_old) for o in xrange(len(name_old_result)): vo = name_old_result[o].split()[0] check_volume_name_old.append(vo) check_volume_name = [] cmd_volume_name ="nova volume-list |grep in-use|awk '{print $2}'" name_result = shell.shell_cmd(cmd_volume_name) for l in xrange(len(name_result)): vl = name_result[l].split()[0] check_volume_name.append(vl) ####################################### if len(check_volume_name) == len(check_volume_name_old): logging.info((check_volume_result)) logging.info("Tenant:%s All volume attach successfully!" %tenant_name) print "\033[1;31;40m Tenant:%s All volume attach successfully!\033[0m" %tenant_name flag = open(list_dir+list_file_volume_flag,'w') flag.writelines('0') flag.close() return 0 elif len(check_volume_name) == 0: logging.info(check_volume_result) logging.critical("Tenant: %s All volume attach failure!" %tenant_name) flag = open(list_dir+list_file_volume_flag,'w') flag.writelines('1') flag.close() return 1 else: logging.info(check_volume_result) logging.error("Tenant:%s All your started operating withou success!" %tenant_name) flag = open(list_dir+list_file_volume_flag,'w') flag.writelines('-1') flag.close() return -1