This repository has been archived on 2020-06-17. You can view files and clone it, but cannot push or open issues or pull requests.
autonomic/autonomic/command/init.py

90 lines
2.3 KiB
Python

"""Initialise the toolbelt."""
from os import mkdir
from os.path import exists
from subprocess import STDOUT
import click
from emoji import emojize
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
from autonomic.utils import is_proc, question_ask, run
@click.command()
@click.pass_context
def init(ctx):
"""Initialise the toolbelt."""
create_configuration()
clone_infrastructure_repo()
store_username()
install_dependencies()
ssh_sanity_check()
msg = "Hack the planet! :earth_africa:"
log.success(emojize(msg, use_aliases=True))
def create_configuration():
"""Create toolbelt config directory."""
if not exists(CONFIG_DIR):
mkdir(CONFIG_DIR)
if not exists(CONFIG_YAML):
with open(CONFIG_YAML, "w") as handle:
handle.write("---")
def clone_infrastructure_repo():
"""Clone or update the infrastructure repository."""
if not exists(INFRA_DIR):
cmd = ["git", "clone", INFRA_REPO, INFRA_DIR]
run(cmd, stderr=STDOUT)
else:
cmd = ["git", "pull", "origin", "master"]
run(cmd, cwd=INFRA_DIR, stderr=STDOUT)
def store_username():
"""Store Autonomic username in the settings."""
usernames = members(flatten="username")
username = question_ask(
"username", "What is you Autonomic username?", usernames,
)
add({"username": username})
msg = "Welcome comrade {} :kissing:".format(username)
log.success(emojize(msg, use_aliases=True))
def install_dependencies():
"""Install infrastructure dependencies."""
cmd = ["/usr/bin/python3", "-m", "venv", ".venv"]
run(cmd, cwd=INFRA_DIR, stderr=STDOUT)
cmd = [".venv/bin/pip", "install", "-r", "requirements.txt"]
run(cmd, stderr=STDOUT)
cmd = [
".venv/bin/ansible-galaxy",
"install",
"-r",
"requirements.yml",
"--force",
]
run(cmd, stderr=STDOUT)
def ssh_sanity_check():
"""Try to recommend some SSH tips."""
if not is_proc("ssh-agent"):
msg = "warning: ssh-agent is not running :sob:"
log.warning(emojize(msg, use_aliases=True))
else:
msg = "ssh-agent is running :rocket:"
log.success(emojize(msg, use_aliases=True))