Add pre/post script support

Former-commit-id: bb39d90522
This commit is contained in:
David Boreham 2023-02-17 15:31:43 -07:00
parent 912483df58
commit 46c22f4e4f

View File

@ -124,10 +124,10 @@ def command(ctx, include, exclude, cluster, command, extra_args):
if verbose: if verbose:
print(f"Running compose up for extra_args: {extra_args_list}") print(f"Running compose up for extra_args: {extra_args_list}")
for pre_start_command in pre_start_commands: for pre_start_command in pre_start_commands:
_run_command(ctx.obj, pre_start_command) _run_command(ctx.obj, cluster, pre_start_command)
docker.compose.up(detach=True, services=extra_args_list) docker.compose.up(detach=True, services=extra_args_list)
for post_start_command in post_start_commands: for post_start_command in post_start_commands:
_run_command(ctx.obj, post_start_command) _run_command(ctx.obj, cluster, post_start_command)
elif command == "down": elif command == "down":
if verbose: if verbose:
print("Running compose down") print("Running compose down")
@ -196,14 +196,17 @@ def _convert_to_new_format(old_pod_array):
return new_pod_array return new_pod_array
def _run_command(ctx, command): def _run_command(ctx, cluster_name, command):
if ctx.verbose: if ctx.verbose:
print(f"Running command: {command}") print(f"Running command: {command}")
command_dir = os.path.dirname(command) command_dir = os.path.dirname(command)
print(f"command_dir: {command_dir}") print(f"command_dir: {command_dir}")
command_file = os.path.join(".", os.path.basename(command)) command_file = os.path.join(".", os.path.basename(command))
my_env = os.environ.copy() command_env = os.environ.copy()
command_result = subprocess.run(f"bash {command_file}", shell=True, env=my_env, cwd=command_dir) command_env["CERC_SO_COMPOSE_PROJECT"] = cluster_name
if ctx.debug:
command_env["CERC_SCRIPT_DEBUG"] = "true"
command_result = subprocess.run(command_file, shell=True, env=command_env, cwd=command_dir)
if command_result.returncode != 0: if command_result.returncode != 0:
print(f"FATAL Error running command: {command}") print(f"FATAL Error running command: {command}")
sys.exit(1) sys.exit(1)