unicorn_service 0.5.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: c0f009377762962b5f9714f0dde6e495d71a2880
4
+ data.tar.gz: 77eecff7878a1080bc179fa601de0c1860fb9685
5
+ SHA512:
6
+ metadata.gz: d2bb91a22ba10e6a179e0664cb8ffd11a591439d542425f528e1e553caa19a60c95ebf9da989ecd4eda25b1261b0517d4b43582a07282c5325a9ffe58281238b
7
+ data.tar.gz: 8bcdb414ba8df3afade9b4de926e35463d229b4c64427c273b2b7e92d8bcc9819f615790066d007efac6bdd2e9dba8563b20e11284b400bc9e1617e4a86e7ead
@@ -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
+
19
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in unicorn_service.gemspec
4
+ gemspec
5
+
6
+ gem 'capistrano-unicorn'
7
+
8
+ group :test do
9
+ gem 'rspec'
10
+ gem 'capistrano-spec'
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 max-konin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # UnicornService
2
+
3
+ Capistrano plugin that provide opportunity to create unicorn service. It gives capistrano task for create script in
4
+ /etc/init.d/ for unicorn's deamon and update rc.d for auto start up unicorn application after reboot system
5
+
6
+
7
+ ## Usage
8
+ ### Setup
9
+
10
+ Add the library to your Gemfile:
11
+
12
+ group :development do
13
+ gem 'unicorn_service', :require => false
14
+ end
15
+
16
+ And load it into your deployment script config/deploy.rb:
17
+
18
+ require 'unicorn_service'
19
+
20
+ Set constants:
21
+
22
+ set :application, 'your_application'
23
+ set :user, 'your_user'
24
+ set :deploy_to, '/path/to/app'
25
+
26
+ ###Run
27
+
28
+ For create deamon and update rc.d run:
29
+
30
+ bundle exec cap unicorn_service:create_script
31
+
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( http://github.com/<my-github-username>/unicorn_service/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require "unicorn_service/version"
2
+ require 'unicorn_service/capistrano_integration'
3
+
4
+ module UnicornService
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,52 @@
1
+ require 'unicorn_service/utility'
2
+
3
+ require 'capistrano'
4
+ require 'capistrano/version'
5
+
6
+ module UnicornService
7
+ class CapistranoIntegration
8
+
9
+ TASKS = %w(unicorn_service:create_script unicorn_service:update_rc unicorn_service:start)
10
+
11
+
12
+ def self.load_into(capistrano_config)
13
+
14
+ capistrano_config.load do
15
+ before(CapistranoIntegration::TASKS) do
16
+ not_seted = []
17
+ not_seted << 'application' if fetch(:application, nil).nil?
18
+ not_seted << 'deploy_to' if fetch(:deploy_to, nil).nil?
19
+ not_seted << 'user' if fetch(:user, nil).nil?
20
+ unless not_seted.empty?
21
+ fail "Necessary constants have not been initialized: #{not_seted.inject(''){|s, item| s + "#{item}; " }}"
22
+ end
23
+ end
24
+
25
+ extend Utility
26
+
27
+ namespace :unicorn_service do
28
+ desc 'Add script in /etc/init.d'
29
+ task :create_script do
30
+ put_sudo (create_initd_file deploy_to, user), "/etc/init.d/unicorn.#{application}"
31
+ run "#{sudo} chmod +x /etc/init.d/unicorn.#{application}"
32
+ end
33
+
34
+ desc 'Update rc.d'
35
+ task :update_rc do
36
+ run "#{sudo} update-rc.d unicorn.#{application} defaults"
37
+ end
38
+
39
+ desc 'start service'
40
+ task :start do
41
+ create_script
42
+ update_rc
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ if Capistrano::Configuration.instance
51
+ UnicornService::CapistranoIntegration.load_into(Capistrano::Configuration.instance)
52
+ end
@@ -0,0 +1,98 @@
1
+ module UnicornService
2
+ module Utility
3
+ #https://gist.github.com/mklnz/2407925
4
+ def put_sudo(data, to)
5
+ filename = File.basename(to)
6
+ to_directory = File.dirname(to)
7
+ put data, "/tmp/#{filename}"
8
+ run "#{sudo} mv /tmp/#{filename} #{to_directory}"
9
+ end
10
+
11
+ def create_initd_file(path_to_app, user)
12
+ <<-EOF
13
+ #!/bin/sh
14
+ ### BEGIN INIT INFO
15
+ # Provides: Unicorn
16
+ # Required-Start: $remote_fs $syslog
17
+ # Required-Stop: $remote_fs $syslog
18
+ # Default-Start: 2 3 4 5
19
+ # Default-Stop: 0 1 6
20
+ # Short-Description: Init script for unicorn instace
21
+ ### END INIT INFO
22
+ set -e
23
+
24
+ TIMEOUT=${TIMEOUT-60}
25
+ APP_ROOT=#{path_to_app}
26
+ APP_CURRENT=$APP_ROOT/current
27
+ APP_USER=#{user}
28
+ PID=$APP_ROOT/shared/pids/unicorn.pid
29
+ ENV=production
30
+ CMD="cd $APP_CURRENT && bundle exec unicorn -E $ENV -D -c $APP_CURRENT/config/unicorn.rb"
31
+ action="$1"
32
+ set -u
33
+
34
+ old_pid="$PID.oldbin"
35
+
36
+ cd $APP_CURRENT || exit 1
37
+
38
+ sig () {
39
+ test -s "$PID" && kill -$1 `cat $PID`
40
+ }
41
+
42
+ oldsig () {
43
+ test -s $old_pid && kill -$1 `cat $old_pid`
44
+ }
45
+
46
+ case $action in
47
+ start)
48
+ sig 0 && echo >&2 "Already running" && exit 0
49
+ pwd
50
+ su --login $APP_USER -c "$CMD"
51
+ ;;
52
+ stop)
53
+ sig QUIT && exit 0
54
+ echo >&2 "Not running"
55
+ ;;
56
+ force-stop)
57
+ sig TERM && exit 0
58
+
59
+ echo >&2 "Not running"
60
+ ;;
61
+ restart|reload)
62
+ sig HUP && echo reloaded OK && exit 0
63
+ echo >&2 "Couldn't reload, starting '$CMD' instead"
64
+ su --login $APP_USER -c "$CMD"
65
+ ;;
66
+
67
+ upgrade)
68
+ if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
69
+ then
70
+ n=$TIMEOUT
71
+ while test -s $old_pid && test $n -ge 0
72
+ do
73
+ printf '.' && sleep 1 && n=$(( $n - 1 ))
74
+ done
75
+ echo
76
+
77
+ if test $n -lt 0 && test -s $old_pid
78
+ then
79
+ echo >&2 "$old_pid still exists after $TIMEOUT seconds"
80
+ exit 1
81
+ fi
82
+ exit 0
83
+ fi
84
+ echo >&2 "Couldn't upgrade, starting '$CMD' instead"
85
+ su --login $APP_USER -c "$CMD"
86
+ ;;
87
+ reopen-logs)
88
+ sig USR1
89
+ ;;
90
+ *)
91
+ echo >&2 "Usage: $0 "
92
+ exit 1
93
+ ;;
94
+ esac
95
+ EOF
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,3 @@
1
+ module UnicornService
2
+ VERSION = "0.5.1"
3
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe UnicornService::CapistranoIntegration do
4
+ include UnicornService::Utility
5
+ before do
6
+ @configuration = Capistrano::Configuration.new
7
+ @configuration.extend(Capistrano::Spec::ConfigurationExtension)
8
+ UnicornService::CapistranoIntegration.load_into(@configuration)
9
+ @configuration.set :application, 'myapp'
10
+ @configuration.set :user, 'myuser'
11
+ @configuration.set :deploy_to, '/path/to/deploy'
12
+ end
13
+
14
+ it "defines unicorn_service:create_script" do
15
+ @configuration.find_task('unicorn_service:create_script').should_not == nil
16
+ end
17
+
18
+ it "defines unicorn_service:update_rc" do
19
+ @configuration.find_task('unicorn_service:update_rc').should_not == nil
20
+ end
21
+
22
+ it "defines unicorn_service:start" do
23
+ @configuration.find_task('unicorn_service:start').should_not == nil
24
+ end
25
+
26
+ describe 'task unicorn_service:create_script' do
27
+ before do
28
+ @configuration.find_and_execute_task 'unicorn_service:create_script'
29
+ end
30
+
31
+ it 'set file as executable' do
32
+ @configuration.should have_run("sudo -p 'sudo password: ' chmod +x /etc/init.d/unicorn.myapp")
33
+ end
34
+ end
35
+
36
+ describe 'task unicorn_service:update_rc' do
37
+ before do
38
+ @configuration.find_and_execute_task 'unicorn_service:update_rc'
39
+ end
40
+
41
+ it 'runs update rc commands' do
42
+ @configuration.should have_run("sudo -p 'sudo password: ' update-rc.d unicorn.myapp defaults")
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ require 'capistrano-spec'
2
+ require 'unicorn_service'
3
+
4
+ RSpec.configure do |config|
5
+ config.include Capistrano::Spec::Matchers
6
+ config.include Capistrano::Spec::Helpers
7
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+ require 'unicorn_service/utility'
3
+
4
+ describe UnicornService::Utility do
5
+ include UnicornService::Utility
6
+
7
+ describe '#create_initd_file' do
8
+ let(:example) {
9
+ <<-EOF
10
+ #!/bin/sh
11
+ ### BEGIN INIT INFO
12
+ # Provides: Unicorn
13
+ # Required-Start: $remote_fs $syslog
14
+ # Required-Stop: $remote_fs $syslog
15
+ # Default-Start: 2 3 4 5
16
+ # Default-Stop: 0 1 6
17
+ # Short-Description: Init script for unicorn instace
18
+ ### END INIT INFO
19
+ set -e
20
+ # Feel free to change any of the following variables for your app:
21
+ TIMEOUT=${TIMEOUT-60}
22
+ APP_ROOT=/path/to/app
23
+ APP_CURRENT=$APP_ROOT/current
24
+ APP_USER=myuser
25
+ PID=$APP_ROOT/shared/pids/unicorn.pid
26
+ ENV=production
27
+ CMD="cd $APP_CURRENT && bundle exec unicorn -E $ENV -D -c $APP_CURRENT/config/unicorn.rb"
28
+ action="$1"
29
+ set -u
30
+
31
+ old_pid="$PID.oldbin"
32
+
33
+ cd $APP_CURRENT || exit 1
34
+
35
+ sig () {
36
+ test -s "$PID" && kill -$1 'cat $PID'
37
+ }
38
+
39
+ oldsig () {
40
+ test -s $old_pid && kill -$1 'cat $old_pid'
41
+ }
42
+
43
+ case $action in
44
+ start)
45
+ sig 0 && echo >&2 "Already running" && exit 0
46
+ pwd
47
+ su --login $APP_USER -c "$CMD"
48
+ ;;
49
+ stop)
50
+ sig QUIT && exit 0
51
+ echo >&2 "Not running"
52
+ ;;
53
+ force-stop)
54
+ sig TERM && exit 0
55
+
56
+ echo >&2 "Not running"
57
+ ;;
58
+ restart|reload)
59
+ sig HUP && echo reloaded OK && exit 0
60
+ echo >&2 "Couldn't reload, starting '$CMD' instead"
61
+ su --login $APP_USER -c "$CMD"
62
+ ;;
63
+ upgrade)
64
+ if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
65
+ then
66
+ n=$TIMEOUT
67
+ while test -s $old_pid && test $n -ge 0
68
+ do
69
+ printf '.' && sleep 1 && n=$(( $n - 1 ))
70
+ done
71
+ echo
72
+
73
+ if test $n -lt 0 && test -s $old_pid
74
+ then
75
+ echo >&2 "$old_pid still exists after $TIMEOUT seconds"
76
+ exit 1
77
+ fi
78
+ exit 0
79
+ fi
80
+ echo >&2 "Couldn't upgrade, starting '$CMD' instead"
81
+ su --login $APP_USER -c "$CMD"
82
+ ;;
83
+ reopen-logs)
84
+ sig USR1
85
+ ;;
86
+ *)
87
+ echo >&2 "Usage: $0 "
88
+ exit 1
89
+ ;;
90
+ esac
91
+ EOF
92
+ }
93
+
94
+ it 'generate config for specific path to rails aplication and user' do
95
+ expect(create_initd_file('/path/to/app', 'myuser')).to eq(example)
96
+ end
97
+
98
+ end
99
+
100
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unicorn_service/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unicorn_service"
8
+ spec.version = UnicornService::VERSION
9
+ spec.authors = ["max-konin"]
10
+ spec.email = ["mk@kernel-corp.com"]
11
+ spec.summary = %q{Create unicorn service on linux server through capistrano.}
12
+ spec.description = %q{Capistrano plugin that provide opportunity to create unicorn service for autostart unicorn }
13
+ spec.homepage = "https://github.com/max-konin/unicorn_service.git"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "unicorn"
24
+ spec.add_runtime_dependency "capistrano", "< 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unicorn_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - max-konin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: unicorn
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
+ - !ruby/object:Gem::Dependency
56
+ name: capistrano
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - <
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - <
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: 'Capistrano plugin that provide opportunity to create unicorn service
70
+ for autostart unicorn '
71
+ email:
72
+ - mk@kernel-corp.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/unicorn_service.rb
83
+ - lib/unicorn_service/capistrano_integration.rb
84
+ - lib/unicorn_service/utility.rb
85
+ - lib/unicorn_service/version.rb
86
+ - spec/capistrano_integration_spec.rb
87
+ - spec/spec_helper.rb
88
+ - spec/utility_spec.rb
89
+ - unicorn_service.gemspec
90
+ homepage: https://github.com/max-konin/unicorn_service.git
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.0.14
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Create unicorn service on linux server through capistrano.
114
+ test_files:
115
+ - spec/capistrano_integration_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/utility_spec.rb