Rails in da Cloud
Почему?
• Сатанисты хотят в облака. WTF?
• Это модно.
• Это удобно, выгодно, отказоустойчиво и дёшево.




                                                   2
Цель
Запуск rails в Amazon EC2




                            3
План
•   Запуск инстанса
•   Подключение к нему
•   Настройка окружения
•   Деплой
•   ??????
•   PROFIT!




                          4
5
6
7
8
9
10
11
12
13
14
15
16
17
Connect!
                      ~ $   mv Downloads/igas.pem .ssh/igas.pem
                      ~ $   cd .ssh
                 ~/.ssh $   chmod 400 igas.pem
                 ~/.ssh $   ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.com
ubuntu@ip-10-203-42-73:~$   exit
                      ~ $   cat ~/.ssh/id_rsa.pub| pbcopy
                 ~/.ssh $   ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.com
ubuntu@ip-10-203-42-73:~$   echo 'ssh-rsa AAAAB3NEww== igasgeek@me.com' >> .ssh/authorized_keys
ubuntu@ip-10-203-42-73:~$   sudo apt-get update
ubuntu@ip-10-203-42-73:~$   sudo apt-get -y upgrade
ubuntu@ip-10-203-42-73:~$   sudo apt-get -y install git-core build-essential zlib1g-dev 
                            libssl-dev libreadline-gplv2-dev libyaml-dev 
                            python-software-properties libffi-dev nginx-light 
                            postgresql-server-dev-9.1
ubuntu@ip-10-203-42-73:~$   sudo bash -c 'echo "RAILS_ENV=production" >> /etc/environment'
ubuntu@ip-10-203-42-73:~$   echo "gem: --no-ri --no-rdoc" > ~/.gemrc
ubuntu@ip-10-203-42-73:~$   curl -L https://get.rvm.io | bash -s stable --ruby
ubuntu@ip-10-203-42-73:~$   source $HOME/.rvm/scripts/rvm
ubuntu@ip-10-203-42-73:~$   sudo rm /etc/nginx/sites-enabled/default
ubuntu@ip-10-203-42-73:~$   sudo vi /etc/nginx/sites-enabled/test
ubuntu@ip-10-203-42-73:~$   sudo service nginx start




                                                                                                  18
nginx
 upstream unicorn {
   server unix:/tmp/testaws.socket fail_timeout=0;
 }

 server {
   listen 80 default;
   server_name 184.72.181.253;

     root /home/ubuntu/www/testaws/current/public;
     access_log /var/log/nginx/testaws.log;
     rewrite_log on;

     location / {
       proxy_pass http://unicorn;
       proxy_redirect    off;

       proxy_set_header     Host              $host;
       proxy_set_header     X-Real-IP         $remote_addr;
       proxy_set_header     X-Forwarded-For   $proxy_add_x_forwarded_for;
     }
     location ~ ^/assets/    {
       expires max;
       break;
     }
 }




                                                                            19
Capfile
 load 'deploy'
 load 'deploy/assets'
 load 'config/deploy' # remove this line to skip loading any of the default tasks




                                                                                    20
Gemfile
 source 'https://rubygems.org'
 gem 'rails', '3.2.9'
 gem 'pg'
 gem 'libv8', '~> 3.11.8.3'
 group :assets do
   gem 'sass-rails',   '~> 3.2.3'
   gem 'coffee-rails', '~> 3.2.1'
   gem 'uglifier', '>= 1.0.3'
   gem 'therubyracer', '~> 0.11.0beta5'
 end
 gem 'jquery-rails'
 gem 'rvm-capistrano'
 gem 'unicorn'
 gem 'capistrano'




                                          21
config/deploy.rb
 require 'bundler/capistrano'
 require "rvm/capistrano"
 set :rvm_ruby_string, '1.9.3'
 set :application,     'testaws'
 set :user,            'ubuntu'
 set :scm,             'git'
 set :repository,      'git@github.com:igas/testaws.git'
 set :branch,          'master'
 set :deploy_to,       "/home/#{user}/www/#{application}"
 set :use_sudo,        false
 set :domain,          '184.72.181.253'
 default_run_options[:pty] = true
 ssh_options[:forward_agent] = true

 server domain, :web, :app, :db, primary: true

 namespace :deploy do
   task :start, except: { no_release: true } do
     run "cd #{current_path} && bundle exec unicorn -c config/unicorn.rb -D"
   end

   task :stop, except: { no_release: true } do
     run "cd #{current_path} && kill `cat tmp/pids/unicorn.#{application}.pid`"
   end
 end




                                                                                  22
config/unicorn.rb
 env = ENV["RAILS_ENV"] || "development"
 user = "ubuntu"
 app = "testaws"
 root_path = "/home/#{user}/www/#{app}"
 current_path = "#{root_path}/current"
 shared_path = "#{root_path}/shared"
 log_path = "#{shared_path}/log"

 worker_processes 2
 preload_app true
 timeout 240
 listen "/tmp/#{app}.socket", backlog: 64
 pid "#{shared_path}/pids/unicorn.#{app}.pid"
 working_directory current_path
 stderr_path "#{shared_path}/log/unicorn.stderr.log"
 stdout_path "#{shared_path}/log/unicorn.stdout.log"

 before_fork do |server, worker|
   defined?(ActiveRecord::Base) and
     ActiveRecord::Base.connection.disconnect!
 end

 after_fork do |server, worker|
   defined?(ActiveRecord::Base) and
     ActiveRecord::Base.establish_connection
 end




                                                       23
unicorn
 #! /bin/sh

 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 DAEMON=/usr/local/rvm/gems/ree-1.8.7-2010.02/bin/unicorn
 DAEMON_OPTS="-c /web_apps/example_rack_app/unicorn.rb -E production -D"
 NAME=unicorn
 DESC="Unicorn app for example_rack_app"
 PID=/web_apps/example_rack_app/pids/unicorn.pid

 case "$1" in
   start)
 !   echo -n "Starting $DESC: "
 !   $DAEMON $DAEMON_OPTS
 !   echo "$NAME."
 !   ;;
   stop)
 !   echo -n "Stopping $DESC: "
          kill -QUIT `cat $PID`
 !   echo "$NAME."
 !   ;;
   restart)
 !   echo -n "Restarting $DESC: "
          kill -QUIT `cat $PID`
 !   sleep 1
 !   $DAEMON $DAEMON_OPTS
 !   echo "$NAME."




                                                                           24
unicorn
 sudo chmod +x /etc/init.d/unicorn
 sudo /usr/sbin/update-rc.d -f unicorn defaults
 /etc/init.d/unicorn start




                                                  25
Вопросы?




           Igas, Ruby Ninja

EC2

  • 1.
  • 2.
    Почему? • Сатанисты хотятв облака. WTF? • Это модно. • Это удобно, выгодно, отказоустойчиво и дёшево. 2
  • 3.
  • 4.
    План • Запуск инстанса • Подключение к нему • Настройка окружения • Деплой • ?????? • PROFIT! 4
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
    Connect! ~ $ mv Downloads/igas.pem .ssh/igas.pem ~ $ cd .ssh ~/.ssh $ chmod 400 igas.pem ~/.ssh $ ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.com ubuntu@ip-10-203-42-73:~$ exit ~ $ cat ~/.ssh/id_rsa.pub| pbcopy ~/.ssh $ ssh -i igas.pem ubuntu@ec2-184-72-181-253.compute-1.amazonaws.com ubuntu@ip-10-203-42-73:~$ echo 'ssh-rsa AAAAB3NEww== igasgeek@me.com' >> .ssh/authorized_keys ubuntu@ip-10-203-42-73:~$ sudo apt-get update ubuntu@ip-10-203-42-73:~$ sudo apt-get -y upgrade ubuntu@ip-10-203-42-73:~$ sudo apt-get -y install git-core build-essential zlib1g-dev libssl-dev libreadline-gplv2-dev libyaml-dev python-software-properties libffi-dev nginx-light postgresql-server-dev-9.1 ubuntu@ip-10-203-42-73:~$ sudo bash -c 'echo "RAILS_ENV=production" >> /etc/environment' ubuntu@ip-10-203-42-73:~$ echo "gem: --no-ri --no-rdoc" > ~/.gemrc ubuntu@ip-10-203-42-73:~$ curl -L https://get.rvm.io | bash -s stable --ruby ubuntu@ip-10-203-42-73:~$ source $HOME/.rvm/scripts/rvm ubuntu@ip-10-203-42-73:~$ sudo rm /etc/nginx/sites-enabled/default ubuntu@ip-10-203-42-73:~$ sudo vi /etc/nginx/sites-enabled/test ubuntu@ip-10-203-42-73:~$ sudo service nginx start 18
  • 19.
    nginx upstream unicorn{ server unix:/tmp/testaws.socket fail_timeout=0; } server { listen 80 default; server_name 184.72.181.253; root /home/ubuntu/www/testaws/current/public; access_log /var/log/nginx/testaws.log; rewrite_log on; location / { proxy_pass http://unicorn; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ ^/assets/ { expires max; break; } } 19
  • 20.
    Capfile load 'deploy' load 'deploy/assets' load 'config/deploy' # remove this line to skip loading any of the default tasks 20
  • 21.
    Gemfile source 'https://rubygems.org' gem 'rails', '3.2.9' gem 'pg' gem 'libv8', '~> 3.11.8.3' group :assets do gem 'sass-rails', '~> 3.2.3' gem 'coffee-rails', '~> 3.2.1' gem 'uglifier', '>= 1.0.3' gem 'therubyracer', '~> 0.11.0beta5' end gem 'jquery-rails' gem 'rvm-capistrano' gem 'unicorn' gem 'capistrano' 21
  • 22.
    config/deploy.rb require 'bundler/capistrano' require "rvm/capistrano" set :rvm_ruby_string, '1.9.3' set :application, 'testaws' set :user, 'ubuntu' set :scm, 'git' set :repository, 'git@github.com:igas/testaws.git' set :branch, 'master' set :deploy_to, "/home/#{user}/www/#{application}" set :use_sudo, false set :domain, '184.72.181.253' default_run_options[:pty] = true ssh_options[:forward_agent] = true server domain, :web, :app, :db, primary: true namespace :deploy do task :start, except: { no_release: true } do run "cd #{current_path} && bundle exec unicorn -c config/unicorn.rb -D" end task :stop, except: { no_release: true } do run "cd #{current_path} && kill `cat tmp/pids/unicorn.#{application}.pid`" end end 22
  • 23.
    config/unicorn.rb env =ENV["RAILS_ENV"] || "development" user = "ubuntu" app = "testaws" root_path = "/home/#{user}/www/#{app}" current_path = "#{root_path}/current" shared_path = "#{root_path}/shared" log_path = "#{shared_path}/log" worker_processes 2 preload_app true timeout 240 listen "/tmp/#{app}.socket", backlog: 64 pid "#{shared_path}/pids/unicorn.#{app}.pid" working_directory current_path stderr_path "#{shared_path}/log/unicorn.stderr.log" stdout_path "#{shared_path}/log/unicorn.stdout.log" before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end 23
  • 24.
    unicorn #! /bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/rvm/gems/ree-1.8.7-2010.02/bin/unicorn DAEMON_OPTS="-c /web_apps/example_rack_app/unicorn.rb -E production -D" NAME=unicorn DESC="Unicorn app for example_rack_app" PID=/web_apps/example_rack_app/pids/unicorn.pid case "$1" in start) ! echo -n "Starting $DESC: " ! $DAEMON $DAEMON_OPTS ! echo "$NAME." ! ;; stop) ! echo -n "Stopping $DESC: " kill -QUIT `cat $PID` ! echo "$NAME." ! ;; restart) ! echo -n "Restarting $DESC: " kill -QUIT `cat $PID` ! sleep 1 ! $DAEMON $DAEMON_OPTS ! echo "$NAME." 24
  • 25.
    unicorn sudo chmod+x /etc/init.d/unicorn sudo /usr/sbin/update-rc.d -f unicorn defaults /etc/init.d/unicorn start 25
  • 26.
    Вопросы? Igas, Ruby Ninja