From 08438b1cd5fcd3e6ec3554ce1af979a3f286b399 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Wed, 21 Feb 2024 23:48:52 +0000 Subject: [PATCH] More logging for webapp deployments (#755) Reviewed-on: https://git.vdb.to/cerc-io/stack-orchestrator/pulls/755 Co-authored-by: Thomas E Lackey Co-committed-by: Thomas E Lackey --- .../webapp/deploy_webapp_from_registry.py | 5 +++- stack_orchestrator/deploy/webapp/util.py | 28 ++++++++++--------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/stack_orchestrator/deploy/webapp/deploy_webapp_from_registry.py b/stack_orchestrator/deploy/webapp/deploy_webapp_from_registry.py index 19497749..15e89d9e 100644 --- a/stack_orchestrator/deploy/webapp/deploy_webapp_from_registry.py +++ b/stack_orchestrator/deploy/webapp/deploy_webapp_from_registry.py @@ -283,6 +283,7 @@ def command(ctx, kube_config, laconic_config, image_registry, deployment_parent_ dump_known_requests(state_file, [r], "DEPLOYING") status = "ERROR" run_log_file = None + run_reg_client = laconic try: run_id = f"{r.id}-{str(time.time()).split('.')[0]}-{str(uuid.uuid4()).split('-')[0]}" if log_dir: @@ -292,10 +293,12 @@ def command(ctx, kube_config, laconic_config, image_registry, deployment_parent_ run_log_file_path = os.path.join(run_log_dir, f"{run_id}.log") print(f"Directing deployment logs to: {run_log_file_path}") run_log_file = open(run_log_file_path, "wt") + run_reg_client = LaconicRegistryClient(laconic_config, log_file=run_log_file) + process_app_deployment_request( run_id, ctx, - laconic, + run_reg_client, r, record_namespace_deployments, record_namespace_dns, diff --git a/stack_orchestrator/deploy/webapp/util.py b/stack_orchestrator/deploy/webapp/util.py index e6fd9ffe..9dbc19c4 100644 --- a/stack_orchestrator/deploy/webapp/util.py +++ b/stack_orchestrator/deploy/webapp/util.py @@ -39,13 +39,14 @@ class AttrDict(dict): return v -def cmd(*vargs): +def logged_cmd(log_file, *vargs): try: - result = subprocess.run(vargs, capture_output=True) + print(" ".join(vargs), file=log_file) + result = subprocess.run(vargs, capture_output=True, stdout=log_file, stderr=log_file) result.check_returncode() return result.stdout.decode() except Exception as err: - print(result.stderr.decode()) + print(result.stderr.decode(), log_file) raise err @@ -58,8 +59,9 @@ def match_owner(recordA, *records): class LaconicRegistryClient: - def __init__(self, config_file): + def __init__(self, config_file, log_file=None): self.config_file = config_file + self.log_file = log_file self.cache = AttrDict( { "name_or_id": {}, @@ -77,7 +79,7 @@ class LaconicRegistryClient: args.append("--%s" % k) args.append(str(v)) - results = [AttrDict(r) for r in json.loads(cmd(*args))] + results = [AttrDict(r) for r in json.loads(logged_cmd(self.log_file, *args))] # Most recent records first results.sort(key=lambda r: r.createTime) @@ -115,7 +117,7 @@ class LaconicRegistryClient: args = ["laconic", "-c", self.config_file, "cns", "name", "resolve", name] - parsed = [AttrDict(r) for r in json.loads(cmd(*args))] + parsed = [AttrDict(r) for r in json.loads(logged_cmd(self.log_file, *args))] if parsed: self._add_to_cache(parsed) return parsed[0] @@ -145,7 +147,7 @@ class LaconicRegistryClient: name_or_id, ] - parsed = [AttrDict(r) for r in json.loads(cmd(*args))] + parsed = [AttrDict(r) for r in json.loads(logged_cmd(self.log_file, *args))] if len(parsed): self._add_to_cache(parsed) return parsed[0] @@ -173,22 +175,22 @@ class LaconicRegistryClient: record_file = open(record_fname, 'w') yaml.dump(record, record_file) record_file.close() - print(open(record_fname, 'r').read()) + print(open(record_fname, 'r').read(), file=self.log_file) new_record_id = json.loads( - cmd("laconic", "-c", self.config_file, "cns", "record", "publish", "--filename", record_fname) + logged_cmd(self.log_file, "laconic", "-c", self.config_file, "cns", "record", "publish", "--filename", record_fname) )["id"] for name in names: self.set_name(name, new_record_id) return new_record_id finally: - cmd("rm", "-rf", tmpdir) + logged_cmd("rm", "-rf", tmpdir) def set_name(self, name, record_id): - cmd("laconic", "-c", self.config_file, "cns", "name", "set", name, record_id) + logged_cmd(self.log_file, "laconic", "-c", self.config_file, "cns", "name", "set", name, record_id) def delete_name(self, name): - cmd("laconic", "-c", self.config_file, "cns", "name", "delete", name) + logged_cmd(self.log_file, "laconic", "-c", self.config_file, "cns", "name", "delete", name) def file_hash(filename): @@ -257,7 +259,7 @@ def build_container_image(app_record, tag, extra_build_args=[], log_file=None): result = subprocess.run(build_command, stdout=log_file, stderr=log_file) result.check_returncode() finally: - cmd("rm", "-rf", tmpdir) + logged_cmd(log_file, "rm", "-rf", tmpdir) def push_container_image(deployment_dir, log_file=None):