Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
7 total  XML / RSS feed 

sftp capistrano deployment strategy

// capistrano deployment strategy used when you have sftp access only (no ssh access)

require 'capistrano/recipes/deploy/strategy/base'
require 'fileutils'
require 'tempfile'  # Dir.tmpdir

require 'net/ssh'
require 'net/sftp'
require 'find'

module Capistrano
  module Deploy
    module Strategy

                        # Special Strategy for a special price!
                        # This strategy is created in favor of the sftp only apps, in lack of ssh login support
                        # 
                        # * exports the repository to a local directory
                        # * uploads all the files to the remote server using an sftp script
                        # * renames current directory to a directory with a timestamp of 1 hour ago
                        # * renames the uploaded directory to current
      class SftpCopy < Base
        def deploy!
          logger.debug "getting (via #{copy_strategy}) revision #{revision} to #{destination}"
          system(command)
          File.open(File.join(destination, "REVISION"), "w") { |f| f.puts(revision) }

                                        logger.debug "Connecting to sftp user = #{configuration[:user]}"

                                        Net::SSH.start(configuration[:host], configuration[:user], configuration[:password]) do |ssh|
                                                ssh.sftp.connect do |sftp|
                                                        logger.debug "Creating directory: #{remote_dir}"
                                                        sftp.mkdir remote_dir, :permissions => 0755
                                                        
                                                        logger.debug "Uploading files from #{destination} to #{remote_dir}"
                                                        logger.debug "Why don't you grab a cup of coffee while you wait, i might be busy for some time...\nJust sit back and enjoy the show..."
                                                        Find.find(destination) do |file|
                                                                if File.stat(file).directory?
                                                                        
                                                                        remote_directory = remote_dir + file.sub(destination, '')
                                                                        begin
                                                                                sftp.stat(remote_directory)
                                                                        rescue Net::SFTP::Operations::StatusException => e
                                                                                raise "BOEM" unless e.code == 2
                                                                                sftp.mkdir(remote_directory, :permissions => 0755)
                                                                        end
                                                                else
                                                                        remote_file = remote_dir + file.sub(destination, '')
                                                                        sftp.put_file file, remote_file
                                                                        sftp.setstat(remote_file, :permissions => 0644)
                                                                end
                                                        end
                                                        
                                                        logger.debug "RENAMING DIRECTORIES"
                                                        sftp.rename "#{configuration[:copy_remote_dir]}/current", "#{configuration[:copy_remote_dir]}/#{(Time.now - 1.hour).utc.strftime("%Y%m%d%H%M%S")}"
                                                        sftp.rename "#{remote_dir}", "#{configuration[:copy_remote_dir]}/current"
                                                end
                                        end
        ensure
                                        logger.debug "Remove local export"
          FileUtils.rm_rf destination rescue nil
        end

        def check!
          super.check do |d|
          end
        end

        private

          # Returns the basename of the release_path, which will be used to
          # name the local copy and archive file.
          def destination
            @destination ||= File.join(tmpdir, File.basename(configuration[:release_path]))
          end

          # Returns the value of the :copy_strategy variable, defaulting to
          # :checkout if it has not been set.
          def copy_strategy
            @copy_strategy ||= configuration.fetch(:copy_strategy, :checkout)
          end

          # Should return the command(s) necessary to obtain the source code
          # locally.
          def command
            @command ||= case copy_strategy
            when :checkout
              source.checkout(revision, destination)
            when :export
              source.export(revision, destination)
            end
          end

          # Returns the name of the file that the source code will be
          # compressed to.
          def filename
            @filename ||= File.join(tmpdir, "#{File.basename(destination)}.#{compression_extension}")
          end

          # The directory to which the copy should be checked out
          def tmpdir
            @tmpdir ||= configuration[:copy_dir] || Dir.tmpdir
          end

          # The directory on the remote server to which the archive should be
          # copied
          def remote_dir
            @remote_dir ||= "#{configuration[:copy_remote_dir]}/#{File.basename(configuration[:release_path])}" || "/tmp"
          end
      end

    end
  end
end

Capistrano SFTP recipe

// webistrano recipe used to deploy with sftp access only

desc "Setup the rail environment"
namespace :deploy do
    task :setup do

    end

  task :default do
     update_code
     restart
  end

  task :update_code, :except => { :no_release => true } do
    on_rollback { run "rm -rf #{release_path}; true" }
    strategy.deploy!
  end

  task :symlink, :except => { :no_release => true } do

  end

  task :restart, :roles => :app do
     Net::SSH.start(host, user, password) do |ssh|
        ssh.sftp.connect do |sftp|
            sftp.remove("#{restart_file}")
        end
     end
  end
end


Capistrano task to load production data

# load production data, still needs some polish
# based on code from http://push.cx/2007/capistrano-task-to-load-production-data
# cap 2.0 compatible
# exec is being weird, had to end w/ a syscall :\ any ideas?

desc "Load production data into development database"
task :load_production_data, :roles => :db, :only => { :primary => true } do
  require 'yaml'
  ['config/database.yml'].each do |file|

    database = YAML::load_file(file)

    filename = "dump.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql.gz"
    # on_rollback { delete "/tmp/#{filename}" }

    # run "mysqldump -u #{database['production']['username']} --password=#{database['production']['password']} #{database['production']['database']} > /tmp/#{filename}" do |channel, stream, data|
    run "mysqldump -h #{database['production']['host']} -u #{database['production']['username']} --password=#{database['production']['password']} #{database['production']['database']} | gzip > /tmp/#{filename}" do |channel, stream, data|
      puts data
    end
    get "/tmp/#{filename}", filename
    # exec "/tmp/#{filename}"
    password = database['development']['password'].nil? ? '' : "--password=#{database['development']['password']}"  # FIXME pass shows up in process list, do not use in shared hosting!!! Use a .my.cnf instead
    # FIXME exec and run w/ localhost as host not working :\
    # exec "mysql -u #{database['development']['username']} #{password} #{database['development']['database']} < #{filename}; rm -f #{filename}"
    `gunzip -c #{filename} | mysql -u #{database['development']['username']} #{password} #{database['development']['database']} && rm -f gunzip #{filename}`
  end
  
end

Variables for capistrano

Cap uses lots of symbols for pointing to variables.

When adjusting the value of a cap variable that is located in the standard.rb task library, you must use :set to adjust its value within your own recipe file (such as deploy.rb).

Example: :migrate_env is declared in Cap's standard.rb. To adjust its value, do not use a local variable named migrate_env to do so, it won't apply to the standard.rb library tasks.

set :migrate_geoip, "load_geoipcountry=false"
set :migrate_env, "#{migrate_geoip}"

Capistrano namespaces

when namespace is specified for a task, all subsequent tasks are assumed to be in the same namespace, reverting to non-namespaced task if not found

desc "OVERRIDE of deploy:cold"
deploy.task :cold, :roles => :app do
  puts "Overridden!"
  puts "Deploy method: #{deploy_via.to_s}"
  puts "Copy Strat: #{copy_strategy.to_s}"
  update
  migrate
  refresh_db
  mrs
end

Solaris Capistrano deploy.rb

// capiwhatisit recipe for solaris
// most important thing is overriding some tasks to use GNU ln, since solaris' ln are subtly different

set :application, "awesomeapp"
set :repository, "http://example.com/repos/#{application}/trunk"

role :web, "foo.com"
role :app, "bar.com"
role :db,  "baz.com", :primary => true

set :deploy_to, "/path/to/app"
set :user, "appuser"
set :svn, "/opt/csw/bin/svn"
set :rake, "/opt/csw/bin/rake"
set :mongrel_config, "#{deploy_to}/shared/config/mongrel_cluster.yml"

set :use_sudo, false

desc "The spinner task is used by :cold_deploy to start the application up"
task :spinner, :roles => [:web, :app, :node] do
  run "/opt/csw/bin/mongrel_rails cluster::start -C #{mongrel_config}" # or SMF
end

desc "Restart the Mongrel processes on the app server."
task :restart, :roles => [:web, :app, :node] do
  run "/opt/csw/bin/mongrel_rails cluster::restart -C #{mongrel_config}" # or SMF
end

desc "Symlinks the database.yml into the current release"
task :after_update_code, :roles => [:web, :app, :node] do
    run "/opt/csw/bin/gln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end

# Run rake migrate after the symlinking has been done, just because I'm a lazy bum
task :after_symlink, :roles => :db do
  backup
  migrate
end

# =====================================================================================
# = Overriding these tasks because we need to use GNU ln and need the full path to it =

desc <<-DESC
Update the 'current' symlink to point to the latest version of
the application's code. Using GNU ln
DESC
task :symlink, :roles => [:app, :db, :web, :node] do
  on_rollback { run "/opt/csw/bin/gln -nfs #{previous_release} #{current_path}" }
  run "/opt/csw/bin/gln -nfs #{current_release} #{current_path}"
end

desc <<-DESC
Rollback the latest checked-out version to the previous one by fixing the
symlinks and deleting the current release from all servers.
DESC
task :rollback_code, :roles => [:app, :db, :web] do
  if releases.length < 2
    raise "could not rollback the code because there is no prior release"
  else
    run <<-CMD
      /opt/csw/bin/gln -nfs #{previous_release} #{current_path} &&
      rm -rf #{current_release}
    CMD
  end
end



Capistrano for TXD Containers

A mostly-standardized Capistrano deploy.rb file for the TXD containers. Adjust :application, :smf, :repository, :deploy_to as required; adjust the role definitions too if you want to use different hosts.

Copy your database.yml file to /home/example/rails/example.org/shared/config/database.yml and it'll get symlinked into place before the app is restarted.

This works with my SMF manifest here

set :application, "example.org"
set :smf, "mongrel/example"
set :repository, "https://example.textdriven.com/svn/#{application}"
set :deploy_to, "/home/example/rails/#{application}"
role :web, "#{application}", :primary => true
role :app, "#{application}", :primary => true
role :db, "#{application}", :primary => true

set :checkout, "export"
set :svn, "/opt/csw/bin/svn"
set :sudo, "/opt/csw/bin/sudo"
set :rake, "/opt/csw/bin/rake"

task :after_update_code do
  run "ln -s #{deploy_to}/#{shared_dir}/config/database.yml #{current_release}/config/database.yml"
  run "rm -f #{current_path}"
end

task :spinner, :roles => :app do
  send(run_method, "/usr/sbin/svcadm start #{smf}")
end

task :restart, :roles => :app do
  send(run_method, "/usr/sbin/svcadm restart #{smf}")
end
« Newer Snippets
Older Snippets »
7 total  XML / RSS feed