72 lines
1.8 KiB
Python
72 lines
1.8 KiB
Python
"""Initialise the toolbelt."""
|
|
|
|
import os
|
|
|
|
import click
|
|
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 ensure_installed, run_command
|
|
|
|
log = logger.get_logger(__name__)
|
|
|
|
|
|
@click.command()
|
|
@click.pass_context
|
|
def init(ctx):
|
|
"""Initialise the toolbelt."""
|
|
create_configuration_directory()
|
|
clone_infrastructure_repo()
|
|
create_configuration_file()
|
|
ask_to_login()
|
|
|
|
|
|
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):
|
|
ensure_installed("git")
|
|
run_command(
|
|
["git", "clone", INFRASTRUCTURE_REPOSITORY, INFRASTRUCTURE_PATH]
|
|
)
|
|
else:
|
|
os.chdir(INFRASTRUCTURE_PATH)
|
|
run_command(["git", "pull", "origin", "master"])
|
|
|
|
|
|
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)
|
|
|
|
|
|
def create_configuration_file():
|
|
"""Create toolbelt config file."""
|
|
if not os.path.exists(AUTONOMIC_YAML):
|
|
with open(AUTONOMIC_YAML, "w") as handle:
|
|
handle.write("---")
|