capistrano-rails-server 1.2.0 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,18 @@
1
- def template(from, to)
2
- erb = File.read(File.expand_path("../templates/#{from}", __FILE__))
3
- put ERB.new(erb).result(binding), to
4
- end
5
-
6
- def set_default(name, *args, &block)
7
- set(name, *args, &block) unless exists?(name)
8
- end
1
+ def template(from, to)
2
+ if templates[from]
3
+ file = templates[from]
4
+ else
5
+ file = File.expand_path("../templates/#{from}", __FILE__)
6
+ end
7
+
8
+ erb = File.read(file)
9
+ put ERB.new(erb).result(binding), to
10
+ end
11
+
12
+ def set_default(name, *args, &block)
13
+ set(name, *args, &block) unless exists?(name)
14
+ end
15
+
16
+ def remote_file_exists?(full_path)
17
+ 'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
18
+ end
@@ -1,28 +1,24 @@
1
- require "capistrano-rails-server/recipes/common"
2
-
3
- configuration = Capistrano::Configuration.respond_to?(:instance) ?
4
- Capistrano::Configuration.instance(:must_exist) :
5
- Capistrano.configuration(:must_exist)
6
-
7
- configuration.load do
8
- namespace :key do
9
- desc "Generate deployment key for repository"
10
- task :generate, roles: :web do
11
- run %Q(ssh-keygen -q -N "" -t rsa -C #{user}@#{application} -f ~/.ssh/id_rsa)
12
- #run "ssh-add ~/.ssh/id_rsa"
13
- end
14
- after "deploy:install", "key:generate"
15
-
16
- desc "Show public key"
17
- task :show, roles: :web do
18
- run "cat ~/.ssh/id_rsa.pub"
19
- end
20
- after "key:generate", "key:show"
21
-
22
- desc "Remove key from server"
23
- task :remove, roles: :web do
24
- run "rm -rf ~/.ssh/id_rsa"
25
- run "rm -rf ~/.ssh/id_rsa.pub"
26
- end
27
- end
1
+ @configuration.load do
2
+ namespace :key do
3
+ desc "Generate deployment key for repository"
4
+ task :generate, roles: :web do
5
+ unless remote_file_exists?("~/.ssh/id_rsa")
6
+ run %Q(ssh-keygen -q -N "" -t rsa -C #{user}@#{application} -f ~/.ssh/id_rsa)
7
+ #run "ssh-add ~/.ssh/id_rsa"
8
+ end
9
+ end
10
+ after "deploy:install", "key:generate"
11
+
12
+ desc "Show public key"
13
+ task :show, roles: :web do
14
+ run "cat ~/.ssh/id_rsa.pub"
15
+ end
16
+ after "key:generate", "key:show"
17
+
18
+ desc "Remove key from server"
19
+ task :remove, roles: :web do
20
+ run "rm -rf ~/.ssh/id_rsa"
21
+ run "rm -rf ~/.ssh/id_rsa.pub"
22
+ end
23
+ end
28
24
  end
@@ -1,28 +1,18 @@
1
- require "capistrano-rails-server/recipes/common"
2
-
3
- configuration = Capistrano::Configuration.respond_to?(:instance) ?
4
- Capistrano::Configuration.instance(:must_exist) :
5
- Capistrano.configuration(:must_exist)
6
-
7
- configuration.load do
1
+ @configuration.load do
8
2
  namespace :nginx do
9
- desc "Install latest stable release of nginx"
10
- task :install, roles: :web do
11
- run "#{sudo} add-apt-repository -y ppa:nginx/stable"
12
- run "#{sudo} apt-get -y update"
13
- run "#{sudo} apt-get -y install nginx"
14
- end
15
- after "deploy:install", "nginx:install"
16
-
17
3
  desc "Setup nginx configuration for this application"
18
4
  task :setup, roles: :web do
19
- template "nginx_unicorn.erb", "/tmp/nginx_conf"
20
- run "#{sudo} mv /tmp/nginx_conf /etc/nginx/sites-enabled/#{application}"
21
- run "#{sudo} rm -f /etc/nginx/sites-enabled/default"
22
- restart
5
+ template "nginx_passenger.erb", "/tmp/nginx_conf"
6
+ run "#{sudo} mv /tmp/nginx_conf /opt/nginx/conf/nginx.conf"
7
+ template "nginx_init.erb", "/tmp/nginx_init"
8
+ run "chmod +x /tmp/nginx_init"
9
+ run "#{sudo} update-rc.d -f nginx remove"
10
+ run "#{sudo} mv /tmp/nginx_init /etc/init.d/nginx"
11
+ run "#{sudo} update-rc.d -f nginx defaults"
23
12
  end
24
13
  after "deploy:setup", "nginx:setup"
25
-
14
+ after "nginx:setup", "nginx:restart"
15
+
26
16
  %w[start stop restart].each do |command|
27
17
  desc "#{command} nginx"
28
18
  task command, roles: :web do
@@ -0,0 +1,23 @@
1
+ @configuration.load do
2
+ set_default(:passenger_user) { user }
3
+ set_default(:passenger_env, "production")
4
+
5
+ namespace :passenger do
6
+ desc "Install Passenger"
7
+ task :install, roles: :app do
8
+ run "#{sudo} apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7"
9
+ run "#{sudo} apt-get -y install apt-transport-https ca-certificates ruby-dev rubygems rake"
10
+ run "#{sudo} add-apt-repository -y https://oss-binaries.phusionpassenger.com/apt/passenger"
11
+ run "#{sudo} apt-get -y update"
12
+ run "#{sudo} apt-get -y install nginx-extras passenger libpcre3-dev libcurl4-openssl-dev zlib1g-dev bison libreadline-dev libncurses5-dev"
13
+ run "#{sudo} passenger-install-nginx-module --auto --prefix=/opt/nginx --auto-download --languages ruby"
14
+ end
15
+ after "deploy:install", "passenger:install"
16
+
17
+ desc "Reload Passenger"
18
+ task :restart, roles: :app do
19
+ run "touch #{current_path}/tmp/restart.txt"
20
+ end
21
+ after "deploy:restart", "passenger:restart"
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ @configuration.load do
2
+ namespace :postfix do
3
+ desc "Install postfix"
4
+ task :install, roles: :mail do
5
+ run "#{sudo} DEBIAN_FRONTEND=noninteractive apt-get -y install postfix"
6
+ end
7
+ after "deploy:install", "postfix:install"
8
+
9
+ desc "Setup postfix configuration"
10
+ task :setup, roles: :mail do
11
+ set :hostname, find_servers_for_task(current_task).first.host
12
+ template "postfix_main.erb", "/tmp/postfix_main_cf"
13
+ run "#{sudo} mv /etc/postfix/main.cf /tmp/old_postfix_main_cf"
14
+ run "#{sudo} mv /tmp/postfix_main_cf /etc/postfix/main.cf"
15
+ run "#{sudo} /etc/init.d/postfix reload"
16
+ end
17
+ after "postfix:install", "postfix:setup"
18
+
19
+ desc "Show postfix stats"
20
+ task :stats, roles: :mail do
21
+ run "#{sudo} ls /var/spool/postfix/incoming|wc -l"
22
+ run "#{sudo} ls /var/spool/postfix/active|wc -l"
23
+ run "#{sudo} ls -R /var/spool/postfix/deferred|wc -l"
24
+ end
25
+ end
26
+ end
@@ -1,10 +1,4 @@
1
- require "capistrano-rails-server/recipes/common"
2
-
3
- configuration = Capistrano::Configuration.respond_to?(:instance) ?
4
- Capistrano::Configuration.instance(:must_exist) :
5
- Capistrano.configuration(:must_exist)
6
-
7
- configuration.load do
1
+ @configuration.load do
8
2
  set_default(:postgresql_host, "localhost")
9
3
  set_default(:postgresql_user) { application }
10
4
  set_default(:postgresql_password) { Capistrano::CLI.password_prompt "PostgreSQL Password: " }
@@ -14,27 +8,31 @@ configuration.load do
14
8
  set_default(:postgresql_locale, "en_US")
15
9
 
16
10
  namespace :postgresql do
17
- desc "Install the latest stable release of PostgreSQL."
11
+ desc "Install the latest stable release of PostgreSQL"
18
12
  task :install, roles: :db, only: {primary: true} do
19
- run "#{sudo} add-apt-repository -y ppa:pitti/postgresql"
13
+ run "#{sudo} add-apt-repository -y 'deb http://apt.postgresql.org/pub/repos/apt/ #{system_codename}-pgdg main'"
14
+ run "#{sudo} wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | #{sudo} apt-key add -"
20
15
  run "#{sudo} apt-get -y update"
21
- run "#{sudo} apt-get -y install postgresql libpq-dev"
16
+ run "#{sudo} apt-get -y install postgresql-9.3 pgadmin3 libpq-dev"
22
17
  end
23
18
  after "deploy:install", "postgresql:install"
24
19
 
25
- desc "Create a database for this application."
26
- task :create_database, roles: :db, only: {primary: true} do
27
- run %Q{#{sudo} -u postgres psql -c "create user #{postgresql_user} with password '#{postgresql_password}';"}
28
- run %Q{#{sudo} -u postgres psql -c "create database #{postgresql_database} owner #{postgresql_user};"}
29
- end
30
- after "deploy:setup", "postgresql:create_database"
31
-
32
- desc "Generate the database.yml configuration file."
20
+ desc "Setup PostgreSQL configuration"
33
21
  task :setup, roles: :app do
34
22
  run "mkdir -p #{shared_path}/config"
35
23
  template "postgresql.yml.erb", "#{shared_path}/config/database.yml"
24
+ template "postgresql.conf.erb", "/tmp/postgresql_conf"
25
+ run "#{sudo} mv /tmp/postgresql_conf /etc/postgresql/9.3/main/postgresql.conf"
36
26
  end
37
27
  after "deploy:setup", "postgresql:setup"
28
+ after "postgresql:setup", "postgresql:restart"
29
+
30
+ desc "Create a database for this application"
31
+ task :create_database, roles: :db, only: {primary: true} do
32
+ run %Q{#{sudo} -u postgres psql -c "create user #{postgresql_user} with password '#{postgresql_password}';"}
33
+ run %Q{#{sudo} -u postgres psql -c "create database #{postgresql_database} owner #{postgresql_user};"}
34
+ end
35
+ after "postgresql:setup", "postgresql:create_database"
38
36
 
39
37
  desc "Symlink the database.yml file into latest release"
40
38
  task :symlink, roles: :app do
@@ -42,24 +40,47 @@ configuration.load do
42
40
  end
43
41
  after "deploy:finalize_update", "postgresql:symlink"
44
42
 
43
+ %w[start stop restart reload force-reload status].each do |command|
44
+ desc "#{command} PostgreSQL"
45
+ task command, roles: :db, only: {primary: true} do
46
+ run "#{sudo} service postgresql #{command}"
47
+ end
48
+ end
49
+
45
50
  desc "Rebuild cluster with another encoding and locale. WARNING! This task will remove all databases."
46
- task :rebuild, roles: :app do
51
+ task :rebuild, roles: :db, only: {primary: true} do
47
52
  answer =
48
53
  Capistrano::CLI.ui.ask "This task will remove all existing DBs, run it only on fresh server installation!
49
54
  Do you want to run this task? Otherwise this task will be skipped. (y/n):"
50
55
  if %w(Y y yes).include? answer
51
56
  run "#{sudo} locale-gen #{postgresql_locale}.#{postgresql_encoding}"
52
57
  run "#{sudo} service postgresql stop"
53
- run "#{sudo} pg_dropcluster --stop 9.1 main"
54
- run "#{sudo} pg_createcluster --start --locale #{postgresql_locale}.#{postgresql_encoding} -e #{postgresql_encoding} 9.1 main"
58
+ run "#{sudo} pg_dropcluster --stop 9.3 main"
59
+ run "#{sudo} pg_createcluster --start --locale #{postgresql_locale}.#{postgresql_encoding} -e #{postgresql_encoding} 9.3 main"
55
60
  else
56
61
  Capistrano::CLI.ui.say "postgresql:rebuild task skipped."
57
62
  end
58
63
 
59
64
  end
60
65
  if postgresql_rebuild
61
- before "postgresql:create_database", "postgresql:rebuild"
66
+ before "postgresql:setup", "postgresql:rebuild"
67
+ end
68
+
69
+ desc "Export and download database to tmp dir"
70
+ task :export, roles: :db, only: {primary: true} do
71
+ run "pg_dump #{postgresql_database} > /tmp/#{postgresql_database}.sql", :shell => "sh"
72
+ download "/tmp/#{postgresql_database}.sql", "tmp/#{postgresql_database}.sql"
62
73
  end
63
74
 
75
+ desc "Upload and import database from tmp dir"
76
+ task :import, roles: :db, only: {primary: true} do
77
+ upload "tmp/#{postgresql_database}.sql", "/tmp/#{postgresql_database}.sql"
78
+ answer = Capistrano::CLI.ui.ask "Are you sure want to import DB on this server?"
79
+ if %w(Y y yes).include? answer
80
+ run "psql #{postgresql_database} < /tmp/#{postgresql_database}.sql"
81
+ else
82
+ Capistrano::CLI.ui.say "postgresql:import task skipped."
83
+ end
84
+ end
64
85
  end
65
86
  end
@@ -0,0 +1,25 @@
1
+ require 'rvm/capistrano'
2
+
3
+ @configuration.load do
4
+ set_default :rvm_ruby_string, "2.0.0-p353"
5
+ set_default :rvm_autolibs_flag, :enable
6
+ set_default :use_sudo, true
7
+
8
+ namespace :rvm_wrap do
9
+ desc "Set default shell when rvm isn't installed yet"
10
+ task :set_default_shell do
11
+ set :default_shell, "sh"
12
+ end
13
+ before "deploy:ssh", "rvm_wrap:set_default_shell"
14
+
15
+ desc "Install RVM and Ruby"
16
+ task :install do
17
+ run "/bin/bash --login -c 'rvm use #{rvm_ruby_string} --default'"
18
+ run "/bin/bash --login -c 'rvm wrapper #{rvm_ruby_string} #{application} ruby'"
19
+ run "/bin/bash --login -c 'rvm wrapper #{rvm_ruby_string} #{application} bundle'"
20
+ end
21
+ before "rvm_wrap:install", "rvm:install_rvm"
22
+ before "rvm_wrap:install", "rvm:install_ruby"
23
+ after "deploy:install", "rvm_wrap:install"
24
+ end
25
+ end
@@ -0,0 +1,75 @@
1
+ #! /bin/sh
2
+
3
+ ### BEGIN INIT INFO
4
+ # Provides: nginx
5
+ # Required-Start: $all
6
+ # Required-Stop: $all
7
+ # Default-Start: 2 3 4 5
8
+ # Default-Stop: 0 1 6
9
+ # Short-Description: starts the nginx web server
10
+ # Description: starts nginx using start-stop-daemon
11
+ ### END INIT INFO
12
+
13
+ PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin
14
+ DAEMON=/opt/nginx/sbin/nginx
15
+ NAME=nginx
16
+ DESC=nginx
17
+ PIDFILE=/opt/nginx/logs/$NAME.pid
18
+
19
+ test -x $DAEMON || exit 0
20
+
21
+ # Include nginx defaults if available
22
+ if [ -f /etc/default/nginx ] ; then
23
+ . /etc/default/nginx
24
+ fi
25
+
26
+ set -e
27
+
28
+ case "$1" in
29
+ start)
30
+ echo -n "Starting $DESC: "
31
+ if [ ! -f $PIDFILE ]
32
+ then
33
+ start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
34
+ echo "$NAME."
35
+ else
36
+ echo "Already running."
37
+ fi
38
+ ;;
39
+ stop)
40
+ echo -n "Stopping $DESC: "
41
+ if [ -f $PIDFILE ]
42
+ then
43
+ start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
44
+ echo "$NAME."
45
+ else
46
+ echo "Already not running."
47
+ fi
48
+ ;;
49
+ restart|force-reload)
50
+ echo -n "Restarting $DESC: "
51
+ if [ -f $PIDFILE ]
52
+ then
53
+ start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
54
+ sleep 1
55
+ fi
56
+ start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
57
+ echo "$NAME."
58
+ ;;
59
+ reload)
60
+ echo -n "Reloading $DESC configuration: "
61
+ if [ -f $PIDFILE ]
62
+ then
63
+ start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDFILE --exec $DAEMON
64
+ echo "$NAME."
65
+ else
66
+ echo "Not running, can't reload."
67
+ fi
68
+ ;;
69
+ *)
70
+ N=/etc/init.d/$NAME
71
+ echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
72
+ exit 1
73
+ ;;
74
+ esac
75
+ exit 0
@@ -0,0 +1,127 @@
1
+ #user nobody;
2
+ worker_processes 1;
3
+
4
+ #error_log logs/error.log;
5
+ #error_log logs/error.log notice;
6
+ #error_log logs/error.log info;
7
+
8
+ #pid logs/nginx.pid;
9
+
10
+ events {
11
+ worker_connections 1024;
12
+ }
13
+
14
+
15
+ http {
16
+ passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
17
+ passenger_ruby /home/<%= user %>/.rvm/bin/<%= application %>_ruby;
18
+
19
+ passenger_show_version_in_header off;
20
+ rails_env <%= passenger_env %>;
21
+ passenger_friendly_error_pages <%= passenger_env == "production" ? "off" : "on" %>;
22
+ passenger_pre_start http://localhost/;
23
+
24
+ include mime.types;
25
+ default_type application/octet-stream;
26
+
27
+ #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
28
+ # '$status $body_bytes_sent "$http_referer" '
29
+ # '"$http_user_agent" "$http_x_forwarded_for"';
30
+
31
+ #access_log logs/access.log main;
32
+
33
+ sendfile on;
34
+ #tcp_nopush on;
35
+
36
+ #keepalive_timeout 0;
37
+ keepalive_timeout 65;
38
+
39
+ #gzip on;
40
+
41
+ server {
42
+ listen 80 default deferred;
43
+ server_name localhost;
44
+
45
+ root <%= current_path %>/public;
46
+ passenger_enabled on;
47
+
48
+ #charset koi8-r;
49
+
50
+ #access_log logs/host.access.log main;
51
+
52
+ #location / {
53
+ # root html;
54
+ # index index.html index.htm;
55
+ #}
56
+
57
+ #error_page 404 /404.html;
58
+
59
+ # redirect server error pages to the static page /50x.html
60
+ #
61
+ error_page 500 502 503 504 /50x.html;
62
+ location = /50x.html {
63
+ root html;
64
+ }
65
+
66
+ # proxy the PHP scripts to Apache listening on 127.0.0.1:80
67
+ #
68
+ #location ~ \.php$ {
69
+ # proxy_pass http://127.0.0.1;
70
+ #}
71
+
72
+ # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
73
+ #
74
+ #location ~ \.php$ {
75
+ # root html;
76
+ # fastcgi_pass 127.0.0.1:9000;
77
+ # fastcgi_index index.php;
78
+ # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
79
+ # include fastcgi_params;
80
+ #}
81
+
82
+ # deny access to .htaccess files, if Apache's document root
83
+ # concurs with nginx's one
84
+ #
85
+ #location ~ /\.ht {
86
+ # deny all;
87
+ #}
88
+ }
89
+
90
+
91
+ # another virtual host using mix of IP-, name-, and port-based configuration
92
+ #
93
+ #server {
94
+ # listen 8000;
95
+ # listen somename:8080;
96
+ # server_name somename alias another.alias;
97
+
98
+ # location / {
99
+ # root html;
100
+ # index index.html index.htm;
101
+ # }
102
+ #}
103
+
104
+
105
+ # HTTPS server
106
+ #
107
+ #server {
108
+ # listen 443;
109
+ # server_name localhost;
110
+
111
+ # ssl on;
112
+ # ssl_certificate cert.pem;
113
+ # ssl_certificate_key cert.key;
114
+
115
+ # ssl_session_timeout 5m;
116
+
117
+ # ssl_protocols SSLv2 SSLv3 TLSv1;
118
+ # ssl_ciphers HIGH:!aNULL:!MD5;
119
+ # ssl_prefer_server_ciphers on;
120
+
121
+ # location / {
122
+ # root html;
123
+ # index index.html index.htm;
124
+ # }
125
+ #}
126
+
127
+ }