Skip to content

Commit

Permalink
Merge pull request foundriesio#102 from doanac/sim-fix
Browse files Browse the repository at this point in the history
Minor improvements to the simulator
  • Loading branch information
doanac committed Oct 6, 2023
2 parents a206f8f + a8ede07 commit 2fd7eaa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
8 changes: 2 additions & 6 deletions jobserv/api/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,6 @@ def run_get_simulate_sh(proj, build_id, run):
rundef_str = rundef_str.replace("$", "\\$")
rundef_str = rundef_str.replace("\\", "\\\\")

volumes = ""
for vol in (rundef.get("shared-volumes") or {}).keys():
volumes += " -v " + vol + "=<TODO>"

script = """#!/bin/sh -e
SIMDIR="${{SIMDIR-/tmp/sim-run}}"
Expand All @@ -360,9 +356,9 @@ def run_get_simulate_sh(proj, build_id, run):
EIEIO
wget -O runner {runner}
PYTHONPATH=./runner python3 -m jobserv_runner.simulator -w `pwd` {volumes} rundef.json
PYTHONPATH=./runner python3 -m jobserv_runner.simulator -w `pwd` rundef.json
""".format(
rundef=rundef_str, volumes=volumes, runner=runner
rundef=rundef_str, runner=runner
)
return script, 200, {"Content-Type": "text/plain"}

Expand Down
37 changes: 34 additions & 3 deletions runner/jobserv_runner/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Author: Andy Doan <[email protected]>

import argparse
from base64 import b64encode
import importlib
import json
import os
Expand All @@ -15,7 +16,7 @@ def main(args):
m.handler.execute(args.worker_dir, args.runner_dir, args.rundef)


def _update_shared_volumes_mapping(volumes, rundef):
def _update_shared_volumes_mapping(worker_dir, volumes, rundef):
"""Convert rundef mappings:
name1: /path/in/container1
name2: /path/in/container2
Expand All @@ -36,10 +37,34 @@ def _update_shared_volumes_mapping(volumes, rundef):
host_path = volumes[name]
mapping[host_path] = container_path
except KeyError:
sys.exit(f"Please specify a shared volume path for: {name}")
volpath = os.path.join(worker_dir, "shared-volumes", name)
print(f"Shared volume not specified for: {name}. Default to: {volpath}")
try:
os.makedirs(volpath)
except FileExistsError:
pass
rundef["shared-volumes"] = mapping


def _handle_inputs(rundef_path, rundef):
inputs = rundef.get("simulator-inputs")
if not inputs:
return
for item in inputs:
value = input(item["prompt"])
for name, handler in item["secrets"].items():
transform = handler.get("transform")
if transform and transform == "BasicAuth":
encoded = b64encode(value.encode()).decode()
value = f"Authorization: basic {encoded}"
elif transform:
sys.exit(f"unknown secret transform for {item}")
rundef["secrets"][name] = value
del rundef["simulator-inputs"]
with open(rundef_path, "w") as f:
json.dump(rundef, f, indent=2)


def get_args(args=None):
parser = argparse.ArgumentParser(description="Execute a JobServ run definition")
parser.add_argument("-w", "--worker-dir", help="Location to store the run")
Expand All @@ -63,9 +88,15 @@ def get_args(args=None):
sys.exit(f"Invalid shared-volume: {k}. {v} does not exist")
vols[k] = v

rundef_path = args.rundef.name
args.rundef = json.load(args.rundef)
args.rundef["simulator"] = True
_update_shared_volumes_mapping(vols, args.rundef)
_update_shared_volumes_mapping(args.worker_dir, vols, args.rundef)
_handle_inputs(rundef_path, args.rundef)

for name, val in (args.rundef.get("secrets") or {}).items():
if val == "TODO":
sys.exit(f"Missing required secret value in run definition: {name}")

if not os.path.isdir(args.worker_dir):
sys.exit("worker-dir does not exist: " + args.worker_dir)
Expand Down

0 comments on commit 2fd7eaa

Please sign in to comment.