简体   繁体   中英

Rails Capistrano Deployment

for the first time I'm trying to deploy a rails app not to Heroku but to DigitalOcean, mainly because of the price. I'm completely new to Capistrano as well as VPS deployment and I'm totally lost. I've created the 1-click-Rails-droplet and followed this tutorial: http://guides.beanstalkapp.com/deployments/deploy-with-capistrano.html

This are the settings in my deploy.rb file:

require 'capistrano/ext/multistage'

set :stages, ["staging", "production"]
set :default_stage, "staging"

set :application, "myAppName"
set :scm, :git
set :repository, "myGitRepository"
set :use_sudo, :false

set :deploy_via, :remote_cache

set :scm_passphrase, "myPassword"
set :user, "root"
role :web, "xx.xxx.x.xxx" 
role :app, "xx.xxx.x.xxx" 

The staging.rb file:

server "xx.xxx.x.xxx", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/xx.xxx.x.xxx_staging"

And the production.rb file

server "xx.xxx.x.xxx", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/xx.xxx.x.xxx"

Now, when I run

cap deploy:check

in the terminal I get the following:

[xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 88ms
  * executing "test -w /var/www/xx.xxx.x.xxx_staging"
    servers: ["xx.xxx.x.xxx"]
    [xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 79ms
  * executing "test -w /var/www/xx.xxx.x.xxx_staging/releases"
    servers: ["xx.xxx.x.xxx"]
    [xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 72ms
  * executing "which git"
    servers: ["xx.xxx.x.xxx"]
    [xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 85ms
  * executing "test -w /var/www/xx.xxx.x.xxx_staging/shared"
    servers: ["xx.xxx.x.xxx"]
    [xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 81ms
The following dependencies failed. Please check them and try again:
--> `/var/www/xx.xxx.x.xxx_staging/releases' does not exist. Please run `cap staging deploy:setup'. (xx.xxx.x.xxx)
--> You do not have permissions to write to `/var/www/xx.xxx.x.xxx_staging'. (xx.xxx.x.xxx)
--> You do not have permissions to write to `/var/www/3xx.xxx.x.xxx_staging/releases'. (xx.xxx.x.xxx)
--> `git' could not be found in the path (xx.xxx.x.xxx)
--> `/var/www/xx.xxx.x.xxx_staging/shared' is not writable (xx.xxx.x.xxx)

I've googled for a long time now but can't fix this. As I'm totally new to this and feel like there is a huge gap between learning to create a rails app and knowing all the things about deploying them (btw: what happened to ftp upload from earlier days?), I really hope someone knows how to fix this and could guide me into a direction to a not so steep learning curve for deployment. By the way: are there "easier" ways to do what I'm trying to do? Thank you very much!

Edit: One more question: Do I really need to deploy the app at github on the way to the server?

require "bundler/capistrano"

server "XXX.XX.XXX.XXX", :web, :app, :db, primary: true

This is just informing Capistrano which server you want to be accessing. For now since you have only one server it will be acting as a web, app and db server. The web server is your nginx + unicorn. App server is your rails application and db is your database server. The primary true indicates that this is your primary database server once you expand to more database servers. These are called roles you can define more but these are the main ones that are required. You can specify some tasks to work on some roles and not others, but that's all when you have more than 1 server.

set :application, "applicationName"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, :git 
set :repository,  "GIT REPO URL"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true

These just set up some variables for Capistrano to use. Your application name, the user that you will be deploying as (don't use root create another user), where you want to deploy, whether to use sudo when running commands (Ryan says sometimes he encounters permission errors if sudo is used). Then the source code management system as git / subversion, and a link to your repository and the branch with which you want to deploy to. The next 2 options If I recall were to handle some things with the ssh keys.

So when it comes to thinking of how Capistrano works think of exactly how a Rakefile and Rake tasks work.

after "deploy", "deploy:cleanup"    

if you see this kind of statement after "XXX", "YYY", it's just saying after task XXX is done do YYY. So in this case once task deploy is done, do deploy:cleanup.

namespace :deploy do
  %w[start,stop,restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      run "/etc/init.d/unicorn_#{application} #{command}"
  end
end

This task is just creating tasks to be able to stop start and restart the unicorn server. Therefore this task can be used in conjunction later as soon as the site is deployed to restart the unicorn server.

task :setup_config, roles: :app do
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
    sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
    puts "Now edit the config files in #{shared_path}."
  end
  after "deploy:setup", "deploy:setup_config"
  task :symlink_config, roles: :app do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end
  after "deploy:finalize_update", "deploy:symlink_config"

So this is a very complex looking task, but it's actually not doing much. Since in the railscasts Ryan was not putting his database.yml file in git (with production values), he just created a template database.example.yml file which is being put in folder apps/APPNAME/shared/ you have to manually go and edit it. These shared configurations are being symlinked to the current folder.

In the other files nginx.conf unicorn.rb and unicorn_init.sh you'd want to edit the paths to point to correct path.

Additional Resources

Also take a look at the Railscasts Capistrano Recipes where he expands on the details of his recipes and makes them more DRY. The Capistrano Wiki also has a great resource called from the beginning . In addition Nginx & Unicorn covers more on those 2 things. I'd also suggest reading Github's blog post on Unicorn and this Nginx primer they were help me clear up some other doubts I had.

But most of all you don't worry if you get errors just try breaking it down 1 error at a time and good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM