"""Initialise the toolbelt.""" import os import click import emoji from PyInquirer import prompt from autonomic import logger from autonomic.config import add_to_config from autonomic.infrastructure import get_members from autonomic.settings import ( AUTONOMIC_YAML, CONFIG_PATH, INFRASTRUCTURE_PATH, INFRASTRUCTURE_REPOSITORY, ) from autonomic.system import run_command log = logger.get_logger(__name__) @click.command() @click.pass_context def init(ctx): """Initialise the toolbelt.""" create_configuration_directory() create_configuration_file() clone_infrastructure_repo() ask_to_login() install_dependencies() def create_configuration_directory(): """Create toolbelt config directory.""" if not os.path.exists(CONFIG_PATH): os.mkdir(CONFIG_PATH) def clone_infrastructure_repo(): """Clone the infrastructure repository.""" if not os.path.exists(INFRASTRUCTURE_PATH): run_command( ["git", "clone", INFRASTRUCTURE_REPOSITORY, INFRASTRUCTURE_PATH] ) else: run_command( ["git", "pull", "origin", "master"], cwd=INFRASTRUCTURE_PATH ) def ask_to_login(): """Log in as your autonomic member username.""" members = get_members() choices = [info["username"] for info in members["autonomic_members"]] question = [ { "type": "list", "name": "username", "message": "What is your Autonomic username?", "choices": choices, "filter": lambda val: val.lower(), } ] answer = prompt(question) add_to_config(answer) msg = "Welcome comrade {} :kissing:".format(answer["username"]) emojized = emoji.emojize(msg, use_aliases=True) log.info(emojized) def create_configuration_file(): """Create toolbelt config file.""" if not os.path.exists(AUTONOMIC_YAML): with open(AUTONOMIC_YAML, "w") as handle: handle.write("---") def install_dependencies(): """Install infrastructure dependencies.""" run_command( ["/usr/bin/python3", "-m", "venv", ".venv"], cwd=INFRASTRUCTURE_PATH, ) run_command( [".venv/bin/pip", "install", "-r", "requirements.txt"], cwd=INFRASTRUCTURE_PATH, ) run_command( [".venv/bin/ansible-galaxy", "install", "-r", "requirements.yml"], cwd=INFRASTRUCTURE_PATH, )