capistrano-simple-unicorn 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1db2c480f35f80cae01006349015f008c7861d19
4
+ data.tar.gz: 891c868795519e72f1256ebb0674c07c94aeeae8
5
+ SHA512:
6
+ metadata.gz: 6316c3545f7084e83448fa109737b836c297f4b6d20ad21e2e9d3d1e52d6efa9e4e5c59ffe61e21e5afd60ba40920bd0c8eb77502e8e5ce482aeb3ca2d1ca41f
7
+ data.tar.gz: 7c5406d260126805d3eeeadbe64b4f76760fd1038f8a4c957cc02d2b9db68c8630db5d1e8c52fe9319436905cdccdbc7d526c61c3d6a6aabf8a1e65f72ab8829
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,56 @@
1
+ Capistrano task for automatic and unicorn configuration
2
+
3
+ This gem customize from [capistrano-unicorn-nginx](https://github.com/capistrano-plugins/capistrano-unicorn-nginx), and support for Ubuntu server, CentOs server, EC2 server...
4
+
5
+ Highlight of Gem:
6
+
7
+ * Automatic config unicorn for rails app
8
+ * Zero downtime deployments enabled
9
+
10
+ ## Installation
11
+
12
+ Add this to `Gemfile`:
13
+ ```
14
+ group :development do
15
+ gem "capistrano"
16
+ gem "capistrano-simple-unicorn"
17
+ end
18
+ ```
19
+ And then:
20
+ ```
21
+ $ bundle install
22
+ ```
23
+ ## Setup and usage
24
+
25
+ 1. Update `Capfile`
26
+ ```
27
+ require "capistrano/simple_unicorn"
28
+ ```
29
+ 2. Run deploy
30
+ ```
31
+ $ bundle exec cap production deploy
32
+ ```
33
+
34
+ ## configuration
35
+
36
+ Default value:
37
+ ```
38
+ set :unicorn_service, -> { "unicorn_#{fetch(:application)}" }
39
+ set :user_home_path, -> { "/home/#{fetch(:user)}" }
40
+ set :unicorn_config_file, -> { shared_path.join("config", "unicorn.rb") }
41
+ set :unicorn_pid_file, -> { shared_path.join("tmp", "pids", "unicorn.pid") }
42
+ set :unicorn_sock_file, -> { shared_path.join("tmp", "unicorn.sock") }
43
+ set :unicorn_log_file, -> { shaed_path.join("log", "unicorn.stdout.log") }
44
+ set :unicorn_error_log_file, -> {shared_path.join("log","unicorn.stderr.log")}
45
+ set :ruby_version, -> { fetch(:rvm_ruby_version) || fetch(:rbenv_ruby) }
46
+ set :unicorn_worker_processes, 2
47
+ set :unicorn_timeout, 30
48
+ ```
49
+
50
+ If you want to change config:
51
+ Example
52
+ ```
53
+ # in config/deploy/production.rb
54
+ set :unicorn_worker_processes, 4
55
+ set :unicorn_timeout, 60
56
+ ```
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'capistrano/simple_unicorn/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'capistrano-simple-unicorn'
8
+ s.version = Capistrano::SimpleUnicorn::VERSION
9
+ s.date = '2018-07-20'
10
+ s.summary = "Capistrano tasks for automatic and sensible unicorn configuration"
11
+ s.description = <<-EOF.gsub(/^\s+/, '')
12
+ Capistrano tasks for automatic and sensible unicorn configuration
13
+ Work *only* with Capistrano 3+
14
+ Enable Zero downtime deployments of rails application.
15
+ Support for Ubuntu server and Centos, EC2... server same fedora, .
16
+ This gem customize from https://github.com/bruno-/capistrano-unicorn-nginx
17
+ EOF
18
+ s.description = "Capistrano deploy rails app with unicorn zero downtime."
19
+ s.authors = ["truongkma"]
20
+ s.email = 'truong.nd1902@gmail.com'
21
+ s.files = `git ls-files`.split("\n")
22
+ s.require_paths = ['lib']
23
+ s.homepage = 'http://rubygems.org/gems/capistrano-simple-unicorn'
24
+ s.license = 'MIT'
25
+
26
+ s.add_runtime_dependency 'capistrano', '~> 3.1'
27
+ s.add_runtime_dependency 'sshkit', '~> 1.2'
28
+
29
+ s.add_development_dependency 'rake', '~> 0'
30
+ end
File without changes
@@ -0,0 +1 @@
1
+ load File.expand_path("../tasks/unicorn.rake", __FILE__)
@@ -0,0 +1,60 @@
1
+ require 'erb'
2
+
3
+ module Capistrano
4
+ module SimpleUnicorn
5
+ module Helpers
6
+ def template template_name
7
+ StringIO.new(template_to_s(template_name))
8
+ end
9
+
10
+ def template_to_s template_name
11
+ config_file = File.join(File.dirname(__FILE__), "../../generators/templates/#{template_name}")
12
+ ERB.new(File.read(config_file), nil, '-').result(binding)
13
+ end
14
+
15
+ def sudo_upload! from, to
16
+ filename = File.basename(to)
17
+ to_dir = File.dirname(to)
18
+ tmp_file = "/tmp/#{filename}"
19
+ upload! from, tmp_file
20
+ sudo :mv, tmp_file, to_dir
21
+ end
22
+
23
+ def file_exists? path
24
+ test "[ -e #{path} ]"
25
+ end
26
+
27
+ def deploy_user
28
+ capture :id, '-un'
29
+ end
30
+
31
+ def os_is_ubuntu?
32
+ capture(:cat, "/etc/*-release").include? "ubuntu"
33
+ end
34
+
35
+ def unicorn_initd_file
36
+ "/etc/init.d/#{fetch(:unicorn_service)}"
37
+ end
38
+
39
+ def unicorn_sock_file
40
+ shared_path.join("tmp", "unicorn.sock")
41
+ end
42
+
43
+ def unicorn_config_file
44
+ shared_path.join("config", "unicorn.rb")
45
+ end
46
+
47
+ def unicorn_pid_file
48
+ shared_path.join("tmp", "pids", "unicorn.pid")
49
+ end
50
+
51
+ def unicorn_error_log_file
52
+ shared_path.join("log", "unicorn.stderr.log")
53
+ end
54
+
55
+ def unicorn_log_file
56
+ shared_path.join("log", "unicorn.stdout.log")
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module SimpleUnicorn
3
+ VERSION = '0.0.1'.freeze
4
+ end
5
+ end
@@ -0,0 +1,61 @@
1
+ require 'capistrano/simple_unicorn/helpers'
2
+ include Capistrano::SimpleUnicorn::Helpers
3
+
4
+ namespace :load do
5
+ task :defaults do
6
+ set :unicorn_service, -> { "unicorn_#{fetch(:application)}" }
7
+ set :user_home_path, -> { "/home/#{fetch(:user)}" }
8
+ set :unicorn_config_file, -> { unicorn_config_file }
9
+ set :unicorn_pid_file, -> { unicorn_pid_file }
10
+ set :unicorn_sock_file, -> { unicorn_sock_file }
11
+ set :unicorn_log_file, -> { unicorn_log_file }
12
+ set :unicorn_error_log_file, -> { unicorn_error_log_file }
13
+ set :ruby_version, -> { fetch(:rvm_ruby_version) || fetch(:rbenv_ruby) }
14
+ set :unicorn_worker_processes, 2
15
+ set :unicorn_timeout, 30
16
+
17
+ set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids')
18
+ end
19
+ end
20
+
21
+ namespace :unicorn do
22
+ desc 'Unicorn Initializer'
23
+ task :unicorn_init do
24
+ on roles :app do
25
+ sudo_upload! template('unicorn_init.erb'), unicorn_initd_file
26
+ execute :chmod, '+x', unicorn_initd_file
27
+ if os_is_ubuntu?
28
+ sudo 'update-rc.d', '-f', fetch(:unicorn_service), 'defaults'
29
+ else
30
+ sudo 'chkconfig', '--add', fetch(:unicorn_service)
31
+ end
32
+ end
33
+ end
34
+
35
+ desc 'Setup unicorn config'
36
+ task :setup_unicorn_config do
37
+ on roles :app do
38
+ unless file_exists? fetch(:unicorn_config_file)
39
+ execute :mkdir, '-pv', File.dirname(fetch(:unicorn_config_file))
40
+ end
41
+ upload! template('unicorn.rb.erb'), fetch(:unicorn_config_file)
42
+ end
43
+ end
44
+
45
+ desc 'restart unicorn'
46
+ task 'restart' do
47
+ on roles :app do
48
+ invoke "unicorn:unicorn_init" unless file_exists?(unicorn_initd_file)
49
+ invoke 'unicorn:setup_unicorn_config'
50
+ if test "[ -f #{fetch(:unicorn_pid)} ]"
51
+ sudo 'service', fetch(:unicorn_service), 'restart'
52
+ else
53
+ sudo 'service', fetch(:unicorn_service), 'start'
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ namespace :deploy do
60
+ after :publishing, 'unicorn:restart'
61
+ end
@@ -0,0 +1,26 @@
1
+ worker_processes <%= fetch(:unicorn_worker_processes) %>
2
+ timeout <%= fetch(:unicorn_timeout) %>
3
+
4
+ listen "<%= fetch(:unicorn_sock_file) %>"
5
+ pid "<%= fetch(:unicorn_pid_file) %>"
6
+
7
+ stderr_path "<%= fetch(:unicorn_error_log_file) %>"
8
+ stdout_path "<%= fetch(:unicorn_log_file) %>"
9
+
10
+ preload_app true
11
+
12
+ before_fork do |server, worker|
13
+ defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
14
+
15
+ old_pid = "#{ server.config[:pid] }.oldbin"
16
+ unless old_pid == server.pid
17
+ begin
18
+ Process.kill :QUIT, File.read(old_pid).to_i
19
+ rescue Errno::ENOENT, Errno::ESRCH
20
+ end
21
+ end
22
+ end
23
+
24
+ after_fork do |server, worker|
25
+ defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
26
+ end
@@ -0,0 +1,66 @@
1
+ #!/bin/bash
2
+ ### BEGIN INIT INFO
3
+ # Provides: unicorn
4
+ # Required-Start: $remote_fs $syslog
5
+ # Required-Stop: $remote_fs $syslog
6
+ # Default-Start: 2 3 4 5
7
+ # Default-Stop: 0 1 6
8
+ # Short-Description: Manage unicorn server
9
+ # Description: Start, stop, restart unicorn server for a specific application.
10
+ ### END INIT INFO
11
+ set -e
12
+ HOME=<%= fetch(:user_home_path) %>
13
+ APP_ROOT=<%= current_path %>
14
+ PID=<%= fetch(:unicorn_pid_file) %>
15
+ USER=<%= fetch(:user) %>
16
+ ENV="<%= fetch(:stage) %>"
17
+ RUBY_VERSION="<%= fetch(:ruby_version) %>"
18
+ CONFIG_FILE="<%= fetch(:unicorn_config_file) %>"
19
+ CMD="cd $APP_ROOT && export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION=$RUBY_VERSION; $HOME/.rbenv/bin/rbenv exec bundle exec unicorn -c $CONFIG_FILE -E $ENV -D"
20
+
21
+ set -u
22
+
23
+ OLD_PIN="$PID.oldbin"
24
+
25
+ sig () {
26
+ test -s "$PID" && kill -$1 `cat $PID`
27
+ }
28
+
29
+ oldsig () {
30
+ test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
31
+ }
32
+
33
+ run () {
34
+ if [ "$(id -un)" = "$USER" ]; then
35
+ eval $1
36
+ else
37
+ su -c "$1" - $USER
38
+ fi
39
+ }
40
+
41
+ case "$1" in
42
+ start)
43
+ sig 0 && echo >&2 "Already running" && exit 0
44
+ run "$CMD"
45
+ ;;
46
+ stop)
47
+ sig QUIT && exit 0
48
+ echo >&2 "Not running"
49
+ ;;
50
+ force-stop)
51
+ sig TERM && exit 0
52
+ echo >&2 "Not running"
53
+ ;;
54
+ restart|reload|upgrade)
55
+ sig USR2 && sleep 3 && echo reloaded OK && exit 0
56
+ echo >&2 "Couldn't reload, starting '$CMD' instead"
57
+ run "$CMD"
58
+ ;;
59
+ reopen-logs)
60
+ sig USR1
61
+ ;;
62
+ *)
63
+ echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
64
+ exit 1
65
+ ;;
66
+ esac
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-simple-unicorn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - truongkma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-20 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'
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'
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 deploy rails app with unicorn zero downtime.
56
+ email: truong.nd1902@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - Gemfile
62
+ - README.md
63
+ - Rakefile
64
+ - capistrano-simple-unicorn.gemspec
65
+ - lib/capistrano-simple-unicorn.rb
66
+ - lib/capistrano/simple_unicorn.rb
67
+ - lib/capistrano/simple_unicorn/helpers.rb
68
+ - lib/capistrano/simple_unicorn/version.rb
69
+ - lib/capistrano/tasks/unicorn.rake
70
+ - lib/generators/templates/unicorn.rb.erb
71
+ - lib/generators/templates/unicorn_init.erb
72
+ homepage: http://rubygems.org/gems/capistrano-simple-unicorn
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.6.14
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Capistrano tasks for automatic and sensible unicorn configuration
96
+ test_files: []