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