lib/lux/docker_tasks.rb in lux-1.0.5 vs lib/lux/docker_tasks.rb in lux-1.0.6
- old
+ new
@@ -3,15 +3,10 @@
require 'docker'
require 'lux'
require 'rake'
require 'socket'
-# Set this to avoid 5 sec timeouts with .local addresses
-# which don't resolve in IPv6
-#
-Docker.options = { family: Socket::Constants::AF_INET }
-
module Rake
module DSL
def dockerimage(*args, &block)
Lux::DockerImageTask.define_task(*args, &block)
end
@@ -20,28 +15,51 @@
end
end
end
module Lux
+
+ addr, var, uri = Lux.dockerip
+
+ DISABLED = case
+ when !addr && var && uri.scheme == 'tcp'
+ error "Invalid Docker server, disabling Docker tasks!"
+ true
+ else
+ begin
+ # Set this to avoid 5 sec timeouts with .local addresses
+ # which don't resolve in IPv6
+ Docker.options = { family: Socket::Constants::AF_INET, connect_timeout: 5 }
+ Docker.validate_version!
+ false
+ rescue Exception => e
+ error "Docker problem (#{e.message}), tasks will be disabled"
+ true
+ end
+ end
+
class DockerImageTask < Rake::FileTask
# This task checks to see if the image is present in the local Docker Server
# If present it returns the creation date as the timestamp, if not then it
# returns Rake::Early. This allows dependencies to execute correctly
# The action block should build the container.
#
def initialize(task_name, app)
super(task_name, app)
@imagename = @name
@imagename += ':latest' unless @imagename.index(':')
- @image = Docker::Image.all.select{|i| i.info['RepoTags'].include? @imagename}.first
+ @image = DISABLED ? nil :
+ Docker::Image.all.select{|i| i.info['RepoTags'].include? @imagename}.first
end
def needed?
+ return false if DISABLED
! @image || out_of_date?(timestamp) || @application.options.build_all
end
def timestamp
+ return Time.now if DISABLED
if @image = Docker::Image.all.select{|i| i.info['RepoTags'].include? @imagename}.first
Time.at(@image.info['Created'])
else
Rake::EARLY
end
@@ -59,16 +77,19 @@
#
def initialize(task_name, app)
super(task_name, app)
@containername, sep, @imagename = task_name.partition('@')
raise "Task #{task_name} must be name@image" if @containername.empty? or @imagename.empty?
- @container = Docker::Container.get(@containername) rescue nil
- @imagename += ':latest' unless @imagename.index(':')
- @image = Docker::Image.all.select{|i| i.info['RepoTags'].include? @imagename}.first
+ unless DISABLED
+ @container = Docker::Container.get(@containername) rescue nil
+ @imagename += ':latest' unless @imagename.index(':')
+ @image = Docker::Image.all.select{|i| i.info['RepoTags'].include? @imagename}.first
+ end
end
def needed?
+ return false if DISABLED
not @container or
@container.info["Image"] != @image.id or
not @container.info["State"]["Running"] or
out_of_date?(timestamp) or
@application.options.build_all
@@ -100,9 +121,10 @@
end
@container = nil
end
def timestamp
+ return Time.now if DISABLED
if @container = Docker::Container.get(@containername) rescue nil
Time.iso8601(@container.info['Created'])
else
Rake::EARLY
end