Skip to content

Commit 3f102c0

Browse files
committed
remove kubectl from snapshot_bitcoin_datadir
1 parent ceb9093 commit 3f102c0

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
@@ -220,15 +220,13 @@ def snapshot_bitcoin_datadir(
220220
if resp.peek_stderr():
221221
print(f"Error: {resp.read_stderr()}")
222222
resp.close()
223+
223224
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)
229227

230228
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]
232230
stream(
233231
sclient.connect_get_namespaced_pod_exec,
234232
pod_name,
@@ -247,6 +245,36 @@ def snapshot_bitcoin_datadir(
247245
print(f"An error occurred: {str(e)}")
248246

249247

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+
250278
def wait_for_pod_ready(name, namespace, timeout=300):
251279
sclient = get_static_client()
252280
w = watch.Watch()

0 commit comments

Comments
 (0)