-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from jiri-otoupal/refactor-improvements
Refactor and improvements
- Loading branch information
Showing
21 changed files
with
673 additions
and
540 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import click | ||
import rich | ||
from InquirerPy import inquirer | ||
|
||
from abst.bastion_support.oci_bastion import Bastion | ||
from abst.cfg_func import __upgrade | ||
from abst.config import default_contexts_location | ||
from abst.utils.misc_funcs import setup_calls | ||
from abst.tools import get_context_path | ||
|
||
|
||
@click.group(help="Group of commands for operations with config") | ||
def config(): | ||
pass | ||
|
||
|
||
@config.command("generate", help="Will generate sample json and overwrite changes") | ||
@click.option("--debug", is_flag=True, default=False) | ||
@click.argument("context-name", default=None, required=False) | ||
def generate(debug, context_name): | ||
setup_calls(debug) | ||
|
||
path = get_context_path(context_name) | ||
|
||
Bastion.create_default_location() | ||
td = Bastion.generate_sample_dict() | ||
creds_path = Bastion.write_creds_json(td, path) | ||
print( | ||
f"Sample credentials generated, please fill 'creds.json' in {creds_path} with " | ||
f"your credentials for this to work, you can use 'abst json fill " | ||
f"{context_name if context_name else ''}'" | ||
) | ||
|
||
|
||
@config.command( | ||
"fill", help="Fills Json config with credentials you enter interactively" | ||
) | ||
@click.option("--debug", is_flag=True, default=False) | ||
@click.argument("context-name", default=None, required=False) | ||
def fill(debug, context_name): | ||
setup_calls(debug) | ||
|
||
path = get_context_path(context_name) | ||
|
||
if not path.exists(): | ||
rich.print("Generating sample Creds file") | ||
Bastion.create_default_location() | ||
td = Bastion.generate_sample_dict() | ||
Bastion.write_creds_json(td, path) | ||
|
||
if not default_contexts_location.exists(): | ||
rich.print("Generating contexts location") | ||
Bastion.create_default_location() | ||
|
||
rich.print(f"[green]Filling {str(path)}") | ||
rich.print("Please fill field one by one as displayed") | ||
n_dict = dict() | ||
|
||
creds_json_ro = Bastion.load_json(path) | ||
|
||
for key, value in creds_json_ro.items(): | ||
n_dict[key] = inquirer.text( | ||
message=f"{key.capitalize()}:", default=value | ||
).execute() | ||
rich.print("\n[red]New json looks like this:[/red]") | ||
rich.print_json(data=n_dict) | ||
if inquirer.confirm(message="Write New Json ?", default=False).execute(): | ||
Bastion.write_creds_json(n_dict, path) | ||
rich.print("[green]Wrote changes[/green]") | ||
else: | ||
rich.print("[red]Fill interrupted, nothing changed[/red]") | ||
|
||
|
||
@config.command( | ||
"locate", help="Locates Json config with credentials you enter interactively" | ||
) | ||
@click.option("--debug", is_flag=True, default=False) | ||
@click.argument("context-name", default=None, required=False) | ||
def locate(debug, context_name): | ||
setup_calls(debug) | ||
|
||
path = get_context_path(context_name) | ||
|
||
if path.exists(): | ||
rich.print(f"[green]Config file location: {path.absolute()}[/green]") | ||
else: | ||
rich.print( | ||
f"[red]Config does not exist yet, future location" | ||
f" {path.absolute()}[/red]" | ||
) | ||
|
||
|
||
@config.command( | ||
"upgrade", help="Locates Json config with credentials you enter interactively" | ||
) | ||
@click.option("--debug", is_flag=True, default=False) | ||
@click.argument("context-name", default=None, required=False) | ||
def upgrade(debug, context_name): | ||
setup_calls(debug) | ||
|
||
path = get_context_path(context_name) | ||
|
||
if not path.exists(): | ||
rich.print("[green]No config to upgrade[/green]") | ||
return | ||
|
||
__upgrade(context_name, path) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
import click | ||
import pyperclip | ||
import rich | ||
|
||
from abst.config import default_contexts_location, share_excluded_keys | ||
from abst.utils.misc_funcs import get_context_data, setup_calls | ||
|
||
|
||
@click.group(help="Contexts commands") | ||
def context(): | ||
pass | ||
|
||
|
||
@context.command("list", help="Will list all contexts in ~/.abst/context/ folder") | ||
@click.option("--debug", is_flag=True, default=False) | ||
def _list(debug=False): | ||
setup_calls(debug) | ||
rich.print("Contexts:") | ||
for file in Path(default_contexts_location).iterdir(): | ||
rich.print(f" {file.name.replace('.json', '')}") | ||
|
||
|
||
@context.command(help="Will display JSON format of context") | ||
@click.option("--debug", is_flag=True, default=False) | ||
@click.argument("name") | ||
def display(name, debug=False): | ||
setup_calls(debug) | ||
data = get_context_data(name) | ||
if data is None: | ||
return | ||
rich.print_json(data=data) | ||
|
||
|
||
@context.command(help="Will print context without local paths and put it in clipboard for sharing") | ||
@click.option("--debug", is_flag=True, default=False) | ||
@click.argument("name") | ||
def share(name, debug=False): | ||
setup_calls(debug) | ||
|
||
data = get_context_data(name) | ||
if data is None: | ||
return | ||
for key in share_excluded_keys: | ||
data.pop(key) | ||
data["default-name"] = "!YOUR NAME!" | ||
rich.print_json(data=data) | ||
logging.info("Data transmitted into clipboard") | ||
pyperclip.copy(str(data)) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os | ||
|
||
import click | ||
import rich | ||
|
||
from abst.utils.misc_funcs import setup_calls | ||
|
||
|
||
@click.group("cp", help="Copy commands for special usage") | ||
def cp(): | ||
pass | ||
|
||
|
||
@cp.command("login", help="Login to docker and helm registry") | ||
@click.argument("secret_name") | ||
@click.argument("source_namespace") | ||
@click.argument("target_namespace") | ||
@click.option("--debug", is_flag=True, default=False) | ||
def cp_secret( | ||
secret_name: str, | ||
target_namespace: str, | ||
source_namespace: str = "default", | ||
debug=False, | ||
): | ||
""" | ||
Copy Secret in current cluster from source namespace to target | ||
@param secret_name: Secret Name | ||
@param target_namespace: Target Namespace name | ||
@param source_namespace: Source Namespace name | ||
@return: | ||
:param debug: | ||
""" | ||
setup_calls(debug) | ||
try: | ||
rich.print("Trying Copy secret") | ||
os.system( | ||
f"kubectl get secret {secret_name} --namespace={source_namespace} -o yaml | sed " | ||
f"'s/namespace: .*/namespace: {target_namespace}/' | kubectl apply -f -" | ||
) | ||
except FileNotFoundError: | ||
rich.print("[red]kubectl not found on this machine[/red]") | ||
return |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from time import sleep | ||
|
||
import click | ||
|
||
from abst.bastion_support.oci_bastion import Bastion | ||
from abst.utils.misc_funcs import setup_calls, print_eligible | ||
|
||
|
||
@click.group(help="Group of commands for creating Bastion sessions") | ||
def create(): | ||
pass | ||
|
||
|
||
@create.command( | ||
"forward", | ||
help="Creates and connects to Bastion session indefinitely until terminated by user", | ||
) | ||
@click.option("--shell", is_flag=True, default=False) | ||
@click.option("--debug", is_flag=True, default=False) | ||
@click.argument("context-name", default=None, required=False) | ||
def fullauto_forward(shell, debug, context_name): | ||
"""Creates and connects to bastion sessions | ||
automatically until terminated""" | ||
|
||
setup_calls(debug) | ||
|
||
if context_name == "?": | ||
print_eligible("required only for Port Forward session") | ||
return | ||
|
||
if context_name is None: | ||
conf = Bastion.load_config() | ||
used_name = conf["used_context"] | ||
else: | ||
used_name = context_name | ||
|
||
while True: | ||
Bastion(used_name, region= | ||
Bastion.load_json(Bastion.get_creds_path_resolve(context_name)).get("region", | ||
None)).create_forward_loop( | ||
shell=shell) | ||
|
||
sleep(1) | ||
|
||
|
||
@create.command( | ||
"managed", | ||
help="Creates and connects to Bastion session indefinitely until terminated by user", | ||
) | ||
@click.option("--shell", is_flag=True, default=False) | ||
@click.option("--debug", is_flag=True, default=False) | ||
@click.argument("context-name", default=None, required=False) | ||
def fullauto_managed(shell, debug, context_name): | ||
"""Creates and connects to bastion sessions | ||
automatically until terminated""" | ||
setup_calls(debug) | ||
|
||
if context_name == "?": | ||
print_eligible("required only for Managed SSH session") | ||
return | ||
|
||
if context_name is None: | ||
conf = Bastion.load_config() | ||
used_name = conf["used_context"] | ||
else: | ||
used_name = context_name | ||
|
||
while True: | ||
Bastion(used_name, region= | ||
Bastion.load_json(Bastion.get_creds_path_resolve(context_name)).get("region", | ||
None)).create_managed_loop( | ||
shell=shell) | ||
|
||
sleep(1) |
Empty file.
Oops, something went wrong.