Skip to content

Commit

Permalink
add relative path calculations in preparation for pid auto-detection
Browse files Browse the repository at this point in the history
Also re-order and re-structure assignment and display of variables
for legibility.
  • Loading branch information
Adam Spiers committed Aug 24, 2013
1 parent 69ddc6e commit 6258ea2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 34 deletions.
33 changes: 24 additions & 9 deletions README.md
Expand Up @@ -62,19 +62,34 @@ cap unicorn:reload

## Configuration

You can modify any of the following options in your `deploy.rb` config.
You can modify any of the following Capistrano variables in your `deploy.rb` config.

### Environment parameters

- `unicorn_pid` - Set unicorn PID file path. Default to `current_path/tmp/pids/unicorn.pid`
- `unicorn_bin` - Set unicorn executable file. Default to `unicorn`.
- `unicorn_bundle` - Set bundler command for unicorn. Default to `bundle`.
- `unicorn_user` - Launch unicorn master as the specified user via `sudo`. Default to `nil`, which means no use of `sudo`, i.e. run as the user defined by the `user` variable.
- `unicorn_roles` - Define which roles to perform unicorn recipes on. Default to `:app`.
- `unicorn_config_path` - Set the directory where unicorn config files reside. Default to `current_path/config`.
- `unicorn_config_filename` - Set the filename of the unicorn config file loaded from `unicorn_config_path`. Should not be present in multistage installations. Default to `unicorn.rb`.
- `unicorn_env` - Set unicorn config file to be used loaded from `unicorn_config_path`. Default to `rails_env` variable if set, otherwise `production`.
- `unicorn_rack_env` - Set the value which will be passed to unicorn via [the `-E` parameter as the Rack environment](http://unicorn.bogomips.org/unicorn_1.html). Valid values are `development`, `deployment`, and `none`. Default to `development` if `rails_env` is `development`, otherwise `deployment`.

### Execution parameters

- `unicorn_user` - Launch unicorn master as the specified user via `sudo`. Default to `nil`, which means no use of `sudo`, i.e. run as the user defined by the `user` variable.
- `unicorn_roles` - Define which roles to perform unicorn recipes on. Default to `:app`.
- `unicorn_bundle` - Set bundler command for unicorn. Default to `bundle`.
- `unicorn_bin` - Set unicorn executable file. Default to `unicorn`.
- `unicorn_options` - Set any additional options to be passed to unicorn on startup.
- `app_subdir` - If your app lives in a subdirectory 'rails' (say) of your repository, set this to '/rails' (the leading slash is required).
- `unicorn_restart_sleep_time` - Number of seconds to wait for (old) pidfile to show up when restarting unicorn. Defaults to 2.

### Relative path parameters

- `app_subdir` - If your app lives in a subdirectory 'rails' (say) of your repository, set this to `/rails` (the leading slash is required).
- `unicorn_config_rel_path` - Set the directory path (relative to `app_path` - see below) where unicorn config files reside. Default to `config`.
- `unicorn_config_filename` - Set the filename of the unicorn config file loaded from `unicorn_config_path`. Should not be present in multistage installations. Default to `unicorn.rb`.

### Absolute path parameters

- `app_path` - Set path to app root. Default to `current_path + app_subdir`.
- `unicorn_pid` - Set unicorn PID file path. Default to `#{current_path}/tmp/pids/unicorn.pid`
- `bundle_gemfile` - Set path to Gemfile. Default to `#{app_path}/Gemfile`
- `unicorn_config_path` - Set the directory where unicorn config files reside. Default to `#{current_path}/config`.

You can use the `unicorn:show_vars` task to test the impact of your
`deploy.rb`.
Expand Down
69 changes: 44 additions & 25 deletions lib/capistrano-unicorn/capistrano_integration.rb
Expand Up @@ -25,23 +25,33 @@ def self.load_into(capistrano_config)
fetch(:rails_env) == 'development' ? 'development' : 'deployment'
end

# Paths
_cset(:app_subdir) { '' }
_cset(:app_path) { current_path + app_subdir }
_cset(:unicorn_pid) { app_path + "/tmp/pids/unicorn.pid" }
_cset(:bundle_gemfile) { app_path + '/Gemfile' }

# Execution
_cset(:unicorn_user) { nil }
_cset(:unicorn_bundle) { fetch(:bundle_cmd, "bundle") }
_cset(:unicorn_bin) { "unicorn" }
_cset(:unicorn_options) { '' }
_cset(:unicorn_restart_sleep_time) { 2 }
_cset(:unicorn_user) { nil }
_cset(:unicorn_config_path) { app_path + "/config" }

# Relative paths
_cset(:app_subdir) { '' }
_cset(:unicorn_config_rel_path) { "config" }
_cset(:unicorn_config_filename) { "unicorn.rb" }
_cset(:unicorn_config_file_path) { "#{unicorn_config_path}/#{unicorn_config_filename}" }
_cset(:unicorn_config_rel_file_path) \
{ unicorn_config_rel_path + '/' + unicorn_config_filename }
_cset(:unicorn_config_stage_rel_file_path) \
{ [ unicorn_config_rel_path, 'unicorn',
"#{unicorn_env}.rb" ].join('/') }

# Absolute paths
# If you find the following confusing, try running 'cap unicorn:show_vars' -
# it might help :-)
_cset(:app_path) { current_path + app_subdir }
_cset(:unicorn_pid) { app_path + "/tmp/pids/unicorn.pid" }
_cset(:bundle_gemfile) { app_path + '/Gemfile' }
_cset(:unicorn_config_path) { app_path + '/' + unicorn_config_rel_path }
_cset(:unicorn_config_file_path) { app_path + '/' + unicorn_config_rel_file_path }
_cset(:unicorn_config_stage_file_path) \
{ "#{unicorn_config_path}/unicorn/#{unicorn_env}.rb" }
{ app_path + '/' + unicorn_config_stage_rel_file_path }
end

# Check if a remote process exists using its pid file
Expand Down Expand Up @@ -163,23 +173,32 @@ def unicorn_roles
puts <<-EOF.gsub(/^ +/, '')
# Environments
rails_env "#{rails_env}"
unicorn_env "#{unicorn_env}"
unicorn_rack_env "#{unicorn_rack_env}"
# Paths
app_subdir "#{app_subdir}"
app_path "#{app_path}"
unicorn_pid "#{unicorn_pid}"
bundle_gemfile "#{bundle_gemfile}"
unicorn_config_path "#{unicorn_config_path}"
unicorn_config_filename "#{unicorn_config_filename}"
rails_env "#{rails_env}"
unicorn_env "#{unicorn_env}"
unicorn_rack_env "#{unicorn_rack_env}"
# Execution
unicorn_user #{unicorn_user.inspect}
unicorn_bundle "#{unicorn_bundle}"
unicorn_bin "#{unicorn_bin}"
unicorn_options "#{unicorn_options}"
unicorn_user #{unicorn_user.inspect}
unicorn_bundle "#{unicorn_bundle}"
unicorn_bin "#{unicorn_bin}"
unicorn_options "#{unicorn_options}"
unicorn_restart_sleep_time #{unicorn_restart_sleep_time}
# Relative paths
app_subdir "#{app_subdir}"
unicorn_config_rel_path "#{unicorn_config_rel_path}"
unicorn_config_filename "#{unicorn_config_filename}"
unicorn_config_rel_file_path "#{unicorn_config_rel_file_path}"
unicorn_config_stage_rel_file_path "#{unicorn_config_stage_rel_file_path}"
# Absolute paths
app_path "#{app_path}"
unicorn_pid "#{unicorn_pid}"
bundle_gemfile "#{bundle_gemfile}"
unicorn_config_path "#{unicorn_config_path}"
unicorn_config_file_path "#{unicorn_config_file_path}"
unicorn_config_stage_file_path
-> "#{unicorn_config_stage_file_path}"
EOF
end

Expand Down

0 comments on commit 6258ea2

Please sign in to comment.