@@ -103,13 +103,17 @@ def download_backup(ctx, app, index=None, path=None, datestamp=False):
103
103
104
104
@task
105
105
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 = "" ,
107
112
):
108
113
"""Creates a proxy to RDS. Can supply either the app name or a DSN
109
114
110
115
Usage: gitops db.proxy app_name
111
116
or gitops db.proxy postgres://...:...@5432/db
112
- gitops db.proxy app_name --file=/tmp/address will write the proxy url to the file
113
117
"""
114
118
try :
115
119
database_url = app_name
@@ -159,21 +163,24 @@ def proxy(
159
163
)
160
164
proxy_dsn = modified_dsn .geturl ()
161
165
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 )
166
166
# Create ssh tunnel
167
167
cmd = f"""ssh -i /tmp/temp \
168
168
-N -M -L { local_port } :{ database_dsn .hostname } :{ database_dsn .port } \
169
169
-o "UserKnownHostsFile=/dev/null" \
170
170
-o "StrictHostKeyChecking=no" \
171
171
-o "ServerAliveInterval=60" \
172
172
-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 :
176
177
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