Skip to content

Commit 665bcae

Browse files
author
william chu
committed
feat: Add gitops.db-pgcli as a command
1 parent b1028dd commit 665bcae

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

gitops/db.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,17 @@ def download_backup(ctx, app, index=None, path=None, datestamp=False):
103103

104104
@task
105105
def proxy(
106-
ctx, app_name, local_port=None, bastion_instance_id=None, aws_availability_zone=None, file=None
106+
ctx,
107+
app_name,
108+
local_port=None,
109+
bastion_instance_id=None,
110+
aws_availability_zone=None,
111+
post_cmd="",
107112
):
108113
"""Creates a proxy to RDS. Can supply either the app name or a DSN
109114
110115
Usage: gitops db.proxy app_name
111116
or gitops db.proxy postgres://...:...@5432/db
112-
gitops db.proxy app_name --file=/tmp/address will write the proxy url to the file
113117
"""
114118
try:
115119
database_url = app_name
@@ -159,21 +163,24 @@ def proxy(
159163
)
160164
proxy_dsn = modified_dsn.geturl()
161165
print(progress(f"Connect to the db using: {proxy_dsn}\n"))
162-
if file:
163-
print(f"Outputing the proxy url to `{file}`")
164-
with open(file, "w") as fout:
165-
fout.write(proxy_dsn)
166166
# Create ssh tunnel
167167
cmd = f"""ssh -i /tmp/temp \
168168
-N -M -L {local_port}:{database_dsn.hostname}:{database_dsn.port} \
169169
-o "UserKnownHostsFile=/dev/null" \
170170
-o "StrictHostKeyChecking=no" \
171171
-o "ServerAliveInterval=60" \
172172
-o ProxyCommand="aws ssm start-session --target %h --document AWS-StartSSHSession --parameters portNumber=%p --region={aws_availability_zone[:-1]}" \
173-
ec2-user@{bastion_instance_id}
174-
"""
175-
try:
173+
ec2-user@{bastion_instance_id}"""
174+
if post_cmd:
175+
run(f"{cmd}& sleep 5 && {post_cmd.format(dsn=proxy_dsn)}", pty=True, hide=False, warn=True)
176+
else:
176177
run(cmd, hide=True)
177-
finally:
178-
if file:
179-
os.remove(file)
178+
179+
180+
@task
181+
def pgcli(
182+
ctx,
183+
app_name,
184+
):
185+
""" Opens pgcli to a remote DB"""
186+
proxy(ctx, app_name, post_cmd="pgcli {dsn}")

0 commit comments

Comments
 (0)