Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
BioHPC
clair-singularity
Commits
e8f940b6
Commit
e8f940b6
authored
Aug 24, 2017
by
David Trudgian
Browse files
Tidy exceptions etc
parent
405ece3c
Changes
3
Hide whitespace changes
Inline
Side-by-side
clair_singularity/clair.py
View file @
e8f940b6
...
...
@@ -2,7 +2,7 @@ from __future__ import print_function
import
requests
import
sys
from
.util
import
pretty_json
class
ClairException
(
Exception
):
pass
...
...
clair_singularity/cli.py
View file @
e8f940b6
...
...
@@ -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
)
clair_singularity/image.py
View file @
e8f940b6
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment