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
Raw Normal View History

"""Initialise the toolbelt."""
2020-04-11 19:10:29 +00:00
from os import mkdir
from os.path import exists
2020-04-11 19:25:49 +00:00
from subprocess import STDOUT
import click
2020-04-11 19:10:29 +00:00
from emoji import emojize
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
2020-04-11 19:25:49 +00:00
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."""
2020-04-11 20:47:16 +00:00
create_configuration()
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()
2020-04-11 19:25:49 +00:00
ssh_sanity_check()
msg = "Hack the planet! :earth_africa:"
log.success(emojize(msg, use_aliases=True))
2020-04-11 20:47:16 +00:00
def create_configuration():
"""Create toolbelt config directory."""
2020-04-11 19:10:29 +00:00
if not exists(CONFIG_DIR):
mkdir(CONFIG_DIR)
2020-04-11 20:47:16 +00:00
if not exists(CONFIG_YAML):
with open(CONFIG_YAML, "w") as handle:
handle.write("---")
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]
2020-04-11 19:25:49 +00:00
run(cmd, stderr=STDOUT)
else:
2020-04-11 19:10:29 +00:00
cmd = ["git", "pull", "origin", "master"]
2020-04-11 19:25:49 +00:00
run(cmd, cwd=INFRA_DIR, stderr=STDOUT)
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."""
usernames = members(flatten="username")
username = question_ask(
"username", "What is you Autonomic username?", usernames,
)
add({"username": username})
msg = "Welcome comrade {} :kissing:".format(username)
2020-04-11 19:25:49 +00:00
log.success(emojize(msg, use_aliases=True))
2020-04-10 22:26:13 +00:00
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"]
2020-04-11 19:25:49 +00:00
run(cmd, cwd=INFRA_DIR, stderr=STDOUT)
2020-04-11 19:10:29 +00:00
cmd = [".venv/bin/pip", "install", "-r", "requirements.txt"]
2020-04-11 19:25:49 +00:00
run(cmd, stderr=STDOUT)
2020-04-11 19:10:29 +00:00
cmd = [
".venv/bin/ansible-galaxy",
"install",
"-r",
"requirements.yml",
"--force",
]
2020-04-11 19:25:49 +00:00
run(cmd, stderr=STDOUT)
def ssh_sanity_check():
"""Try to recommend some SSH tips."""
if not is_proc("ssh-agent"):
2020-04-11 20:47:16 +00:00
msg = "warning: ssh-agent is not running :sob:"
2020-04-11 19:25:49 +00:00
log.warning(emojize(msg, use_aliases=True))
else:
msg = "ssh-agent is running :rocket:"
log.success(emojize(msg, use_aliases=True))