def initRepository(repository_dir, repository_url, build_dir): if not os.path.isdir(repository_dir): log.info("Clone repository: {} ... ".format(repository_url), end='', flush=True) cloneResult = helper.execCommand( u"git clone {}".format(repository_url), build_dir) if cloneResult.returncode == 0: log.info(u"done", flush=True) else: log.error(u"error: {}".format(cloneResult.stdout.decode("utf-8")), flush=True, file=sys.stderr)
def destroyMachine(vid): vmCleaned = False if vid != None: machines = getRegisteredMachines() if vid in machines: name = machines[vid] log.info(u"VM - vid: '{}', name: '{}' - destroyed.".format(vid,name)) helper.execCommand("VBoxManage controlvm \"{}\" poweroff".format(vid)) helper.execCommand("VBoxManage unregistervm --delete \"{}\"".format(vid)) vmCleaned = True else: log.error(u"VM - vid: '{}' not found.".format(vid)) return vmCleaned
def checkMachines(show_info): machines = getRegisteredMachines() if len(machines.keys()) > 0: if show_info: log.info(u"Following machines are registered.") else: log.info(u"Some machines are still there. Check leftovers.") for vid,name in machines.items(): log.info(u" VM - vid: '{}', name: '{}'".format(vid,name)) elif show_info: log.info(u"No machines are registered.")
def stopRunningJob(status_file, log_dir): cleaned = False pid = getPid() if pid != None: log.info(u"Job with pid '{}' killed.".format(pid)) os.kill(int(pid), signal.SIGTERM) cleaned = True # Prepare file job.modifyStoppedFile(log_dir) state_obj = status.getState(status_file) if state_obj["vid"] != None: if virtualbox.destroyMachine(state_obj["vid"]): cleaned = True else: log.info(u"Cleaning status file.") status.setVID(status_file, None) virtualbox.checkMachines(False) if cleaned: if state_obj != None: status.setState(status_file, u"cleaned") return status.getState(status_file) else: log.info(u"Nothing stopped.") return None
def showRunningJobs(): pid = getPid() if pid != None: log.info(u"Main process is running with pid '{}'.".format(pid)) else: log.info(u"Main process is not running.") processes = [] ci_result = helper.execCommand("ps -alx | grep ci_service") ci_lines = ci_result.stdout.decode("utf-8").split(u"\n") formatProcesses(ci_lines, processes) vm_result = helper.execCommand("ps -alx | grep virtualbox") vm_lines = vm_result.stdout.decode("utf-8").split(u"\n") formatProcesses(vm_lines, processes) if len(processes) > 0: log.info(u"Following sub processes are running.") log.info(u"\n".join(processes)) else: log.info(u"No sub processes are running.") virtualbox.checkMachines(True)
def checkRunningJob(status_file): state_obj = status.getState(status_file) if state_obj: # check 4 hours if time() - state_obj["last_modified"] > 14000: if state_obj["status"] == "running": status.setState(status_file, u"crashed") log.error( u"Check for commit '{}' is running too long. Marked as 'crashed' now. Maybe it is stucked and you should try to cleanup manually." .format(state_obj["git_hash"]), file=sys.stderr) # check for frozen processes exit(1) elif state_obj["status"] == "crashed": log.info( u"Skipped check. Previous check for commit '{}' was crashing." .format(state_obj["git_hash"])) exit(0) else: if state_obj["status"] == "running": pid = getPid() if pid != None: log.info( u"Check for commit '{}' is still running with pid '{}'." .format(state_obj["git_hash"], pid)) exit(0) else: status.setState(status_file, u"crashed") log.error( u"Check for commit '{}' marked as 'running', but pid was not found. Marked as 'crashed' now." .format(state_obj["git_hash"]), file=sys.stderr) exit(1) return state_obj["git_hash"] return ""
def modifyStoppedFile(log_dir): files = glob.glob("{}*-0-running-*.log".format(log_dir)) files.sort(key=os.path.getmtime, reverse=True) for deployment_log_file in files: filename = deployment_log_file.split('/')[-1] parts = filename.split("-") start_time = datetime.strptime(parts[0], START_TIME_STR_FORMAT) duration = int( round(datetime.now().timestamp() - start_time.timestamp())) with open(deployment_log_file, 'a') as f: f.write("\n") lf = LogFile(f) lf.write("Stopped after {}.\n".format(timedelta(seconds=duration))) finished_log_file = deployment_log_file.replace( "-0-running-", "-{}-stopped-".format(duration)) os.rename(deployment_log_file, finished_log_file) log.info(u"Logfile '{}' processed.".format( finished_log_file.split('/')[-1])) break