Check ssh-agent

This commit is contained in:
Luke Murphy 2020-04-11 21:25:49 +02:00
parent b58b9c3296
commit 7ee473c9b6
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
3 changed files with 34 additions and 16 deletions

View File

@ -3,6 +3,7 @@
from os import mkdir
from os.path import exists
from subprocess import STDOUT
import click
from emoji import emojize
@ -11,8 +12,8 @@ from PyInquirer import prompt
from autonomic.config import CONFIG_DIR, CONFIG_YAML, INFRA_DIR, INFRA_REPO
from autonomic.infra import members
from autonomic.logger import log
from autonomic.settings import add, get
from autonomic.utils import qlist, run
from autonomic.settings import add
from autonomic.utils import is_proc, qlist, run
@click.command()
@ -24,6 +25,7 @@ def init(ctx):
clone_infrastructure_repo()
store_username()
install_dependencies()
ssh_sanity_check()
def create_configuration_directory():
@ -36,29 +38,21 @@ def clone_infrastructure_repo():
"""Clone or update the infrastructure repository."""
if not exists(INFRA_DIR):
cmd = ["git", "clone", INFRA_REPO, INFRA_DIR]
run(cmd)
run(cmd, stderr=STDOUT)
else:
cmd = ["git", "pull", "origin", "master"]
run(cmd, cwd=INFRA_DIR)
run(cmd, cwd=INFRA_DIR, stderr=STDOUT)
def store_username():
"""Store Autonomic username in the settings."""
if exists(CONFIG_YAML):
username = get("username")
if username is not None:
msg = "Username already configured as {}".format(username)
log.info(msg)
return
usernames = members(flatten="username")
question = qlist("username", "What is you Autonomic username?", usernames)
answer = prompt(question)
add(answer)
msg = "Welcome comrade {} :kissing:".format(answer["username"])
emojized = emojize(msg, use_aliases=True)
log.info(emojized)
log.success(emojize(msg, use_aliases=True))
def create_settings_file():
@ -71,10 +65,10 @@ def create_settings_file():
def install_dependencies():
"""Install infrastructure dependencies."""
cmd = ["/usr/bin/python3", "-m", "venv", ".venv"]
run(cmd, cwd=INFRA_DIR)
run(cmd, cwd=INFRA_DIR, stderr=STDOUT)
cmd = [".venv/bin/pip", "install", "-r", "requirements.txt"]
run(cmd, cwd=INFRA_DIR)
run(cmd, stderr=STDOUT)
cmd = [
".venv/bin/ansible-galaxy",
@ -83,4 +77,14 @@ def install_dependencies():
"requirements.yml",
"--force",
]
run(cmd, cwd=INFRA_DIR)
run(cmd, stderr=STDOUT)
def ssh_sanity_check():
"""Try to recommend some SSH tips."""
if not is_proc("ssh-agent"):
msg = "ssh-agent is not running :confounded:"
log.warning(emojize(msg, use_aliases=True))
else:
msg = "ssh-agent is running :rocket:"
log.success(emojize(msg, use_aliases=True))

View File

@ -10,6 +10,8 @@ things here.
from os import chdir
from subprocess import CalledProcessError, check_output
from psutil import process_iter
from autonomic.logger import log
from autonomic.yaml import yaml
@ -71,3 +73,14 @@ def yaml_dump(fpath, data):
yaml.dump(data, handle)
except Exception as exception:
log.error(str(exception))
def is_proc(name):
"""Determine if a process is running or not."""
for process in process_iter():
try:
if name.lower() in process.name().lower():
return True
except Exception:
pass
return False

View File

@ -45,6 +45,7 @@ install_requires =
click >= 7.1.1, <= 8.0
colorama >= 0.4.3, <= 0.5
emoji >= 0.5.4, <= 0.6
psutil >= 5.7.0, <= 6.0
pyinquirer >= 1.0.3, <= 1.1
ruamel.yaml >= 0.16.10, <= 0.17