Class: Docker::Stack::Controller
- Inherits:
-
Object
- Object
- Docker::Stack::Controller
- Defined in:
- lib/docker/stack/controller.rb
Instance Attribute Summary collapse
-
#cleanup ⇒ Object
readonly
Returns the value of attribute cleanup.
-
#daemon ⇒ Object
readonly
Returns the value of attribute daemon.
-
#dc ⇒ Object
readonly
Returns the value of attribute dc.
Class Method Summary collapse
Instance Method Summary collapse
- #down(cleanup: @cleanup) ⇒ Object
-
#initialize(project: self.class.default_project_name, env: nil, cleanup: false, daemon: false) ⇒ Controller
constructor
A new instance of Controller.
- #logs(*services) ⇒ Object
- #reset! ⇒ Object
-
#run ⇒ Object
rubocop:enable Style/StderrPuts.
- #start(&block) ⇒ Object
- #status ⇒ Object
-
#wait_for_services ⇒ Object
rubocop:disable Style/StderrPuts.
- #with_containers(&block) ⇒ Object
Constructor Details
#initialize(project: self.class.default_project_name, env: nil, cleanup: false, daemon: false) ⇒ Controller
Returns a new instance of Controller.
13 14 15 16 17 18 19 20 |
# File 'lib/docker/stack/controller.rb', line 13 def initialize(project: self.class.default_project_name, env: nil, cleanup: false, daemon: false) env = Rails.env if env.nil? project_and_env = [project, env].join('-') @workdir = Rails.root.join('.docker-stack', project_and_env) @dc = ::Docker::Compose::Session.new(dir: @workdir) @cleanup = cleanup @daemon = daemon end |
Instance Attribute Details
#cleanup ⇒ Object (readonly)
Returns the value of attribute cleanup.
7 8 9 |
# File 'lib/docker/stack/controller.rb', line 7 def cleanup @cleanup end |
#daemon ⇒ Object (readonly)
Returns the value of attribute daemon.
7 8 9 |
# File 'lib/docker/stack/controller.rb', line 7 def daemon @daemon end |
#dc ⇒ Object (readonly)
Returns the value of attribute dc.
7 8 9 |
# File 'lib/docker/stack/controller.rb', line 7 def dc @dc end |
Class Method Details
.default_project_name ⇒ Object
9 10 11 |
# File 'lib/docker/stack/controller.rb', line 9 def self.default_project_name Rails.application.class.parent_name.underscore end |
Instance Method Details
#down(cleanup: @cleanup) ⇒ Object
90 91 92 |
# File 'lib/docker/stack/controller.rb', line 90 def down(cleanup: @cleanup) dc.run!('down', v: cleanup) end |
#logs(*services) ⇒ Object
78 79 80 |
# File 'lib/docker/stack/controller.rb', line 78 def logs(*services) trap_int(terminate: false) { dc.run!('logs', '-f', services) } end |
#reset! ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/docker/stack/controller.rb', line 22 def reset! down(cleanup: true) images = config['services'].values.map { |conf| conf['image'] } images.each do |image_name| begin image = ::Docker::Image.get(image_name) result = image.remove(prune: true) yield result if block_given? rescue ::Docker::Error::NotFoundError yield %{[{"Skipped":"#{image_name} (image not present)"}]} end end ::Docker::Image.prune end |
#run ⇒ Object
rubocop:enable Style/StderrPuts
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/docker/stack/controller.rb', line 66 def run dc.up(detached: @daemon || block_given?) return true unless block_given? begin wait_for_services yield ensure down end end |
#start(&block) ⇒ Object
82 83 84 |
# File 'lib/docker/stack/controller.rb', line 82 def start(&block) trap_int { run(&block) } end |
#status ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/docker/stack/controller.rb', line 37 def status containers = dc.ps.map(&:id) containers.map do |c| begin container = Container.new(c) container.to_h rescue StandardError { id: c, service: 'unknown', status: 'unknown', started: 'unknown', running: 'unknown' } end end end |
#wait_for_services ⇒ Object
rubocop:disable Style/StderrPuts
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/docker/stack/controller.rb', line 50 def wait_for_services Timeout.timeout(120) do $stderr.print 'Waiting up to two minutes for services to become healthy.' if warn? loop do break if status.all? { |v| v[:status] == 'healthy' } $stderr.print '.' if warn? sleep 2 end $stderr.puts if warn? end true rescue Timeout::Error raise 'Timed out waiting for services to become healthy' end |
#with_containers(&block) ⇒ Object
86 87 88 |
# File 'lib/docker/stack/controller.rb', line 86 def with_containers(&block) trap_int { run(&block) } end |