class Lux::DockerContainerTask

Public Class Methods

new(task_name, app) click to toggle source

This task checks whether a named container is running from a given image. The name of the task is <container-name>@<image-name> The container name must match /?[a-zA-Z0-9_-]+

If a container is already running for this image, the creation time is checked against the current image modification date. If the container is not running or out-of-date it is restarted.

Calls superclass method
# File lib/lux/docker_tasks.rb, line 78
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?
  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

Public Instance Methods

destroy_container(msg) { || ... } click to toggle source

Destroy the container if the block yields true

# File lib/lux/docker_tasks.rb, line 108
def destroy_container msg, &block
  return if !@container or (block_given? and not yield)
  application.trace "** Prepare #{name} (destroying container: #{msg})" if application.options.trace
  return if application.options.dryrun
  # It may be hard to kill this container, try but if something goes wrong
  # then print a message and proceed with the action block. The start command
  # in there will fail in a more user-friendly way.
  begin
    @container.stop # @container.kill(signal: 'SIGTERM') better?
    @container.wait(10)
    @container.delete
  rescue Exception => e
    puts "Exception destroying container: #{e.to_s}"
  end
  @container = nil
end
execute(args) click to toggle source

Before the task actions are done, remove the container

Calls superclass method
# File lib/lux/docker_tasks.rb, line 99
def execute(args)
  destroy_container("it is out-of-date") { out_of_date?(timestamp) }
  destroy_container("it uses the wrong image") { @container.info["Image"] != @image.id }
  destroy_container("it is not running") { not @container.info["State"]["Running"] }
  destroy_container("it exists!") { @container }
  super args
end
needed?() click to toggle source
# File lib/lux/docker_tasks.rb, line 89
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
end
timestamp() click to toggle source
# File lib/lux/docker_tasks.rb, line 125
def timestamp
  return Time.now if DISABLED
  if @container = Docker::Container.get(@containername) rescue nil
    Time.iso8601(@container.info['Created'])
  else
    Rake::EARLY
  end
end