summaryrefslogtreecommitdiffstats
path: root/roles/docker_img_proxy/files/proxy_container/ipc-watcher.rb
blob: 202a58124b0631a2032c6b10c85aa92529ee9037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
#!/usr/bin/env ruby

require 'fileutils'

module OpenShift
  module Ops
    class Notify
      def self.puts(msg)
        $stdout.puts "#{Time.now}: #{msg}"
      end
    end

    class WatchForIpcs
      IPC_DIR = '/var/run/ctr-ipc'
      POLL_INTERVAL = 10 # second
      HAPROXY_CONF = '/etc/haproxy/haproxy.cfg'
      HAPROXY_PID_FILE = '/var/run/haproxy.pid'

      def self.wait_for_service()
        loop do
          Dir.glob("#{IPC_DIR}/service/*").each do |svc_file|
            svc = File.basename(svc_file)
            action = File.read(svc_file)
            Notify.puts "Found IPC service file: #{svc}"
            Notify.puts "      Action requested: #{action}"

            # Make sure we don't handle this multiple times
            FileUtils.rm(svc_file)

            handle_service_ipc(svc, action)
          end

          sleep POLL_INTERVAL
        end
      end

      def self.handle_service_ipc(svc, action)
        cmd = nil
        case svc
        when 'httpd'
          case action
          when 'restart', 'reload'
            cmd = "/usr/sbin/apachectl -k graceful"
          end
        when 'haproxy'
          case action
          when 'restart'
            cmd = "/usr/sbin/haproxy -f #{HAPROXY_CONF} -p #{HAPROXY_PID_FILE} -sf $(/bin/cat #{HAPROXY_PID_FILE})"
          end
        end

  if cmd.nil?
          Notify.puts "  Warning: Not handling #{svc} #{action}"
          return
        end

        Notify.puts "  Running: #{cmd}"
        output = %x[#{cmd} 2>&1]
        Notify.puts "  Output: #{output}"
      end
    end
  end
end

if __FILE__ == $0
  OpenShift::Ops::WatchForIpcs.wait_for_service()
end