Class: Docker::Stack::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/docker/stack/controller.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#cleanupObject (readonly)

Returns the value of attribute cleanup.



9
10
11
# File 'lib/docker/stack/controller.rb', line 9

def cleanup
  @cleanup
end

#daemonObject (readonly)

Returns the value of attribute daemon.



9
10
11
# File 'lib/docker/stack/controller.rb', line 9

def daemon
  @daemon
end

#dcObject (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

.default_project_nameObject



11
12
13
14
15
# File 'lib/docker/stack/controller.rb', line 11

def self.default_project_name
  Rails.application.class.parent_name.underscore
rescue NoMethodError
  Docker::Stack.root.basename.to_s
end

Instance Method Details

#all_healthy?Boolean

Returns:

  • (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

#runObject



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

#statusObject



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_servicesObject



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