Class: Appserver::App
- Inherits:
-
Struct
- Object
- Struct
- Appserver::App
- Defined in:
- lib/appserver/app.rb
Constant Summary collapse
- SETTINGS_DEFAULTS =
{ :branch => 'master', :ruby => Utils.find_in_path('ruby') || '/usr/bin/ruby', :environment => 'production', :user => nil, :group => nil, :instances => Utils.number_of_cpus || 1, :preload => false, :env_whitelist => [], :env => {}, :max_cpu_usage => nil, :max_memory_usage => nil, :usage_check_cycles => 5, :http_check_timeout => 30, :domain => Utils.system_domainname, :hostname => nil, :ssl_cert => nil, :ssl_key => nil, :public_dir => 'public', }
- SETTINGS_EXPAND =
[ :ssl_cert, :ssl_key ]
- ALWAYS_WHITELIST =
['PATH', 'PWD', 'GEM_HOME', 'GEM_PATH', 'RACK_ENV']
Instance Attribute Summary collapse
-
#branch ⇒ Object
Returns the value of attribute branch.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#env ⇒ Object
Returns the value of attribute env.
-
#env_whitelist ⇒ Object
Returns the value of attribute env_whitelist.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#group ⇒ Object
Returns the value of attribute group.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#http_check_timeout ⇒ Object
Returns the value of attribute http_check_timeout.
-
#instances ⇒ Object
Returns the value of attribute instances.
-
#max_cpu_usage ⇒ Object
Returns the value of attribute max_cpu_usage.
-
#max_memory_usage ⇒ Object
Returns the value of attribute max_memory_usage.
-
#name ⇒ Object
Returns the value of attribute name.
-
#preload ⇒ Object
Returns the value of attribute preload.
-
#public_dir ⇒ Object
Returns the value of attribute public_dir.
-
#ruby ⇒ Object
Returns the value of attribute ruby.
-
#server_dir ⇒ Object
Returns the value of attribute server_dir.
-
#ssl_cert ⇒ Object
Returns the value of attribute ssl_cert.
-
#ssl_key ⇒ Object
Returns the value of attribute ssl_key.
-
#usage_check_cycles ⇒ Object
Returns the value of attribute usage_check_cycles.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #access_log ⇒ Object
- #bundle_path ⇒ Object
- #exist? ⇒ Boolean
- #gem_file ⇒ Object
-
#initialize(server_dir, name, config) ⇒ App
constructor
A new instance of App.
- #path ⇒ Object
- #pid_file ⇒ Object
- #public_path ⇒ Object
- #rack? ⇒ Boolean
- #rack_config ⇒ Object
- #reopen_server_log ⇒ Object
- #restart_server ⇒ Object
- #revision_file ⇒ Object
- #server_log ⇒ Object
- #server_pid ⇒ Object
- #setup_env! ⇒ Object
- #socket ⇒ Object
- #ssl? ⇒ Boolean
- #start_server ⇒ Object
- #start_server? ⇒ Boolean
- #start_server_cmd ⇒ Object
- #stop_server ⇒ Object
- #unicorn_config ⇒ Object
Constructor Details
#initialize(server_dir, name, config) ⇒ App
Returns a new instance of App.
33 34 35 36 37 38 39 40 41 |
# File 'lib/appserver/app.rb', line 33 def initialize (server_dir, name, config) self.server_dir, self.name = server_dir, name # Apply configuration settings config.apply!(self, name) # Use the directory owner as the user to run instances under by default self.user ||= exist? ? Etc.getpwuid(File.stat(path).uid).name : 'www-data' # Use a subdomain if no hostname was given specifically for this app self.hostname ||= "#{name.gsub(/[^a-z0-9_-]+/i, '_')}.#{domain}" end |
Instance Attribute Details
#branch ⇒ Object
Returns the value of attribute branch
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def branch @branch end |
#domain ⇒ Object
Returns the value of attribute domain
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def domain @domain end |
#env ⇒ Object
Returns the value of attribute env
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def env @env end |
#env_whitelist ⇒ Object
Returns the value of attribute env_whitelist
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def env_whitelist @env_whitelist end |
#environment ⇒ Object
Returns the value of attribute environment
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def environment @environment end |
#group ⇒ Object
Returns the value of attribute group
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def group @group end |
#hostname ⇒ Object
Returns the value of attribute hostname
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def hostname @hostname end |
#http_check_timeout ⇒ Object
Returns the value of attribute http_check_timeout
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def http_check_timeout @http_check_timeout end |
#instances ⇒ Object
Returns the value of attribute instances
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def instances @instances end |
#max_cpu_usage ⇒ Object
Returns the value of attribute max_cpu_usage
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def max_cpu_usage @max_cpu_usage end |
#max_memory_usage ⇒ Object
Returns the value of attribute max_memory_usage
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def max_memory_usage @max_memory_usage end |
#name ⇒ Object
Returns the value of attribute name
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def name @name end |
#preload ⇒ Object
Returns the value of attribute preload
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def preload @preload end |
#public_dir ⇒ Object
Returns the value of attribute public_dir
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def public_dir @public_dir end |
#ruby ⇒ Object
Returns the value of attribute ruby
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def ruby @ruby end |
#server_dir ⇒ Object
Returns the value of attribute server_dir
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def server_dir @server_dir end |
#ssl_cert ⇒ Object
Returns the value of attribute ssl_cert
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def ssl_cert @ssl_cert end |
#ssl_key ⇒ Object
Returns the value of attribute ssl_key
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def ssl_key @ssl_key end |
#usage_check_cycles ⇒ Object
Returns the value of attribute usage_check_cycles
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def usage_check_cycles @usage_check_cycles end |
#user ⇒ Object
Returns the value of attribute user
4 5 6 |
# File 'lib/appserver/app.rb', line 4 def user @user end |
Instance Method Details
#access_log ⇒ Object
95 96 97 |
# File 'lib/appserver/app.rb', line 95 def access_log File.join(server_dir.log_path, "#{name}.access.log") end |
#bundle_path ⇒ Object
79 80 81 |
# File 'lib/appserver/app.rb', line 79 def bundle_path File.join(path, '.bundle') end |
#exist? ⇒ Boolean
47 48 49 |
# File 'lib/appserver/app.rb', line 47 def exist? File.directory?(path) end |
#gem_file ⇒ Object
75 76 77 |
# File 'lib/appserver/app.rb', line 75 def gem_file File.join(path, 'Gemfile') end |
#path ⇒ Object
43 44 45 |
# File 'lib/appserver/app.rb', line 43 def path File.join(server_dir.apps_path, name) end |
#pid_file ⇒ Object
83 84 85 |
# File 'lib/appserver/app.rb', line 83 def pid_file File.join(server_dir.tmp_path, "#{name}.pid") end |
#public_path ⇒ Object
59 60 61 |
# File 'lib/appserver/app.rb', line 59 def public_path File.(public_dir, path) end |
#rack? ⇒ Boolean
67 68 69 |
# File 'lib/appserver/app.rb', line 67 def rack? File.exist?(rack_config) end |
#rack_config ⇒ Object
63 64 65 |
# File 'lib/appserver/app.rb', line 63 def rack_config File.join(path, 'config.ru') end |
#reopen_server_log ⇒ Object
147 148 149 |
# File 'lib/appserver/app.rb', line 147 def reopen_server_log Process.kill(:USR1, server_pid) end |
#restart_server ⇒ Object
143 144 145 |
# File 'lib/appserver/app.rb', line 143 def restart_server Process.kill(:USR2, server_pid) end |
#revision_file ⇒ Object
51 52 53 |
# File 'lib/appserver/app.rb', line 51 def revision_file File.join(path, 'REVISION') end |
#server_log ⇒ Object
91 92 93 |
# File 'lib/appserver/app.rb', line 91 def server_log File.join(server_dir.log_path, "#{name}.server.log") end |
#server_pid ⇒ Object
135 136 137 |
# File 'lib/appserver/app.rb', line 135 def server_pid File.readlines(pid_file)[0].to_i rescue nil end |
#setup_env! ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/appserver/app.rb', line 99 def setup_env! # Apply whitelist if set if env_whitelist != '*' && env_whitelist != ['*'] ENV.reject! { |key, value| !env_whitelist.include?(key) && !ALWAYS_WHITELIST.include?(key) } end # Set environment variables if env ENV.update(env) end # Setup gem bundle if present if File.exist?(gem_file) && File.directory?(bundle_path) ENV.update({ 'GEM_HOME' => bundle_path, 'BUNDLE_PATH' => bundle_path }) # Remember load paths of required gems (which use autloading), before bundler takes away the load path remember_paths = $LOAD_PATH.select { |path| path =~ %r(/(unicorn|rack|appserver)[^/]*/) } # Load bundler and setup gem bundle require 'bundler' Bundler.setup # Re-add remembered load paths $LOAD_PATH.unshift *remember_paths end end |
#socket ⇒ Object
87 88 89 |
# File 'lib/appserver/app.rb', line 87 def socket File.join(server_dir.tmp_path, "#{name}.socket") end |
#ssl? ⇒ Boolean
55 56 57 |
# File 'lib/appserver/app.rb', line 55 def ssl? ssl_cert && ssl_key end |
#start_server ⇒ Object
131 132 133 |
# File 'lib/appserver/app.rb', line 131 def start_server exec start_server_cmd if start_server? end |
#start_server? ⇒ Boolean
127 128 129 |
# File 'lib/appserver/app.rb', line 127 def start_server? !!start_server_cmd end |
#start_server_cmd ⇒ Object
121 122 123 124 125 |
# File 'lib/appserver/app.rb', line 121 def start_server_cmd if rack? "#{ruby} -S -- unicorn -E #{environment} -Dc #{unicorn_config} #{rack_config}" end end |
#stop_server ⇒ Object
139 140 141 |
# File 'lib/appserver/app.rb', line 139 def stop_server Process.kill(:TERM, server_pid) end |
#unicorn_config ⇒ Object
71 72 73 |
# File 'lib/appserver/app.rb', line 71 def unicorn_config File.('../unicorn.conf.rb', __FILE__) end |