forked from foundriesio/fioctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request foundriesio#102 from doanac/sim-fix
Minor improvements to the simulator
- Loading branch information
Showing
2 changed files
with
36 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
# Author: Andy Doan <[email protected]> | ||
|
||
import argparse | ||
from base64 import b64encode | ||
import importlib | ||
import json | ||
import os | ||
|
@@ -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 | ||
|
@@ -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") | ||
|
@@ -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) | ||
|