Skip to content

Commit ee17b82

Browse files
committed
Improve error reporting
1 parent 51d00bd commit ee17b82

5 files changed

Lines changed: 43 additions & 33 deletions

File tree

python/mavis/server/aws.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import os
21
import json
32
import subprocess
43

4+
from .helpers import run_command
5+
56
REGION = "eu-west-2"
67
PRODUCTION_ENVS = {"production", "production-data-replication"}
78

@@ -138,11 +139,11 @@ def resolve_task(env, task_id=None, task_ip=None, service=None):
138139
)
139140

140141

141-
def run_command(
142-
env, task_id, command, container=None, interactive=True, replace_process=False
142+
def run_remote_command(
143+
env, task_id, remote_command, container=None, replace_process=False
143144
):
144145
"""Execute a command in an ECS task, returning the exit code."""
145-
cmd = [
146+
command = [
146147
"aws",
147148
"ecs",
148149
"execute-command",
@@ -153,15 +154,12 @@ def run_command(
153154
"--task",
154155
task_id,
155156
"--command",
156-
command,
157+
remote_command,
158+
"--interactive",
157159
]
158160
if container:
159-
cmd += ["--container", container]
160-
if interactive:
161-
cmd.append("--interactive")
162-
if replace_process:
163-
os.execvp(cmd[0], cmd)
164-
return subprocess.run(cmd).returncode
161+
command += ["--container", container]
162+
return run_command(command, replace_process=replace_process)
165163

166164

167165
# --- private helpers ---

python/mavis/server/get_file.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import os
22
import secrets
3-
import subprocess
43
import sys
54

65
from . import aws
7-
from .helpers import confirm_production
6+
from .helpers import confirm_production, run_command
87

98

109
def register(subparsers):
@@ -59,24 +58,23 @@ def run(args):
5958
local_dest = _local_destination(args.remote_path, args.local_path)
6059

6160
try:
62-
exit_code = aws.run_command(
61+
upload_result = aws.run_remote_command(
6362
env,
6463
task_id,
6564
f"aws s3 cp {args.remote_path} {s3_uri} --region {aws.REGION}",
6665
container=container,
6766
)
68-
if exit_code != 0:
67+
if not upload_result:
6968
sys.exit("Error: Failed to copy file from container to S3")
7069

71-
download = subprocess.run(
70+
download_result = run_command(
7271
["aws", "s3", "cp", s3_uri, local_dest, "--region", aws.REGION]
7372
)
74-
if download.returncode != 0:
75-
sys.exit("Error: Failed to download file from S3")
73+
if not download_result:
74+
sys.exit("Error: Download from S3 failed with code")
7675
finally:
77-
subprocess.run(
76+
run_command(
7877
["aws", "s3", "rm", s3_uri, "--region", aws.REGION],
79-
capture_output=True,
8078
)
8179

8280
print(f"File successfully downloaded to {local_dest}")

python/mavis/server/helpers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import os
2+
import sys
3+
import subprocess
4+
5+
16
def confirm_production(env):
27
"""Prompt for confirmation before operating on production."""
38
if env != "production":
@@ -6,3 +11,16 @@ def confirm_production(env):
611
answer = input("Type 'production' to continue: ").strip()
712
if answer != "production":
813
raise RuntimeError("Production confirmation failed")
14+
15+
16+
def run_command(cmd, replace_process=False):
17+
"""Run command and print an error if it fails."""
18+
if replace_process:
19+
os.execvp(cmd[0], cmd)
20+
return_code = subprocess.run(cmd).returncode
21+
if return_code != 0:
22+
print(
23+
f"Command failed with exit code '{return_code}':\n {' '.join(cmd)}",
24+
file=sys.stderr,
25+
)
26+
return return_code == 0

python/mavis/server/put_file.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import os
22
import secrets
3-
import subprocess
43
import sys
54

65
from . import aws
7-
from .helpers import confirm_production
6+
from .helpers import confirm_production, run_command
87

98

109
def register(subparsers):
@@ -61,25 +60,24 @@ def run(args):
6160
key = f"temp-{secrets.token_hex(8)}"
6261
s3_uri = f"s3://{bucket}/{key}"
6362

64-
upload = subprocess.run(
63+
upload_result = run_command(
6564
["aws", "s3", "cp", args.local_file, s3_uri, "--region", aws.REGION]
6665
)
67-
if upload.returncode != 0:
68-
sys.exit("Error: Failed to upload file to S3")
66+
if not upload_result != 0:
67+
sys.exit("Error: Upload to S3 failed with code")
6968

7069
try:
71-
exit_code = aws.run_command(
70+
download_result = aws.run_remote_command(
7271
env,
7372
task_id,
7473
f"aws s3 cp {s3_uri} {remote_path} --region {aws.REGION}",
7574
container=container,
7675
)
7776
finally:
78-
subprocess.run(
77+
run_command(
7978
["aws", "s3", "rm", s3_uri, "--region", aws.REGION],
80-
capture_output=True,
8179
)
8280

83-
if exit_code != 0:
81+
if not download_result:
8482
sys.exit("Error: Failed to copy file into container")
8583
print("File successfully uploaded to container")

python/mavis/server/shell.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22

33
from . import aws
4-
from .helpers import confirm_production
4+
from .helpers import confirm_production, run_command
55

66

77
def register(subparsers):
@@ -50,12 +50,10 @@ def run(args):
5050
f"Opening shell in task {task_id}"
5151
+ (f" (service {args.service})" if args.service else "")
5252
)
53-
exit_code = aws.run_command(
53+
aws.run_remote_command(
5454
env,
5555
task_id,
5656
"/rails/bin/docker-entrypoint /bin/bash",
5757
container=container,
5858
replace_process=True,
5959
)
60-
61-
sys.exit(exit_code)

0 commit comments

Comments
 (0)