@@ -178,12 +178,16 @@ def render_postgres_script(namespace,template,data):
178
178
file = ntp .spool_template (template , out , data )
179
179
return os .path .abspath (file )
180
180
181
- def exec_psql_command (pod_name ,path_to_psql_script ,path_to_pgpass ):
181
+ def exec_psql_command (pod_name ,path_to_psql_script ,path_to_pgpass , additional_psql_args = '' ):
182
182
logging .info (f"passing script { path_to_psql_script } to pod { pod_name } " )
183
183
res = kube .kubectl ("cp" ,path_to_psql_script ,f"{ pod_name } :{ path_to_psql_script } " )
184
184
res = kube .kubectl ("cp" ,path_to_pgpass ,f"{ pod_name } :/tmp/.pgpass" )
185
185
res = kube .kubectl ("exec" ,"-it" ,pod_name ,"--" ,"/bin/bash" ,"-c" ,f"chmod 600 /tmp/.pgpass" )
186
- res = kube .kubectl ("exec" ,"-it" ,pod_name ,"--" ,"/bin/bash" ,"-c" ,f"PGPASSFILE='/tmp/.pgpass' psql --username postgres --dbname postgres -f { path_to_psql_script } " )
186
+
187
+ cmd = f"PGPASSFILE='/tmp/.pgpass' psql --username postgres --dbname postgres { additional_psql_args } -f { path_to_psql_script } "
188
+ logging .info (f"executing command: { cmd } " )
189
+ res = kube .kubectl ("exec" ,"-it" ,pod_name ,"--" ,"/bin/bash" ,"-c" ,cmd )
190
+
187
191
os .remove (path_to_psql_script )
188
192
os .remove (path_to_pgpass )
189
193
return res
@@ -209,6 +213,10 @@ def create_db_user(ucfg: UserConfig, user_metadata: UserMetadata):
209
213
if res :
210
214
_add_pdb_user_metadata (ucfg , user_metadata )
211
215
216
+ path_to_pgpass = render_postgres_script (ucfg .get ('namespace' ),"dbname_pgpass_tpl.properties" ,data )
217
+ path_to_schema_script = render_postgres_script (ucfg .get ('namespace' ),"postgres_manage_user_schema_tpl.sql" ,data )
218
+ res = exec_psql_command_in_db (database ,pod_name ,path_to_schema_script ,path_to_pgpass )
219
+
212
220
data ["extensions" ]= ["vector" ]
213
221
path_to_pgpass = render_postgres_script (ucfg .get ('namespace' ),"dbname_pgpass_tpl.properties" ,data )
214
222
path_to_extensions_script = render_postgres_script (ucfg .get ('namespace' ),"postgres_manage_user_extension_tpl.sql" ,data )
@@ -232,12 +240,17 @@ def delete_db_user(namespace, database):
232
240
data ["database" ]= database
233
241
data ["mode" ]= "delete"
234
242
235
- path_to_pgpass = render_postgres_script (namespace ,"pgpass_tpl.properties" ,data )
236
- path_to_mdb_script = render_postgres_script (namespace ,"postgres_manage_user_tpl.sql" ,data )
243
+
237
244
pod_name = util .get_pod_name_by_selector ("app=nuvolaris-postgres" ,"{.items[?(@.metadata.labels.replicationRole == 'primary')].metadata.name}" )
238
245
239
246
if (pod_name ):
240
- res = exec_psql_command (pod_name ,path_to_mdb_script ,path_to_pgpass )
247
+ path_to_pgpass = render_postgres_script (namespace ,"pgpass_tpl.properties" ,data )
248
+ path_to_ter_script = render_postgres_script (namespace ,"postgres_terminate_tpl.sql" ,data )
249
+ res = exec_psql_command (pod_name ,path_to_ter_script ,path_to_pgpass ,' -q -t ' )
250
+
251
+ path_to_pgpass = render_postgres_script (namespace ,"pgpass_tpl.properties" ,data )
252
+ path_to_mdb_script = render_postgres_script (namespace ,"postgres_manage_user_tpl.sql" ,data )
253
+ res += exec_psql_command (pod_name ,path_to_mdb_script ,path_to_pgpass )
241
254
return res
242
255
243
256
return None
0 commit comments