Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion getdeck/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import argparse
import logging
import os
import traceback


os.environ["PYOXIDIZER"] = "1"
Expand Down Expand Up @@ -161,7 +162,9 @@ def main():
parser.print_help()
exit(0)
except Exception as e:
logger.fatal(f"There was an error running Deck: {e}")
if args.debug:
traceback.print_exc()
logger.fatal(f"There was an error running deck: {e}")
exit(1)


Expand Down
12 changes: 9 additions & 3 deletions getdeck/api/get.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import shutil
from typing import Callable

from getdeck.api import stopwatch, remove
Expand Down Expand Up @@ -28,8 +29,9 @@ def run_deck( # noqa: C901
if progress_callback:
progress_callback(0)

deckfile = read_deckfile_from_location(deckfile_location, config)

deckfile, working_dir_path, is_temp_dir = read_deckfile_from_location(
deckfile_location, config
)
if progress_callback:
progress_callback(5)
#
Expand All @@ -52,7 +54,9 @@ def run_deck( # noqa: C901
# 2. generate the Deck's workload
#
try:
generated_deck = prepare_k8s_workload_for_deck(config, deckfile, deck_name)
generated_deck = prepare_k8s_workload_for_deck(
config, deckfile, deck_name, working_dir_path
)
except Exception as e:
if cluster_created:
# remove this just created cluster as it probably is in an inconsistent state from the beginning
Expand Down Expand Up @@ -120,6 +124,8 @@ def run_deck( # noqa: C901
if notes := deckfile.get_deck(deck_name).notes:
logger.info(notes)

if is_temp_dir:
shutil.rmtree(working_dir_path)
if wait:
_wait_ready(config, generated_deck, timeout)
return True
Expand Down
57 changes: 33 additions & 24 deletions getdeck/api/hosts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import shutil
import socket

from python_hosts import Hosts, HostsEntry
Expand All @@ -17,35 +18,43 @@ def run_hosts(
deck_name: str = None,
config=default_configuration,
) -> bool:
deckfile = read_deckfile_from_location(deckfile_location, config)
deckfile, working_dir_path, is_temp_dir = read_deckfile_from_location(
deckfile_location, config
)
deck = deckfile.get_deck(deck_name)
deck_hosts = deck.hosts

hosts = Hosts()
if host_action == "list":

logger.info("Ingress hosts:")
for host in deck_hosts:
logger.info(f"{host}")

elif host_action == "remove":
logger.debug("Removing hosts from hosts file...")
for host in deck_hosts:
hosts.remove_all_matching(name=host)
hosts.write()
logger.info("Hosts have been removed from hosts file...")

elif host_action == "write":

logger.info("Writing hosts to hosts file...")
new_entry = HostsEntry(entry_type="ipv4", address="127.0.0.1", names=deck_hosts)
hosts.add([new_entry])
hosts.write()
logger.info("Hosts should resolve to '127.0.0.1' now.")

if deck_hosts:
if host_action == "list":

logger.info("Ingress hosts:")
for host in deck_hosts:
logger.info(f"{host}")

elif host_action == "remove":
logger.debug("Removing hosts from hosts file...")
for host in deck_hosts:
hosts.remove_all_matching(name=host)
hosts.write()
logger.info("Hosts have been removed from hosts file...")

elif host_action == "write":

logger.info("Writing hosts to hosts file...")
new_entry = HostsEntry(
entry_type="ipv4", address="127.0.0.1", names=deck_hosts
)
hosts.add([new_entry])
hosts.write()
logger.info("Hosts should resolve to '127.0.0.1' now.")

else:
logger.error(f"Unknown host action '{host_action}'")
else:
logger.error(f"Unknown host action '{host_action}'")

logger.info("No hosts specified in Deckfile")
if is_temp_dir:
shutil.rmtree(working_dir_path)
return True


Expand Down
7 changes: 6 additions & 1 deletion getdeck/api/list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import shutil
from typing import List

from getdeck.api.utils import stopwatch
Expand All @@ -11,7 +12,11 @@
def get_available_decks(deckfile_location: str, config=default_configuration) -> List:
from getdeck.utils import read_deckfile_from_location

deckfile = read_deckfile_from_location(deckfile_location, config)
deckfile, working_dir_path, is_temp_dir = read_deckfile_from_location(
deckfile_location, config
)
available_decks = deckfile.get_decks()
logger.debug(available_decks)
if is_temp_dir:
shutil.rmtree(working_dir_path)
return available_decks
18 changes: 15 additions & 3 deletions getdeck/api/remove.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import shutil
from typing import Callable

from getdeck.configuration import default_configuration
Expand All @@ -16,12 +17,16 @@ def remove_cluster(
) -> bool:
from getdeck.utils import read_deckfile_from_location, ensure_cluster

deckfile = read_deckfile_from_location(deckfile_location, config)
deckfile, working_dir_path, is_temp_dir = read_deckfile_from_location(
deckfile_location, config
)
k8s_provider = ensure_cluster(deckfile, config, ignore_cluster, do_install=False)
if k8s_provider.exists():
k8s_provider.delete()
else:
logger.info("Cluster does not exist")
if is_temp_dir:
shutil.rmtree(working_dir_path)
return True


Expand All @@ -40,7 +45,9 @@ def remove_deck(
from getdeck.k8s import k8s_delete_object
from getdeck.sources.utils import prepare_k8s_workload_for_deck

deckfile = read_deckfile_from_location(deckfile_location, config)
deckfile, working_dir_path, is_temp_dir = read_deckfile_from_location(
deckfile_location, config
)
if progress_callback:
progress_callback(10)
k8s_provider = ensure_cluster(deckfile, config, ignore_cluster, do_install=False)
Expand All @@ -49,7 +56,9 @@ def remove_deck(

config.kubeconfig = k8s_provider.get_kubeconfig()
if k8s_provider.exists():
generated_deck = prepare_k8s_workload_for_deck(config, deckfile, deck_name)
generated_deck = prepare_k8s_workload_for_deck(
config, deckfile, deck_name, working_dir_path
)
logger.info(f"Removing Deck {generated_deck.name}")
if progress_callback:
progress_callback(30)
Expand All @@ -73,4 +82,7 @@ def remove_deck(
logger.info(f"All workloads from Deck {generated_deck.name} removed")
else:
logger.info("Cluster does not exist")

if is_temp_dir:
shutil.rmtree(working_dir_path)
return True
7 changes: 6 additions & 1 deletion getdeck/api/stop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import shutil
from typing import Callable

from getdeck.api import stopwatch
Expand All @@ -16,7 +17,11 @@ def stop_cluster(
) -> bool:
from getdeck.utils import read_deckfile_from_location, ensure_cluster

deckfile = read_deckfile_from_location(deckfile_location, config)
deckfile, working_dir_path, is_temp_dir = read_deckfile_from_location(
deckfile_location, config
)
k8s_provider = ensure_cluster(deckfile, config, ignore_cluster, do_install=False)
logger.info("Stopping cluster")
if is_temp_dir:
shutil.rmtree(working_dir_path)
k8s_provider.stop()
2 changes: 2 additions & 0 deletions getdeck/sources/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ def __init__(
source: Union[DeckfileFileSource, DeckfileKustomizeSource, DeckfileHelmSource],
config: ClientConfiguration,
namespace: str,
working_dir: str = None,
):
self.path = path
self.source = source
self.config = config
self.namespace = namespace
self.working_dir = working_dir

@property
def not_supported_message(self):
Expand Down
15 changes: 10 additions & 5 deletions getdeck/sources/file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import os
from pathlib import PurePath
import tempfile
from typing import List

Expand Down Expand Up @@ -55,7 +54,9 @@ def _parse_source_directory(self, ref: str) -> List[K8sSourceFile]:
k8s_workload_files = self._parse_source_files(refs=refs)
return k8s_workload_files

def _parse_source(self, ref: str) -> List[K8sSourceFile]:
def _parse_source(self, ref: str, working_dir: str = None) -> List[K8sSourceFile]:
if working_dir:
ref = os.path.join(working_dir, ref.removeprefix("./"))
if os.path.isdir(ref):
k8s_workload_files = self._parse_source_directory(
ref=ref,
Expand Down Expand Up @@ -96,9 +97,13 @@ def fetch_https(self, **kwargs):
def fetch_local(self, **kwargs):
try:
logger.debug(f"Reading file {self.source.ref}")
ref = str(PurePath(os.path.join(self.path, self.source.ref)))

k8s_workload_files = self._parse_source(ref=ref)
if not os.path.isabs(self.source.ref):
fpath = os.path.join(
self.working_dir, self.source.ref.removeprefix("./")
)
k8s_workload_files = self._parse_source(ref=fpath)
else:
k8s_workload_files = self._parse_source(ref=self.source.ref)
return k8s_workload_files
except Exception as e:
logger.error(f"Error loading file from http {e}")
Expand Down
27 changes: 26 additions & 1 deletion getdeck/sources/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def k8s_api_version(self) -> str:
return f"{data['major']}.{data['minor']}"

def _helm_prep(self) -> List[str]:
if self.type == "git":
if self.type in ["git", "local"]:
return self._helm_dep_up()
else: # http(s)
return self._helm_repo_add()
Expand All @@ -67,6 +67,8 @@ def _helm_repo_add(self) -> List[str]:
return ["helm", "repo", "add", "this", self.source.ref]

def _helm_dep_up(self) -> List[str]:
if self.type == "local":
return ["helm", "dep", "up", self.source.ref]
return ["helm", "dep", "up", self.source.path]

def _helm_with_plugins(self) -> List[str]:
Expand All @@ -88,6 +90,29 @@ def _helm_template(self) -> List[str]:
["--values", os.path.join(self.source.path, _valuefile)]
)
return temp
elif self.type == "local":
temp = [
"template",
f"{self.source.releaseName}",
f"/sources/{self.source.ref.removeprefix('./')}",
"--include-crds",
"--namespace",
self.namespace,
]
if self.source.valueFiles:
for _valuefile in self.source.valueFiles:
temp.extend(
[
"--values",
os.path.join(
"/sources",
self.source.ref.removeprefix("./"),
self.source.path or "",
_valuefile.removeprefix("./"),
),
]
)
return temp
else: # http(s)
return [
"template",
Expand Down
21 changes: 20 additions & 1 deletion getdeck/sources/tooler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
import logging
import os
import shutil
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -121,7 +122,25 @@ def fetch_content(self, **kwargs) -> List[K8sSourceFile]:
raise NotImplementedError

def fetch_local(self, **kwargs):
raise NotImplementedError
cmd = self.build_command()
try:
if not os.path.isabs(self.source.ref):
path = os.path.join(
self.working_dir, self.source.ref.removeprefix("./")
)
self._parse_source(ref=path, working_dir=self.working_dir)
dst = os.path.join(
self.tmp_source.name, self.source.ref.removeprefix("./")
)
shutil.copytree(path, dst, dirs_exist_ok=True)
else:
self._parse_source(ref=self.source.ref)
dst = os.path.join(self.tmp_source.name, self.source.ref)
shutil.copytree(self.source.ref, dst, dirs_exist_ok=True)
self.run_tooler(cmd)
return self.collect_workload_files()
finally:
self.cleanup()

def fetch_remote(self, git=False):
cmd = self.build_command()
Expand Down
7 changes: 5 additions & 2 deletions getdeck/sources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@


def prepare_k8s_workload_for_deck(
config: ClientConfiguration, deckfile: Deckfile, deck_name: str
config: ClientConfiguration,
deckfile: Deckfile,
deck_name: str,
working_dir: str = None,
) -> GeneratedDeck:
deck = deckfile.get_deck(deck_name)
logger.debug(deck)
Expand Down Expand Up @@ -46,7 +49,7 @@ def prepare_k8s_workload_for_deck(
namespace = deck.namespace or "default"

fetcher_context.strategy = strategy(
deckfile.file_path, source, config, namespace
deckfile.file_path, source, config, namespace, working_dir
)

# fetch source files
Expand Down
Loading