From 917a153b38a71159e0c91369fee83ffee008f1d5 Mon Sep 17 00:00:00 2001 From: Nisha K Date: Mon, 10 Sep 2018 13:47:22 -0700 Subject: [PATCH] Used subprocess check_output for docker save The docker save command is what creates the tar of the container image. For larger images the pipe buffer is too small. So using subprocess.check_output to get the result of docker save. - Moved logic to check for sudo use in is_sudo function - Added function docker_command_check for use of subprocess.check_output to fork processes - Used docker_command_check in the extract_image_metadata function Resolves #69 Signed-off-by: Nisha K --- utils/container.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/utils/container.py b/utils/container.py index b3dfa8e3..54ac4ea4 100644 --- a/utils/container.py +++ b/utils/container.py @@ -43,10 +43,8 @@ logger = logging.getLogger(logger_name) -def docker_command(command, *extra): - '''Invoke docker command. If the command fails nothing is returned - If it passes then the result is returned''' - full_cmd = [] +def is_sudo(): + '''Check if the user uses sudo for docker commands''' sudo = True try: members = grp.getgrnam('docker').gr_mem @@ -54,7 +52,14 @@ def docker_command(command, *extra): sudo = False except KeyError: pass - if sudo: + return sudo + + +def docker_command(command, *extra): + '''Invoke docker command. If the command fails nothing is returned + If it passes then the result is returned''' + full_cmd = [] + if is_sudo(): full_cmd.append('sudo') full_cmd.extend(command) for arg in extra: @@ -70,6 +75,23 @@ def docker_command(command, *extra): return result +def docker_command_check(command, *extra): + '''Invoke docker command using subprocess.check_output''' + full_cmd = [] + if is_sudo(): + full_cmd.append('sudo') + full_cmd.extend(command) + for arg in extra: + full_cmd.append(arg) + # invoke + logger.debug("Running command: " + ' '.join(full_cmd)) + try: + result = subprocess.check_output(full_cmd) + return result + except subprocess.CalledProcessError: + raise + + def check_container(): '''Check if a container exists''' is_container = False @@ -155,7 +177,7 @@ def get_image_id(image_tag_string): def extract_image_metadata(image_tag_string): '''Run docker save and extract the files in a temporary directory''' temp_path = os.path.abspath(temp_folder) - result = docker_command(save, image_tag_string) + result = docker_command_check(save, image_tag_string) with tarfile.open(fileobj=io.BytesIO(result)) as tar: tar.extractall(temp_path) if not os.path.exists(temp_path):