@@ -214,15 +214,13 @@ def snapshot_bitcoin_datadir(
214
214
if resp .peek_stderr ():
215
215
print (f"Error: { resp .read_stderr ()} " )
216
216
resp .close ()
217
+
217
218
local_file_path = Path (local_path ) / f"{ pod_name } _bitcoin_data.tar.gz"
218
- copy_command = (
219
- f"kubectl cp { namespace } /{ pod_name } :/tmp/bitcoin_data.tar.gz { local_file_path } "
220
- )
221
- if not stream_command (copy_command ):
222
- raise Exception ("Failed to copy tar file from pod to local machine" )
219
+ temp_bitcoin_data_path = "/tmp/bitcoin_data.tar.gz"
220
+ copy_file_from_pod (namespace , pod_name , temp_bitcoin_data_path , local_file_path )
223
221
224
222
print (f"Bitcoin data exported successfully to { local_file_path } " )
225
- cleanup_command = ["rm" , "/tmp/bitcoin_data.tar.gz" ]
223
+ cleanup_command = ["rm" , temp_bitcoin_data_path ]
226
224
stream (
227
225
sclient .connect_get_namespaced_pod_exec ,
228
226
pod_name ,
@@ -241,6 +239,36 @@ def snapshot_bitcoin_datadir(
241
239
print (f"An error occurred: { str (e )} " )
242
240
243
241
242
+ def copy_file_from_pod (namespace , pod_name , pod_path , local_path ):
243
+ exec_command = ["cat" , pod_path ]
244
+
245
+ v1 = client .CoreV1Api ()
246
+
247
+ # Note: We do not specify the container name here; if we pack multiple containers in a pod
248
+ # we will need to change this
249
+ resp = stream (
250
+ v1 .connect_get_namespaced_pod_exec ,
251
+ pod_name ,
252
+ namespace ,
253
+ command = exec_command ,
254
+ stderr = True ,
255
+ stdin = False ,
256
+ stdout = True ,
257
+ tty = False ,
258
+ _preload_content = False ,
259
+ )
260
+
261
+ with open (local_path , "wb" ) as local_file :
262
+ while resp .is_open ():
263
+ resp .update (timeout = 1 )
264
+ if resp .peek_stdout ():
265
+ local_file .write (resp .read_stdout ().encode ("utf-8" ))
266
+ if resp .peek_stderr ():
267
+ print ("Error:" , resp .read_stderr ())
268
+
269
+ resp .close ()
270
+
271
+
244
272
def wait_for_caddy_ready (name , namespace , timeout = 300 ):
245
273
sclient = get_static_client ()
246
274
w = watch .Watch ()
0 commit comments