From 8a38ef1badf9e67dc8a8cb02288db3a47b7c65bb Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Thu, 8 Aug 2024 11:38:27 +0200 Subject: [PATCH 01/12] Add kube command --- src/tasks_downstream.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/tasks_downstream.py b/src/tasks_downstream.py index 777ca96..2f03e74 100644 --- a/src/tasks_downstream.py +++ b/src/tasks_downstream.py @@ -764,6 +764,24 @@ def down(c, purge=False): c.run(cmd, pty=True) +@task() +def kube(c, command, namespace="default"): + """Run a kubectl command in the provided namespace.""" + if command == "shell": + cmd = "kubectl exec deployment/odoo-web -it -n {namespace} -- odoo shell --no-http" + elif command == "logs": + cmd = "kubectl logs -f deployment/odoo-web -n {namespace} --all-containers" + elif command == "bash": + cmd = "kubectl exec deployment/odoo-web -it -n {namespace} -- bash" + elif command == "upgradelog": + cmd = "kubectl logs -f job/odoo-upgrade -n {namespace}" + elif command == "pgactivity": + cmd = "kubectl exec deployment/odoo-web -it -n {namespace} -- pg_activity" + else: + raise exceptions.PlatformError(f"Command {command} not found.") + c.run(cmd, pty=True) + + @task() def preparedb(c, database=False): """ From c1ecc4644879064168831b9e2a875ae8aa1baae0 Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Thu, 8 Aug 2024 12:05:56 +0200 Subject: [PATCH 02/12] Better version ;) --- src/tasks_downstream.py | 48 ++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/tasks_downstream.py b/src/tasks_downstream.py index 2f03e74..f24b141 100644 --- a/src/tasks_downstream.py +++ b/src/tasks_downstream.py @@ -764,21 +764,53 @@ def down(c, purge=False): c.run(cmd, pty=True) -@task() -def kube(c, command, namespace="default"): +@task( + help={ + "command": "Command to run in the container. " + "Options: ['shell', 'logs', 'bash', 'upgradelog', 'pgactivity']", + } +) +def kube(c, command, namespace="none"): """Run a kubectl command in the provided namespace.""" + + yaml_exists = os.path.exists(PROJECT_ROOT / "project.yaml") + + # Fetch the namespace from a project.yaml file + if namespace == "none": + if yaml_exists: + namespace = yaml.safe_load((PROJECT_ROOT / "project.yaml").read_text())[ + "namespace" + ] + else: + _logger.error( + "Namespace not provided or found in project.yaml file. " + "Please provide a namespace.\n" + "Use: invoke kube {command} -n MyNamespace" + ) + return False + + # If the yaml file does not exist, create one with the namespace provided + if not yaml_exists: + namespace = namespace.strip() + with open(PROJECT_ROOT / "project.yaml", "w") as f: + f.write(f"namespace: {namespace}") + if command == "shell": - cmd = "kubectl exec deployment/odoo-web -it -n {namespace} -- odoo shell --no-http" + cmd = f"kubectl exec deployment/odoo-web -it -n {namespace} -- odoo shell --no-http" elif command == "logs": - cmd = "kubectl logs -f deployment/odoo-web -n {namespace} --all-containers" + cmd = f"kubectl logs -f deployment/odoo-web -n {namespace} --all-containers" elif command == "bash": - cmd = "kubectl exec deployment/odoo-web -it -n {namespace} -- bash" + cmd = f"kubectl exec deployment/odoo-web -it -n {namespace} -- bash" elif command == "upgradelog": - cmd = "kubectl logs -f job/odoo-upgrade -n {namespace}" + cmd = f"kubectl logs -f job/odoo-upgrade -n {namespace}" elif command == "pgactivity": - cmd = "kubectl exec deployment/odoo-web -it -n {namespace} -- pg_activity" + cmd = f"kubectl exec deployment/odoo-web -it -n {namespace} -- pg_activity" else: - raise exceptions.PlatformError(f"Command {command} not found.") + _logger.error( + f"Command {command} not found.\n" + "Options: ['shell', 'logs', 'bash', 'upgradelog', 'pgactivity']" + ) + return c.run(cmd, pty=True) From c29d2c68d72f5b2a6b8807c64881270f8d1dbb33 Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Thu, 8 Aug 2024 17:17:08 +0200 Subject: [PATCH 03/12] Update src/tasks_downstream.py Co-authored-by: Karl --- src/tasks_downstream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks_downstream.py b/src/tasks_downstream.py index f24b141..aeddc60 100644 --- a/src/tasks_downstream.py +++ b/src/tasks_downstream.py @@ -773,7 +773,7 @@ def down(c, purge=False): def kube(c, command, namespace="none"): """Run a kubectl command in the provided namespace.""" - yaml_exists = os.path.exists(PROJECT_ROOT / "project.yaml") + yaml_exists = os.path.exists(PROJECT_ROOT / ".glo.yaml") # Fetch the namespace from a project.yaml file if namespace == "none": From 574db103c1784005bb69fe263c7c83d8fd2b4dde Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Thu, 8 Aug 2024 17:19:04 +0200 Subject: [PATCH 04/12] Change file name --- src/tasks_downstream.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tasks_downstream.py b/src/tasks_downstream.py index aeddc60..b36a27f 100644 --- a/src/tasks_downstream.py +++ b/src/tasks_downstream.py @@ -775,15 +775,15 @@ def kube(c, command, namespace="none"): yaml_exists = os.path.exists(PROJECT_ROOT / ".glo.yaml") - # Fetch the namespace from a project.yaml file + # Fetch the namespace from a .glo.yaml file if namespace == "none": if yaml_exists: - namespace = yaml.safe_load((PROJECT_ROOT / "project.yaml").read_text())[ + namespace = yaml.safe_load((PROJECT_ROOT / ".glo.yaml").read_text())[ "namespace" ] else: _logger.error( - "Namespace not provided or found in project.yaml file. " + "Namespace not provided or found in .glo.yaml file. " "Please provide a namespace.\n" "Use: invoke kube {command} -n MyNamespace" ) From acfd07d6aa146072e79b5a76f329d865776f927f Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Fri, 9 Aug 2024 10:48:23 +0200 Subject: [PATCH 05/12] fix file mismatch --- src/tasks_downstream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks_downstream.py b/src/tasks_downstream.py index b36a27f..92111b2 100644 --- a/src/tasks_downstream.py +++ b/src/tasks_downstream.py @@ -792,7 +792,7 @@ def kube(c, command, namespace="none"): # If the yaml file does not exist, create one with the namespace provided if not yaml_exists: namespace = namespace.strip() - with open(PROJECT_ROOT / "project.yaml", "w") as f: + with open(PROJECT_ROOT / ".glo.yaml", "w") as f: f.write(f"namespace: {namespace}") if command == "shell": From dd7acc4466a3c001b48875738658e923ee8548aa Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Fri, 9 Aug 2024 11:10:37 +0200 Subject: [PATCH 06/12] Add inquirer library to provisioning --- guides/provision.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/guides/provision.sh b/guides/provision.sh index f94ab56..faf4a81 100644 --- a/guides/provision.sh +++ b/guides/provision.sh @@ -168,6 +168,7 @@ install_pipx() { ~/.local/bin/pipx install copier ~/.local/bin/pipx install invoke ~/.local/bin/pipx install pre-commit + ~/.local/bin/pipx install inquirer grep -qxF 'export PATH=$PATH:~/.local/bin/' ~/.bashrc || echo 'export PATH=$PATH:~/.local/bin/' >> ~/.bashrc From 1163b1a553465611065cced2fbca9798bace091c Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Fri, 9 Aug 2024 11:12:14 +0200 Subject: [PATCH 07/12] Inquirer is not available for pipx --- guides/provision.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guides/provision.sh b/guides/provision.sh index faf4a81..5f46c61 100644 --- a/guides/provision.sh +++ b/guides/provision.sh @@ -168,7 +168,7 @@ install_pipx() { ~/.local/bin/pipx install copier ~/.local/bin/pipx install invoke ~/.local/bin/pipx install pre-commit - ~/.local/bin/pipx install inquirer + python3 -m pip install --user inquirer grep -qxF 'export PATH=$PATH:~/.local/bin/' ~/.bashrc || echo 'export PATH=$PATH:~/.local/bin/' >> ~/.bashrc From 84b5d2ece6a5f6093d272f46ee2e5ccb5f3e5599 Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Fri, 9 Aug 2024 12:32:39 +0200 Subject: [PATCH 08/12] Nope --- guides/provision.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/guides/provision.sh b/guides/provision.sh index 5f46c61..f94ab56 100644 --- a/guides/provision.sh +++ b/guides/provision.sh @@ -168,7 +168,6 @@ install_pipx() { ~/.local/bin/pipx install copier ~/.local/bin/pipx install invoke ~/.local/bin/pipx install pre-commit - python3 -m pip install --user inquirer grep -qxF 'export PATH=$PATH:~/.local/bin/' ~/.bashrc || echo 'export PATH=$PATH:~/.local/bin/' >> ~/.bashrc From 2b760c1b27fe3133c2826e34723017f1226026c6 Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Fri, 9 Aug 2024 12:47:00 +0200 Subject: [PATCH 09/12] Allow for multiple namespaces Had to use warning for logging as info doesn't show up --- src/tasks_downstream.py | 65 +++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/src/tasks_downstream.py b/src/tasks_downstream.py index 92111b2..fc0a7bc 100644 --- a/src/tasks_downstream.py +++ b/src/tasks_downstream.py @@ -767,20 +767,45 @@ def down(c, purge=False): @task( help={ "command": "Command to run in the container. " - "Options: ['shell', 'logs', 'bash', 'upgradelog', 'pgactivity']", + "Options: ['shell', 'logs', 'bash', 'upgradelog', 'pgactivity', 'removens']", } ) def kube(c, command, namespace="none"): """Run a kubectl command in the provided namespace.""" + namespace = namespace.strip() + command = command.strip().lower() yaml_exists = os.path.exists(PROJECT_ROOT / ".glo.yaml") + if not yaml_exists: + # Create empty yaml file + with open(PROJECT_ROOT / ".glo.yaml", "w") as f: + f.write("namespaces: []\n") + + namespaces = yaml.safe_load((PROJECT_ROOT / ".glo.yaml").read_text()).get( + "namespaces", [] + ) # Fetch the namespace from a .glo.yaml file if namespace == "none": - if yaml_exists: - namespace = yaml.safe_load((PROJECT_ROOT / ".glo.yaml").read_text())[ - "namespace" - ] + if len(namespaces) == 1: + namespace = namespaces[0] + elif len(namespaces) > 1: + _logger.warning( + "Select a namespace: (use -n {MyNamespace} to add a new one)" + ) + counter = 0 + for namespace in namespaces: + _logger.warning(f" [{counter}] {namespace}") + counter += 1 + response = input("Default [0]: ") + if not response: + namespace = namespaces[0] + else: + try: + namespace = namespaces[int(response)] + except IndexError: + _logger.error("Namespace not found. Please try again.") + return False else: _logger.error( "Namespace not provided or found in .glo.yaml file. " @@ -788,15 +813,20 @@ def kube(c, command, namespace="none"): "Use: invoke kube {command} -n MyNamespace" ) return False - - # If the yaml file does not exist, create one with the namespace provided - if not yaml_exists: - namespace = namespace.strip() - with open(PROJECT_ROOT / ".glo.yaml", "w") as f: - f.write(f"namespace: {namespace}") + else: + if namespace not in namespaces: + # Add new namespace to the .glo.yaml file + namespaces.append(namespace) + yaml.safe_dump( + {"namespaces": namespaces}, + (PROJECT_ROOT / ".glo.yaml").open("w"), + ) if command == "shell": - cmd = f"kubectl exec deployment/odoo-web -it -n {namespace} -- odoo shell --no-http" + cmd = ( + f"kubectl exec deployment/odoo-web -it -n {namespace}" + " -- odoo shell --no-http" + ) elif command == "logs": cmd = f"kubectl logs -f deployment/odoo-web -n {namespace} --all-containers" elif command == "bash": @@ -805,6 +835,17 @@ def kube(c, command, namespace="none"): cmd = f"kubectl logs -f job/odoo-upgrade -n {namespace}" elif command == "pgactivity": cmd = f"kubectl exec deployment/odoo-web -it -n {namespace} -- pg_activity" + elif command == "removens": + if namespace in namespaces: + namespaces.remove(namespace) + yaml.safe_dump( + {"namespaces": namespaces}, + (PROJECT_ROOT / ".glo.yaml").open("w"), + ) + _logger.warning(f"Namespace {namespace} removed from .glo.yaml") + else: + _logger.error(f"Namespace {namespace} not found in .glo.yaml") + return else: _logger.error( f"Command {command} not found.\n" From 634d5fb823fdf42e6615c0f89bf83d7ef5198e05 Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Fri, 9 Aug 2024 12:57:16 +0200 Subject: [PATCH 10/12] Add support for db attribute in kube command --- src/tasks_downstream.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/tasks_downstream.py b/src/tasks_downstream.py index fc0a7bc..a610c99 100644 --- a/src/tasks_downstream.py +++ b/src/tasks_downstream.py @@ -770,18 +770,14 @@ def down(c, purge=False): "Options: ['shell', 'logs', 'bash', 'upgradelog', 'pgactivity', 'removens']", } ) -def kube(c, command, namespace="none"): +def kube(c, command, namespace="none", db="none"): """Run a kubectl command in the provided namespace.""" namespace = namespace.strip() command = command.strip().lower() + filename = ".glo.yaml" + check_make_yaml(filename) - yaml_exists = os.path.exists(PROJECT_ROOT / ".glo.yaml") - if not yaml_exists: - # Create empty yaml file - with open(PROJECT_ROOT / ".glo.yaml", "w") as f: - f.write("namespaces: []\n") - - namespaces = yaml.safe_load((PROJECT_ROOT / ".glo.yaml").read_text()).get( + namespaces = yaml.safe_load((PROJECT_ROOT / filename).read_text()).get( "namespaces", [] ) @@ -819,7 +815,7 @@ def kube(c, command, namespace="none"): namespaces.append(namespace) yaml.safe_dump( {"namespaces": namespaces}, - (PROJECT_ROOT / ".glo.yaml").open("w"), + (PROJECT_ROOT / filename).open("w"), ) if command == "shell": @@ -827,6 +823,8 @@ def kube(c, command, namespace="none"): f"kubectl exec deployment/odoo-web -it -n {namespace}" " -- odoo shell --no-http" ) + if db != "none": + cmd += f" -d {db}" elif command == "logs": cmd = f"kubectl logs -f deployment/odoo-web -n {namespace} --all-containers" elif command == "bash": @@ -840,11 +838,11 @@ def kube(c, command, namespace="none"): namespaces.remove(namespace) yaml.safe_dump( {"namespaces": namespaces}, - (PROJECT_ROOT / ".glo.yaml").open("w"), + (PROJECT_ROOT / filename).open("w"), ) - _logger.warning(f"Namespace {namespace} removed from .glo.yaml") + _logger.warning(f"Namespace {namespace} removed from {filename}") else: - _logger.error(f"Namespace {namespace} not found in .glo.yaml") + _logger.error(f"Namespace {namespace} not found in {filename}") return else: _logger.error( From 48fb69aeea365dadf7be476dd3bae265aa853d8c Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Fri, 9 Aug 2024 12:58:38 +0200 Subject: [PATCH 11/12] Missed a bit --- src/tasks_downstream.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/tasks_downstream.py b/src/tasks_downstream.py index a610c99..7d412ff 100644 --- a/src/tasks_downstream.py +++ b/src/tasks_downstream.py @@ -764,6 +764,14 @@ def down(c, purge=False): c.run(cmd, pty=True) +def check_make_yaml(filename): + yaml_exists = os.path.exists(PROJECT_ROOT / filename) + if not yaml_exists: + # Create empty yaml file + with open(PROJECT_ROOT / filename, "w") as f: + f.write("namespaces: []\n") + + @task( help={ "command": "Command to run in the container. " From a0cce1f128d79bfb47292bb8c700e2469503dff2 Mon Sep 17 00:00:00 2001 From: Elliott Bristow Date: Fri, 9 Aug 2024 13:33:03 +0200 Subject: [PATCH 12/12] Remove db flag... not needed --- src/tasks_downstream.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/tasks_downstream.py b/src/tasks_downstream.py index 7d412ff..489d545 100644 --- a/src/tasks_downstream.py +++ b/src/tasks_downstream.py @@ -778,7 +778,7 @@ def check_make_yaml(filename): "Options: ['shell', 'logs', 'bash', 'upgradelog', 'pgactivity', 'removens']", } ) -def kube(c, command, namespace="none", db="none"): +def kube(c, command, namespace=None): """Run a kubectl command in the provided namespace.""" namespace = namespace.strip() command = command.strip().lower() @@ -790,7 +790,7 @@ def kube(c, command, namespace="none", db="none"): ) # Fetch the namespace from a .glo.yaml file - if namespace == "none": + if namespace is None: if len(namespaces) == 1: namespace = namespaces[0] elif len(namespaces) > 1: @@ -831,8 +831,6 @@ def kube(c, command, namespace="none", db="none"): f"kubectl exec deployment/odoo-web -it -n {namespace}" " -- odoo shell --no-http" ) - if db != "none": - cmd += f" -d {db}" elif command == "logs": cmd = f"kubectl logs -f deployment/odoo-web -n {namespace} --all-containers" elif command == "bash":