Commit c2525fc4 authored by Tim Bleimehl's avatar Tim Bleimehl 🤸🏼
Browse files

wip

parent 2b268ecb
...@@ -149,13 +149,6 @@ class BaseBackupper: ...@@ -149,13 +149,6 @@ class BaseBackupper:
) )
def list_databases(self, show_all: bool = False) -> List[str]: def list_databases(self, show_all: bool = False) -> List[str]:
print("FU")
result = self.executer._run_docker_exec(
command="cat -n", stream_file_to_stdin="/tmp/myfile.txt"
)
print(result)
# "#############"
exit()
result = self.executer.container_exec(command=self.get_list_database_command()) result = self.executer.container_exec(command=self.get_list_database_command())
return [ return [
database.strip() database.strip()
......
...@@ -338,7 +338,6 @@ def backup_docker(target_dir): ...@@ -338,7 +338,6 @@ def backup_docker(target_dir):
for container in ContainerHelper.docker_get_container_to_be_backed_up( for container in ContainerHelper.docker_get_container_to_be_backed_up(
describe=True describe=True
): ):
print(container)
if container.coda_labels[ValidLabels.enabled].val: if container.coda_labels[ValidLabels.enabled].val:
BackupperClass = database_type_backupper_mapping[ BackupperClass = database_type_backupper_mapping[
container.coda_labels[ValidLabels.database_type].val container.coda_labels[ValidLabels.database_type].val
......
...@@ -334,7 +334,6 @@ class ContainerHelper: ...@@ -334,7 +334,6 @@ class ContainerHelper:
containers: List[Container] = [] containers: List[Container] = []
for c in docker_containers: for c in docker_containers:
containers.append(Container.from_docker_container(c)) containers.append(Container.from_docker_container(c))
log.debug(f"Containers found: {containers}")
return containers return containers
@classmethod @classmethod
......
import logging import logging
from typing import Callable, Union, List from typing import Callable, Union, List
from unicodedata import decimal
from Configs import getConfig from Configs import getConfig
from pathlib import Path from pathlib import Path
from config import DEFAULT from config import DEFAULT
...@@ -15,6 +16,7 @@ from kubernetes.stream.ws_client import WSClient ...@@ -15,6 +16,7 @@ from kubernetes.stream.ws_client import WSClient
import time import time
import tarfile import tarfile
import io import io
import uuid
# docker api # docker api
import docker import docker
...@@ -67,7 +69,7 @@ class Executer: ...@@ -67,7 +69,7 @@ class Executer:
docker_client = docker.from_env() docker_client = docker.from_env()
container: DockerContainer = docker_client.containers.get(self.container_name) container: DockerContainer = docker_client.containers.get(self.container_name)
##### METHOD 2 of "streaming"/"piping" a file into a container command. we cheat here: ##### "streaming"/"piping" a file into a container command. we cheat here a little bit:
# put_archive. Instead of streaming the file to our command we first create an tar-archive of the file and copy&save the tar-file into the container and stream the file from inside the container via a pipe bash in exec_run # put_archive. Instead of streaming the file to our command we first create an tar-archive of the file and copy&save the tar-file into the container and stream the file from inside the container via a pipe bash in exec_run
# helping links: # helping links:
# https://docker-py.readthedocs.io/en/stable/api.html#docker.api.container.ContainerApiMixin.put_archive # https://docker-py.readthedocs.io/en/stable/api.html#docker.api.container.ContainerApiMixin.put_archive
...@@ -91,21 +93,15 @@ class Executer: ...@@ -91,21 +93,15 @@ class Executer:
) )
cmd = cmd + f" < {container_target_path}" cmd = cmd + f" < {container_target_path}"
if stream_result_to_file: if stream_result_to_file:
if compress_result_to_file: internal_output_file_path = f"/tmp/output_codabuddy_{str(uuid.uuid4())[:8]}"
f = gzip.open(stream_result_to_file, "wb") cmd = cmd + f" > {internal_output_file_path}"
else:
f = open(stream_result_to_file, "wb") # RUN the actual command
# ToDo: with https://docker-py.readthedocs.io/en/stable/api.html#docker.api.container.ContainerApiMixin.get_archive print(cmd)
print("cmd:", cmd)
process = container.exec_run( process = container.exec_run(
cmd=["/bin/sh", "-c", f'"{cmd}"'], cmd=["/bin/sh", "-c", cmd],
stream=True, stream=True,
demux=True, demux=True,
# METHOD 1 (see further down for explaination)
# socket=True if stream_file_to_stdin else False,
# stdin=True if stream_file_to_stdin else False,
# tty=True if stream_file_to_stdin else False,
# METHOD 2
socket=False, socket=False,
stdin=False, stdin=False,
tty=False, tty=False,
...@@ -114,41 +110,38 @@ class Executer: ...@@ -114,41 +110,38 @@ class Executer:
stdout = b"" stdout = b""
stderr = b"" stderr = b""
for output in process.output: for output in process.output:
if stream_result_to_file and output[0]: if output[0]:
f.write(output) log.debug(f"stdout: {stdout}")
elif output[0]:
stdout += output[0] stdout += output[0]
if output[1]: if output[1]:
stderr += output[1] stderr += output[1]
if stream_result_to_file:
f.close
if stderr: if stderr:
raise Exception( raise Exception(
f"ERROR during container ({self.container_name}) exec:\nCommand: '{command}'\nError: '{stderr.decode('utf-8')}'" f"ERROR during container ({self.container_name}) exec:\nCommand: '{cmd}'\nError: '{stderr.decode('utf-8')}'"
) )
##### "streaming"/"piping" the result out of the container command. we cheat here a little bit:
# instead of streaming the file we created a file in the container itself and output it via get_archive
# https://docker-py.readthedocs.io/en/stable/api.html#docker.api.container.ContainerApiMixin.get_archive
# this is less error prone, but a real streaming solution would be more faster and less resource intensive.
# but for now we go with this method
if stream_result_to_file:
if compress_result_to_file:
f = gzip.open(stream_result_to_file, "wb")
else:
f = open(stream_result_to_file, "wb")
stream, meta = container.get_archive(path=internal_output_file_path)
for chunk in stream:
f.write(chunk)
# tidy up the internal temp result file
rm_res = container.exec_run(["rm", internal_output_file_path], demux=True)
elif decode_byte_output: if rm_res.exit_code != 0:
print(stdout) # there was an error while tiding up the tmp internal result file
return stdout.decode("utf-8").rstrip("\n") raise Exception(
else: f"ERROR during removing internal temp result file ({internal_output_file_path}) in container '{self.container_name}' Error:\n'{rm_res.output[1].decode('utf-8')}'"
print(stdout) )
return stdout # done
exit() return stdout.decode("utf-8").rstrip("\n") if decode_byte_output else stdout
stdout = b""
stderr = b""
for output in process.output:
stdout += output[0]
if stdout:
log.debug(f"{self.container_name} - {stdout}")
stderr += output[1]
if stderr:
log.debug(f"{self.container_name} - ERROR - {stderr}")
if stderr:
raise Exception(
f"ERROR during container ({self.container_name}) exec:\nCommand: '{command}'\nError: '{stderr}'"
)
else:
return stdout
def _run_kubernetes_exec( def _run_kubernetes_exec(
self, self,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment