Class: MKIt::DockerListener

Inherits:
Object
  • Object
show all
Includes:
DockerHelper
Defined in:
lib/mkit/docker_listener.rb

Instance Method Summary collapse

Methods included from DockerHelper

#attach_network, #create_instance, #create_network, #create_volume, #delete_volume, #dettach_network, #execute_local, #inspect_instance, #inspect_volume, #logs, #network_exists?, #remove_instance, #remove_network, #start_instance, #stop_instance

Constructor Details

#initializeDockerListener

Returns a new instance of DockerListener.



11
12
13
# File 'lib/mkit/docker_listener.rb', line 11

def initialize
  @consumers = []
end

Instance Method Details

#parse_message(msg) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mkit/docker_listener.rb', line 18

def parse_message(msg)
  action = msg['Action'].to_sym
  type = msg['Type'].to_sym
  MKItLogger.info("docker <#{type}> <#{action}> received: \n\t#{msg}")
  case type
  when :container
    pod_id = msg.id
    pod_name = msg.Actor.Attributes.name
    pod = Pod.find_by(name: pod_name)
    unless pod.nil?
      case action
      when :create
        pod.pod_id = pod_id
        pod.status = MKIt::Status::CREATED
        pod.save
        pod.service.update_status!
      when :start
        pod.pod_id = pod_id
        pod.save
        pod.service.update_status!
      when :kill
        MKItLogger.debug("	#{type} #{action} <<NOOP / TODO>>")
      when :die
        MKItLogger.debug("	#{type} #{action} <<NOOP / TODO>>")
      when :stop
        pod.service.update_status!
      else
        MKItLogger.debug("	#{type} #{action} <<TODO>>")
      end
    else
      MKItLogger.warn("docker <<#{type}>> <#{action}> received: #{msg}. But I don't know anything about pod #{pod_id}")
    end
  when :network
    pod_id = msg.Actor.Attributes.container
    pod = Pod.find_by(pod_id: pod_id)
    unless pod.nil?
      case action
      when :connect
        MKItLogger.info("docker network #{action} received: #{msg}")
        pod.update_ip
        pod.save
      when :disconnect
        MKItLogger.debug("  #{type} #{action} <<NOOP / TODO>>")
      else
        MKItLogger.debug("  #{type} #{action} <<TODO>>")
      end
    else
      MKItLogger.warn("docker <<#{type}>> <#{action}> received: #{msg}. But I don't know anything about pod #{pod_id}")
    end
  else
    MKItLogger.info("\t#{type} #{action} <<unknown>>")
  end
end

#register_consumer(consumer:) ⇒ Object



15
16
# File 'lib/mkit/docker_listener.rb', line 15

def register_consumer(consumer:)
end

#startObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mkit/docker_listener.rb', line 72

def start
  @thread ||= Thread.new {
    cmd = "docker events --format '{{json .}}'"
    begin
      PTY.spawn( cmd ) do |stdout, stdin, pid|
        begin
          stdout.each { |line| parse_message JSON.parse(line).to_o }
        rescue Errno::EIO
          MKItLogger.warn("Errno:EIO error, but this probably just means " +
            "that the process has finished giving output")
        end
      end
    rescue PTY::ChildExited
      MKItLogger.warn("docker event listener process exited!")
    end
  }
  @thread.run
  MKItLogger.info("docker listener started")
end

#stopObject



91
92
93
94
# File 'lib/mkit/docker_listener.rb', line 91

def stop
  @thread.exit if @thread
  MKItLogger.info("docker listener stopped")
end