Class: DockerBoss::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/docker_boss/engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, config) ⇒ Engine

Returns a new instance of Engine.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/docker_boss/engine.rb', line 8

def initialize(options, config)
  @containers = []
  @options = options
  @config = config
  @mutex = Mutex.new
  @last_etcds
  @modules = []

  @config.each do |k,v|
    @modules << DockerBoss::ModuleManager[k].new(v)
  end
end

Instance Method Details

#event_loopObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/docker_boss/engine.rb', line 76

def event_loop
  @events = Queue.new
  threads = []
  threads << Thread.new do
    loop do
      event = @events.deq
      process_event(event)
    end
  end

  threads << Thread.new do
    loop do
      begin
        #Docker::Event.stream({}, Docker::Connection.new(Docker.url, {:nonblock => true})) do |event|
        Docker::Event.stream do |event|
          DockerBoss.logger.debug "New event on socket: #{event}"
          @events.enq({:id => event.id, :status => event.status})
        end
      rescue Docker::Error::TimeoutError
        next
      end
    end
  end

  @modules.each do |mod|
    begin
      threads << mod.run
    rescue NoMethodError
    end
  end

  ThreadsWait.new(*threads)
end

#process_event(event) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/docker_boss/engine.rb', line 48

def process_event(event)
  DockerBoss.logger.info "Processing event: #{event}"
  case event[:status]
  when 'start' # 'create' also triggers 'start'
    @mutex.synchronize {
      if @options[:incr_refresh]
        new_container = Docker::Container.get(event[:id]).json
        @containers.delete_if { |c| c['Id'] == event[:id] }
        @containers << xform_container(new_container)
      else
        refresh_all
      end
      trigger(event[:id])
    }
  when 'die' # 'destroy', 'kill', 'stop' also trigger 'die'
    @mutex.synchronize {
      if @options[:incr_refresh]
        @containers.delete_if { |c| c['Id'] == event[:id] }
      else
        refresh_all
      end
      trigger(event[:id])
    }
  when 'pause'
  when 'unpause'
  end
end

#refresh_allObject



27
28
29
# File 'lib/docker_boss/engine.rb', line 27

def refresh_all
  @containers = Docker::Container.all.map { |c| xform_container(c.json) }
end

#refresh_and_triggerObject



31
32
33
34
35
36
# File 'lib/docker_boss/engine.rb', line 31

def refresh_and_trigger
  @mutex.synchronize {
    refresh_all
    trigger
  }
end

#trigger(id = nil) ⇒ Object



21
22
23
24
25
# File 'lib/docker_boss/engine.rb', line 21

def trigger(id = nil)
  @modules.each do |mod|
    mod.trigger(@containers, id)
  end
end

#xform_container(container) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/docker_boss/engine.rb', line 38

def xform_container(container)
  new_env = {}
  container['Config']['Env'].each do |env|
    (k,v) = env.split('=', 2)
    new_env[k] = v || true
  end
  container['Config']['Env'] = new_env
  container
end