Skip to content

Commit cac0e68

Browse files
committed
remove kubectl from snapshot_bitcoin_datadir
1 parent 96ceac8 commit cac0e68

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/warnet/k8s.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,13 @@ def snapshot_bitcoin_datadir(
214214
if resp.peek_stderr():
215215
print(f"Error: {resp.read_stderr()}")
216216
resp.close()
217+
217218
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)
223221

224222
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]
226224
stream(
227225
sclient.connect_get_namespaced_pod_exec,
228226
pod_name,
@@ -241,6 +239,36 @@ def snapshot_bitcoin_datadir(
241239
print(f"An error occurred: {str(e)}")
242240

243241

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+
244272
def wait_for_caddy_ready(name, namespace, timeout=300):
245273
sclient = get_static_client()
246274
w = watch.Watch()

0 commit comments

Comments
 (0)