Skip to content

Instantly share code, notes, and snippets.

@techcrunch
Forked from bjeanes/README.markdown
Created October 18, 2008 02:31
Show Gist options
  • Save techcrunch/17585 to your computer and use it in GitHub Desktop.
Save techcrunch/17585 to your computer and use it in GitHub Desktop.

Media Temple Capistrano Deployment Recipe

With GitHub

See comments in the files for explanation. This works flawlessly for me on MediaTemple + Git(Hub).

NOTE: this assumes that the application name on the grid container and the application name in GitHub are the same.

To use:

On the Server

  1. SSH into your MediaTemple server.
  2. Put your public key into ~/.ssh/authorized_keys2 with mode 0600
  3. Run mtr generate_config and give your serveradmin username and password

Then, On Your Computer

  1. Have a file config/database.yml.mt with your Media Temple MySQL details (you can delete it once your site is running, and it should not be in your git repository)
  2. Modify the first 4 lines of the attached deploy.rb file
  3. On your local computer, commit and push your changes to GitHub
  4. Run cap deploy:setup deploy:cold
  5. Navigate to your domain, and you should see your app running.
#
# This deployment recipe assumes public keys are in use and that `mtr generate_config`
# has been run on the sever. This eliminates any need for passwords to be stored or typed
# in.
#
require 'mt-capistrano.rb' # use local copy instead of mt-capistrano gem
# Configure this...
set :site, "" # e.g. 12345
set :application, "" # e.g. myblog
set :webpath, "" # e.g. myblog.com
set :git_user, "" # bjeanes (github url taken from this + application -- change this below)
# Shouldn't have to change anything below (except repository details if different...)
set :domain, "s#{site}.gridserver.com"
set :user, "serveradmin%#{webpath}"
# Other options
ssh_options[:username] = user
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
set :use_sudo, false # MediaTemple don't allow sudo command
# Repo stuff
set :scm, :git
set :repository, "[email protected]:#{git_user}/#{application}.git"
set :deploy_to, "/home/#{site}/containers/rails/#{application}"
set :current_deploy_dir, "#{deploy_to}/current"
set :deploy_via, :remote_cache
set :tmp_dir, "#{deploy_to}/tmp"
set :branch, "master"
set :git_enable_submodules, 1
set :checkout, "export"
# Roles
role :web, domain
role :app, domain
role :db, domain, :primary => true
# Tasks
namespace :deploy do
desc "Upload the database.yml file to the shared folder."
task :upload_database_yml do
yml = 'config/database.yml.mt'
if File.exists?(yml)
put(File.read(yml), "#{shared_path}/database.yml", :mode => 0644)
else
puts "Create #{yml} then re-run `cap deploy:upload_database_yml` before proceeding..."
end
end
after "deploy:cold", "mt:create_link"
after "deploy:start", "mt:generate_htaccess"
after "deploy:restart", "mt:generate_htaccess"
before "deploy:symlink" do
run "rm -drf #{release_path}/public/uploads"
run "ln -s #{shared_path}/uploads #{release_path}/public/uploads"
run "rm -f #{release_path}/config/database.yml"
run "ln -s #{shared_path}/database.yml #{release_path}/config/database.yml"
end
after "deploy:setup" do
upload_database_yml
mt.add
end
end
# This file is part of the Media Temple capistrano gem
# (sudo gem install mt-capistrano --source=http://gems.mediatemple.net/)
#
# I have modified it because the way it sets up the rake command is flawed.
# Instead I just `source .bash_profile` which has these same details in it
# from the `mtr setup_rubygems` command
#
# Also, I've commented out the username and password flags because this requires
# saving the password in your deploy.rb. Instead, you can run `mtr generate_config`
# to have the mtr command save your username and password. If you prefer to have it saved
# in your deploy.rb, switch the commented commands below.
# -------------------------------------------------------------------------------------------------
# make sure that the site specific rake (and other libs) are found
Capistrano::Configuration.instance(:must_exist).load do
# took this out because it doesn't work
#set(:rake) { "PATH=$PATH:/home/#{site}/data/rubygems/bin:/home/#{site}/data/rubygems/gems/bin RUBYLIB=/home/#{site}/data/rubygems/local/lib/site_ruby/1.8 GEM_HOME=/home/#{site}/data/rubygems/gems rake" }
# this does!
set(:rake) { "source $HOME/.bash_profile && rake"}
# set up the mt namespace and tasks
namespace :mt do
desc "Register the application using 'mtr add'"
task :add do
# run "mtr -u #{user} -p #{password} add #{application} #{deploy_to}/current #{webpath}"
run "mtr add #{application} #{deploy_to}/current #{webpath}"
end
%w{remove start stop restart create_link generate_htaccess info}.each do |command|
desc "Runs 'mtr #{command} <appname>' on the server."
task command.to_sym do
# run "mtr -u #{user} -p #{password} #{command} #{application}"
run "mtr #{command} #{application}"
end
end
%w{list status}.each do |command|
desc "Runs 'mtr #{command}' on the server."
task command.to_sym do
# run "mtr -u #{user} -p #{password} #{command}"
run "mtr #{command}"
end
end
end
# override built-in capistrano application lifecycle tasks
namespace :deploy do
%w{start stop restart}.each do |command|
desc "#{command.capitalize} the application."
task command.to_sym do
mt.send(command)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment