"""Initialise the toolbelt.""" from os import mkdir from os.path import exists from subprocess import STDOUT import click from emoji import emojize 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 from autonomic.utils import is_proc, qlist, run @click.command() @click.pass_context def init(ctx): """Initialise the toolbelt.""" create_configuration_directory() create_settings_file() 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_directory(): """Create toolbelt config directory.""" if not exists(CONFIG_DIR): mkdir(CONFIG_DIR) 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") question = qlist("username", "What is you Autonomic username?", usernames) answer = prompt(question) add(answer) msg = "Welcome comrade {} :kissing:".format(answer["username"]) log.success(emojize(msg, use_aliases=True)) def create_settings_file(): """Create settings file.""" if not exists(CONFIG_YAML): with open(CONFIG_YAML, "w") as handle: handle.write("---") 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 = "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))