Search code examples
ruby-on-railsrubywordpresscapistrano

Capistrano 2 tasks file, syntax needs fixing


Find below my tasks.rb file from a capistrano 2 deployment. Its originally from WP Stack and I've hacked it so allow me to do "cap dev db:sync" as well as "cap staging db:sync" now this works fine, but I've put the "end" syntax somewhere incorrect and broken the "db" section at the end as my deployment now reports that "db:make_config" doesn't exist. I have no idea what I'm doing and I've blindly tried moving the "end" syntax around to no avail, could someone with some idea have a look at this for me and point out what might need moving.

namespace :shared do
task :make_shared_dir do
    run "if [ ! -d #{shared_path}/files ]; then mkdir #{shared_path}/files; fi"
end
task :make_symlinks do
    run "if [ ! -h #{release_path}/shared ]; then ln -s #{shared_path}/files/ #{release_path}/shared; fi"
end
end

namespace :nginx do
desc "Restarts nginx"
task :restart do
    run "sudo /etc/init.d/nginx reload"
end
end

namespace :phpfpm do
desc" Restarts PHP-FPM"
task :restart do
    run "sudo /etc/init.d/php-fpm restart"
end
end

 namespace :git do
desc "Updates git submodule tags"
task :submodule_tags do
    run "if [ -d #{shared_path}/cached-copy/ ]; then cd #{shared_path}/cached-copy/ && git submodule foreach --recursive git fetch origin --tags; fi"
end
end

namespace :memcached do
desc "Restarts Memcached"
task :restart do
    run "echo 'flush_all' | nc localhost 11211", :roles => [:memcached]
end
desc "Updates the pool of memcached servers"
task :update do
    unless find_servers( :roles => :memcached ).empty? then
        mc_servers = '<?php return array( "' + find_servers( :roles => :memcached ).join( ':11211", "' ) + ':11211" ); ?>'
        run "echo '#{mc_servers}' > #{current_path}/memcached.php", :roles => :memcached
    end
end
end

namespace :db do
desc "Syncs the staging database from production"
task :sync, :roles => :web  do
if stage == :production then
    puts "[Error] You must run db:sync from staging with cap staging db:sync or from local with cap local db:sync"
        else
        if stage == :staging then
        puts "Hang on... this might take a while."
        random = rand( 10 ** 5 ).to_s.rjust( 5, '0' )
        p = wpdb[ :production ]
        s = wpdb[ :staging ]
        puts "db:sync"
        puts stage
        system "mysqldump -u #{p[:user]} --result-file=/tmp/wpstack-#{random}.sql -h #{p[:host]} -p#{p[:password]} #{p[:name]}"
        system "mysql -u #{s[:user]} -h #{s[:host]} -p#{s[:password]} #{s[:name]} < /tmp/wpstack-#{random}.sql && rm /tmp/wpstack-#{random}.sql"
        puts "Database synced to staging"
        # memcached.restart
        puts "Memcached flushed"
end
if stage == :dev then
puts "Hang on... this might take a while."
random = rand( 10 ** 5 ).to_s.rjust( 5, '0' )
p = wpdb[ :production ]
d = wpdb[ :dev ]
puts "db:sync"
puts dev
system "mysqldump -u #{p[:user]} --result-file=/tmp/wpstack-#{random}.sql -h #{p[:host]} -p#{p[:password]} #{p[:name]}"
system "mysql -u #{d[:user]} -h #{d[:host]} -p#{d[:password]} #{d[:name]} < /tmp/wpstack-#{random}.sql && rm /tmp/wpstack-#{random}.sql"
puts "Database synced to development repo"
# memcached.restart
puts "Memcached flushed"
end
desc "Sets the database credentials (and other settings) in wp-config.php"
task :make_config do
    set :staging_domain, '077397919.45' unless defined? staging_domain
    {:'%%WP_STAGING_DOMAIN%%' => staging_domain, :'%%WP_STAGE%%' => stage, :'%%DB_NAME%%' => wpdb[stage][:name], :'%%DB_USER%%' => wpdb[stage][:user], :'%%DB_PASSWORD%%' => wpdb[stage][:password], :'%%DB_HOST%%' => wpdb[stage][:host]}.each do |k,v|
        run "sed -i 's/#{k}/#{v}/' #{release_path}/wp-config.php", :roles => :web
    end
end
end

Solution

  • Try this:

    namespace :shared do
      task :make_shared_dir do
        run "if [ ! -d #{shared_path}/files ]; then mkdir #{shared_path}/files; fi"
      end
    
      task :make_symlinks do
        run "if [ ! -h #{release_path}/shared ]; then ln -s #{shared_path}/files/ #{release_path}/shared; fi"
      end
    end
    
    namespace :nginx do
      desc "Restarts nginx"
      task :restart do
        run "sudo /etc/init.d/nginx reload"
      end
    end
    
    namespace :phpfpm do
      desc" Restarts PHP-FPM"
      task :restart do
        run "sudo /etc/init.d/php-fpm restart"
      end
    end
    
    namespace :git do
      desc "Updates git submodule tags"
      task :submodule_tags do
        run "if [ -d #{shared_path}/cached-copy/ ]; then cd #{shared_path}/cached-copy/ && git submodule foreach --recursive git fetch origin --tags; fi"
      end
    end
    
    namespace :memcached do
      desc "Restarts Memcached"
      task :restart do
        run "echo 'flush_all' | nc localhost 11211", :roles => [:memcached]
      end
    
      desc "Updates the pool of memcached servers"
      task :update do
        unless find_servers( :roles => :memcached ).empty?
          mc_servers = '<?php return array( "' + find_servers( :roles => :memcached ).join( ':11211", "' ) + ':11211" ); ?>'
          run "echo '#{mc_servers}' > #{current_path}/memcached.php", :roles => :memcached
        end
      end
    end
    
    namespace :db do
      desc "Syncs the staging database from production"
      task :sync, :roles => :web do
        case stage
        when :production
          puts "[Error] You must run db:sync from staging with cap staging db:sync or from local with cap local db:sync"
        when :staging
          puts "Hang on... this might take a while."
          random = rand( 10 ** 5 ).to_s.rjust( 5, '0' )
          p = wpdb[ :production ]
          s = wpdb[ :staging ]
          puts "db:sync"
          puts stage
          system "mysqldump -u #{p[:user]} --result-file=/tmp/wpstack-#{random}.sql -h #{p[:host]} -p#{p[:password]} #{p[:name]}"
          system "mysql -u #{s[:user]} -h #{s[:host]} -p#{s[:password]} #{s[:name]} < /tmp/wpstack-#{random}.sql && rm /tmp/wpstack-#{random}.sql"
          puts "Database synced to staging"
          # memcached.restart
          puts "Memcached flushed"
        when :dev
          puts "Hang on... this might take a while."
          random = rand( 10 ** 5 ).to_s.rjust( 5, '0' )
          p = wpdb[ :production ]
          d = wpdb[ :dev ]
          puts "db:sync"
          puts dev
          system "mysqldump -u #{p[:user]} --result-file=/tmp/wpstack-#{random}.sql -h #{p[:host]} -p#{p[:password]} #{p[:name]}"
          system "mysql -u #{d[:user]} -h #{d[:host]} -p#{d[:password]} #{d[:name]} < /tmp/wpstack-#{random}.sql && rm /tmp/wpstack-#{random}.sql"
          puts "Database synced to development repo"
          # memcached.restart
          puts "Memcached flushed"
        end
      end
    
      desc "Sets the database credentials (and other settings) in wp-config.php"
      task :make_config do
        set :staging_domain, '077397919.45' unless defined? staging_domain
        {:'%%WP_STAGING_DOMAIN%%' => staging_domain, :'%%WP_STAGE%%' => stage, :'%%DB_NAME%%' => wpdb[stage][:name], :'%%DB_USER%%' => wpdb[stage][:user], :'%%DB_PASSWORD%%' => wpdb[stage][:password], :'%%DB_HOST%%' => wpdb[stage][:host]}.each do |k,v|
          run "sed -i 's/#{k}/#{v}/' #{release_path}/wp-config.php", :roles => :web
        end
      end
    end