From 0c162ee3372073ee34fdeb31edcc39ae87270d6f Mon Sep 17 00:00:00 2001 From: David Shipman Date: Mon, 14 Aug 2023 12:17:03 +1000 Subject: [PATCH] Add auto-shutdown to springboard --- .../remote/springboard/launch.py | 30 ++++++++++++++++--- .../remote/springboard/scripting.py | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/autumn/infrastructure/remote/springboard/launch.py b/autumn/infrastructure/remote/springboard/launch.py index 1b37b5d56..1def41343 100644 --- a/autumn/infrastructure/remote/springboard/launch.py +++ b/autumn/infrastructure/remote/springboard/launch.py @@ -20,11 +20,30 @@ def launch_synced_autumn_task( task_spec: TaskSpec, mspec: EC2MachineSpec, run_path: str, - branch="master", - job_id=None, - set_alarm=True, - extra_commands=None, + branch: str = "master", + job_id: str = None, + set_alarm: bool = True, + extra_commands: list = None, + auto_shutdown_time: int = 4 * 60, # Time in minutes ) -> SpringboardTaskRunner: + """Launch a task on an EC2 instance + + Args: + task_spec: The task to run + mspec: The specifications of an EC2 instance to launch + run_path: The run_path (or 'run_id') of the launched job + branch: The branch of autumn to pull, or pass None if not using autumn + job_id: The EC2 instance descriptor; defaults to using run_path + set_alarm: Set a cloudwatch alarm to shutdown on inactivity. Defaults to True. + extra_commands: List of strings to add to bash script (will be run before the python task) + auto_shutdown_time: Time in minutes to automatically terminate this instance (defaults to 240; ie 4 hours) + + Raises: + Exception: run_path already exists + + Returns: + The active task runner + """ s3t = task.S3TaskManager(run_path) if s3t.exists(): raise Exception("Task already exists", run_path) @@ -41,6 +60,9 @@ def launch_synced_autumn_task( aws.set_cpu_termination_alarm(rinst["InstanceId"]) srunner = task.SpringboardTaskRunner(rinst, run_path) + + sdown_res = srunner.sshr.run(f"sudo shutdown -P +{auto_shutdown_time}") + script = scripting.gen_autumn_run_bash(run_path, branch, extra_commands=extra_commands) s3t._write_taskdata("taskscript.sh", script) diff --git a/autumn/infrastructure/remote/springboard/scripting.py b/autumn/infrastructure/remote/springboard/scripting.py index 90245b55d..8285fb6d4 100644 --- a/autumn/infrastructure/remote/springboard/scripting.py +++ b/autumn/infrastructure/remote/springboard/scripting.py @@ -9,7 +9,7 @@ def gen_autumn_run_bash( branch: str, shutdown: bool = True, bucket: PurePosixPath = PurePosixPath("autumn-data"), - extra_commands=None, + extra_commands: list = None, ) -> List[str]: define_dump_io = f""" write_ios_s3 () {{