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
- #all_healthy? ⇒ Boolean
- #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
- #start(&block) ⇒ Object
- #status ⇒ Object
- #wait_for_services ⇒ Object
- #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.
17 18 19 20 21 22 23 24 |
# File 'lib/docker/stack/controller.rb', line 17 def initialize(project: self.class.default_project_name, env: nil, cleanup: false, daemon: false) env = Docker::Stack.env if env.nil? project_and_env = [project, env].join('-') @workdir = Docker::Stack.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.
9 10 11 |
# File 'lib/docker/stack/controller.rb', line 9 def cleanup @cleanup end |
#daemon ⇒ Object (readonly)
Returns the value of attribute daemon.
9 10 11 |
# File 'lib/docker/stack/controller.rb', line 9 def daemon @daemon end |
#dc ⇒ Object (readonly)
Returns the value of attribute dc.
9 10 11 |
# File 'lib/docker/stack/controller.rb', line 9 def dc @dc end |
Class Method Details
Instance Method Details
#all_healthy? ⇒ Boolean
67 68 69 |
# File 'lib/docker/stack/controller.rb', line 67 def all_healthy? status.all? { |v| v[:status] == 'healthy' } end |
#down(cleanup: @cleanup) ⇒ Object
95 96 97 |
# File 'lib/docker/stack/controller.rb', line 95 def down(cleanup: @cleanup) dc.run!('down', v: cleanup) end |
#logs(*services) ⇒ Object
83 84 85 |
# File 'lib/docker/stack/controller.rb', line 83 def logs(*services) trap_int(terminate: false) { dc.run!('logs', '-f', services) } end |
#reset! ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/docker/stack/controller.rb', line 26 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
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/docker/stack/controller.rb', line 71 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
87 88 89 |
# File 'lib/docker/stack/controller.rb', line 87 def start(&block) trap_int { run(&block) } end |
#status ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/docker/stack/controller.rb', line 41 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
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/docker/stack/controller.rb', line 53 def wait_for_services Timeout.timeout(120) do warn_inline 'Waiting up to two minutes for services to become healthy.' until all_healthy? warn_inline '.' sleep 2 end warn_inline end true rescue Timeout::Error raise 'Timed out waiting for services to become healthy' end |
#with_containers(&block) ⇒ Object
91 92 93 |
# File 'lib/docker/stack/controller.rb', line 91 def with_containers(&block) trap_int { run(&block) } end |