File: nginx_controller.rb

package info (click to toggle)
passenger 2.2.11debian-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 11,576 kB
  • ctags: 28,138
  • sloc: cpp: 66,323; ruby: 9,646; ansic: 2,425; python: 141; sh: 56; makefile: 29
file content (98 lines) | stat: -rw-r--r-- 2,044 bytes parent folder | download | duplicates (2)
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
68
69
70
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
require 'erb'
require 'phusion_passenger/platform_info'
begin
	require 'daemon_controller'
rescue LoadError
	STDERR.puts "*** ERROR: daemon_controller is not installed. Please install with: "
	STDERR.puts
	STDERR.puts "  gem install FooBarWidget-daemon_controller -s http://gems.github.com"
	STDERR.puts
	exit!(1)
end

class NginxController
	PASSENGER_ROOT = File.expand_path(File.dirname(__FILE__) + "/../..")
	TEMPLATE_DIR = File.expand_path(File.dirname(__FILE__) + "/../stub/nginx")
	PORT = 64507
	
	def initialize(root_dir)
		root_dir     = File.expand_path(root_dir)
		@passenger_root = PASSENGER_ROOT
		@nginx_root  = root_dir
		@port        = PORT
		@config_file = "#{root_dir}/nginx.conf"
		@pid_file    = "#{root_dir}/nginx.pid"
		@log_file    = "#{root_dir}/error.log"
		@controller  = DaemonController.new(
		      :identifier    => 'Nginx',
		      :start_command => "#{CONFIG['nginx']} -c '#{@config_file}'",
		      :ping_command  => lambda { TCPSocket.new('localhost', PORT) },
		      :pid_file      => @pid_file,
		      :log_file      => @log_file,
		      :timeout       => 25,
		      :before_start  => method(:write_nginx_config_files)
		)
		
		@servers = []
	end
	
	def set(options)
		options.each_pair do |key, value|
			instance_variable_set("@#{key}", value)
		end
	end
	
	def start
		@controller.stop
		@controller.start
	end
	
	def stop
		@controller.stop
	end
	
	def running?
		return @controller.running?
	end
	
	def port
		return @port
	end
	
	def add_server
		server = Server.new
		yield server
		@servers << server
	end

private
	class Server
		attr_accessor :values
		attr_accessor :extra
		
		def initialize
			@values = { :passenger_enabled => "on" }
		end
		
		def [](key)
			return @values[key]
		end
		
		def []=(key, value)
			@values[key] = value
		end
		
		def <<(text)
			@extra = text
		end
	end

	def write_nginx_config_files
		template = ERB.new(File.read("#{TEMPLATE_DIR}/nginx.conf.erb"))
		File.write(@config_file, template.result(get_binding))
	end
	
	def get_binding
		return binding
	end
end