Skip to content

Commit

Permalink
Used subprocess check_output for docker save
Browse files Browse the repository at this point in the history
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 tern-tools#69

Signed-off-by: Nisha K <[email protected]>
  • Loading branch information
Nisha K committed Sep 10, 2018
1 parent ffcad6a commit 917a153
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions utils/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,23 @@
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
if pwd.getpwuid(os.getuid()).pw_name in members:
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:
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 917a153

Please sign in to comment.