This repository has been archived by the owner on Jul 8, 2019. It is now read-only.
/
app.rb
110 lines (97 loc) · 3.68 KB
/
app.rb
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
99
100
101
102
103
104
105
106
107
108
109
110
module Appserver
class App < Struct.new(:server, :name, :unicorn, :environment, :instances,
:max_cpu_usage, :max_memory_usage, :usage_check_cycles,
:http_check_timeout, :hostname, :public_dir)
DEFAULTS = {
:unicorn => '/usr/local/bin/unicorn',
:environment => 'production',
:instances => 3,
:max_cpu_usage => nil,
:max_memory_usage => nil,
:usage_check_cycles => 5,
:http_check_timeout => 30,
:hostname => `/bin/hostname -f`.chomp.gsub(/^[^.]+\./, ''),
:public_dir => 'public',
}
def self.unicorn_config
File.expand_path('../unicorn.conf.rb', __FILE__)
end
def initialize (server, name, config)
super()
self.server, self.name = server, name
appconfig = (config[:apps] || {})[name.to_sym] || {}
DEFAULTS.each do |key, default_value|
self[key] = appconfig[key] || config[key] || default_value
end
# Use a subdomain of the default hostname if no hostname was given specifically for this app
self.hostname = "#{name}.#{hostname}" unless appconfig[:hostname]
end
def dir
File.join(server.dir, name)
end
def rack_config
File.join(dir, 'config.ru')
end
def rack?
File.exist?(rack_config)
end
def pid_file
File.join(server.tmp_dir, "#{name}.pid")
end
def socket
File.join(server.tmp_dir, "#{name}.socket")
end
def server_log
File.join(server.log_dir, "#{name}.server.log")
end
def access_log
File.join(server.log_dir, "#{name}.access.log")
end
def write_monit_config (f)
f.puts %Q()
f.puts %Q(# Application: #{name})
if rack?
cyclecheck = usage_check_cycles > 1 ? " for #{usage_check_cycles} cycles" : ''
f.puts %Q(check process #{name} with pidfile #{expand_path(pid_file)})
f.puts %Q( start program = "#{unicorn} -E #{environment} -Dc #{self.class.unicorn_config} #{rack_config}")
f.puts %Q( stop program = "/bin/kill `cat #{expand_path(pid_file)}`")
f.puts %Q( if totalcpu usage > #{max_cpu_usage}#{cyclecheck} then restart) if max_cpu_usage
f.puts %Q( if totalmemory usage > #{max_memory_usage}#{cyclecheck} then restart) if max_memory_usage
f.puts %Q( if failed unixsocket #{expand_path(socket)} protocol http request "/" timeout #{http_check_timeout} seconds then restart) if http_check_timeout > 0
f.puts %Q( if 5 restarts within 5 cycles then timeout)
f.puts %Q( group #{name})
end
end
def write_nginx_config (f)
f.puts ""
f.puts "# Application: #{name}"
if rack?
f.puts "upstream #{name}_cluster {"
f.puts " server unix:#{expand_path(socket)} fail_timeout=0;"
f.puts "}"
f.puts "server {"
f.puts " listen 80;"
f.puts " server_name #{hostname};"
f.puts " root #{expand_path(public_dir)};"
f.puts " access_log #{expand_path(access_log)};"
f.puts " location / {"
f.puts " proxy_set_header X-Real-IP $remote_addr;"
f.puts " proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;"
f.puts " proxy_set_header Host $http_host;"
f.puts " proxy_redirect off;"
# TODO: maintenance mode rewriting
f.puts " try_files $uri/index.html $uri.html $uri @#{name}_cluster;"
f.puts " error_page 500 502 503 504 /500.html;"
f.puts " }"
f.puts " location @#{name}_cluster {"
f.puts " proxy_pass http://#{name}_cluster;"
f.puts " }"
f.puts "}"
end
end
protected
def expand_path (path)
File.expand_path(path, dir)
end
end
end