如何从顶部(对于FastCGI)部署带有Capistrano的Rails 2.3.2应用程序?

时间:2021-08-28 23:10:50

Disclaimer: i am a noob to Rails and Capistrano (but an intermediate .NET C# developer) trying to learn more.

免责声明:我是Rails和Capistrano(但是一个中级.NET C#开发人员)的菜鸟,试图了解更多信息。

i have been have the toughest time getting a Capistrano development process set-up. here's what i have so far for my deploy.rb:

我一直有最艰难的时间来进行Capistrano开发流程设置。这是我到目前为止我的deploy.rb:


set :application, "MyAppName"
set :domain, "domainname"
set :user, "userid" 
set :repository,  "svn+ssh://#{user}@#{domain}/home/#{user}/svn/#{application}/trunk"
set :rails_env, :production
set :chmod755, %w(app config db lib public vendor script tmp public/dispatch.cgi public/dispatch.fcgi public/dispatch.rb)
set :deploy_to, "/home/#{user}/apps/#{application}"
set :use_sudo, false
set :deploy_via, :checkout
set :group_writable, false
default_run_options[:pty] = true
role :app, domain
role :web, domain
role :db,  domain, :primary => true

here's my Capfile:

这是我的Capfile:


load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy'

namespace :deploy do

  task :restart, :roles => :app do
    run "#{current_path}/script/process/reaper --dispatcher=dispatch.fcgi"
  end

  desc "Set the proper permissions for directories and files on HostingRails accounts"
  task :after_deploy do
    run(chmod755.collect do |item|
      "chmod 755 #{current_path}/#{item}"
    end.join(" && "))
  end

end
  1. Rails 2.3.2 doesn't have the script/process directory so the restart task doesn't work at all. How do I fix this?
  2. Rails 2.3.2没有脚本/进程目录,因此重启任务根本不起作用。我该如何解决?

  3. Is there a way to set the RAILS_ENV variable in environment.rb to "production" when the application is deployed ("set :rails_env, :production" does not do it)?
  4. 有没有办法在部署应用程序时将environment.rb中的RAILS_ENV变量设置为“production”(“set:rails_env,:production”不执行此操作)?

  5. Also, am I missing anything else from these 2 files?
  6. 另外,我错过了这2个文件中的其他内容吗?

2 个解决方案

#1


Here's my deploy.rb, which hasn't really been updated in a long time. I run two sed commands to set the environment inside my :after_update_code task. I'm certain there's an easier way to do it, but this is my current method.

这是我的deploy.rb,它在很长一段时间内都没有真正更新过。我运行两个sed命令来设置my:after_update_code任务中的环境。我确信有一种更简单的方法,但这是我现在的方法。

require 'erb'

before "deploy:setup", :db
after "deploy:update_code", "db:symlink" 
after "deploy", "deploy:cleanup"

def prompt_with_default(var, default)
  set(var) do
    Capistrano::CLI.ui.ask "Enter #{var} [#{default}] : "
  end

  set var, default if eval("#{var.to_s}.empty?")
end

default_run_options[:pty] = true 

set :rake, "/usr/local/bin/rake"

set :user, "your username"
set :application, "application name"
set :repository,  "svn repository path"
set :host, "domain name"
set :db_host, "DB host name"
set :db_user, "DB user name"
set :db_prefix, "any DB name prefix your host might require"
set :rails_env, "production"

set :deploy_to, "/home/username/rails/#{host}" # your deployment directory

role :app, host
role :web, host
role :db,  host, :primary => true

set :use_sudo, false
set :checkout, "export"

#
# TASKS
#
desc "Tasks to execute after code update"
task :after_update_code, :roles => [:app, :db, :web] do
  # Make sure we're running in production
  run "sed -i -e '/ENV.*RAILS_ENV/s/# //' #{release_path}/config/environment.rb"
  run "sed -i -e '/ENV.*RAILS_ENV/s/production/#{rails_env}/' #{release_path}/config/environment.rb"

  # Add banned IPs
  deny_lines = []
  File.readlines("#{release_path}/config/banned_ips").each {|ip|
    deny_lines << "deny from #{ip}"
  }

  ip_ban_block = <<EOBAN
# Bannination
order allow,deny
#{deny_lines.join}
allow from all
EOBAN

  run "sed -i -e 's/# BANNED IPS/#{ip_ban_block}/' #{release_path}/public/.htaccess"

  run "chmod +x #{release_path}/script/runner" 
  run "chmod +x #{release_path}/script/process/reaper" 
  run "chmod +x #{release_path}/script/process/spawner" 
  run "chmod 755 #{release_path}/public/dispatch.*" 
end

desc "Restarting after deployment"
task :after_deploy, :roles => [:app, :db, :web] do
  run "cd #{current_path} && rake RAILS_ENV=production db:sessions:clear tmp:clear"
  # fix permissions
  run "dos2unix #{release_path}/public/dispatch.*"
  run "dos2unix #{release_path}/public/.htaccess"
  run "chmod -R 755 #{release_path}"
  run "chmod -R 775 #{release_path}/log #{release_path}/tmp #{release_path}/script"
  run "find #{release_path}/ | xargs touch"
  run "touch #{deploy_to}/current/public/dispatch.fcgi"
end

desc "Restarting after rollback"
task :after_rollback, :roles => [:app, :db, :web] do
  run "touch #{deploy_to}/current/public/dispatch.fcgi"
end

namespace :db do
  desc "Create database yaml in shared path" 
  task :default do
  prompt_with_default(:db_password, "")
  db_config = ERB.new <<-EOF
base: &base
  adapter: mysql
  port: 3306
  host: #{db_host}
  username: #{db_user}
  password: #{db_password}

development:
  database: #{db_prefix}#{application}development
  <<: *base

test:
  database: #{db_prefix}#{application}test
  <<: *base

production:
  database: #{db_prefix}#{application}production
  <<: *base
EOF

    run "mkdir -p #{shared_path}/config" 
    put db_config.result, "#{shared_path}/config/database.yml" 
  end

  desc "Make symlink for database yaml" 
  task :symlink do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" 
  end
end

Once I set up my variables, I have a bash script that runs:

一旦我设置了变量,我就会运行一个bash脚本:

svn export $svn_path . --force
cap deploy

That'll ask me for my password twice. Once for exporting from SVN, and once as a prompt to automatically generate my database.yml in the shared path.

那会问我两次密码。一次从SVN导出,一次作为提示在共享路径中自动生成我的database.yml。

#2


Updated my answer, see #3...

更新了我的回答,请参阅#3 ...

I don't use FastCGI for deployment, but a few things that come to mind:

我没有使用FastCGI进行部署,但有些事情会浮现在脑海中:

  1. The process scripts have been moved out of Rails core and into a plugin, which you can grab from GitHub.
  2. 流程脚本已从Rails核心移出并插入到插件中,您可以从GitHub中获取该插件。

  3. Looking at my deploy.rb file, you'll likely need to pass the RAILS_ENV environment variable into the script through the command line, similar to:

    查看我的deploy.rb文件,您可能需要通过命令行将RAILS_ENV环境变量传递到脚本中,类似于:

    run #{current_path}/script/process/reaper RAILS_ENV=#{rails_env} --dispatcher=dispatch.fcgi

    run#{current_path} / script / process / reaper RAILS_ENV =#{rails_env} --dispatcher = dispatch.fcgi

  4. EDIT: Actually, it is slightly odd to have your tasks in the Capfile instead of deploy.rb - although looking at this thread on the Capistrano Google Group, it's not completely frowned upon. Up to you/your team, I suppose.
  5. 编辑:实际上,将你的任务放在Capfile而不是deploy.rb中有点奇怪 - 尽管在Capistrano Google Group上查看这个帖子,但并不完全不赞成。我想,由您/您的团队决定。

Hope this points you in the right direction.

希望这能指出你正确的方向。

#1


Here's my deploy.rb, which hasn't really been updated in a long time. I run two sed commands to set the environment inside my :after_update_code task. I'm certain there's an easier way to do it, but this is my current method.

这是我的deploy.rb,它在很长一段时间内都没有真正更新过。我运行两个sed命令来设置my:after_update_code任务中的环境。我确信有一种更简单的方法,但这是我现在的方法。

require 'erb'

before "deploy:setup", :db
after "deploy:update_code", "db:symlink" 
after "deploy", "deploy:cleanup"

def prompt_with_default(var, default)
  set(var) do
    Capistrano::CLI.ui.ask "Enter #{var} [#{default}] : "
  end

  set var, default if eval("#{var.to_s}.empty?")
end

default_run_options[:pty] = true 

set :rake, "/usr/local/bin/rake"

set :user, "your username"
set :application, "application name"
set :repository,  "svn repository path"
set :host, "domain name"
set :db_host, "DB host name"
set :db_user, "DB user name"
set :db_prefix, "any DB name prefix your host might require"
set :rails_env, "production"

set :deploy_to, "/home/username/rails/#{host}" # your deployment directory

role :app, host
role :web, host
role :db,  host, :primary => true

set :use_sudo, false
set :checkout, "export"

#
# TASKS
#
desc "Tasks to execute after code update"
task :after_update_code, :roles => [:app, :db, :web] do
  # Make sure we're running in production
  run "sed -i -e '/ENV.*RAILS_ENV/s/# //' #{release_path}/config/environment.rb"
  run "sed -i -e '/ENV.*RAILS_ENV/s/production/#{rails_env}/' #{release_path}/config/environment.rb"

  # Add banned IPs
  deny_lines = []
  File.readlines("#{release_path}/config/banned_ips").each {|ip|
    deny_lines << "deny from #{ip}"
  }

  ip_ban_block = <<EOBAN
# Bannination
order allow,deny
#{deny_lines.join}
allow from all
EOBAN

  run "sed -i -e 's/# BANNED IPS/#{ip_ban_block}/' #{release_path}/public/.htaccess"

  run "chmod +x #{release_path}/script/runner" 
  run "chmod +x #{release_path}/script/process/reaper" 
  run "chmod +x #{release_path}/script/process/spawner" 
  run "chmod 755 #{release_path}/public/dispatch.*" 
end

desc "Restarting after deployment"
task :after_deploy, :roles => [:app, :db, :web] do
  run "cd #{current_path} && rake RAILS_ENV=production db:sessions:clear tmp:clear"
  # fix permissions
  run "dos2unix #{release_path}/public/dispatch.*"
  run "dos2unix #{release_path}/public/.htaccess"
  run "chmod -R 755 #{release_path}"
  run "chmod -R 775 #{release_path}/log #{release_path}/tmp #{release_path}/script"
  run "find #{release_path}/ | xargs touch"
  run "touch #{deploy_to}/current/public/dispatch.fcgi"
end

desc "Restarting after rollback"
task :after_rollback, :roles => [:app, :db, :web] do
  run "touch #{deploy_to}/current/public/dispatch.fcgi"
end

namespace :db do
  desc "Create database yaml in shared path" 
  task :default do
  prompt_with_default(:db_password, "")
  db_config = ERB.new <<-EOF
base: &base
  adapter: mysql
  port: 3306
  host: #{db_host}
  username: #{db_user}
  password: #{db_password}

development:
  database: #{db_prefix}#{application}development
  <<: *base

test:
  database: #{db_prefix}#{application}test
  <<: *base

production:
  database: #{db_prefix}#{application}production
  <<: *base
EOF

    run "mkdir -p #{shared_path}/config" 
    put db_config.result, "#{shared_path}/config/database.yml" 
  end

  desc "Make symlink for database yaml" 
  task :symlink do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" 
  end
end

Once I set up my variables, I have a bash script that runs:

一旦我设置了变量,我就会运行一个bash脚本:

svn export $svn_path . --force
cap deploy

That'll ask me for my password twice. Once for exporting from SVN, and once as a prompt to automatically generate my database.yml in the shared path.

那会问我两次密码。一次从SVN导出,一次作为提示在共享路径中自动生成我的database.yml。

#2


Updated my answer, see #3...

更新了我的回答,请参阅#3 ...

I don't use FastCGI for deployment, but a few things that come to mind:

我没有使用FastCGI进行部署,但有些事情会浮现在脑海中:

  1. The process scripts have been moved out of Rails core and into a plugin, which you can grab from GitHub.
  2. 流程脚本已从Rails核心移出并插入到插件中,您可以从GitHub中获取该插件。

  3. Looking at my deploy.rb file, you'll likely need to pass the RAILS_ENV environment variable into the script through the command line, similar to:

    查看我的deploy.rb文件,您可能需要通过命令行将RAILS_ENV环境变量传递到脚本中,类似于:

    run #{current_path}/script/process/reaper RAILS_ENV=#{rails_env} --dispatcher=dispatch.fcgi

    run#{current_path} / script / process / reaper RAILS_ENV =#{rails_env} --dispatcher = dispatch.fcgi

  4. EDIT: Actually, it is slightly odd to have your tasks in the Capfile instead of deploy.rb - although looking at this thread on the Capistrano Google Group, it's not completely frowned upon. Up to you/your team, I suppose.
  5. 编辑:实际上,将你的任务放在Capfile而不是deploy.rb中有点奇怪 - 尽管在Capistrano Google Group上查看这个帖子,但并不完全不赞成。我想,由您/您的团队决定。

Hope this points you in the right direction.

希望这能指出你正确的方向。