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



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

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

.getUUIDObject



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

def self.getUUID
	@@global_app_uuid
end

.getVersionObject



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

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

.is_linuxObject



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

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

.is_windowsObject



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

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

.notifier_engineObject



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

def self.notifier_engine
	@@notifier_engine
end

.pidObject



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

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
# 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.



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

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.



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

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



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

def self.web_server
	@@web_server
end