From 2fca21cc19d6227cfaa29229c7065413058adb4d Mon Sep 17 00:00:00 2001 From: Raymond Negron Date: Mon, 4 Sep 2023 11:32:28 -0500 Subject: [PATCH 1/3] Adding context verification name and first set config file --- .gitignore | 1 + estela_cli/context.py | 29 +++++++++++++++++++++++++++-- estela_cli/templates.py | 1 + estela_cli/utils.py | 16 ++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b80ed7e..2489422 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ venv/ .venv/ .DS_Store site/ +.vscode/ diff --git a/estela_cli/context.py b/estela_cli/context.py index 706ca35..c7567db 100644 --- a/estela_cli/context.py +++ b/estela_cli/context.py @@ -5,11 +5,12 @@ from estela_cli.utils import ( get_estela_auth, get_estela_settings, + get_estela_config, get_host_from_env, get_username_from_env, get_password_from_env, ) -from estela_cli.templates import ESTELA_AUTH_NAME, OK_EMOJI, BAD_EMOJI +from estela_cli.templates import ESTELA_AUTH_NAME, ESTELA_CONFIG_NAME, OK_EMOJI, BAD_EMOJI SHORT_HELP = "Show your current context" @@ -31,11 +32,35 @@ def test_host(host): @click.command(name="context", short_help=SHORT_HELP) -def estela_command(): +@click.argument("name", required=False) +def estela_command(name): + click.echo(f"Checking your current context for {name}...") host = get_host_from_env() username = get_username_from_env() password = get_password_from_env() + if name: + click.echo(f"Checking context {name}...") + estela_config = get_estela_config() + + if estela_config is None or estela_config.get(name, None) is None: + click.echo( + "Context {} not found.\nInitializing context...".format(name) + ) + + else: + click.echo( + "Context {} found.".format(name) + ) + + try: + test_host(estela_config[name]["host"]) + click.echo(OK_HOST.format(estela_config[name]["host"])) + except: + click.echo(BAD_HOST) + return + return + if host is None or username is None or password is None: estela_auth = get_estela_auth() diff --git a/estela_cli/templates.py b/estela_cli/templates.py index 6b2d4b0..eb8034b 100644 --- a/estela_cli/templates.py +++ b/estela_cli/templates.py @@ -1,6 +1,7 @@ # Auth templates ESTELA_AUTH_NAME = ".estela.yaml" +ESTELA_CONFIG_NAME = ".estela-config.yaml" ESTELA_AUTH = """\ token: $estela_token diff --git a/estela_cli/utils.py b/estela_cli/utils.py index 682c374..7c1ca37 100644 --- a/estela_cli/utils.py +++ b/estela_cli/utils.py @@ -6,6 +6,7 @@ from datetime import datetime from estela_cli.templates import ( + ESTELA_CONFIG_NAME, ESTELA_AUTH_NAME, ESTELA_YAML_NAME, ESTELA_DIR, @@ -68,6 +69,21 @@ def get_estela_auth(): return estela_auth +def get_estela_config(): + home_path = get_home_path() + estela_config_path = os.path.join(home_path, ESTELA_CONFIG_NAME) + + if not os.path.exists(estela_config_path): + file = open(estela_config_path, "x") + file.close() + return None + + with open(estela_config_path, "r") as estela_config_yaml: + estela_config = yaml.full_load(estela_config_yaml) + + return estela_config + + def format_time(date): date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ") return date.strftime("%Y-%m-%d %H:%M") From 101942814ad613eb734c28cd72bf9a7b005face7 Mon Sep 17 00:00:00 2001 From: Raymond Negron Date: Mon, 4 Sep 2023 12:57:58 -0500 Subject: [PATCH 2/3] get context variables to set in config_file --- estela_cli/context.py | 53 +++++++++++++++++++++++++++++++++++-------- estela_cli/utils.py | 11 ++++++--- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/estela_cli/context.py b/estela_cli/context.py index c7567db..b3f5eab 100644 --- a/estela_cli/context.py +++ b/estela_cli/context.py @@ -1,14 +1,19 @@ +import os import click import requests +from getpass import getpass from estela_cli.login import env_login, yaml_login +from estela_cli.estela_client import EstelaClient from estela_cli.utils import ( + get_home_path, get_estela_auth, get_estela_settings, get_estela_config, get_host_from_env, get_username_from_env, get_password_from_env, + get_estela_config_path, ) from estela_cli.templates import ESTELA_AUTH_NAME, ESTELA_CONFIG_NAME, OK_EMOJI, BAD_EMOJI @@ -31,6 +36,37 @@ def test_host(host): assert "projects" in response.json() +def prompt_context(name, username=None, password=None, host=None): + try: + if host is None: + host = click.prompt("Host") + if username is None: + username = click.prompt("Username") + if password is None: + password = getpass() + + estela_config_path = get_estela_config_path() + estela_config = get_estela_config() + estela_config["name"] = { + "host": host, + "username": username, + "password": password, + } + with open(estela_config_path, "w") as estela_config_yaml: + estela_config_yaml.write(estela_config) + click.echo( + "Successful login. Context {} stored in ~/{}.".format( + name, ESTELA_CONFIG_NAME + ) + ) + estela_client = EstelaClient(host, username, password) + + except: + raise Exception("Unable to login with provided credentials.") + + return estela_client + + @click.command(name="context", short_help=SHORT_HELP) @click.argument("name", required=False) def estela_command(name): @@ -42,23 +78,20 @@ def estela_command(name): if name: click.echo(f"Checking context {name}...") estela_config = get_estela_config() - - if estela_config is None or estela_config.get(name, None) is None: - click.echo( - "Context {} not found.\nInitializing context...".format(name) - ) - - else: - click.echo( - "Context {} found.".format(name) - ) + if estela_config and estela_config.get(name, None): + click.echo("Context {} found.".format(name)) try: test_host(estela_config[name]["host"]) click.echo(OK_HOST.format(estela_config[name]["host"])) except: click.echo(BAD_HOST) return + else: + click.echo( + "Context {} not found.\nInitializing context...".format(name) + ) + estela_client = prompt_context(name, username, password, host) return if host is None or username is None or password is None: diff --git a/estela_cli/utils.py b/estela_cli/utils.py index 7c1ca37..df8acc4 100644 --- a/estela_cli/utils.py +++ b/estela_cli/utils.py @@ -69,18 +69,23 @@ def get_estela_auth(): return estela_auth -def get_estela_config(): +def get_estela_config_path(): home_path = get_home_path() estela_config_path = os.path.join(home_path, ESTELA_CONFIG_NAME) + return estela_config_path + + +def get_estela_config(): + estela_config_path = get_estela_config_path() if not os.path.exists(estela_config_path): file = open(estela_config_path, "x") file.close() - return None + return {} with open(estela_config_path, "r") as estela_config_yaml: estela_config = yaml.full_load(estela_config_yaml) - + return estela_config From 76cb4fd1aae4617ec859815dbea5d6e3d04a3f0a Mon Sep 17 00:00:00 2001 From: Raymond Negron Date: Mon, 4 Sep 2023 16:38:36 -0500 Subject: [PATCH 3/3] Update .estela/yaml every switching context --- estela_cli/context.py | 21 +++++++++++++++------ estela_cli/login.py | 22 +++++----------------- estela_cli/utils.py | 25 ++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/estela_cli/context.py b/estela_cli/context.py index b3f5eab..1f7c488 100644 --- a/estela_cli/context.py +++ b/estela_cli/context.py @@ -1,4 +1,4 @@ -import os +import yaml import click import requests @@ -6,8 +6,8 @@ from estela_cli.login import env_login, yaml_login from estela_cli.estela_client import EstelaClient from estela_cli.utils import ( - get_home_path, get_estela_auth, + update_estela_auth, get_estela_settings, get_estela_config, get_host_from_env, @@ -47,13 +47,17 @@ def prompt_context(name, username=None, password=None, host=None): estela_config_path = get_estela_config_path() estela_config = get_estela_config() - estela_config["name"] = { + + if estela_config is None: + estela_config = {} + + estela_config[name] = { "host": host, "username": username, "password": password, } with open(estela_config_path, "w") as estela_config_yaml: - estela_config_yaml.write(estela_config) + yaml.dump(estela_config, estela_config_yaml) click.echo( "Successful login. Context {} stored in ~/{}.".format( name, ESTELA_CONFIG_NAME @@ -70,13 +74,12 @@ def prompt_context(name, username=None, password=None, host=None): @click.command(name="context", short_help=SHORT_HELP) @click.argument("name", required=False) def estela_command(name): - click.echo(f"Checking your current context for {name}...") host = get_host_from_env() username = get_username_from_env() password = get_password_from_env() if name: - click.echo(f"Checking context {name}...") + click.echo(f"Checking your current context for {name}...") estela_config = get_estela_config() if estela_config and estela_config.get(name, None): @@ -84,6 +87,11 @@ def estela_command(name): try: test_host(estela_config[name]["host"]) click.echo(OK_HOST.format(estela_config[name]["host"])) + estela_client = EstelaClient( + estela_config[name]["host"], + estela_config[name]["username"], + estela_config[name]["password"], + ) except: click.echo(BAD_HOST) return @@ -92,6 +100,7 @@ def estela_command(name): "Context {} not found.\nInitializing context...".format(name) ) estela_client = prompt_context(name, username, password, host) + update_estela_auth(estela_client.host, estela_client.token) return if host is None or username is None or password is None: diff --git a/estela_cli/login.py b/estela_cli/login.py index 15c6277..180d95e 100644 --- a/estela_cli/login.py +++ b/estela_cli/login.py @@ -2,16 +2,16 @@ import click from getpass import getpass -from string import Template from estela_cli.estela_client import EstelaClient from estela_cli.utils import ( get_host_from_env, get_username_from_env, get_password_from_env, get_estela_auth, - get_home_path, + update_estela_auth, + get_estela_auth_path, ) -from estela_cli.templates import ESTELA_AUTH_NAME, ESTELA_AUTH +from estela_cli.templates import ESTELA_AUTH_NAME DEFAULT_ESTELA_API_HOST = "http://localhost" @@ -109,8 +109,7 @@ def login(username=None, password=None, host=None): help="API endpoint to send the requests. If host is not given, it will be asked", ) def estela_command(username, password, host): - home_path = get_home_path() - estela_auth_path = os.path.join(home_path, ESTELA_AUTH_NAME) + estela_auth_path = get_estela_auth_path() if os.path.exists(estela_auth_path): raise click.ClickException( @@ -122,15 +121,4 @@ def estela_command(username, password, host): except Exception as ex: raise click.ClickException(str(ex)) - template = Template(ESTELA_AUTH) - values = { - "estela_host": estela_client.host, - "estela_token": estela_client.token, - } - result = template.substitute(values) - - with open(estela_auth_path, "w") as estela_auth_yaml: - estela_auth_yaml.write(result) - click.echo( - "Successful login. API Token stored in ~/{}.".format(ESTELA_AUTH_NAME) - ) + update_estela_auth(estela_client.host, estela_client.token) diff --git a/estela_cli/utils.py b/estela_cli/utils.py index df8acc4..5ad060b 100644 --- a/estela_cli/utils.py +++ b/estela_cli/utils.py @@ -4,11 +4,13 @@ import json import click +from string import Template from datetime import datetime from estela_cli.templates import ( ESTELA_CONFIG_NAME, ESTELA_AUTH_NAME, ESTELA_YAML_NAME, + ESTELA_AUTH, ESTELA_DIR, DATA_DIR, DOCKERFILE_NAME, @@ -56,9 +58,14 @@ def get_estela_settings(): return estela_config -def get_estela_auth(): +def get_estela_auth_path(): home_path = get_home_path() estela_auth_path = os.path.join(home_path, ESTELA_AUTH_NAME) + return estela_auth_path + + +def get_estela_auth(): + estela_auth_path = get_estela_auth_path() if not os.path.exists(estela_auth_path): return None @@ -69,6 +76,22 @@ def get_estela_auth(): return estela_auth +def update_estela_auth(host, token): + estela_auth_path = get_estela_auth_path() + template = Template(ESTELA_AUTH) + values = { + "estela_host": host, + "estela_token": token, + } + result = template.substitute(values) + + with open(estela_auth_path, "w") as estela_auth_yaml: + estela_auth_yaml.write(result) + click.echo( + "Successful login. API Token stored in ~/{}.".format(ESTELA_AUTH_NAME) + ) + + def get_estela_config_path(): home_path = get_home_path() estela_config_path = os.path.join(home_path, ESTELA_CONFIG_NAME)