More logging for webapp deployments #755

Merged
telackey merged 1 commits from telackey/morelogging into main 2024-02-21 23:48:53 +00:00
2 changed files with 19 additions and 14 deletions
Showing only changes of commit 07f381d640 - Show all commits

View File

@ -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,

View File

@ -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):