Commit 440e0834 authored by David Trudgian's avatar David Trudgian
Browse files

Bubble up exceptions from clair to cli

parent 2fe74ff0
...@@ -2,7 +2,11 @@ from __future__ import print_function ...@@ -2,7 +2,11 @@ from __future__ import print_function
import requests import requests
import sys import sys
from util import pretty_json, err_and_exit from util import pretty_json
class ClairException(Exception):
pass
def check_clair(API_URI, quiet): def check_clair(API_URI, quiet):
...@@ -16,7 +20,7 @@ def check_clair(API_URI, quiet): ...@@ -16,7 +20,7 @@ def check_clair(API_URI, quiet):
if not quiet: if not quiet:
sys.stderr.write("Found Clair server with %d namespaces\n" % namespace_count) sys.stderr.write("Found Clair server with %d namespaces\n" % namespace_count)
except Exception as e: except Exception as e:
err_and_exit("Error - couldn't access Clair v1 API at %s\n%s\n" % (API_URI, e.message), 1) raise ClairException("Error - couldn't access Clair v1 API at %s\n%s\n" % (API_URI, e.message))
return True return True
...@@ -35,10 +39,10 @@ def post_layer(API_URI, image_name, image_uri, quiet): ...@@ -35,10 +39,10 @@ def post_layer(API_URI, image_name, image_uri, quiet):
if not quiet: if not quiet:
sys.stderr.write("Image registered as layer with Clair\n") sys.stderr.write("Image registered as layer with Clair\n")
else: else:
err_and_exit("Failed registering image with Clair\n %s\n" % pretty_json(r), 1) raise ClairException("Failed registering image with Clair\n %s\n" % pretty_json(r))
except Exception as e: except Exception as e:
err_and_exit("Error - couldn't send image to Clair - %s\n" % (e), 1) raise ClairException("Error - couldn't send image to Clair - %s\n" % (e))
def get_report(API_URI, image_name): def get_report(API_URI, image_name):
...@@ -50,10 +54,10 @@ def get_report(API_URI, image_name): ...@@ -50,10 +54,10 @@ def get_report(API_URI, image_name):
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
return r.json() return r.json()
else: else:
err_and_exit("Failed retrieving report from Clair\n %s\n" % pretty_json(r), 1) raise ClairException("Failed retrieving report from Clair\n %s\n" % pretty_json(r))
except Exception as e: except Exception as e:
err_and_exit("Error - couldn't retrieve report from Clair - %s\n" % (e), 1) raise ClairException("Error - couldn't retrieve report from Clair - %s\n" % (e))
def format_report_text(report): def format_report_text(report):
......
...@@ -5,7 +5,7 @@ import shutil ...@@ -5,7 +5,7 @@ import shutil
from multiprocessing import Process from multiprocessing import Process
from . import VERSION from . import VERSION
from .clair import check_clair, post_layer, get_report, format_report_text from .clair import check_clair, post_layer, get_report, format_report_text, ClairException
from .util import sha256, wait_net_service, err_and_exit from .util import sha256, wait_net_service, err_and_exit
from .image import check_image, image_to_tgz, http_server from .image import check_image, image_to_tgz, http_server
...@@ -36,7 +36,10 @@ def cli(image, clair_uri, text_output, json_output, bind_ip, bind_port, quiet): ...@@ -36,7 +36,10 @@ def cli(image, clair_uri, text_output, json_output, bind_ip, bind_port, quiet):
click.echo("Image has SHA256: %s" % image_name, err=True) click.echo("Image has SHA256: %s" % image_name, err=True)
# Make sure we can talk to Clair OK # Make sure we can talk to Clair OK
check_clair(API_URI, quiet) try:
check_clair(API_URI, quiet)
except ClairException as e:
err_and_exit(e.message)
# Start an HTTP server to serve the .tar.gz from our temporary directory # Start an HTTP server to serve the .tar.gz from our temporary directory
# so that Clair can retrieve it # so that Clair can retrieve it
...@@ -46,12 +49,18 @@ def cli(image, clair_uri, text_output, json_output, bind_ip, bind_port, quiet): ...@@ -46,12 +49,18 @@ def cli(image, clair_uri, text_output, json_output, bind_ip, bind_port, quiet):
httpd_ready = wait_net_service(bind_ip, bind_port, 30) httpd_ready = wait_net_service(bind_ip, bind_port, 30)
if not httpd_ready: if not httpd_ready:
httpd.terminate() httpd.terminate()
err_and_exit("HTTP server did not become ready\n", 1) shutil.rmtree()
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)) image_uri = 'http://%s:%d/%s' % (bind_ip, bind_port, path.basename(tar_file))
# Register the iamge with Clair as a docker layer that has no parent # Register the iamge with Clair as a docker layer that has no parent
post_layer(API_URI, image_name, image_uri, quiet) try:
post_layer(API_URI, image_name, image_uri, quiet)
except ClairException as e:
httpd.terminate()
shutil.rmtree()
err_and_exit(e.message, 1)
# Done with the .tar.gz so stop serving it and remove the temp dir # Done with the .tar.gz so stop serving it and remove the temp dir
httpd.terminate() httpd.terminate()
......
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