Skip to content

Commit

Permalink
Extract common method for exiting if no image available
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Barton <[email protected]>
  • Loading branch information
michaelbarton committed Sep 5, 2015
1 parent e22af7d commit b50f8ef
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
3 changes: 1 addition & 2 deletions biobox_cli/biobox_type/short_read_assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def run(argv):
contig_file = opts['--output']
task = opts['--task']

if not ctn.image_available(image):
util.err_exit('unknown_image', {'image': image})
ctn.exit_if_no_image_available(image)

cntr_fastq_file = "/fastq/input.fq.gz"
fastq_values = [(cntr_fastq_file, "paired")]
Expand Down
3 changes: 1 addition & 2 deletions biobox_cli/command/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def run(argv):
image = opts['<image>']
task = opts['--task']

if not ctn.image_available(image):
util.err_exit('unknown_image', {'image': image})
ctn.exit_if_no_image_available(image)

results = behave.run(biobox, image, task)

Expand Down
16 changes: 10 additions & 6 deletions biobox_cli/container.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import logging
import os
import logging, os
logging.getLogger("requests").setLevel(logging.WARNING)

import docker
import docker.utils

import biobox_cli.util as util

def client():
client = docker.Client(**docker.utils.kwargs_from_env(assert_hostname = False))
return client
Expand All @@ -13,19 +14,22 @@ def get_image_tags(docker_dict):
return reduce(lambda acc, x: acc + [x, x.split(":")[0]],
docker_dict['RepoTags'], [])

def image_available_locally(image):
def is_image_available_locally(image):
image_tags = map(get_image_tags, client().images())
images = set(reduce(lambda acc, x: acc + x, image_tags, []))
return image in images

def image_available(image):
from docker.errors import APIError
if not image_available_locally(image):
def is_image_available(image):
if not is_image_available_locally(image):
output = client().pull(image)
if "error" in output:
return False
return True

def exit_if_no_image_available(image):
if not is_image_available(image):
util.err_exit('unknown_image', {'image': image})

def mount_string(host_dir, container_dir, read_only = True):
access = "ro" if read_only else "rw"
return ":".join([os.path.abspath(host_dir), container_dir, access])
Expand Down
10 changes: 5 additions & 5 deletions test/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ def test_get_client():
ctn.client()

def test_checking_a_local_available_image():
nose.assert_equal(ctn.image_available_locally("python"), True)
nose.assert_equal(ctn.is_image_available_locally("python"), True)

def test_checking_an_local_available_image_with_tag():
nose.assert_equal(ctn.image_available_locally("python:2.7"), True)
nose.assert_equal(ctn.is_image_available_locally("python:2.7"), True)

def test_checking_a_locally_non_existent_image():
nose.assert_equal(ctn.image_available_locally("biobox/unknown"), False)
nose.assert_equal(ctn.is_image_available_locally("biobox/unknown"), False)

def test_checking_an_available_image():
nose.assert_equal(ctn.image_available("python"), True)
nose.assert_equal(ctn.is_image_available("python"), True)

def test_checking_an_available_image_with_tag():
nose.assert_equal(ctn.image_available("python:2.7"), True)
nose.assert_equal(ctn.is_image_available("python:2.7"), True)

def test_create_mount_string_with_relative_dir():
expected = "{}/tmp:/tmp:ro".format(hlpr.project_root())
Expand Down

0 comments on commit b50f8ef

Please sign in to comment.