capistrano-passenger5-nginx 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8fe28665d633a045a29d5147385c5cf8c44ac083
4
+ data.tar.gz: 73b5253dcd355d3b12ada43be3a0b15f8de0b6fc
5
+ SHA512:
6
+ metadata.gz: a46a236c69ec8353bad25974ff2d294782c2e231c6fb8dca918d19c4bf58fe84372a09951c1fa3b1fd13b5c9fde88e6f0b808b5d53c5644d2039242a2d0f978e
7
+ data.tar.gz: 05bb460e973dd5b87f20cbae5a2e594052afbe6b49ad070f7aca3960e5e7784e7e8d763990fbe02d60cf0e6d7763874a00d0f9cefb69401caa7874b5bd9caac3
@@ -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
@@ -0,0 +1 @@
1
+ # Changelog
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in *.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright © 2015 Standout AB
2
+ Copyright (c) 2014 Bruno Sutic
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the "Software"),
6
+ to deal in the Software without restriction, including without limitation
7
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
+ and/or sell copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included
12
+ in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20
+ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,84 @@
1
+ # Capistrano Passenger 5 and Nginx
2
+
3
+ This gem intends to ease the deployment of Rails apps with capistrano to passenger5 and nginx setup.
4
+
5
+ Warning: It is only tested with Capistrano 3 and some parts of the gem might be specific to our own usage (live `rbenv`). You are welcome to change it or send pull requests.
6
+
7
+
8
+ ### What it does?
9
+
10
+ * Deploy Rails apps to passenger 5 and Nginx combination on Server.
11
+ * Ability to generate and upload a default Nginx configuration file for the application
12
+
13
+
14
+ # How to use
15
+
16
+ ## Gemfile
17
+
18
+ Add this to `Gemfile`:
19
+
20
+ group :development do
21
+ gem "capistrano", "~> 3.3"
22
+ gem 'capistrano-rbenv', '~> 2.0'
23
+ gem 'capistrano-passenger5-nginx' , :git => 'https://github.com/standout/capistrano-passenger5-nginx.git'
24
+ end
25
+
26
+ And then:
27
+
28
+ $ bundle
29
+
30
+ ## Capfile
31
+
32
+ Add this to your `Capfile`
33
+
34
+
35
+ require 'capistrano/passenger5_nginx'
36
+
37
+
38
+ ## Setup and usage
39
+
40
+ rails g capistrano:passenger5_nginx:config
41
+
42
+ Above command will generate the default nginx config file in your project which will be at living at `/config/deploy/templates/nginx_conf.erb`
43
+
44
+ Warning: Below line is commented out in `nginx_conf.rb` file, if your ruby version is different than the version passenger5 uses, then uncomment this line and change the path with the one one your application should use.
45
+
46
+ `# passenger_ruby /your/ruby/version/path/2.2.0/bin/ruby;`
47
+
48
+ ## Default log file directories
49
+
50
+ Nginx will be logging everything to `/opt/nginx/log/` directory
51
+
52
+ ## Configuration
53
+
54
+ There are some flags that should be added to your production/staging.rb
55
+
56
+ These are:
57
+ * rails_env
58
+ * nginx_server_name
59
+ * nginx_use_ssl (true|false)
60
+ * passenger_min_instances (if you want to have more than one instances running your app, default 1)
61
+
62
+ Here is an example for production.rb
63
+
64
+ set :nginx_server_name, 'app.example.com'
65
+ set :nginx_use_ssl, true
66
+ set :rails_env, 'production'
67
+ set :passenger_min_instances, 2
68
+
69
+
70
+ ## After deploy
71
+
72
+ Run the command below for uploading the nginx configuration and reloading the nginx for the first time deploy. This config file is not uploaded after each deploy so should be done manually.
73
+
74
+ `bundle exec cap production nginx:setup bundle exec cap production nginx:reload`
75
+
76
+ ### Thanks
77
+
78
+ [@kalys](https://github.com/kalys) - for his
79
+ [capistrano-nginx-unicorn](https://github.com/kalys/capistrano-nginx-unicorn)
80
+ plugin.
81
+
82
+ [@bruno-](https://github.com/bruno-) - for his
83
+ [capistrano-unicorn-nginx](https://github.com/capistrano-plugins/capistrano-unicorn-nginx)
84
+ plugin.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'capistrano/passenger5_nginx/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "capistrano-passenger5-nginx"
8
+ gem.version = Capistrano::Passenger5Nginx::VERSION
9
+ gem.authors = ["Hüseyin Öztürk"]
10
+ gem.email = ["support@standout.se"]
11
+ gem.description = "Capistrano meets passenger 5"
12
+ gem.summary = "Capistrano deploys to Passenger 5 + nginx configuraion"
13
+ gem.homepage = "https://github.com/standout/capistrano-passenger5-nginx"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "capistrano", ">= 3.1"
21
+ gem.add_dependency "sshkit", ">= 1.2.0"
22
+
23
+ gem.add_development_dependency "rake"
24
+ end
File without changes
@@ -0,0 +1,49 @@
1
+ module Capistrano
2
+ module DSL
3
+ module NginxPaths
4
+
5
+ def nginx_sites_available_file
6
+ "#{fetch(:nginx_location)}/sites-available/#{fetch(:nginx_config_name)}"
7
+ end
8
+
9
+ def nginx_sites_enabled_file
10
+ "#{fetch(:nginx_location)}/sites-enabled/#{fetch(:nginx_config_name)}"
11
+ end
12
+
13
+ def nginx_service_path
14
+ '/etc/init.d/nginx'
15
+ end
16
+
17
+ def nginx_default_pid_file
18
+ '/opt/nginx/logs/nginx.pid'
19
+ end
20
+
21
+ # ssl related files
22
+ def nginx_default_ssl_cert_file_name
23
+ "#{fetch(:nginx_server_name)}.crt"
24
+ end
25
+
26
+ def nginx_default_ssl_cert_key_file_name
27
+ "#{fetch(:nginx_server_name)}.key"
28
+ end
29
+
30
+ def nginx_ssl_cert_file
31
+ "/etc/ssl/certs/#{fetch(:nginx_ssl_cert)}"
32
+ end
33
+
34
+ def nginx_ssl_cert_key_file
35
+ "/etc/ssl/private/#{fetch(:nginx_ssl_cert_key)}"
36
+ end
37
+
38
+ # log files
39
+ def nginx_access_log_file
40
+ "/opt/nginx/access.log"
41
+ end
42
+
43
+ def nginx_error_log_file
44
+ "/opt/nginx/error.log"
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,2 @@
1
+ require 'capistrano/dsl/nginx_paths'
2
+ load File.expand_path("../tasks/nginx.rake", __FILE__)
@@ -0,0 +1,54 @@
1
+ require 'erb'
2
+
3
+ module Capistrano
4
+ module Passenger5Nginx
5
+ module Helpers
6
+
7
+ # renders the ERB template specified by template_name to string. Use the locals variable to pass locals to the
8
+ # ERB template
9
+ def template_to_s(template_name, locals = {})
10
+ config_file = "#{fetch(:templates_path)}/#{template_name}"
11
+ # if no customized file, proceed with default
12
+ unless File.exists?(config_file)
13
+ config_file = File.join(File.dirname(__FILE__), "../../generators/capistrano/passenger5_nginx/templates/#{template_name}")
14
+ end
15
+
16
+ ERB.new(File.read(config_file)).result(ERBNamespace.new(locals).get_binding)
17
+ end
18
+
19
+ # renders the ERB template specified by template_name to a StringIO buffer
20
+ def template(template_name, locals = {})
21
+ StringIO.new(template_to_s(template_name, locals))
22
+ end
23
+
24
+ def file_exists?(path)
25
+ test "[ -e #{path} ]"
26
+ end
27
+
28
+ def deploy_user
29
+ capture :id, '-un'
30
+ end
31
+
32
+ def sudo_upload!(from, to)
33
+ filename = File.basename(to)
34
+ to_dir = File.dirname(to)
35
+ tmp_file = "#{fetch(:tmp_dir)}/#{filename}"
36
+ upload! from, tmp_file
37
+ sudo :mv, tmp_file, to_dir
38
+ end
39
+
40
+ # Helper class to pass local variables to an ERB template
41
+ class ERBNamespace
42
+ def initialize(hash)
43
+ hash.each do |key, value|
44
+ singleton_class.send(:define_method, key) { value }
45
+ end
46
+ end
47
+
48
+ def get_binding
49
+ binding
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module Passenger5Nginx
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,80 @@
1
+ require 'capistrano/dsl/nginx_paths'
2
+ require 'capistrano/passenger5_nginx/helpers'
3
+
4
+ include Capistrano::Passenger5Nginx::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, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
11
+ set :nginx_pid, nginx_default_pid_file
12
+ # set :nginx_server_name # default set in the `nginx:defaults` task
13
+ # ssl options
14
+ set :nginx_location, '/opt/nginx'
15
+ set :nginx_use_ssl, false
16
+ set :nginx_use_spdy, false
17
+ # if true, passes the SSL client certificate to the application server for consumption in Ruby code
18
+ set :nginx_pass_ssl_client_cert, false
19
+ set :nginx_ssl_cert, -> { nginx_default_ssl_cert_file_name }
20
+ set :nginx_ssl_cert_key, -> { nginx_default_ssl_cert_key_file_name }
21
+ set :nginx_upload_local_cert, true
22
+ set :nginx_ssl_cert_local_path, -> { ask(:nginx_ssl_cert_local_path, 'Local path to ssl certificate: ') }
23
+ set :nginx_ssl_cert_key_local_path, -> { ask(:nginx_ssl_cert_key_local_path, 'Local path to ssl certificate key: ') }
24
+ set :nginx_fail_timeout, 0 # see http://nginx.org/en/docs/http/ngx_http_upstream_module.html#fail_timeout
25
+
26
+ set :linked_dirs, fetch(:linked_dirs, []).push('log')
27
+ end
28
+ end
29
+
30
+ namespace :nginx do
31
+
32
+ task :defaults do
33
+ on roles :web do
34
+ set :nginx_server_name, fetch(:nginx_server_name, host.to_s)
35
+ end
36
+ end
37
+
38
+ desc 'Setup nginx configuration'
39
+ task :setup do
40
+ on roles :web do
41
+ sudo_upload! template('nginx_conf.erb'), nginx_sites_available_file
42
+ sudo :ln, '-fs', nginx_sites_available_file, nginx_sites_enabled_file
43
+ end
44
+ end
45
+
46
+ desc 'Setup nginx ssl certs'
47
+ task :setup_ssl do
48
+ next unless fetch(:nginx_use_ssl)
49
+ on roles :web do
50
+ next if file_exists?(nginx_ssl_cert_file) && file_exists?(nginx_ssl_cert_key_file)
51
+ if fetch(:nginx_upload_local_cert)
52
+ sudo_upload! fetch(:nginx_ssl_cert_local_path), nginx_ssl_cert_file
53
+ sudo_upload! fetch(:nginx_ssl_cert_key_local_path), nginx_ssl_cert_key_file
54
+ end
55
+ sudo :chown, 'root:root', nginx_ssl_cert_file
56
+ sudo :chown, 'root:root', nginx_ssl_cert_key_file
57
+ end
58
+ end
59
+
60
+ desc 'Reload nginx configuration'
61
+ task :reload do
62
+ on roles :web do
63
+ sudo nginx_service_path, 'reload'
64
+ end
65
+ end
66
+
67
+ before :setup, :defaults
68
+ before :setup_ssl, :defaults
69
+
70
+ end
71
+
72
+ namespace :deploy do
73
+ after :publishing, 'nginx:reload'
74
+ end
75
+
76
+ desc 'Server setup tasks'
77
+ task :setup do
78
+ invoke 'nginx:setup'
79
+ invoke 'nginx:setup_ssl'
80
+ end
@@ -0,0 +1,9 @@
1
+ To create local nginx and passenger 5 configuration files call
2
+
3
+ bundle exec rails generate capistrano:passenger5_nginx:config [path]
4
+
5
+ The default path is "config/deploy/templates". You can override it like so:
6
+
7
+ bundle rails generate capistrano:passenger5_nginx: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,17 @@
1
+ module Capistrano
2
+ module Passenger5Nginx
3
+ module Generators
4
+ class ConfigGenerator < Rails::Generators::Base
5
+ desc "Create local nginx and passenger 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
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,76 @@
1
+ <% if fetch(:nginx_use_ssl) %>
2
+ server {
3
+ listen 80;
4
+ server_name <%= fetch(:nginx_server_name) %>;
5
+ return 301 https://$server_name$request_uri;
6
+ }
7
+ <% end %>
8
+
9
+ server {
10
+ <% if fetch(:nginx_use_ssl) %>
11
+ listen 443;
12
+ charset UTF-8;
13
+ ssl on;
14
+ ssl_certificate <%= current_path %>/config/ssl/server.crt;
15
+ ssl_certificate_key <%= current_path %>/config/ssl/server.key;
16
+ <% else %>
17
+ listen 80;
18
+ <% end %>
19
+ server_name <%= fetch(:nginx_server_name) %>;
20
+
21
+ # passenger_ruby /your/ruby/version/path/2.2.0/bin/ruby;
22
+ passenger_enabled on;
23
+ passenger_app_env <%= fetch(:rails_env) %>;
24
+
25
+ <% if fetch(:passenger_min_instances) %>
26
+ passenger_min_instances <%= fetch(:passenger_min_instances) %>;
27
+ <% end %>
28
+
29
+ client_max_body_size 4G;
30
+ keepalive_timeout 10;
31
+
32
+ error_page 500 502 504 /500.html;
33
+ error_page 503 @503;
34
+
35
+ root <%= current_path %>/public;
36
+
37
+
38
+ location ^~ /assets/ {
39
+ gzip_static on;
40
+ expires max;
41
+ add_header Cache-Control public;
42
+ }
43
+
44
+ location = /50x.html {
45
+ root html;
46
+ }
47
+
48
+ location = /404.html {
49
+ root html;
50
+ }
51
+
52
+ location @503 {
53
+ error_page 405 = /system/maintenance.html;
54
+ if (-f $document_root/system/maintenance.html) {
55
+ rewrite ^(.*)$ /system/maintenance.html break;
56
+ }
57
+ rewrite ^(.*)$ /503.html break;
58
+ }
59
+
60
+ if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
61
+ return 405;
62
+ }
63
+
64
+ if (-f $document_root/system/maintenance.html) {
65
+ return 503;
66
+ }
67
+
68
+ location ~ \.(php)$ {
69
+ return 405;
70
+ }
71
+ }
72
+ <% if fetch(:nginx_use_ssl) %>
73
+ passenger_pre_start https://<%=fetch(:nginx_server_name)%>:443;
74
+ <% else %>
75
+ passenger_pre_start http://<%=fetch(:nginx_server_name)%>;
76
+ <% end %>
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-passenger5-nginx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hüseyin Öztürk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sshkit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Capistrano meets passenger 5
56
+ email:
57
+ - support@standout.se
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CHANGELOG.md
64
+ - Gemfile
65
+ - LICENSE.md
66
+ - README.md
67
+ - Rakefile
68
+ - capistrano-passenger5-nginx.gemspec
69
+ - lib/capistrano-passenger5-nginx.rb
70
+ - lib/capistrano/dsl/nginx_paths.rb
71
+ - lib/capistrano/passenger5_nginx.rb
72
+ - lib/capistrano/passenger5_nginx/helpers.rb
73
+ - lib/capistrano/passenger5_nginx/version.rb
74
+ - lib/capistrano/tasks/nginx.rake
75
+ - lib/generators/capistrano/passenger5_nginx/USAGE.md
76
+ - lib/generators/capistrano/passenger5_nginx/config_generator.rb
77
+ - lib/generators/capistrano/passenger5_nginx/templates/nginx_conf.erb
78
+ homepage: https://github.com/standout/capistrano-passenger5-nginx
79
+ licenses: []
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Capistrano deploys to Passenger 5 + nginx configuraion
101
+ test_files: []