kapify 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/kapify.gemspec +22 -0
- data/lib/generators/kapify/base.rb +9 -0
- data/lib/generators/kapify/logrotate/USAGE +9 -0
- data/lib/generators/kapify/logrotate/logrotate_generator.rb +14 -0
- data/lib/generators/kapify/logrotate/templates/logrotate.erb +8 -0
- data/lib/generators/kapify/nginx/USAGE +9 -0
- data/lib/generators/kapify/nginx/nginx_generator.rb +14 -0
- data/lib/generators/kapify/nginx/templates/nginx_conf.erb +85 -0
- data/lib/generators/kapify/pg/USAGE +9 -0
- data/lib/generators/kapify/pg/pg_generator.rb +14 -0
- data/lib/generators/kapify/pg/templates/database.yml.erb +8 -0
- data/lib/generators/kapify/resque/USAGE +9 -0
- data/lib/generators/kapify/resque/resque_generator.rb +14 -0
- data/lib/generators/kapify/resque/templates/resque_init.erb +89 -0
- data/lib/generators/kapify/unicorn/USAGE +9 -0
- data/lib/generators/kapify/unicorn/templates/unicorn.rb.erb +48 -0
- data/lib/generators/kapify/unicorn/templates/unicorn_init.erb +84 -0
- data/lib/generators/kapify/unicorn/unicorn_generator.rb +15 -0
- data/lib/kapify/base.rb +17 -0
- data/lib/kapify/logrotate/README.md +83 -0
- data/lib/kapify/logrotate/configuration.rb +15 -0
- data/lib/kapify/logrotate.rb +1 -0
- data/lib/kapify/nginx/README.md +105 -0
- data/lib/kapify/nginx/configuration.rb +41 -0
- data/lib/kapify/nginx.rb +1 -0
- data/lib/kapify/pg/README.md +118 -0
- data/lib/kapify/pg/configuration.rb +52 -0
- data/lib/kapify/pg.rb +1 -0
- data/lib/kapify/resque/README.md +104 -0
- data/lib/kapify/resque/configuration.rb +34 -0
- data/lib/kapify/resque.rb +1 -0
- data/lib/kapify/unicorn/README.md +109 -0
- data/lib/kapify/unicorn/configuration.rb +35 -0
- data/lib/kapify/unicorn.rb +2 -0
- data/lib/kapify/version.rb +3 -0
- data/lib/kapify.rb +5 -0
- metadata +125 -0
@@ -0,0 +1,83 @@
|
|
1
|
+
# kapify/logrotate
|
2
|
+
|
3
|
+
Capistrano task for enabling log rotation for your Rails application
|
4
|
+
|
5
|
+
Provides capistrano tasks to:
|
6
|
+
|
7
|
+
* creates logrotate record to rotate application logs
|
8
|
+
|
9
|
+
For full customization, config can be copied to the application using generator.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Add this line to your `deploy.rb`
|
14
|
+
|
15
|
+
require 'kapify/logrotate'
|
16
|
+
|
17
|
+
Note, that following capistrano variables should be defined:
|
18
|
+
|
19
|
+
shared_path
|
20
|
+
user
|
21
|
+
|
22
|
+
You can check that new tasks are available (`cap -T`):
|
23
|
+
|
24
|
+
# create logrotate record to rotate application logs
|
25
|
+
cap logrotate
|
26
|
+
|
27
|
+
There is no need to execute any of these tasks manually.
|
28
|
+
They will be called automatically on different deploy stages:
|
29
|
+
|
30
|
+
* `logrotate` is hooked to `deploy:setup`
|
31
|
+
|
32
|
+
This means that if you run `cap deploy:setup`,
|
33
|
+
logrotate will be automatically set up.
|
34
|
+
|
35
|
+
## Template Customization
|
36
|
+
|
37
|
+
If you want to change default template, you can generate them using `rails generator`
|
38
|
+
|
39
|
+
rails g kapify:logrotate
|
40
|
+
|
41
|
+
This will copy default templates to `config/deploy/templates` directory,
|
42
|
+
so you can customize them as you like, and capistrano tasks will use this templates instead of default.
|
43
|
+
|
44
|
+
You can also provide path, where to generate templates:
|
45
|
+
|
46
|
+
rails g kapify:logrotate config/templates
|
47
|
+
|
48
|
+
In this case, don't forget to set `templates_path` varibale:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
# path to customized templates (see below for details)
|
52
|
+
# default value: "config/deploy/templates"
|
53
|
+
set :templates_path, "config/deploy/templates"
|
54
|
+
```
|
55
|
+
|
56
|
+
For example, if you also use nginx and unicorn, you want to notify them about rotation.
|
57
|
+
In this case, your template file could look like this:
|
58
|
+
|
59
|
+
```erb
|
60
|
+
<%= shared_path %>/log/*.log {
|
61
|
+
daily
|
62
|
+
missingok
|
63
|
+
rotate 180
|
64
|
+
compress
|
65
|
+
dateext
|
66
|
+
delaycompress
|
67
|
+
|
68
|
+
lastaction
|
69
|
+
pid=<%= unicorn_pid %>
|
70
|
+
test -s $pid && kill -USR1 "$(cat $pid)"
|
71
|
+
endscript
|
72
|
+
|
73
|
+
prerotate
|
74
|
+
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
|
75
|
+
run-parts /etc/logrotate.d/httpd-prerotate; \
|
76
|
+
fi \
|
77
|
+
endscript
|
78
|
+
|
79
|
+
postrotate
|
80
|
+
[ ! -f /run/nginx.pid ] || kill -USR1 `cat /run/nginx.pid`
|
81
|
+
endscript
|
82
|
+
}
|
83
|
+
```
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'capistrano'
|
2
|
+
require 'kapify/base'
|
3
|
+
|
4
|
+
Capistrano::Configuration.instance.load do
|
5
|
+
namespace :kapify do
|
6
|
+
desc "Setup logs rotation for application logs"
|
7
|
+
task :logrotate, roles: [:web, :app] do
|
8
|
+
kapify_template("logrotate", "logrotate.erb", "/tmp/#{application}_logrotate")
|
9
|
+
run "#{sudo} mv /tmp/#{application}_logrotate /etc/logrotate.d/#{application}"
|
10
|
+
run "#{sudo} chown root:root /etc/logrotate.d/#{application}"
|
11
|
+
end
|
12
|
+
|
13
|
+
after "deploy:setup", "kapify:logrotate"
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'kapify/logrotate/configuration'
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# kapify/nginx
|
2
|
+
|
3
|
+
Capistrano tasks for configuration of nginx web server.
|
4
|
+
|
5
|
+
Provides capistrano tasks to:
|
6
|
+
|
7
|
+
* easily add application to nginx and reload it's configuration
|
8
|
+
|
9
|
+
Provides several capistrano variables for easy customization.
|
10
|
+
Also, for full customization, all configs can be copied to the application using generators.
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
Add this line to your `deploy.rb`
|
15
|
+
|
16
|
+
require 'kapify/nginx'
|
17
|
+
|
18
|
+
Note, that following capistrano variables should be defined:
|
19
|
+
|
20
|
+
application
|
21
|
+
current_path
|
22
|
+
shared_path
|
23
|
+
user
|
24
|
+
|
25
|
+
You can check that new tasks are available (`cap -T`):
|
26
|
+
|
27
|
+
# add and enable application to nginx
|
28
|
+
cap kapify:nginx:setup
|
29
|
+
|
30
|
+
# reload nginx configuration
|
31
|
+
cap kapify:nginx:reload
|
32
|
+
|
33
|
+
There is no need to execute any of these tasks manually.
|
34
|
+
They will be called automatically on different deploy stages:
|
35
|
+
|
36
|
+
* `kapify:nginx:setup` and `kapify:nginx:reload` are hooked to `deploy:setup`
|
37
|
+
|
38
|
+
This means that if you run `cap deploy:setup`,
|
39
|
+
nginx will be automatically configured.
|
40
|
+
|
41
|
+
However, if you changed variables or customized templates,
|
42
|
+
you can run any of these tasks to update configuration.
|
43
|
+
|
44
|
+
## Customization
|
45
|
+
|
46
|
+
### Using variables
|
47
|
+
|
48
|
+
You can customize nginx config using capistrano variables:
|
49
|
+
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# path to customized templates (see below for details)
|
53
|
+
# default value: "config/deploy/templates"
|
54
|
+
set :templates_path, "config/deploy/templates"
|
55
|
+
|
56
|
+
# server name for nginx, default value: no (will be prompted if not set)
|
57
|
+
# set this to your site name as it is visible from outside
|
58
|
+
# this will allow 1 nginx to serve several sites with different `server_name`
|
59
|
+
set :nginx_server_name, "example.com"
|
60
|
+
|
61
|
+
# if set, nginx will be configured to 443 port and port 80 will be auto rewritten to 443
|
62
|
+
# also, on `nginx:setup`, paths to ssl certificate and key will be configured
|
63
|
+
# and certificate file and key will be copied to `/etc/ssl/certs` and `/etc/ssl/private/` directories
|
64
|
+
# default value: false
|
65
|
+
set :nginx_use_ssl, false
|
66
|
+
|
67
|
+
# remote file name of the certificate, only makes sense if `nginx_use_ssl` is set
|
68
|
+
# default value: `nginx_server_name + ".crt"`
|
69
|
+
set :nginx_ssl_certificate, "#{nginx_server_name}.crt"
|
70
|
+
|
71
|
+
# remote file name of the certificate, only makes sense if `nginx_use_ssl` is set
|
72
|
+
# default value: `nginx_server_name + ".key"`
|
73
|
+
set :nginx_ssl_certificate_key, "#{nginx_server_name}.key"
|
74
|
+
|
75
|
+
# local path to file with certificate, only makes sense if `nginx_use_ssl` is set
|
76
|
+
# this file will be copied to remote server
|
77
|
+
# default value: none (will be prompted if not set)
|
78
|
+
set :nginx_ssl_certificate_local_path, "/home/ivalkeen/ssl/myssl.cert"
|
79
|
+
|
80
|
+
# local path to file with certificate key, only makes sense if `nginx_use_ssl` is set
|
81
|
+
# this file will be copied to remote server
|
82
|
+
# default value: none (will be prompted if not set)
|
83
|
+
set :nginx_ssl_certificate_key_local_path, "/home/ivalkeen/ssl/myssl.key"
|
84
|
+
```
|
85
|
+
|
86
|
+
For example, of you site name is `example.com`,
|
87
|
+
your `deploy.rb` will look like this:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
set :server_name, "example.com"
|
91
|
+
require 'kapify/nginx'
|
92
|
+
```
|
93
|
+
|
94
|
+
### Template Customization
|
95
|
+
|
96
|
+
If you want to change default templates, you can generate them using `rails generator`
|
97
|
+
|
98
|
+
rails g kapify:nginx
|
99
|
+
|
100
|
+
This will copy default templates to `config/deploy/templates` directory,
|
101
|
+
so you can customize them as you like, and capistrano tasks will use this templates instead of default.
|
102
|
+
|
103
|
+
You can also provide path, where to generate templates:
|
104
|
+
|
105
|
+
rails g kapify/nginx config/templates
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'capistrano'
|
2
|
+
require 'kapify/base'
|
3
|
+
|
4
|
+
Capistrano::Configuration.instance.load do
|
5
|
+
set_default(:nginx_server_name) { Capistrano::CLI.ui.ask "Nginx server name: " }
|
6
|
+
set_default(:nginx_use_ssl, false)
|
7
|
+
set_default(:nginx_ssl_certificate) { "#{nginx_server_name}.crt" }
|
8
|
+
set_default(:nginx_ssl_certificate_key) { "#{nginx_server_name}.key" }
|
9
|
+
set_default(:nginx_ssl_certificate_local_path) {Capistrano::CLI.ui.ask "Local path to ssl certificate: "}
|
10
|
+
set_default(:nginx_ssl_certificate_key_local_path) {Capistrano::CLI.ui.ask "Local path to ssl certificate key: "}
|
11
|
+
|
12
|
+
namespace :kapify do
|
13
|
+
namespace :nginx do
|
14
|
+
desc "Setup nginx configuration for this application"
|
15
|
+
task :setup, roles: :web do
|
16
|
+
kapify_template("nginx", "nginx_conf.erb", "/tmp/#{application}")
|
17
|
+
run "#{sudo} mv /tmp/#{application} /etc/nginx/sites-available/#{application}"
|
18
|
+
run "#{sudo} ln -fs /etc/nginx/sites-available/#{application} /etc/nginx/sites-enabled/#{application}"
|
19
|
+
|
20
|
+
if nginx_use_ssl
|
21
|
+
put File.read(nginx_ssl_certificate_local_path), "/tmp/#{nginx_ssl_certificate}"
|
22
|
+
put File.read(nginx_ssl_certificate_key_local_path), "/tmp/#{nginx_ssl_certificate_key}"
|
23
|
+
|
24
|
+
run "#{sudo} mv /tmp/#{nginx_ssl_certificate} /etc/ssl/certs/#{nginx_ssl_certificate}"
|
25
|
+
run "#{sudo} mv /tmp/#{nginx_ssl_certificate_key} /etc/ssl/private/#{nginx_ssl_certificate_key}"
|
26
|
+
|
27
|
+
run "#{sudo} chown root:root /etc/ssl/certs/#{nginx_ssl_certificate}"
|
28
|
+
run "#{sudo} chown root:root /etc/ssl/private/#{nginx_ssl_certificate_key}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
after "deploy:setup", "kapify:nginx:setup"
|
33
|
+
after "deploy:setup", "kapify:nginx:reload"
|
34
|
+
|
35
|
+
desc "Reload nginx configuration"
|
36
|
+
task :reload, roles: :web do
|
37
|
+
run "#{sudo} /etc/init.d/nginx reload"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/kapify/nginx.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'kapify/nginx/configuration'
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# kapify/pg
|
2
|
+
|
3
|
+
Capistrano tasks for basic configuration and management of PostgreSQL database.
|
4
|
+
|
5
|
+
Provides capistrano tasks to:
|
6
|
+
|
7
|
+
* create database.yml in `shared` folder and symlink into `config`
|
8
|
+
* create user and database in postgres
|
9
|
+
|
10
|
+
Provides several capistrano variables for easy customization.
|
11
|
+
Also, for full customization, template of `database.yml` file can be copied to
|
12
|
+
the application using generator.
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
Add this line to your `deploy.rb`
|
17
|
+
|
18
|
+
require 'kapify/pg'
|
19
|
+
|
20
|
+
Next, add role `pg` to the server which has running instance of PostgreSQL.
|
21
|
+
Address of this server will be automatically used as `pg_host` (see below),
|
22
|
+
`create_user` and `create_database` tasks will be executed on this server:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
# for dedicated database server it could look like this:
|
26
|
+
server "192.168.33.12", :db, :pg, no_release: true
|
27
|
+
|
28
|
+
# for all-in-one server it could look like this:
|
29
|
+
server "192.168.33.11", :web, :app, :db, :pg, primary: true
|
30
|
+
```
|
31
|
+
|
32
|
+
Note, that following capistrano variables should be defined in the `deploy.rb` file:
|
33
|
+
|
34
|
+
application
|
35
|
+
current_path
|
36
|
+
shared_path
|
37
|
+
user
|
38
|
+
|
39
|
+
You can check that new tasks are available (`cap -T`):
|
40
|
+
|
41
|
+
# create database user for the application
|
42
|
+
cap kapify:pg:create_user
|
43
|
+
|
44
|
+
# create database for the application
|
45
|
+
cap kapify:pg:create_database
|
46
|
+
|
47
|
+
# generates `database.yml` file in the `shared/config` folder
|
48
|
+
cap kapify:pg:setup
|
49
|
+
|
50
|
+
# symlinks `database.yml` from the `shared/config` folder to `current/config` folder
|
51
|
+
cap kapify:pg:symlink
|
52
|
+
|
53
|
+
There is no need to execute any of these tasks manually.
|
54
|
+
They will be called automatically on different deploy stages:
|
55
|
+
|
56
|
+
* `kapify:pg:create_user`, `kapify:pg:create_database` and `kapify:pg:setup` are hooked to `deploy:setup`
|
57
|
+
* `kapify:pg:symlink` is hooked to `deploy:finalize_update`
|
58
|
+
|
59
|
+
This means that if you run `cap deploy:setup`,
|
60
|
+
user and database will be created and `database.yml` file will be generated.
|
61
|
+
And on each deploy, `database.yml` will be automatically linked to current version.
|
62
|
+
|
63
|
+
However, if you changed variables or customized templates,
|
64
|
+
you can run any of these tasks to update configuration.
|
65
|
+
|
66
|
+
## Customization
|
67
|
+
|
68
|
+
### Using variables
|
69
|
+
|
70
|
+
You can customize `database.yml` using capistrano variables:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# path to customized templates (see below for details)
|
74
|
+
# default value: "config/deploy/templates"
|
75
|
+
set :templates_path, "config/deploy/templates"
|
76
|
+
|
77
|
+
# `host` value in `database.yml`
|
78
|
+
# default value: address of the server, marked with `pg` role
|
79
|
+
set :pg_host, "localhost"
|
80
|
+
|
81
|
+
# `port` value in `database.yml`
|
82
|
+
# default value: 5432
|
83
|
+
set :pg_port, "5432"
|
84
|
+
|
85
|
+
# `database` value in `database.yml`
|
86
|
+
# default value: application variable value
|
87
|
+
set :pg_database, "myapp_production"
|
88
|
+
|
89
|
+
# `pool` value in `database.yml`
|
90
|
+
# default value: 5
|
91
|
+
set :pg_pool, "5"
|
92
|
+
|
93
|
+
# `username` value in `database.yml`
|
94
|
+
# default value: application variable value
|
95
|
+
set :pg_user, application
|
96
|
+
|
97
|
+
# `password` value in `database.yml`
|
98
|
+
# default value: will be asked if not set
|
99
|
+
# setting this value in config file is not recommended
|
100
|
+
set :pg_password, application
|
101
|
+
|
102
|
+
# indicates, it new database should be created on `deploy:setup` or not
|
103
|
+
# default value: true
|
104
|
+
set :pg_create_db, true
|
105
|
+
```
|
106
|
+
|
107
|
+
### Template Customization
|
108
|
+
|
109
|
+
If you want to change default template, you can generate it using `rails generator`
|
110
|
+
|
111
|
+
rails g kapify:pg
|
112
|
+
|
113
|
+
This will copy default template to `config/deploy/templates` directory,
|
114
|
+
so you can customize them as you like, and capistrano tasks will use this templates instead of default.
|
115
|
+
|
116
|
+
You can also provide path, where to generate template:
|
117
|
+
|
118
|
+
rails g kapify:pg config/templates
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'capistrano'
|
2
|
+
require 'kapify/base'
|
3
|
+
|
4
|
+
Capistrano::Configuration.instance.load do
|
5
|
+
set_default(:pg_host) { find_servers(roles: :pg)[0] || "localhost" }
|
6
|
+
set_default(:pg_port, "5432")
|
7
|
+
set_default(:pg_database, application)
|
8
|
+
set_default(:pg_pool, 5)
|
9
|
+
set_default(:pg_user, application)
|
10
|
+
set_default(:pg_password) { Capistrano::CLI.password_prompt "PG password: " }
|
11
|
+
set_default(:pg_create_db, true)
|
12
|
+
|
13
|
+
namespace :kapify do
|
14
|
+
namespace :pg do
|
15
|
+
desc "Create user for application"
|
16
|
+
task :create_user, roles: :pg do
|
17
|
+
pg_params = "-p #{pg_port}"
|
18
|
+
check_user = %Q[#{sudo} -u postgres psql #{pg_params} postgres -c "select * from pg_user where usename='#{pg_user}'" | grep -c '#{pg_user}']
|
19
|
+
add_user = %Q[#{sudo} -u postgres psql #{pg_params} -c "create user #{pg_user}"]
|
20
|
+
run "#{check_user} || #{add_user}"
|
21
|
+
run %Q{#{sudo} -u postgres psql #{pg_params} -c "alter user #{pg_user} with password '#{pg_password}'" }
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Create database for application"
|
25
|
+
task :create_database, roles: :pg do
|
26
|
+
if pg_create_db
|
27
|
+
run %Q{#{sudo} -u postgres psql -c "create database #{pg_database} owner #{pg_user};"}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
if find_servers(roles: :pg).size > 0
|
32
|
+
after "deploy:setup", "kapify:pg:create_user"
|
33
|
+
after "deploy:setup", "kapify:pg:create_database"
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Generate the database.yml configuration file."
|
37
|
+
task :setup, roles: :app do
|
38
|
+
run "mkdir -p #{shared_path}/config"
|
39
|
+
kapify_template("pg", "database.yml.erb", "#{shared_path}/config/database.yml")
|
40
|
+
end
|
41
|
+
|
42
|
+
after "deploy:setup", "kapify:pg:setup"
|
43
|
+
|
44
|
+
desc "Symlink the database.yml file into latest release"
|
45
|
+
task :symlink, roles: :app do
|
46
|
+
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
|
47
|
+
end
|
48
|
+
|
49
|
+
after "deploy:finalize_update", "kapify:pg:symlink"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/kapify/pg.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'kapify/pg/configuration'
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# kapify/pg
|
2
|
+
|
3
|
+
Capistrano tasks for basic configuration and management of Resque.
|
4
|
+
|
5
|
+
Provides capistrano tasks to:
|
6
|
+
|
7
|
+
* create resque init script for application, so it will be automatically started when OS restarts
|
8
|
+
* start/stop resque (also can be done using `sudo service resque_<your_app> start/stop`)
|
9
|
+
* gracefully restart resque on deployment
|
10
|
+
|
11
|
+
Provides several capistrano variables for easy customization.
|
12
|
+
Also, for full customization, init script's config can be copied to the application using generators.
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
Add this line to your `deploy.rb`
|
17
|
+
|
18
|
+
require 'kapify/resque'
|
19
|
+
|
20
|
+
Next, add role `resque_worker` to the server which you want resque workers to be started on.
|
21
|
+
|
22
|
+
Note, that following capistrano variables should be defined:
|
23
|
+
|
24
|
+
application
|
25
|
+
current_path
|
26
|
+
shared_path
|
27
|
+
user
|
28
|
+
|
29
|
+
You can check that new tasks are available (`cap -T`):
|
30
|
+
|
31
|
+
# create resque configuration and init script
|
32
|
+
cap kapify:resque:setup
|
33
|
+
|
34
|
+
# start resque
|
35
|
+
cap kapify:resque:start
|
36
|
+
|
37
|
+
# stop resque
|
38
|
+
cap kapify:resque:stop
|
39
|
+
|
40
|
+
# gracefully restart resque
|
41
|
+
cap kapify:resque:restart
|
42
|
+
|
43
|
+
There is no need to execute any of these tasks manually.
|
44
|
+
They will be called automatically on different deploy stages:
|
45
|
+
|
46
|
+
* `kapify:resque:setup` is hooked to `deploy:setup`
|
47
|
+
* `kapify:resque:restart` is hooked to `deploy:restart`
|
48
|
+
|
49
|
+
This means that if you run `cap deploy:setup`,
|
50
|
+
resque will be automatically configured.
|
51
|
+
And after each deploy, resque will be automatically reloaded.
|
52
|
+
|
53
|
+
However, if you changed variables or customized templates,
|
54
|
+
you can run any of these tasks to update configuration.
|
55
|
+
|
56
|
+
## Customization
|
57
|
+
|
58
|
+
### Using variables
|
59
|
+
|
60
|
+
You can customize resque configs using capistrano variables:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
# user name to run resque
|
64
|
+
# default value: `user` (user varibale defined in your `deploy.rb`)
|
65
|
+
set :resque_user, "user"
|
66
|
+
|
67
|
+
# number of resque workers
|
68
|
+
# default value: 2
|
69
|
+
set :resque_workers, 4
|
70
|
+
|
71
|
+
# name of resque workers queue
|
72
|
+
# default value: *
|
73
|
+
set :resque_queue, "*"
|
74
|
+
|
75
|
+
# path to the bundle command
|
76
|
+
# default value: /usr/local/rbenv/shims/bundle (for globally installed rbenv)
|
77
|
+
set :resque_bundle, "/usr/bin/bundle"
|
78
|
+
|
79
|
+
# name of rake task to run resque worker
|
80
|
+
# default value: "environment resque:work"
|
81
|
+
set :resque_task, "resque:work"
|
82
|
+
|
83
|
+
```
|
84
|
+
|
85
|
+
For example, if you don't want to load environment for your workers,
|
86
|
+
your `deploy.rb` will look like this:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
set :resque_task, "resque:work"
|
90
|
+
require 'kapify/resque'
|
91
|
+
```
|
92
|
+
|
93
|
+
### Template Customization
|
94
|
+
|
95
|
+
If you want to change default templates, you can generate them using `rails generator`
|
96
|
+
|
97
|
+
rails g kapify:resque
|
98
|
+
|
99
|
+
This will copy default templates to `config/deploy/templates` directory,
|
100
|
+
so you can customize them as you like, and capistrano tasks will use this templates instead of default.
|
101
|
+
|
102
|
+
You can also provide path, where to generate templates:
|
103
|
+
|
104
|
+
rails g kapify:resque config/templates
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'capistrano'
|
2
|
+
require 'kapify/base'
|
3
|
+
|
4
|
+
Capistrano::Configuration.instance.load do
|
5
|
+
set_default(:resque_user, user)
|
6
|
+
set_default(:resque_workers, 2)
|
7
|
+
set_default(:resque_queue, "*")
|
8
|
+
set_default(:resque_bundle, "/usr/local/rbenv/shims/bundle")
|
9
|
+
set_default(:resque_task, "environment resque:work")
|
10
|
+
|
11
|
+
namespace :kapify do
|
12
|
+
namespace :resque do
|
13
|
+
desc "Setup Resque initializer and app configuration"
|
14
|
+
task :setup, roles: :resque_worker do
|
15
|
+
run "mkdir -p #{shared_path}/config"
|
16
|
+
kapify_template "resque", "resque_init.erb", "/tmp/resque_init"
|
17
|
+
run "chmod +x /tmp/resque_init"
|
18
|
+
run "#{sudo} mv /tmp/resque_init /etc/init.d/resque_#{application}"
|
19
|
+
run "#{sudo} update-rc.d -f resque_#{application} defaults"
|
20
|
+
end
|
21
|
+
|
22
|
+
after "deploy:setup", "kapify:resque:setup"
|
23
|
+
|
24
|
+
%w[start stop restart].each do |command|
|
25
|
+
desc "#{command} resque"
|
26
|
+
task command, roles: :resque_worker do
|
27
|
+
run "service resque_#{application} #{command}"
|
28
|
+
end
|
29
|
+
|
30
|
+
after "deploy:#{command}", "kapify:resque:#{command}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'kapify/resque/configuration'
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# kapify/unicorn
|
2
|
+
|
3
|
+
Capistrano tasks for configuration and management unicorn for zero downtime deployments of Rails applications.
|
4
|
+
|
5
|
+
Provides capistrano tasks to:
|
6
|
+
|
7
|
+
* create unicorn init script for application, so it will be automatically started when OS restarts
|
8
|
+
* start/stop unicorn (also can be done using `sudo service unicorn_<your_app> start/stop`)
|
9
|
+
* restart unicorn using `USR2` signal to load new application version with zero downtime
|
10
|
+
|
11
|
+
Provides several capistrano variables for easy customization.
|
12
|
+
Also, for full customization, all configs can be copied to the application using generators.
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
Add this line to your `deploy.rb`
|
17
|
+
|
18
|
+
require 'kapify/unicorn'
|
19
|
+
|
20
|
+
Note, that following capistrano variables should be defined:
|
21
|
+
|
22
|
+
application
|
23
|
+
current_path
|
24
|
+
shared_path
|
25
|
+
user
|
26
|
+
|
27
|
+
You can check that new tasks are available (`cap -T`):
|
28
|
+
|
29
|
+
# create unicorn configuration and init script
|
30
|
+
cap kapify:unicorn:setup
|
31
|
+
|
32
|
+
# start unicorn
|
33
|
+
cap kapify:unicorn:start
|
34
|
+
|
35
|
+
# stop unicorn
|
36
|
+
cap kapify:unicorn:stop
|
37
|
+
|
38
|
+
# restart unicorn with no downtime
|
39
|
+
# old workers will process new request until new master is fully loaded
|
40
|
+
# then old workers will be automatically killed and new workers will start processing requests
|
41
|
+
cap kapify:unicorn:restart
|
42
|
+
|
43
|
+
There is no need to execute any of these tasks manually.
|
44
|
+
They will be called automatically on different deploy stages:
|
45
|
+
|
46
|
+
* `kapify:unicorn:setup` is hooked to `deploy:setup`
|
47
|
+
* `kapify:unicorn:restart` is hooked to `deploy:restart`
|
48
|
+
|
49
|
+
This means that if you run `cap deploy:setup`,
|
50
|
+
unicorn will be automatically configured.
|
51
|
+
And after each deploy, unicorn will be automatically restarted.
|
52
|
+
|
53
|
+
However, if you changed variables or customized templates,
|
54
|
+
you can run any of these tasks to update configuration.
|
55
|
+
|
56
|
+
## Customization
|
57
|
+
|
58
|
+
### Using variables
|
59
|
+
|
60
|
+
You can customize unicorn configs using capistrano variables:
|
61
|
+
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# path to customized templates (see below for details)
|
65
|
+
# default value: "config/deploy/templates"
|
66
|
+
set :templates_path, "config/deploy/templates"
|
67
|
+
|
68
|
+
# path, where unicorn pid file will be stored
|
69
|
+
# default value: `"#{current_path}/tmp/pids/unicorn.pid"`
|
70
|
+
set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid"
|
71
|
+
|
72
|
+
# path, where unicorn config file will be stored
|
73
|
+
# default value: `"#{shared_path}/config/unicorn.rb"`
|
74
|
+
set :unicorn_config, "#{shared_path}/config/unicorn.rb"
|
75
|
+
|
76
|
+
# path, where unicorn log file will be stored
|
77
|
+
# default value: `"#{shared_path}/config/unicorn.rb"`
|
78
|
+
set :unicorn_log, "#{shared_path}/config/unicorn.rb"
|
79
|
+
|
80
|
+
# user name to run unicorn
|
81
|
+
# default value: `user` (user varibale defined in your `deploy.rb`)
|
82
|
+
set :unicorn_user, "user"
|
83
|
+
|
84
|
+
# number of unicorn workers
|
85
|
+
# default value: no (will be prompted if not set)
|
86
|
+
set :unicorn_workers, 4
|
87
|
+
|
88
|
+
```
|
89
|
+
|
90
|
+
For example, if you want to use 8 unicorn workers,
|
91
|
+
your `deploy.rb` will look like this:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
set :unicorn_workers, 8
|
95
|
+
require 'kapify/unicorn'
|
96
|
+
```
|
97
|
+
|
98
|
+
### Template Customization
|
99
|
+
|
100
|
+
If you want to change default templates, you can generate them using `rails generator`
|
101
|
+
|
102
|
+
rails g kapify:unicorn
|
103
|
+
|
104
|
+
This will copy default templates to `config/deploy/templates` directory,
|
105
|
+
so you can customize them as you like, and capistrano tasks will use this templates instead of default.
|
106
|
+
|
107
|
+
You can also provide path, where to generate templates:
|
108
|
+
|
109
|
+
rails g kapify:unicorn config/templates
|