Developer Tips: Cold Deployment

Posted by Jim Gay on Wednesday, March 18, 2009 | |

Have you ever needed to bootstrap an installation of Radiant on your production server with Capistrano, Vlad, or some other method?

You may have thought that you couldn’t because the rake db:bootstrap task requires some user input (such as username and password for the administrator). If you have, you’ve been wrong.

Take a look at the code:


namespace :db do
  desc "Migrate schema to version 0 and back up again. WARNING: Destroys all data in tables!!"
  task :remigrate => :environment do
    require 'highline/import'
    if ENV['OVERWRITE'].to_s.downcase == 'true' or agree("This task will destroy any data in the database. Are you sure you want to \ncontinue? [yn] ")
      
      # Migrate downward
      ActiveRecord::Migrator.migrate("#{RADIANT_ROOT}/db/migrate/", 0)
    
      # Migrate upward 
      Rake::Task["db:migrate"].invoke
      
      # Dump the schema
      Rake::Task["db:schema:dump"].invoke
    else
      say "Task cancelled."
      exit
    end
  end
  
  desc "Bootstrap your database for Radiant."
  task :bootstrap => :remigrate do
    require 'radiant/setup'
    Radiant::Setup.bootstrap(
      :admin_name => ENV['ADMIN_NAME'],
      :admin_username => ENV['ADMIN_USERNAME'],
      :admin_password => ENV['ADMIN_PASSWORD'],
      :database_template => ENV['DATABASE_TEMPLATE']
    )
  end
end

If any of those ENV stand out at you, you might realize that you can easily do something like:

rake db:bootstrap ADMIN_NAME='Admin' ADMIN_USERNAME='admin' ...etc...

Personally, however, if I do an automated bootstrap I then remove the task from my deployment recipe so that it can’t happen again and wipe out all my work.