def _check(name): try: subprocess.check_call( ["systemctl", "is-active", "--quiet", get_service_name(name)]) say(f"Yep, {name} is up!") except subprocess.CalledProcessError: say(f"...something might be wrong; {name} doesn't look like it's up." )
def revert_and_recover(loc): git_response = (subprocess.check_output([ "git", "reset", "--hard", f"{get_branch_head()}@{{'30 seconds ago'}}" ]).decode().strip()) say(f"Rolling back to previous state:\n```\n{git_response}```") subprocess.check_output( ["sudo", "systemctl", "restart", get_service_name(loc)])
def restart_service(loc): say(f"Restarting service for {loc}...") systemctl_response = subprocess.check_output( ["sudo", "systemctl", "restart", get_service_name(loc)]) if systemctl_response.decode().strip() != "": say("Something went wrong and could not restart.") saycode(systemctl_response) else: verify_service_up(loc)
def verify_service_up(loc): say(f"Pausing for {PROCESS_CHECK_SLEEP_TIME}s to verify that {loc} restarted" f" correctly...") try: for attempt in range(PROCESS_CHECK_COUNT): time.sleep(PROCESS_CHECK_SLEEP_TIME / PROCESS_CHECK_COUNT) subprocess.check_call([ "systemctl", "is-active", "--quiet", get_service_name(loc) ]) say(f"Check {attempt + 1}/{PROCESS_CHECK_COUNT} complete!") say("Restarted successfully!") except subprocess.CalledProcessError: revert_and_recover(loc)
def logs(payload): say = payload['extras']['say'] text = payload['cleaned_text'].split() if len(text) == 1: say("What service should I return the logs for?") say(VALID) return service = text[1] if service == "all": say("Sorry, that's a lot of logs. Please specify the service you want." ) say(VALID) result = subprocess.check_output( COMMAND.format(get_service_name(service)).split()) for block in break_large_message(result.decode().strip(), break_at=3800): say(f"```{block}```")