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



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

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

.get_pidObject



121
122
123
124
125
126
127
# File 'lib/kldockeragent/agent.rb', line 121

def self.get_pid
	if pid.nil?
		puts "nil"
	else
		puts "#{pid}"
	end
end

.getUUIDObject



151
152
153
# File 'lib/kldockeragent/agent.rb', line 151

def self.getUUID
	@@global_app_uuid
end

.getVersionObject



155
156
157
# File 'lib/kldockeragent/agent.rb', line 155

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

.is_linuxObject



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

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

.is_windowsObject



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

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

.notifier_engineObject



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

def self.notifier_engine
	@@notifier_engine
end

.pidObject



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

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
69
# 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 
	 		sleep(1)
	 	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.



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

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.



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

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



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

def self.web_server
	@@web_server
end