Class: PhusionPassenger::AdminTools::ServerInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/phusion_passenger/admin_tools/server_instance.rb

Defined Under Namespace

Classes: CorruptedDirectoryError, GenerationsAbsentError, Group, Process, RoleDeniedError, StaleDirectoryError, Stats, UnsupportedGenerationStructureVersionError

Constant Summary collapse

STALE_TIME_THRESHOLD =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ServerInstance

Returns a new instance of ServerInstance.

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 139

def initialize(path)
	raise ArgumentError, "Path may not be nil." if path.nil?
	@path = path
	
	if File.exist?("#{path}/control_process.pid")
		data = File.read("#{path}/control_process.pid").strip
		@pid = data.to_i
	else
		path =~ /passenger\.\d+\.\d+\.(\d+)\Z/
		@pid = $1.to_i
	end
	
	generations = Dir["#{path}/generation-*"]
	if generations.empty?
		raise GenerationsAbsentError, "There are no generation subdirectories in this instance directory."
	end
	highest_generation_number = 0
	generations.each do |generation|
		File.basename(generation) =~ /(\d+)/
		generation_number = $1.to_i
		if generation_number > highest_generation_number
			highest_generation_number = generation_number
		end
	end
	@generation_path = "#{path}/generation-#{highest_generation_number}"
	
	if !File.exist?("#{@generation_path}/structure_version.txt")
		raise CorruptedDirectoryError, "The generation directory doesn't contain a structure version specification file."
	end
	version_data = File.read("#{@generation_path}/structure_version.txt").strip
	major, minor = version_data.split(".", 2)
	if major.nil? || minor.nil? || major !~ /\A\d+\Z/ || minor !~ /\A\d+\Z/
		raise CorruptedDirectoryError, "The generation directory doesn't contain a valid structure version specification file."
	end
	major = major.to_i
	minor = minor.to_i
	if major != PhusionPassenger::SERVER_INSTANCE_DIR_GENERATION_STRUCTURE_MAJOR_VERSION ||
	   minor > PhusionPassenger::SERVER_INSTANCE_DIR_GENERATION_STRUCTURE_MINOR_VERSION
		raise UnsupportedGenerationStructureVersionError, "Unsupported generation directory structure version."
	end
	
	if @pid == 0
		raise CorruptedDirectoryError, "Instance directory contains corrupted control_process.pid file."
	elsif !AdminTools.process_is_alive?(@pid)
		raise StaleDirectoryError, "There is no instance with PID #{@pid}."
	end
end

Instance Attribute Details

#generation_pathObject (readonly)

Returns the value of attribute generation_path.



105
106
107
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 105

def generation_path
  @generation_path
end

#pathObject (readonly)

Returns the value of attribute path.



104
105
106
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 104

def path
  @path
end

#pidObject (readonly)

Returns the value of attribute pid.



106
107
108
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 106

def pid
  @pid
end

Class Method Details

.for_pid(pid, options = {}) ⇒ Object



135
136
137
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 135

def self.for_pid(pid, options = {})
	return list(options).find { |c| c.pid == pid }
end

.list(options = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 108

def self.list(options = {})
	options = {
		:clean_stale_or_corrupted => true
	}.merge(options)
	instances = []
	
	Dir["#{AdminTools.tmpdir}/passenger.*"].each do |dir|
		next if File.basename(dir) !~ /passenger\.#{PhusionPassenger::SERVER_INSTANCE_DIR_STRUCTURE_MAJOR_VERSION}\.(\d+)\.(.+)\Z/
		minor = $1
		next if minor.to_i > PhusionPassenger::SERVER_INSTANCE_DIR_STRUCTURE_MINOR_VERSION
		
		begin
			instances << ServerInstance.new(dir)
		rescue StaleDirectoryError, CorruptedDirectoryError
			if options[:clean_stale_or_corrupted] &&
			   File.stat(dir).mtime < current_time - STALE_TIME_THRESHOLD
				log_cleaning_action(dir)
				FileUtils.chmod_R(0700, dir) rescue nil
				FileUtils.rm_rf(dir)
			end
		rescue UnsupportedGenerationStructureVersionError, GenerationsAbsentError
			# Do nothing.
		end
	end
	return instances
end

Instance Method Details

#analytics_log_dirObject



235
236
237
238
239
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 235

def analytics_log_dir
	return File.read("#{@generation_path}/analytics_log_dir.txt")
rescue Errno::ENOENT
	return nil
end

#connect(options) ⇒ Object

Raises:

  • ArgumentError: Unsupported role

  • RoleDeniedError: The user that the current process is as is not authorized to utilize the given role.

  • EOFError: The server unexpectedly closed the connection during authentication.

  • SecurityError: The server denied our authentication credentials.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 192

def connect(options)
	if options[:role]
		username, password, default_socket_name = infer_connection_info_from_role(options[:role])
		socket_name = options[:socket_name] || default_socket_name
	else
		username = options[:username]
		password = options[:password]
		socket_name = options[:socket_name] || "helper_admin"
		raise ArgumentError, "Either the :role or :username must be set" if !username
		raise ArgumentError, ":password must be set" if !password
	end
	
	client = MessageClient.new(username, password, "unix:#{@generation_path}/#{socket_name}")
	if block_given?
		begin
			yield client
		ensure
			client.close
		end
	else
		return client
	end
end

#get_wait_list_size(client) ⇒ Object



250
251
252
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 250

def get_wait_list_size(client)
	return stats(client).get_wait_list_size
end

#groups(client) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 254

def groups(client)
	doc = REXML::Document.new(client.pool_xml)
	
	groups = []
	doc.elements.each("info/supergroups/supergroup/group") do |group_xml|
		group = Group.new(group_xml.elements["app_root"].text,
			group_xml.elements["name"].text,
			group_xml.elements["environment"].text,
			!!group_xml.elements["spawning"])
		group_xml.elements.each("processes/process") do |process_xml|
			process = Process.new(group)
			process_xml.elements.each do |element|
				if element.name == "sockets"
					element.elements.each("socket") do |server_socket|
						name = server_socket.elements["name"].text.to_sym
						address = server_socket.elements["address"].text
						protocol = server_socket.elements["protocol"].text
						process.server_sockets[name] = OpenStruct.new(
							:name     => name,
							:address  => address,
							:protocol => protocol
						)
					end
				else
					if process.respond_to?("#{element.name}=")
						if Process::INT_PROPERTIES.include?(element.name.to_sym)
							value = element.text.to_i
						elsif Process::BOOL_PROPERTIES.include?(element.name.to_sym)
							value = element.text == "true"
						else
							value = element.text
						end
						process.send("#{element.name}=", value)
					end
				end
			end
			group.processes << process
		end
		groups << group
	end
	return groups
end

#helper_agent_pidObject



231
232
233
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 231

def helper_agent_pid
	return File.read("#{@generation_path}/helper_agent.pid").strip.to_i
end

#processes(client) ⇒ Object



297
298
299
300
301
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 297

def processes(client)
	return groups(client).map do |group|
		group.processes
	end.flatten
end

#stats(client) ⇒ Object



241
242
243
244
245
246
247
248
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 241

def stats(client)
	doc = REXML::Document.new(client.pool_xml)
	stats = Stats.new
	stats.max = doc.elements["info/max"].text.to_i
	stats.usage = doc.elements["info/usage"].text.to_i
	stats.get_wait_list_size = doc.elements["info/get_wait_list_size"].text.to_i
	return stats
end

#web_server_config_filesObject



220
221
222
223
224
225
226
227
228
229
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 220

def web_server_config_files
	config_files = File.read("#{@generation_path}/config_files.txt").split("\n")
	config_files.map! do |filename|
		filename.strip
	end
	config_files.reject do |filename|
		filename.empty?
	end
	return config_files
end

#web_server_descriptionObject



216
217
218
# File 'lib/phusion_passenger/admin_tools/server_instance.rb', line 216

def web_server_description
	return File.read("#{@generation_path}/web_server.txt")
end