Class: Lux::DockerContainerTask
- Inherits:
-
Rake::FileTask
- Object
- Rake::FileTask
- Lux::DockerContainerTask
- Defined in:
- lib/lux/docker_tasks.rb
Instance Method Summary collapse
-
#destroy_container(msg, &block) ⇒ Object
Destroy the container if the block yields true.
-
#execute(args) ⇒ Object
Before the task actions are done, remove the container.
-
#initialize(task_name, app) ⇒ DockerContainerTask
constructor
This task checks whether a named container is running from a given image.
- #needed? ⇒ Boolean
- #timestamp ⇒ Object
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..trace return if application..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?() } 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
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?() or @application..build_all end |