Class: Appserver::App

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

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

#branchObject

Returns the value of attribute branch

Returns:

  • (Object)

    the current value of branch



4
5
6
# File 'lib/appserver/app.rb', line 4

def branch
  @branch
end

#domainObject

Returns the value of attribute domain

Returns:

  • (Object)

    the current value of domain



4
5
6
# File 'lib/appserver/app.rb', line 4

def domain
  @domain
end

#envObject

Returns the value of attribute env

Returns:

  • (Object)

    the current value of env



4
5
6
# File 'lib/appserver/app.rb', line 4

def env
  @env
end

#env_whitelistObject

Returns the value of attribute env_whitelist

Returns:

  • (Object)

    the current value of env_whitelist



4
5
6
# File 'lib/appserver/app.rb', line 4

def env_whitelist
  @env_whitelist
end

#environmentObject

Returns the value of attribute environment

Returns:

  • (Object)

    the current value of environment



4
5
6
# File 'lib/appserver/app.rb', line 4

def environment
  @environment
end

#groupObject

Returns the value of attribute group

Returns:

  • (Object)

    the current value of group



4
5
6
# File 'lib/appserver/app.rb', line 4

def group
  @group
end

#hostnameObject

Returns the value of attribute hostname

Returns:

  • (Object)

    the current value of hostname



4
5
6
# File 'lib/appserver/app.rb', line 4

def hostname
  @hostname
end

#http_check_timeoutObject

Returns the value of attribute http_check_timeout

Returns:

  • (Object)

    the current value of http_check_timeout



4
5
6
# File 'lib/appserver/app.rb', line 4

def http_check_timeout
  @http_check_timeout
end

#instancesObject

Returns the value of attribute instances

Returns:

  • (Object)

    the current value of instances



4
5
6
# File 'lib/appserver/app.rb', line 4

def instances
  @instances
end

#max_cpu_usageObject

Returns the value of attribute max_cpu_usage

Returns:

  • (Object)

    the current value of max_cpu_usage



4
5
6
# File 'lib/appserver/app.rb', line 4

def max_cpu_usage
  @max_cpu_usage
end

#max_memory_usageObject

Returns the value of attribute max_memory_usage

Returns:

  • (Object)

    the current value of max_memory_usage



4
5
6
# File 'lib/appserver/app.rb', line 4

def max_memory_usage
  @max_memory_usage
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



4
5
6
# File 'lib/appserver/app.rb', line 4

def name
  @name
end

#preloadObject

Returns the value of attribute preload

Returns:

  • (Object)

    the current value of preload



4
5
6
# File 'lib/appserver/app.rb', line 4

def preload
  @preload
end

#public_dirObject

Returns the value of attribute public_dir

Returns:

  • (Object)

    the current value of public_dir



4
5
6
# File 'lib/appserver/app.rb', line 4

def public_dir
  @public_dir
end

#rubyObject

Returns the value of attribute ruby

Returns:

  • (Object)

    the current value of ruby



4
5
6
# File 'lib/appserver/app.rb', line 4

def ruby
  @ruby
end

#server_dirObject

Returns the value of attribute server_dir

Returns:

  • (Object)

    the current value of server_dir



4
5
6
# File 'lib/appserver/app.rb', line 4

def server_dir
  @server_dir
end

#ssl_certObject

Returns the value of attribute ssl_cert

Returns:

  • (Object)

    the current value of ssl_cert



4
5
6
# File 'lib/appserver/app.rb', line 4

def ssl_cert
  @ssl_cert
end

#ssl_keyObject

Returns the value of attribute ssl_key

Returns:

  • (Object)

    the current value of ssl_key



4
5
6
# File 'lib/appserver/app.rb', line 4

def ssl_key
  @ssl_key
end

#usage_check_cyclesObject

Returns the value of attribute usage_check_cycles

Returns:

  • (Object)

    the current value of usage_check_cycles



4
5
6
# File 'lib/appserver/app.rb', line 4

def usage_check_cycles
  @usage_check_cycles
end

#userObject

Returns the value of attribute user

Returns:

  • (Object)

    the current value of user



4
5
6
# File 'lib/appserver/app.rb', line 4

def user
  @user
end

Instance Method Details

#access_logObject



95
96
97
# File 'lib/appserver/app.rb', line 95

def access_log
  File.join(server_dir.log_path, "#{name}.access.log")
end

#bundle_pathObject



79
80
81
# File 'lib/appserver/app.rb', line 79

def bundle_path
  File.join(path, '.bundle')
end

#exist?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/appserver/app.rb', line 47

def exist?
  File.directory?(path)
end

#gem_fileObject



75
76
77
# File 'lib/appserver/app.rb', line 75

def gem_file
  File.join(path, 'Gemfile')
end

#pathObject



43
44
45
# File 'lib/appserver/app.rb', line 43

def path
  File.join(server_dir.apps_path, name)
end

#pid_fileObject



83
84
85
# File 'lib/appserver/app.rb', line 83

def pid_file
  File.join(server_dir.tmp_path, "#{name}.pid")
end

#public_pathObject



59
60
61
# File 'lib/appserver/app.rb', line 59

def public_path
  File.expand_path(public_dir, path)
end

#rack?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/appserver/app.rb', line 67

def rack?
  File.exist?(rack_config)
end

#rack_configObject



63
64
65
# File 'lib/appserver/app.rb', line 63

def rack_config
  File.join(path, 'config.ru')
end

#reopen_server_logObject



147
148
149
# File 'lib/appserver/app.rb', line 147

def reopen_server_log
  Process.kill(:USR1, server_pid)
end

#restart_serverObject



143
144
145
# File 'lib/appserver/app.rb', line 143

def restart_server
  Process.kill(:USR2, server_pid)
end

#revision_fileObject



51
52
53
# File 'lib/appserver/app.rb', line 51

def revision_file
  File.join(path, 'REVISION')
end

#server_logObject



91
92
93
# File 'lib/appserver/app.rb', line 91

def server_log
  File.join(server_dir.log_path, "#{name}.server.log")
end

#server_pidObject



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

#socketObject



87
88
89
# File 'lib/appserver/app.rb', line 87

def socket
  File.join(server_dir.tmp_path, "#{name}.socket")
end

#ssl?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/appserver/app.rb', line 55

def ssl?
  ssl_cert && ssl_key
end

#start_serverObject



131
132
133
# File 'lib/appserver/app.rb', line 131

def start_server
  exec start_server_cmd if start_server?
end

#start_server?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/appserver/app.rb', line 127

def start_server?
  !!start_server_cmd
end

#start_server_cmdObject



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_serverObject



139
140
141
# File 'lib/appserver/app.rb', line 139

def stop_server
  Process.kill(:TERM, server_pid)
end

#unicorn_configObject



71
72
73
# File 'lib/appserver/app.rb', line 71

def unicorn_config
  File.expand_path('../unicorn.conf.rb', __FILE__)
end