多个服务器上的Capifony Doctrine Database迁移

时间:2014-03-01 12:25:03

标签: ruby-on-rails symfony doctrine capistrano capifony

我正在尝试使用capifony在多台服务器上运行symfony doctrine迁移,但它似乎只在一台服务器上执行迁移。例如: 这就是我设置服务器地址的方式:

role :web,        "192.168.56.102","192.168.56.103"                         # Your HTTP server, Apache/etc
role :app,        "192.168.56.102","192.168.56.103", :primary => true       # This may be the same as your `Web` server

迁移仅在第一台服务器上执行,尽管两者都进行了代码部署。

这是我的部署脚本:

#### Basic Deploying Script Config ####
set :application, "eConnect"
###set :domain,      "192.168.56.102"
set :user,   "vagrant"
set :use_sudo,  false
set :deploy_to,   "/var/www/SchoolEconnectDeployment"
set :app_path,    "app"

set :repository, "."
set :local_repository, "."
set :deploy_via,  :copy
set :scm,         :mercurial
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `subversion`, `mercurial`, `perforce`, or `none`

set :model_manager, "doctrine"
set :interactive_mode, false
# Or: `propel`

role :web,        "192.168.56.102","192.168.56.103"                         # Your HTTP server, Apache/etc
role :app,        "192.168.56.102","192.168.56.103", :primary => true       # This may be the same as your `Web` server
set :copy_vendors, true
set :cache_warmup, false
set  :keep_releases,  3
set :shared_children, %w()

#############################################################################################################################################
#### Created Custom Methods for Deployment ####
namespace :deploy do
  task :create_release_dir, :except => {:no_release => true} do
    run "mkdir -p #{fetch :releases_path}"
  end
  #### Copying the Latest release to any specified directory ####
  task :move_latest_release, :roles => %w{app web db} do
    run "mkdir -p /var/www/school_econnect"
    run "yes | cp -rf #{ current_release }/* /var/www/school_econnect"
  end
  #### Econnect Migrations Custom Method ####
  desc "Executes a Econnect migration to a specified version or the latest available version"
  task :econnect_migrate, :roles => :app, :only => { :primary => true }, :except => { :no_release => true } do
    currentVersion = nil
    run "#{try_sudo} sh -c 'cd /var/www/school_econnect && #{php_bin} #{symfony_console} --no-ansi doctrine:migrations:status #{console_options}'", :once => true do |ch, stream, out|
      if stream == :out and out =~ /Current Version:.+\(([\w]+)\)/
        currentVersion = Regexp.last_match(1)
      end
      if stream == :out and out =~ /Current Version:\s*0\s*$/
        currentVersion = 0
      end
    end

    if currentVersion == nil
      raise "Could not find current database migration version"
    end
    logger.info "    Current database version: #{currentVersion}"

    on_rollback {
      if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database back to version #{currentVersion}? (y/N)")
        run "#{try_sudo} sh -c 'cd /var/www/school_econnect && #{php_bin} #{symfony_console} doctrine:migrations:migrate #{currentVersion} #{console_options} --no-interaction'", :once => true
      end
    }

    if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database? (y/N)")
      run "#{try_sudo} sh -c ' cd /var/www/school_econnect && #{php_bin} #{symfony_console} doctrine:migrations:migrate #{console_options} --no-interaction'", :once => true
    end
  end

  #### View the status of the migrations available ####
  desc "Views the status of a set of migrations"
  task :econnect_migrate_status, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} sh -c 'cd /var/www/school_econnect && #{php_bin} #{symfony_console} doctrine:migrations:status #{console_options} '", :once => true
  end
  #### Econnect Migrations Rollback Custom Method ####
  desc "Executes a Econnect migration Rollback to a specified version or the latest available version"
  task :econnect_rollback_migrate, :roles => :app, :only => { :primary => true }, :except => { :no_release => true } do
    currentVersion = nil
    run "#{try_sudo} sh -c 'cd /var/www/school_econnect && #{php_bin} #{symfony_console} --no-ansi doctrine:migrations:status #{console_options}'", :once => true do |ch, stream, out|
      if stream == :out and out =~ /Current Version:.+\(([\w]+)\)/
        currentVersion = Regexp.last_match(1)
      end
      if stream == :out and out =~ /Current Version:\s*0\s*$/
        currentVersion = 0
      end
    end

    if currentVersion == nil
      raise "Could not find current database migration version"
    end
    logger.info "    Current database version: #{currentVersion}"

    on_rollback {
      if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database back to version #{currentVersion}? (y/N)")
        run "#{try_sudo} sh -c 'cd /var/www/school_econnect && #{php_bin} #{symfony_console} doctrine:migrations:migrate #{currentVersion} #{console_options} --no-interaction'", :once => true
      end
    }

    if !interactive_mode || Capistrano::CLI.ui.agree("Do you really want to migrate #{symfony_env_prod}'s database? (y/N)")
      run "#{try_sudo} sh -c ' cd /var/www/school_econnect && #{php_bin} #{symfony_console} doctrine:migrations:execute #{currentVersion} --down #{console_options} --no-interaction'", :once => true
    end
  end
end
####################################################################################################################
### Spinner Stuff ###
@spinner_running = false
@chars = ['|', '/', '-', '\\']
@spinner = Thread.new do
  loop do
    unless @spinner_running
      Thread.stop
    end
    print @chars[0]
    sleep(0.1)
    print "\b"
    @chars.push @chars.shift
  end
end

def start_spinner
  @spinner_running = true
  @spinner.wakeup
end

# stops the spinner and backspaces over last displayed character
def stop_spinner
  @spinner_running = false
  print "\b"
end
###############################################################
STDOUT.sync
####### Custom Methods Hooks with Capifony's Deployment Methods #######
after "deploy", "deploy:cleanup"
after "deploy:setup", "deploy:create_release_dir"
 before "deploy:move_latest_release" do
    print " --> Moving the latest version to the correct Destination ........"
    start_spinner()
end
after "deploy:move_latest_release" do
    stop_spinner()
    puts " Done.".green
end
after "deploy:rollback" do
    print " --> Moving the latest version to the correct Destination ........"
    start_spinner()
    run "mkdir -p /var/www/school_econnect"
    run "yes | cp -rf #{ previous_release }/* /var/www/school_econnect"
    stop_spinner()
    puts " Done.".green
end
after "deploy:cleanup", "deploy:move_latest_release"
after "deploy:move_latest_release", "deploy:econnect_migrate"

before "deploy:econnect_migrate" do
    print " --> Running Migrations ......"
    start_spinner()

end
after "deploy:econnect_migrate" do
    stop_spinner()
    puts " Done.".green
end
before "deploy:econnect_rollback_migrate" do
    print " --> Rolling Back Latest Migration ......"
    start_spinner()
end
after "deploy:econnect_rollback_migrate" do
    stop_spinner()
    puts " Done.".green
end
##############################################################################################################
# Be more verbose by uncommenting the following line
logger.level = Logger::MAX_LEVEL

正如你所看到的,我必须做我自己的迁移功能,这个功能略有不同(没有那么不同的硬编码路径)。

我到处寻找,但找不到与我的问题相关的任何内容。

如果没有解决方案,我想知道如何在循环中调用任务并为其提供我想在部署脚本中编写的任何参数。我对红宝石很新,所以你知道。

P.S:我也尝试过多阶段方法,但没有任何方法可以帮助我。 我将不胜感激任何帮助。 非常感谢。

编辑:

我一直试图找到一个我一直在尝试的解决方法:

before "deploy:move_latest_release" do 
 machine_info = ['192.168.56.102','192.168.56.103']
    machine_info.each do |value| 


        role :db,        "#{value}", :primary => true       
        puts "#{value}".red
        deploy.econnect_migrate
    end 
    print " --> Moving the latest version to the correct Destination ........"
    start_spinner()
end

循环工作正常,但角色:db不会改变。

我也试过这个

cap HOSTS=192.168.56.102,192.168.56.103 deploy:econnect_migrate

仍然是同一个问题。

0 个答案:

没有答案