Class: Lux::DockerContainerTask

Inherits:
Rake::FileTask
  • Object
show all
Defined in:
lib/lux/docker_tasks.rb

Instance Method Summary collapse

Constructor Details

#initialize(task_name, app) ⇒ DockerContainerTask

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.



78
79
80
81
82
83
84
85
86
87
# 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

Instance Method Details

#destroy_container(msg, &block) ⇒ Object

Destroy the container if the block yields true



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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) ⇒ Object

Before the task actions are done, remove the container



99
100
101
102
103
104
105
# 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?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
# 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

#timestampObject



125
126
127
128
129
130
131
132
# 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