Commit e8f940b6 authored by David Trudgian's avatar David Trudgian
Browse files

Tidy exceptions etc

parent 405ece3c
......@@ -2,7 +2,7 @@ from __future__ import print_function
import requests
import sys
from .util import pretty_json
class ClairException(Exception):
pass
......
......@@ -6,8 +6,8 @@ from multiprocessing import Process
from . import VERSION
from .clair import check_clair, post_layer, get_report, format_report_text, ClairException
from .util import sha256, wait_net_service, err_and_exit
from .image import check_image, image_to_tgz, http_server
from .util import sha256, wait_net_service, err_and_exit, pretty_json
from .image import check_image, image_to_tgz, http_server, ImageException
@click.command()
......@@ -27,8 +27,11 @@ def cli(image, clair_uri, text_output, json_output, bind_ip, bind_port, quiet):
API_URI = clair_uri + '/v1/'
# Check image exists, and export it to a gzipped tar in a temporary directory
check_image(image)
(tar_dir, tar_file) = image_to_tgz(image, quiet)
try:
check_image(image)
(tar_dir, tar_file) = image_to_tgz(image, quiet)
except ImageError as e:
err_and_exit(e.message, 1)
# Image name for Clair will be the SHA256 of the .tar.gz
image_name = sha256(tar_file)
......@@ -50,7 +53,7 @@ def cli(image, clair_uri, text_output, json_output, bind_ip, bind_port, quiet):
httpd_ready = wait_net_service(bind_ip, bind_port, 30)
if not httpd_ready:
httpd.terminate()
shutil.rmtree()
shutil.rmtree(tar_dir)
err_and_exit("Error: HTTP server did not become ready\n", 1)
image_uri = 'http://%s:%d/%s' % (bind_ip, bind_port, path.basename(tar_file))
......@@ -60,7 +63,7 @@ def cli(image, clair_uri, text_output, json_output, bind_ip, bind_port, quiet):
post_layer(API_URI, image_name, image_uri, quiet)
except ClairException as e:
httpd.terminate()
shutil.rmtree()
shutil.rmtree(tar_dir)
err_and_exit(e.message, 1)
# Done with the .tar.gz so stop serving it and remove the temp dir
......@@ -72,7 +75,7 @@ def cli(image, clair_uri, text_output, json_output, bind_ip, bind_port, quiet):
# Spit out the report on STDOUT
if json_output:
pretty_report = json.dumps(report, separators=(',', ':'), sort_keys=True, indent=2)
pretty_report = pretty_json(report)
click.echo(pretty_report)
else:
format_report_text(report)
......@@ -5,14 +5,15 @@ from os import path, chdir
from six.moves import SimpleHTTPServer, socketserver
from .util import err_and_exit
class ImageException(Exception):
pass
def check_image(image):
"""Check if specified image file exists"""
if not path.isfile(image):
err_and_exit('Error: Singularity image "%s" not found.' % image, 66) # E_NOINPUT
raise ImageException('Error: Singularity image "%s" not found.' % image)
return True
......@@ -31,7 +32,7 @@ def image_to_tgz(image, quiet):
try:
subprocess.check_call(cmd)
except (subprocess.CalledProcessError, OSError) as e:
err_and_exit("Error calling Singularity export to create .tar file\n%s" % e.message, 1)
raise ImageException("Error calling Singularity export to create .tar file\n%s" % e.message)
cmd = ['gzip', tar_file]
......@@ -41,7 +42,7 @@ def image_to_tgz(image, quiet):
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError as e:
err_and_exit("Error calling gzip export to compress .tar file\n%s" % e.message, 1)
raise ImageException("Error calling gzip export to compress .tar file\n%s" % e.message)
return (temp_dir, tar_gz_file)
......
Markdown is supported
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