Commit 90862b8

mo <mo.khan@gmail.com>
2019-07-24 18:04:45
download clair scanner if not detected
1 parent 6f2d43a
lib/examine.rb
@@ -1,5 +1,7 @@
 require "examine/version"
 
+require 'down'
+require 'mkmf'
 require 'socket'
 require 'thor'
 
@@ -8,6 +10,7 @@ module Examine
 
   module CLI
     class Clair < Thor
+      DOWNLOAD_PATH = 'https://github.com/arminc/clair-scanner/releases/download/v12/'
 
       method_option :clair_url, desc: 'clair url', default: 'http://localhost:6060', type: :string
       desc 'start', 'start a clair server'
@@ -33,8 +36,18 @@ module Examine
 
         ip = options[:ip] || Socket.ip_address_list[1].ip_address
         system "docker pull #{image}"
-        # TODO:: ensure that the clair-scanner is found in PATH
-        system "clair-scanner -c #{options[:clair_url]} --ip #{ip} #{image}"
+        unless (clair_exe = find_executable('clair-scanner'))
+          download_path = case RUBY_PLATFORM
+          when 'x86_64-linux'
+            URI.join(DOWNLOAD_PATH, 'clair-scanner_linux_386').to_s
+          else
+            raise 'clair-scanner could not be found in your PATH.Download from https://github.com/arminc/clair-scanner/releases'
+          end
+          clair_exe = File.join(Dir.tmpdir, 'clair-scanner')
+          Down.download(download_path, destination: clair_exe)
+          `chmod +x #{clair_exe}`
+        end
+        system "#{clair_exe} -c #{options[:clair_url]} --ip #{ip} #{image}"
       end
 
       desc 'status', 'status of clair server'
.gitignore
@@ -9,3 +9,4 @@
 
 # rspec failure tracking
 .rspec_status
+*.log
examine.gemspec
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
   spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
   spec.require_paths = ["lib"]
 
+  spec.add_dependency "down", "~> 4.8"
   spec.add_dependency "thor", "~> 0.20"
   spec.add_development_dependency "bundler", "~> 2.0"
   spec.add_development_dependency "rake", "~> 10.0"
Gemfile.lock
@@ -2,12 +2,18 @@ PATH
   remote: .
   specs:
     examine (0.1.0)
+      down (~> 4.8)
       thor (~> 0.20)
 
 GEM
   remote: https://rubygems.org/
   specs:
+    addressable (2.6.0)
+      public_suffix (>= 2.0.2, < 4.0)
     diff-lcs (1.3)
+    down (4.8.1)
+      addressable (~> 2.5)
+    public_suffix (3.1.1)
     rake (10.5.0)
     rspec (3.8.0)
       rspec-core (~> 3.8.0)
@@ -34,4 +40,4 @@ DEPENDENCIES
   rspec (~> 3.0)
 
 BUNDLED WITH
-   2.0.1
+   2.0.2