Skip to content

Instantly share code, notes, and snippets.

@drinks
Created October 11, 2012 21:33
Show Gist options
  • Select an option

  • Save drinks/3875611 to your computer and use it in GitHub Desktop.

Select an option

Save drinks/3875611 to your computer and use it in GitHub Desktop.

Revisions

  1. drinks created this gist Oct 11, 2012.
    127 changes: 127 additions & 0 deletions deploy.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    default_run_options[:pty] = true
    set :application, "<your-application-folder-name>"
    set :domain, "<your-ip-addr>"
    set :user, "<your-deploy-user>"
    set :group, "<your-deploy-group>"
    set :runner, "<your-deploy-user>"
    set :use_sudo, true
    set :scm, :git
    set :deploy_via, :remote_cache
    set :repository, "[email protected]:your-account/your-repo.git"
    set :deploy_to, "/var/www/#{application}"
    set :shared_children, %w(.htaccess uploads sitemap cache w3tc)

    after :deploy, "deploy:cleanup"

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

    namespace :deploy do
    [:start, :stop, :restart].each do |t|
    desc "Restart nginx"
    task t, :roles => :app do
    ['nginx', 'php5-fpm'].each {|svc| sudo "service #{svc} #{t.to_s}" unless t == :finalize_update }
    end
    end

    desc "Setup deploy"
    task :setup, :roles => :app do
    run "mkdir -p #{deploy_to} #{deploy_to}/releases #{deploy_to}/shared && chmod g+w #{deploy_to} #{deploy_to}/releases #{deploy_to}/shared"
    run "mkdir -p #{deploy_to}/shared/uploads #{deploy_to}/shared/sitemap #{deploy_to}/shared/cache"
    run "touch #{deploy_to}/shared/htaccess.txt #{deploy_to}/shared/advanced-cache.php"
    sudo "chown -R #{user}:#{group} #{deploy_to}/shared"
    end

    desc "Symlink everythang"
    task :finalize_update, :except => { :no_release => true } do
    # set permissions on basic folders
    sudo "chown -R #{user}:#{group} #{latest_release}"
    sudo "chmod -R g-w #{latest_release}"
    sudo "chown -R #{user}:#{group} #{shared_path}"
    sudo "chmod -R g-w #{shared_path}"

    # cache folder for wp-super-cache
    sudo "chown -R www-data:www-data #{shared_path}/cache"
    sudo "chmod -R g+w #{shared_path}/cache"
    run "ln -nfs #{shared_path}/cache #{release_path}/public/wp-content/cache"

    # uploads dir
    sudo "chown -R www-data:www-data #{shared_path}/uploads"
    sudo "chmod -R g+w #{shared_path}/uploads"
    run "ln -nfs #{shared_path}/uploads #{release_path}/public/wp-content/uploads"

    # xml sitemap
    sudo "chown -R www-data:www-data #{shared_path}/sitemap"
    sudo "chmod -R g+w #{shared_path}/sitemap"
    run "ln -nfs #{shared_path}/sitemap/sitemap.xml #{release_path}/public/sitemap.xml"
    run "ln -nfs #{shared_path}/sitemap/sitemap.xml.gz #{release_path}/public/sitemap.xml.gz"

    # advanced cache drop-in
    sudo "chown -R www-data:www-data #{shared_path}/w3tc/advanced-cache.php"
    sudo "chmod -R g+w #{shared_path}/w3tc/advanced-cache.php"
    run "ln -nfs #{shared_path}/w3tc/advanced-cache.php #{release_path}/public/wp-content/advanced-cache.php"

    # the rest of the w3tc files in wp-content
    sudo "chown -R www-data:www-data #{shared_path}/w3tc/w3tc"
    sudo "chown -R www-data:www-data #{shared_path}/w3tc/w3-total-cache-config.php"
    sudo "chown -R www-data:www-data #{shared_path}/w3tc/w3-total-cache-config-preview.php"
    sudo "chmod -R g+w #{shared_path}/w3tc/w3tc"
    run "ln -nfs #{shared_path}/w3tc/db.php #{release_path}/public/wp-content/db.php"
    run "ln -nfs #{shared_path}/w3tc/object-cache.php #{release_path}/public/wp-content/object-cache.php"
    run "ln -nfs #{shared_path}/w3tc/w3-total-cache-config.php #{release_path}/public/wp-content/w3-total-cache-config.php"
    run "ln -nfs #{shared_path}/w3tc/w3-total-cache-config-preview.php #{release_path}/public/wp-content/w3-total-cache-config-preview.php"
    run "ln -nfs #{shared_path}/w3tc/w3tc #{release_path}/public/wp-content/w3tc"

    # only if using apache
    # sudo "chown -R www-data:www-data #{shared_path}/htaccess.txt"
    # run "ln -nfs #{shared_path}/htaccess.txt #{release_path}/public/.htaccess"

    # only if using super cache
    # sudo "chown -R www-data:www-data #{shared_path}/wp-cache-config.php"
    # sudo "chmod -R g+w #{shared_path}/wp-cache-config.php"
    # run "ln -nfs #{shared_path}/wp-cache-config.php #{release_path}/public/wp-content/wp-cache-config.php"
    end
    end

    require 'erb'
    require 'open-uri'
    before "deploy:setup", :db
    after "deploy:update_code", "db:symlink"

    set :db_name, '<your-db-name>'
    set :db_user, '<your-db-user>'
    set :db_pass, '<your-db-pw>'
    set :db_host, 'localhost'
    set :db_prfx, 'wp_'

    set :secret_keys, open('https://api.wordpress.org/secret-key/1.1/salt/').read

    namespace :db do
    desc "Create wp-config.php in shared path"
    task :default do
    db_config = ERB.new <<-EOF
    <?php
    define('DB_NAME', '#{db_name}');
    define('DB_USER', '#{db_user}');
    define('DB_PASSWORD', '#{db_pass}');
    define('DB_HOST', '#{db_host}');
    define('DB_CHARSET', 'utf8');
    define('DB_COLLATE', '');
    #{secret_keys}
    $table_prefix = '#{db_prfx}';
    define ('WPLANG', '');
    if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');
    require_once(ABSPATH . 'wp-settings.php');
    EOF

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

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