Module: KL::Agent

Defined in:
lib/kldockeragent/agent.rb

Constant Summary collapse

PIDFile =
"#{KL.home}/kldockeragent.pid"
@@notifier_engine =
KL::Notifier.new
@@web_server =
KL::Server.new
@@global_app_uuid =
KL.getApplicationId

Class Method Summary collapse

Class Method Details

.closeObject



137
138
139
140
# File 'lib/kldockeragent/agent.rb', line 137

def self.close
  KL.logger.info "[main] Stopping agent..."
  @@main_enabled = false
end

.getUUIDObject



142
143
144
# File 'lib/kldockeragent/agent.rb', line 142

def self.getUUID
  @@global_app_uuid
end

.getVersionObject



146
147
148
# File 'lib/kldockeragent/agent.rb', line 146

def self.getVersion
  File.read(File.dirname(__FILE__) + '/../../VERSION').strip
end

.is_linuxObject



125
126
127
# File 'lib/kldockeragent/agent.rb', line 125

def self.is_linux
  (RbConfig::CONFIG['host_os'] =~ /linux/)
end

.is_windowsObject



121
122
123
# File 'lib/kldockeragent/agent.rb', line 121

def self.is_windows
  (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
end

.notifier_engineObject



129
130
131
# File 'lib/kldockeragent/agent.rb', line 129

def self.notifier_engine
  @@notifier_engine
end

.pidObject



102
103
104
105
106
107
108
109
# File 'lib/kldockeragent/agent.rb', line 102

def self.pid
  begin
    pid = File.read(PIDFile).to_i
    return pid if Process.kill 0, pid
  rescue
  end
  nil
end

.start(opts = {}) ⇒ Object

Start the agent.

options: :daemon => true if running as a daemon, false if as a console application :port => port of web server will listen to :ssl => set true to enable HTTPS :certfile => certificate file path for HTTPS :keyfile => key file path for HTTPS



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
# File 'lib/kldockeragent/agent.rb', line 31

def self.start(opts={})
  KL.logger.info "[main] Starting agent: #{KL::Agent.getUUID}"
  puts "Starting agent..."

  @@config = opts

  Process.daemon if opts[:daemon] and not opts[:mock]
  begin
     if not is_windows
      # trap signal
      ['INT', 'HUP'].each do |signal|
        trap(signal) {
          KL.logger.info "[main] Shutting down services..."
          web_server.stop
          notifier_engine.stop
          loop do
            break if notifier_engine.status == :stopped
            sleep 1
          end

          self.close
        }
      end
    end

     File.open(PIDFile, 'w', 0644) { |f| f.write($$.to_s) }

     notifier_engine.start
     web_server.start

     @@main_enabled = true
     while @@main_enabled; end

  rescue Exception => e
     KL.logger.error "Starting the agent [Failed] #{e}\n#{e.backtrace.join("\n")}"
     raise e
  end
end

.statusObject

Return agent’s PID if it is running, otherwise nil.



113
114
115
116
117
118
119
# File 'lib/kldockeragent/agent.rb', line 113

def self.status
  if pid.nil?
    puts "Agent is not running."
  else
    puts "Agent is running with PID #{pid}"
  end
end

.stop(opts = {}) ⇒ Object

Stop the agent’s daemon.



72
73
74
75
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
# File 'lib/kldockeragent/agent.rb', line 72

def self.stop(opts={})
   begin
     pid = File.read(PIDFile).to_i
     puts "Stopping agent with PID #{pid}..."
     Process.kill 'HUP', pid

    if not opts[:mock]
      begin
        sleep (KL::Notifier::SleepTime + 0.5)

        Process.kill 0, pid
        KL.logger.info "[main] Agent is still running."
        puts "Agent is still running."

        KL.logger.info "[main] Killing agent."
        puts "Killing agent."
        Process.kill 9, pid
      rescue
        KL.logger.info "[main] Agent has stopped."
        puts "Agent has stopped."
        File.delete(PIDFile) if File.exist?(PIDFile)
      end
    end

   rescue
     puts "Agent is not running."
     File.delete(PIDFile) if File.exist?(PIDFile)
   end
end

.web_serverObject



133
134
135
# File 'lib/kldockeragent/agent.rb', line 133

def self.web_server
  @@web_server
end