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

87 lines
2.2 KiB
Python
Raw Normal View History

"""Initialise the toolbelt."""
2020-04-11 19:10:29 +00:00
from os import mkdir
from os.path import exists
import click
2020-04-11 19:10:29 +00:00
from emoji import emojize
from PyInquirer import prompt
2020-04-11 19:10:29 +00:00
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
@click.command()
@click.pass_context
def init(ctx):
"""Initialise the toolbelt."""
create_configuration_directory()
2020-04-11 19:10:29 +00:00
create_settings_file()
2020-04-10 23:45:53 +00:00
clone_infrastructure_repo()
2020-04-11 19:10:29 +00:00
store_username()
2020-04-10 23:45:53 +00:00
install_dependencies()
def create_configuration_directory():
"""Create toolbelt config directory."""
2020-04-11 19:10:29 +00:00
if not exists(CONFIG_DIR):
mkdir(CONFIG_DIR)
def clone_infrastructure_repo():
2020-04-11 19:10:29 +00:00
"""Clone or update the infrastructure repository."""
if not exists(INFRA_DIR):
cmd = ["git", "clone", INFRA_REPO, INFRA_DIR]
run(cmd)
else:
2020-04-11 19:10:29 +00:00
cmd = ["git", "pull", "origin", "master"]
run(cmd, cwd=INFRA_DIR)
2020-04-10 22:26:13 +00:00
2020-04-11 19:10:29 +00:00
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)
2020-04-11 19:10:29 +00:00
add(answer)
2020-04-10 22:26:13 +00:00
msg = "Welcome comrade {} :kissing:".format(answer["username"])
2020-04-11 19:10:29 +00:00
emojized = emojize(msg, use_aliases=True)
2020-04-10 22:26:13 +00:00
log.info(emojized)
2020-04-11 19:10:29 +00:00
def create_settings_file():
"""Create settings file."""
if not exists(CONFIG_YAML):
with open(CONFIG_YAML, "w") as handle:
handle.write("---")
2020-04-10 23:45:53 +00:00
def install_dependencies():
"""Install infrastructure dependencies."""
2020-04-11 19:10:29 +00:00
cmd = ["/usr/bin/python3", "-m", "venv", ".venv"]
run(cmd, cwd=INFRA_DIR)
cmd = [".venv/bin/pip", "install", "-r", "requirements.txt"]
run(cmd, cwd=INFRA_DIR)
cmd = [
".venv/bin/ansible-galaxy",
"install",
"-r",
"requirements.yml",
"--force",
]
run(cmd, cwd=INFRA_DIR)