capistrano-unicorn-nginx-osx 3.4.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c8ea5f66f09c11e062950723a340ee5e9ac9ddb
4
+ data.tar.gz: 625d63233826739354e5b4ed91d8a2fdcaee5e6f
5
+ SHA512:
6
+ metadata.gz: 276fc6a2f189c16ddd710c9f59c59c37db0a8a7e1399e0aaf4b6f5b9365b676e00fd60400f28347e63b80b7fe60a808059dff8cadb3068711b688d4cc75f526a
7
+ data.tar.gz: f2dd34faa52ef5066c58f648b9a9244232fbddfb4053181ee1a19b725dd0f9891d4e2b8054315d5a7a92494f8889c41dd10de1283b28c9acf0d5e0d081e38ac9
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ /vendor/ruby
19
+ .idea
data/CHANGELOG.md ADDED
@@ -0,0 +1,63 @@
1
+ # Changelog
2
+
3
+ ### master
4
+
5
+ ### v3.4.0, 2015-09-17
6
+ - Allow customizing paths for SSL certificate and key
7
+ - Use sudo to restart services
8
+ - Remove whitespace in template ERB files
9
+
10
+
11
+ ### v3.3.3, 2015-05-05
12
+ - add `unicorn_env` option for passing environmental variables to unicorn (@rhomeister)
13
+
14
+ ### v3.3.2, 2015-02-16
15
+ - bug fix: replaced non-existent `log_dir` with `unicorn_log_dir` (@rhomeister)
16
+
17
+ ### v3.3.1, 2015-02-16
18
+ - made nginx fail_timeout configurable (@rhomeister)
19
+ - added logrotate configuration for nginx logs (@rhomeister)
20
+
21
+ ### v3.3.0, 2015-02-09
22
+ - added client SSL authentication (@rhomeister)
23
+ - make unicorn timeout configurable (@vicentllongo)
24
+
25
+ ### v3.2.0, 2015-01-28
26
+ - allow 'PATCH' HTTP method in nginx_conf (@lonre)
27
+ - added SPDY support (@rhomeister)
28
+
29
+ ### v3.1.2, 2014-12-14
30
+ - removed HTML 405 method from `nginx.conf`
31
+
32
+ ### v3.1.1, 2014-10-09
33
+ - add `server_name` directive for the port 80 ssl block in nginx_conf
34
+
35
+ ### v3.1.0, 2014-10-07
36
+ - setup nginx and unicorn to use TCP if web and app roles are using different
37
+ servers
38
+ - update default nginx log dir location to `/var/log/nginx`
39
+
40
+ ### v3.0.0, 2014-10-05
41
+ - enable setting unicorn app environment with `rails_env` option.
42
+ If `rails_env` is not set, `stage` option is used as until now. (@bruno-)
43
+ - add load balancing support (@rhomeister)
44
+ - config files are updated each time `setup` task is run (@rhomeister)
45
+
46
+ ### v2.1.0, 2014-08-05
47
+ - add `nginx_location` option that specifies nginx installation dir
48
+ (@jordanyaker)
49
+
50
+ ### v2.0.0, 2014-04-11
51
+ - all the work is moved to the `setup` task
52
+
53
+ ### v1.0.2, 2014-03-30
54
+ - add `sudo_upload!` helper method for easier uploads
55
+ - improve the way how templates are handled
56
+
57
+ ### v1.0.1, 2014-03-30
58
+ - refactor all unicorn and nginx paths to separate modules
59
+ - speed up `nginx:setup_ssl` task
60
+ - small README update
61
+
62
+ ### v1.0.0, 2014-03-29
63
+ - @bruno- all the v1.0.0 features
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in *.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 Bruno Sutic
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the "Software"),
5
+ to deal in the Software without restriction, including without limitation
6
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ and/or sell copies of the Software, and to permit persons to whom the
8
+ Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included
11
+ in all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19
+ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Capistrano::UnicornNginxOsx
2
+
3
+ Capistrano tasks for automatic and sensible unicorn + nginx configuraion, especially, for Mac OSX server. This was customized to work on Mac OSX server, based upon [capistrano-plugins/capistrano-unicorn-nginx](https://github.com/capistrano-plugins/capistrano-unicorn-nginx) gem
4
+
5
+ Goals of this plugin:
6
+
7
+ * automatic unicorn and nginx configuration for Rails apps
8
+ * **no manual ssh** to the server required
9
+ * zero downtime deployments enabled
10
+ * support for single node as well as cluster deployments
11
+
12
+ Specifics:
13
+
14
+ * generates nginx config file on the server (web role)
15
+ * generates unicorn initializer and config files (app role)<br/>
16
+ application is started automatically after server restart
17
+ * capistrano tasks for server management, example: `unicorn:restart`<br/>
18
+ see below for all available tasks
19
+ * automatic load balancing setup when there are multiple `app` nodes
20
+
21
+ `capistrano-unicorn-nginx-osx` works only with Capistrano 3!
22
+
23
+ ### Installation
24
+
25
+ Add this to `Gemfile`:
26
+
27
+ group :development do
28
+ gem 'capistrano', '~> 3.4.0'
29
+ gem 'capistrano-unicorn-nginx-osx', '~> 3.4.0.1'
30
+ end
31
+
32
+ And then:
33
+
34
+ $ bundle install
35
+
36
+ ### Setup and usage
37
+
38
+ Depending on your needs 2 general scenarios are covered:
39
+
40
+ - [single server setup](https://github.com/capistrano-plugins/capistrano-unicorn-nginx/wiki/Single-server-setup)<br/>
41
+ A scenario where you run the webserver (nginx) and application server
42
+ (unicorn) on the same node.
43
+ - [multiple server setup](https://github.com/capistrano-plugins/capistrano-unicorn-nginx/wiki/Multiple-server-setup)<br/>
44
+ Webserver (nginx) and application server (unicorn) run on different nodes.
45
+
46
+ ### Default log file directories
47
+
48
+ - nginx: `/var/log/nginx/`
49
+ - unicorn: `#{shared_path}/log/`
50
+
51
+ ### Configuration
52
+
53
+ See the
54
+ [full options list on the wiki page](https://github.com/capistrano-plugins/capistrano-unicorn-nginx/wiki/Configuration).
55
+ Feel free to skip this unless you're looking for something specific.
56
+
57
+ ### How it works
58
+
59
+ [How it works wiki page](https://github.com/capistrano-plugins/capistrano-unicorn-nginx/wiki/How-it-works)
60
+ contains the list of tasks that the plugin executes.
61
+
62
+ You do not have to know this unless you want to learn more about the plugin internals.
63
+
64
+ ### Template customization
65
+
66
+ [On template customization wiki page](https://github.com/capistrano-plugins/capistrano-unicorn-nginx/wiki/Template-customization)
67
+ see how to inspect, tweak and override templates for `nginx` and `unicorn`
68
+ config files.
69
+
70
+ Do not do this unless you have a specific need.
71
+
72
+ ### More Capistrano automation?
73
+
74
+ Check out [capistrano-plugins](https://github.com/capistrano-plugins) github org.
75
+
76
+ ### Bug reports and pull requests
77
+
78
+ ...are very welcome!
79
+
80
+ ### Thanks
81
+
82
+ [@kalys](https://github.com/kalys) - for his
83
+ [capistrano-nginx-unicorn](https://github.com/kalys/capistrano-nginx-unicorn)
84
+ plugin.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'capistrano/unicorn_nginx_osx/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "capistrano-unicorn-nginx-osx"
8
+ gem.version = Capistrano::UnicornNginxOsx::VERSION
9
+ gem.authors = ["Ruben Stranders", "Bruno Sutic", "Lucius Choi"]
10
+ gem.email = ["r.stranders@gmail.com", "bruno.sutic@gmail.com", "lucius.choi@gmail.com"]
11
+ gem.description = <<-EOF.gsub(/^\s+/, '')
12
+ Capistrano tasks for automatic and sensible unicorn + nginx configuraion.
13
+ This was customized for Mac OSX Server by Lucius Choi.
14
+
15
+ Enables zero downtime deployments of Rails applications. Configs can be
16
+ copied to the application using generators and easily customized.
17
+
18
+ Works *only* with Capistrano 3+. For Capistrano 2 try version 0.0.8 of this
19
+ gem: http://rubygems.org/gems/capistrano-nginx-unicorn
20
+ EOF
21
+ gem.summary = "Capistrano tasks for automatic and sensible unicorn + nginx configuraion for Mac OSX."
22
+ gem.homepage = "https://github.com/luciuschoi/capistrano-unicorn-nginx-osx"
23
+
24
+ gem.files = `git ls-files`.split($/)
25
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
26
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
27
+ gem.require_paths = ["lib"]
28
+
29
+ gem.add_dependency "capistrano", ">= 3.1"
30
+ gem.add_dependency "sshkit", ">= 1.2.0"
31
+ gem.add_dependency "lunchy"
32
+
33
+ gem.add_development_dependency "rake"
34
+ end
File without changes
@@ -0,0 +1,53 @@
1
+ module Capistrano
2
+ module DSL
3
+ module NginxPaths
4
+
5
+ def nginx_servers_file
6
+ "#{fetch(:nginx_location)}/servers/#{fetch(:nginx_config_name)}"
7
+ end
8
+
9
+ def nginx_service_path
10
+ '/usr/local/bin/nginx'
11
+ end
12
+
13
+ def nginx_default_pid_file
14
+ '/usr/local/run/nginx.pid'
15
+ end
16
+
17
+ # ssl related files
18
+ def nginx_default_ssl_cert_file_name
19
+ "#{fetch(:nginx_server_name)}.crt"
20
+ end
21
+
22
+ def nginx_default_ssl_cert_key_file_name
23
+ "#{fetch(:nginx_server_name)}.key"
24
+ end
25
+
26
+ def nginx_default_ssl_cert_file_path
27
+ "/etc/ssl/certs/"
28
+ end
29
+
30
+ def nginx_default_ssl_cert_key_file_path
31
+ "/etc/ssl/private/"
32
+ end
33
+
34
+ def nginx_ssl_cert_file
35
+ "#{fetch(:nginx_ssl_cert_path)}#{fetch(:nginx_ssl_cert)}"
36
+ end
37
+
38
+ def nginx_ssl_cert_key_file
39
+ "#{fetch(:nginx_ssl_cert_key_path)}#{fetch(:nginx_ssl_cert_key)}"
40
+ end
41
+
42
+ # log files
43
+ def nginx_access_log_file
44
+ "/var/log/nginx/#{fetch(:nginx_config_name)}.access.log"
45
+ end
46
+
47
+ def nginx_error_log_file
48
+ "/var/log/nginx/#{fetch(:nginx_config_name)}.error.log"
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,34 @@
1
+ module Capistrano
2
+ module DSL
3
+ module UnicornPaths
4
+
5
+ def unicorn_default_config_file
6
+ shared_path.join('config/unicorn.rb')
7
+ end
8
+
9
+ def unicorn_default_launchd_plist
10
+ "~/Library/LaunchAgents/apps.#{fetch(:application)}.unicorn.plist"
11
+ end
12
+
13
+ def unicorn_default_pid_file
14
+ shared_path.join('tmp/pids/unicorn.pid')
15
+ end
16
+
17
+ def unicorn_log_dir
18
+ shared_path.join('log')
19
+ end
20
+
21
+ def unicorn_log_file
22
+ unicorn_log_dir.join('unicorn.stdout.log')
23
+ end
24
+
25
+ def unicorn_error_log_file
26
+ unicorn_log_dir.join('unicorn.stderr.log')
27
+ end
28
+
29
+ def unicorn_default_logrotate_config_file
30
+ "/usr/local/etc/logrotate.d/#{fetch(:application)}_production"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,84 @@
1
+ require 'capistrano/dsl/nginx_paths'
2
+ require 'capistrano/unicorn_nginx_osx/helpers'
3
+
4
+ include Capistrano::UnicornNginxOsx::Helpers
5
+ include Capistrano::DSL::NginxPaths
6
+
7
+ namespace :load do
8
+ task :defaults do
9
+ set :templates_path, 'config/deploy/templates'
10
+ set :nginx_config_name, "mediforum_production"
11
+ # set :nginx_config_name, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
12
+ set :nginx_pid, nginx_default_pid_file
13
+ # set :nginx_server_name # default set in the `nginx:defaults` task
14
+ # ssl options
15
+ set :nginx_location, -> { "#{fetch(:nginx_service_path)}" }
16
+ set :nginx_use_ssl, false
17
+ set :nginx_use_spdy, false
18
+ # if true, passes the SSL client certificate to the application server for consumption in Ruby code
19
+ set :nginx_pass_ssl_client_cert, false
20
+ set :nginx_ssl_cert, -> { nginx_default_ssl_cert_file_name }
21
+ set :nginx_ssl_cert_key, -> { nginx_default_ssl_cert_key_file_name }
22
+ set :nginx_ssl_cert_path, -> { nginx_default_ssl_cert_file_path }
23
+ set :nginx_ssl_cert_key_path, -> { nginx_default_ssl_cert_key_file_path }
24
+ set :nginx_upload_local_cert, true
25
+ set :nginx_ssl_cert_local_path, -> { ask(:nginx_ssl_cert_local_path, 'Local path to ssl certificate: ') }
26
+ set :nginx_ssl_cert_key_local_path, -> { ask(:nginx_ssl_cert_key_local_path, 'Local path to ssl certificate key: ') }
27
+ set :nginx_fail_timeout, 0 # see http://nginx.org/en/docs/http/ngx_http_upstream_module.html#fail_timeout
28
+ set :nginx_read_timeout, nil
29
+
30
+ set :linked_dirs, fetch(:linked_dirs, []).push('log')
31
+ end
32
+ end
33
+
34
+ namespace :nginx do
35
+
36
+ task :defaults do
37
+ on roles :web do
38
+ set :nginx_server_name, fetch(:nginx_server_name, host.to_s)
39
+ set :nginx_server_port, fetch(:nginx_server_port, 80)
40
+ end
41
+ end
42
+
43
+ desc 'Setup nginx configuration'
44
+ task :setup do
45
+ on roles :web do
46
+ sudo_upload! template('nginx_conf.erb'), nginx_servers_file
47
+ end
48
+ end
49
+
50
+ desc 'Setup nginx ssl certs'
51
+ task :setup_ssl do
52
+ next unless fetch(:nginx_use_ssl)
53
+ on roles :web do
54
+ next if file_exists?(nginx_ssl_cert_file) && file_exists?(nginx_ssl_cert_key_file)
55
+ if fetch(:nginx_upload_local_cert)
56
+ sudo_upload! fetch(:nginx_ssl_cert_local_path), nginx_ssl_cert_file
57
+ sudo_upload! fetch(:nginx_ssl_cert_key_local_path), nginx_ssl_cert_key_file
58
+ end
59
+ sudo :chown, 'root:root', nginx_ssl_cert_file
60
+ sudo :chown, 'root:root', nginx_ssl_cert_key_file
61
+ end
62
+ end
63
+
64
+ desc 'Reload nginx configuration'
65
+ task :reload do
66
+ on roles :web do
67
+ sudo nginx_service_path, '-s', 'reload'
68
+ end
69
+ end
70
+
71
+ before :setup, :defaults
72
+ before :setup_ssl, :defaults
73
+
74
+ end
75
+
76
+ namespace :deploy do
77
+ after :publishing, 'nginx:reload'
78
+ end
79
+
80
+ desc 'Server setup tasks'
81
+ task :setup do
82
+ invoke 'nginx:setup'
83
+ invoke 'nginx:setup_ssl'
84
+ end
@@ -0,0 +1,91 @@
1
+ require 'capistrano/dsl/unicorn_paths'
2
+ require 'capistrano/unicorn_nginx_osx/helpers'
3
+
4
+ include Capistrano::UnicornNginxOsx::Helpers
5
+ include Capistrano::DSL::UnicornPaths
6
+
7
+ namespace :load do
8
+ task :defaults do
9
+ set :templates_path, 'config/deploy/templates'
10
+ set :unicorn_pid, -> { unicorn_default_pid_file }
11
+ set :unicorn_config, -> { unicorn_default_config_file }
12
+ set :unicorn_plist, -> { unicorn_default_launchd_plist }
13
+ set :unicorn_logrotate_config, -> { unicorn_default_logrotate_config_file }
14
+ set :unicorn_workers, 2
15
+ set :unicorn_env, "" # environmental variables passed to unicorn/Ruby. Useful for GC tweaking, etc
16
+ set :unicorn_worker_timeout, 30
17
+ set :unicorn_tcp_listen_port, 8080
18
+ set :unicorn_use_tcp, -> { roles(:app, :web).count > 1 } # use tcp if web and app nodes are on different servers
19
+ set :unicorn_app_env, -> { fetch(:rails_env) || fetch(:stage) }
20
+ # set :unicorn_user # default set in `unicorn:defaults` task
21
+ set :unicorn_logrotate_enabled, false # by default, don't use logrotate to rotate unicorn logs
22
+ set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids')
23
+ end
24
+ end
25
+
26
+ namespace :unicorn do
27
+
28
+ task :defaults do
29
+ on roles :app do
30
+ set :unicorn_user, fetch(:unicorn_user, deploy_user)
31
+ end
32
+ end
33
+
34
+ desc 'Setup Unicorn initializer'
35
+ task :setup_initializer do
36
+ on roles :app do
37
+ execute :mkdir, '-pv', File.dirname(fetch(:unicorn_config))
38
+ upload! template('unicorn.rb.erb'), fetch(:unicorn_config)
39
+ sudo_upload! template('unicorn.plist.erb'), fetch(:unicorn_plist)
40
+ end
41
+ end
42
+
43
+ desc "restart unicorn"
44
+ task :restart do
45
+ on roles :app do
46
+ with RAILS_ENV: fetch(:environment) do
47
+ within "#{fetch(:deploy_to)}/current/" do
48
+ execute :bundle, :exec, :'lunchy', :restart, fetch(:application)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ after "deploy:restart", "unicorn:restart"
54
+
55
+ desc 'Setup Unicorn app configuration'
56
+ task :setup_app_config do
57
+ on roles :app do
58
+ execute :mkdir, '-pv', File.dirname(fetch(:unicorn_config))
59
+ upload! template('unicorn.rb.erb'), fetch(:unicorn_config)
60
+ end
61
+ end
62
+
63
+ desc 'Setup logrotate configuration'
64
+ task :setup_logrotate do
65
+ on roles :app do
66
+ sudo :mkdir, '-pv', File.dirname(fetch(:unicorn_logrotate_config))
67
+ sudo_upload! template('unicorn-logrotate.rb.erb'), fetch(:unicorn_logrotate_config)
68
+ sudo 'chown', 'root:root', fetch(:unicorn_logrotate_config)
69
+ end
70
+ end
71
+
72
+ before :setup_initializer, :defaults
73
+ before :setup_logrotate, :defaults
74
+
75
+ end
76
+
77
+ namespace :deploy do
78
+ after :publishing, 'unicorn:restart'
79
+ end
80
+
81
+ desc 'Server setup tasks'
82
+ task :setup do
83
+ invoke 'unicorn:setup_initializer'
84
+ invoke 'unicorn:setup_app_config'
85
+ if fetch(:unicorn_logrotate_enabled)
86
+ invoke 'unicorn:setup_logrotate'
87
+ end
88
+ end
89
+
90
+
91
+
@@ -0,0 +1,3 @@
1
+ require 'capistrano/dsl/nginx_paths'
2
+ load File.expand_path("../tasks/nginx.rake", __FILE__)
3
+ load File.expand_path("../tasks/unicorn.rake", __FILE__)
@@ -0,0 +1,58 @@
1
+ require 'erb'
2
+
3
+ module Capistrano
4
+ module UnicornNginxOsx
5
+ module Helpers
6
+
7
+ def bundle_unicorn(*args)
8
+ SSHKit::Command.new(:bundle, :exec, :unicorn, args).to_command
9
+ end
10
+
11
+ # renders the ERB template specified by template_name to string.
12
+ # Use the locals variable to pass locals to the ERB template
13
+ def template_to_s(template_name, locals = {})
14
+ config_file = "#{fetch(:templates_path)}/#{template_name}"
15
+ # if no customized file, proceed with default
16
+ unless File.exists?(config_file)
17
+ config_file = File.join(File.dirname(__FILE__), "../../generators/capistrano/unicorn_nginx_osx/templates/#{template_name}")
18
+ end
19
+
20
+ ERB.new(File.read(config_file), nil, '-').result(ERBNamespace.new(locals).get_binding)
21
+ end
22
+
23
+ # renders the ERB template specified by template_name to a StringIO buffer
24
+ def template(template_name, locals = {})
25
+ StringIO.new(template_to_s(template_name, locals))
26
+ end
27
+
28
+ def file_exists?(path)
29
+ test "[ -e #{path} ]"
30
+ end
31
+
32
+ def deploy_user
33
+ capture :id, '-un'
34
+ end
35
+
36
+ def sudo_upload!(from, to)
37
+ filename = File.basename(to)
38
+ to_dir = File.dirname(to)
39
+ tmp_file = "#{fetch(:tmp_dir)}/#{filename}"
40
+ upload! from, tmp_file
41
+ sudo :mv, tmp_file, to_dir
42
+ end
43
+
44
+ # Helper class to pass local variables to an ERB template
45
+ class ERBNamespace
46
+ def initialize(hash)
47
+ hash.each do |key, value|
48
+ singleton_class.send(:define_method, key) { value }
49
+ end
50
+ end
51
+
52
+ def get_binding
53
+ binding
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module UnicornNginxOsx
3
+ VERSION = "3.4.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ To create local nginx and unicorn configuration files call
2
+
3
+ bundle exec rails generate capistrano:unicorn_nginx_osx:config [path]
4
+
5
+ The default path is "config/deploy/templates". You can override it like so:
6
+
7
+ bundle rails generate capistrano:unicorn_nginx_osx:config "config/templates"
8
+
9
+ If you override templates path, don't forget to set "templates_path" variable in your deploy.rb
@@ -0,0 +1,19 @@
1
+ module Capistrano
2
+ module UnicornNginxOsx
3
+ module Generators
4
+ class ConfigGenerator < Rails::Generators::Base
5
+ desc "Create local nginx and unicorn configuration files for customization"
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ argument :templates_path, type: :string,
8
+ default: "config/deploy/templates",
9
+ banner: "path to templates"
10
+
11
+ def copy_template
12
+ copy_file "nginx_conf.erb", "#{templates_path}/nginx_conf.erb"
13
+ copy_file "unicorn.rb.erb", "#{templates_path}/unicorn.rb.erb"
14
+ copy_file "unicorn.plist.erb", "#{templates_path}/unicorn.plist.erb"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,86 @@
1
+ <% if fetch(:nginx_use_ssl) && nginx_pass_ssl_client_cert -%>
2
+ # source: http://forum.nginx.org/read.php?2,236546,236596
3
+ map $ssl_client_raw_cert $a {
4
+ "~^(-.*-\n)(?<1st>[^\n]+)\n((?<b>[^\n]+)\n)?((?<c>[^\n]+)\n)?((?<d>[^\n]+)\n)?((?<e>[^\n]+)\n)?((?<f>[^\n]+)\n)?((?<g>[^\n]+)\n)?((?<h>[^\n]+)\n)?((?<i>[^\n]+)\n)?((?<j>[^\n]+)\n)?((?<k>[^\n]+)\n)?((?<l>[^\n]+)\n)?((?<m>[^\n]+)\n)?((?<n>[^\n]+)\n)?((?<o>[^\n]+)\n)?((?<p>[^\n]+)\n)?((?<q>[^\n]+)\n)?((?<r>[^\n]+)\n)?((?<s>[^\n]+)\n)?((?<t>[^\n]+)\n)?((?<v>[^\n]+)\n)?((?<u>[^\n]+)\n)?((?<w>[^\n]+)\n)?((?<x>[^\n]+)\n)?((?<y>[^\n]+)\n)?((?<z>[^\n]+)\n)?(-.*-)$" $1st;
5
+ }
6
+ <% end -%>
7
+
8
+ server {
9
+ <% if fetch(:nginx_use_ssl) -%>
10
+ <% if fetch(:nginx_use_spdy) -%>
11
+ listen <%= ssl_port %> spdy;
12
+ <% else -%>
13
+ listen <%= ssl_port %>;
14
+ <% end -%>
15
+ ssl on;
16
+ ssl_certificate <%= nginx_ssl_cert_file %>;
17
+ ssl_certificate_key <%= nginx_ssl_cert_key_file %>;
18
+ <% else -%>
19
+ listen <%= fetch(:nginx_server_port) %>;
20
+ <% end -%>
21
+
22
+ <% if fetch(:nginx_use_ssl) && nginx_pass_ssl_client_cert -%>
23
+ ssl_verify_client optional_no_ca;
24
+ <% end -%>
25
+
26
+ client_max_body_size 4G;
27
+ keepalive_timeout 10;
28
+
29
+ error_page 500 502 504 /500.html;
30
+ error_page 503 @503;
31
+
32
+ server_name <%= fetch(:nginx_server_name) %>;
33
+ root <%= current_path %>/public;
34
+ try_files $uri/index.html $uri @unicorn_<%= fetch(:nginx_config_name) %>;
35
+
36
+ location @unicorn_<%= fetch(:nginx_config_name) %> {
37
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
38
+ proxy_set_header Host $http_host;
39
+ proxy_redirect off;
40
+ <% if fetch(:nginx_read_timeout) -%>
41
+ proxy_read_timeout <%= fetch(:nginx_read_timeout) %>;
42
+ <% end -%>
43
+ <% if fetch(:nginx_use_ssl) -%>
44
+ proxy_set_header X-Forwarded-Proto https;
45
+ <% end -%>
46
+ <% if fetch(:nginx_use_ssl) && nginx_pass_ssl_client_cert -%>
47
+ # source: http://forum.nginx.org/read.php?2,236546,236596
48
+ proxy_set_header X-Client-Cert $a$b$c$d$e$f$g$h$i$j$k$l$m$n$o$p$q$r$s$t$v$u$w$x$y$z;
49
+ <% end -%>
50
+
51
+ proxy_pass http://unicorn_<%= fetch(:nginx_config_name) %>;
52
+ # limit_req zone=one;
53
+ access_log <%= nginx_access_log_file %>;
54
+ error_log <%= nginx_error_log_file %>;
55
+ }
56
+
57
+ location ^~ /assets/ {
58
+ gzip_static on;
59
+ expires max;
60
+ add_header Cache-Control public;
61
+ }
62
+
63
+ location = /50x.html {
64
+ root html;
65
+ }
66
+
67
+ location = /404.html {
68
+ root html;
69
+ }
70
+
71
+ location @503 {
72
+ error_page 405 = /system/maintenance.html;
73
+ if (-f $document_root/system/maintenance.html) {
74
+ rewrite ^(.*)$ /system/maintenance.html break;
75
+ }
76
+ rewrite ^(.*)$ /503.html break;
77
+ }
78
+
79
+ if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
80
+ return 405;
81
+ }
82
+
83
+ if (-f $document_root/system/maintenance.html) {
84
+ return 503;
85
+ }
86
+ }
@@ -0,0 +1,25 @@
1
+ upstream unicorn_<%= fetch(:nginx_config_name) %> {
2
+ <% if fetch(:unicorn_use_tcp) -%>
3
+ <% roles(:app).each do |role| -%>
4
+ server <%= role.hostname %>:<%= fetch(:unicorn_tcp_listen_port)%> fail_timeout=<%= fetch(:nginx_fail_timeout) %>;
5
+ <% end -%>
6
+ <% else -%>
7
+ server unix:/tmp/unicorn.<%= fetch(:nginx_config_name) %>.sock fail_timeout=<%= fetch(:nginx_fail_timeout) %>;
8
+ <% end -%>
9
+ }
10
+
11
+ <% if fetch(:nginx_use_ssl) -%>
12
+ server {
13
+ listen <%= fetch(:nginx_server_port) %>;
14
+ server_name <%= fetch(:nginx_server_name) %>;
15
+ rewrite ^(.*) https://$host$1 permanent;
16
+ }
17
+ <% end -%>
18
+
19
+ <% # render the default server directive. If SSL is enabled, port 443 is used %>
20
+ <%= template_to_s("_default_server_directive.erb", ssl_port: 443, nginx_pass_ssl_client_cert: false).to_s %>
21
+
22
+ <% if fetch(:nginx_pass_ssl_client_cert) -%>
23
+ <% # render the server directive with SSL client certificate authentication enabled on port 444 %>
24
+ <%= template_to_s("_default_server_directive.erb", ssl_port: 444, nginx_pass_ssl_client_cert: true).to_s %>
25
+ <% end -%>
@@ -0,0 +1,25 @@
1
+ # logrotate.erb.conf
2
+ # Logrotate config for <%= fetch(:application) %> <%= fetch(:stage) %>
3
+ # Generated at <%= Time.now.strftime("%d.%m.%Y, %H:%M") %>
4
+
5
+ <%= unicorn_log_dir %>/*.log {
6
+ daily
7
+ missingok
8
+ rotate 180
9
+ compress
10
+ dateext
11
+ su <%= fetch(:unicorn_user) %> <%= fetch(:unicorn_user) %>
12
+
13
+ # this is important if using "compress" since we need to call
14
+ # the "lastaction" script below before compressing:
15
+ delaycompress
16
+
17
+ # note the lack of the evil "copytruncate" option in this
18
+ # config. Unicorn supports the USR1 signal and we send it
19
+ # as our "lastaction" action:
20
+ # USR1 - reopen all logs owned by the master and all workers
21
+ lastaction
22
+ pid=<%= fetch(:unicorn_pid) %>
23
+ test -s $pid && kill -USR1 "$(cat $pid)"
24
+ endscript
25
+ }
@@ -0,0 +1,29 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>EnvironmentVariables</key>
6
+ <dict>
7
+ <key>PATH</key>
8
+ <string>/Users/<%= fetch(:deploy_user)%>/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
9
+ <key>RBENV_ROOT</key>
10
+ <string>/Users/<%= fetch(:deploy_user)%>/.rbenv</string>
11
+ <key>RBENV_VERSION</key>
12
+ <string>2.2.3</string>
13
+ </dict>
14
+ <key>Label</key>
15
+ <string>apps.<%= fetch(:application)%>.unicorn</string>
16
+ <key>KeepAlive</key>
17
+ <true/>
18
+ <key>ProgramArguments</key>
19
+ <array>
20
+ <string>/Users/<%= fetch(:deploy_user)%>/.rbenv/shims/unicorn</string>
21
+ <string>-c</string>
22
+ <string>/Users/<%= fetch(:deploy_user)%>/apps/<%= fetch(:application) %>/shared/config/unicorn.rb</string>
23
+ <string>-E</string>
24
+ <string>production</string>
25
+ </array>
26
+ <key>RunAtLoad</key>
27
+ <true/>
28
+ </dict>
29
+ </plist>
@@ -0,0 +1,53 @@
1
+ working_directory "<%= current_path %>"
2
+ pid "<%= fetch(:unicorn_pid) %>"
3
+ stdout_path "<%= unicorn_log_file %>"
4
+ stderr_path "<%= unicorn_error_log_file %>"
5
+
6
+ <% if fetch(:unicorn_use_tcp) -%>
7
+ listen <%= fetch(:unicorn_tcp_listen_port) %>
8
+ <% else -%>
9
+ listen "/tmp/unicorn.<%= fetch(:nginx_config_name) %>.sock"
10
+ <% end -%>
11
+
12
+ worker_processes <%= fetch(:unicorn_workers) %>
13
+ timeout <%= fetch(:unicorn_worker_timeout) %>
14
+
15
+ preload_app true
16
+
17
+ before_exec do |server|
18
+ ENV["BUNDLE_GEMFILE"] = "<%= current_path %>/Gemfile"
19
+ end
20
+
21
+ before_fork do |server, worker|
22
+ # Disconnect since the database connection will not carry over
23
+ if defined? ActiveRecord::Base
24
+ ActiveRecord::Base.connection.disconnect!
25
+ end
26
+
27
+ # Quit the old unicorn process
28
+ old_pid = "#{server.config[:pid]}.oldbin"
29
+ if File.exists?(old_pid) && server.pid != old_pid
30
+ begin
31
+ Process.kill("QUIT", File.read(old_pid).to_i)
32
+ rescue Errno::ENOENT, Errno::ESRCH
33
+ # someone else did our job for us
34
+ end
35
+ end
36
+
37
+ if defined?(Resque)
38
+ Resque.redis.quit
39
+ end
40
+
41
+ sleep 1
42
+ end
43
+
44
+ after_fork do |server, worker|
45
+ # Start up the database connection again in the worker
46
+ if defined?(ActiveRecord::Base)
47
+ ActiveRecord::Base.establish_connection
48
+ end
49
+
50
+ if defined?(Resque)
51
+ Resque.redis = 'localhost:6379'
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-unicorn-nginx-osx
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.4.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ruben Stranders
8
+ - Bruno Sutic
9
+ - Lucius Choi
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-10-13 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: capistrano
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '3.1'
29
+ - !ruby/object:Gem::Dependency
30
+ name: sshkit
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 1.2.0
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.2.0
43
+ - !ruby/object:Gem::Dependency
44
+ name: lunchy
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rake
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ description: |
72
+ Capistrano tasks for automatic and sensible unicorn + nginx configuraion.
73
+ This was customized for Mac OSX Server by Lucius Choi.
74
+ Enables zero downtime deployments of Rails applications. Configs can be
75
+ copied to the application using generators and easily customized.
76
+ Works *only* with Capistrano 3+. For Capistrano 2 try version 0.0.8 of this
77
+ gem: http://rubygems.org/gems/capistrano-nginx-unicorn
78
+ email:
79
+ - r.stranders@gmail.com
80
+ - bruno.sutic@gmail.com
81
+ - lucius.choi@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - ".gitignore"
87
+ - CHANGELOG.md
88
+ - Gemfile
89
+ - LICENSE.md
90
+ - README.md
91
+ - Rakefile
92
+ - capistrano-unicorn-nginx-osx.gemspec
93
+ - lib/capistrano-unicorn-nginx-osx.rb
94
+ - lib/capistrano/dsl/nginx_paths.rb
95
+ - lib/capistrano/dsl/unicorn_paths.rb
96
+ - lib/capistrano/tasks/nginx.rake
97
+ - lib/capistrano/tasks/unicorn.rake
98
+ - lib/capistrano/unicorn_nginx_osx.rb
99
+ - lib/capistrano/unicorn_nginx_osx/helpers.rb
100
+ - lib/capistrano/unicorn_nginx_osx/version.rb
101
+ - lib/generators/capistrano/unicorn_nginx_osx/USAGE.md
102
+ - lib/generators/capistrano/unicorn_nginx_osx/config_generator.rb
103
+ - lib/generators/capistrano/unicorn_nginx_osx/templates/_default_server_directive.erb
104
+ - lib/generators/capistrano/unicorn_nginx_osx/templates/nginx_conf.erb
105
+ - lib/generators/capistrano/unicorn_nginx_osx/templates/unicorn-logrotate.rb.erb
106
+ - lib/generators/capistrano/unicorn_nginx_osx/templates/unicorn.plist.erb
107
+ - lib/generators/capistrano/unicorn_nginx_osx/templates/unicorn.rb.erb
108
+ homepage: https://github.com/luciuschoi/capistrano-unicorn-nginx-osx
109
+ licenses: []
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.5
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Capistrano tasks for automatic and sensible unicorn + nginx configuraion
131
+ for Mac OSX.
132
+ test_files: []