Friday, August 28, 2009

Automating CouchDB Design Document Deployment

‹prev | My Chain | next›

Today I would like to finish my automation of Vlad deployment. Yesterday, I eliminated a misunderstanding / misconfiguration between my monitoring tool (god) and my application servers (thin). That misunderstanding was causing all sorts of difficulties with my deployments. With it resolved, deploys now work just fine. Except...

I need a means to update my CouchDB views. The most recent code work that I did suppresses unpublished meals and recipes. That work was all done in CouchDB views, so deploying the new code is useless without loading the updated CouchDB design documents.

Loading CouchDB design documents is kinda, sorta like migrating a traditional relational DB, so I figure, why not hook into Vlad's already defined vlad:migrate task:
cstrom@jaynestown:~/repos/eee-code$ rake -T
(in /home/cstrom/repos/eee-code)
...
rake vlad:migrate # Run the migrate rake task for the the app.
...
I add a migrate task to my Rakefile:
desc "Migrate the DB by reloading all design documents"
task :migrate => "couchdb:load_design_docs"
I make it dependent on the couchdb:load_design_docs task, which uses my super awesome couch_docs gem to load .js on the filesystem into CouchDB as design documents.

That should about do it, so I stop my thin application servers:
cstrom@jaynestown:~/repos/eee-code$ rake vlad:stop_app
(in /home/cstrom/repos/eee-code)
deploy@beta.eeecooks.com's password:
Could not chdir to home directory /home/deploy: No such file or directory
Sending 'stop' command

The following watches were affected:
eee-thin-8000
eee-thin-8001
eee-thin-8002
eee-thin-8003
Then I update the code, run my migrations, and restart the application servers:
cstrom@jaynestown:~/repos/eee-code$ rake vlad:update vlad:migrate vlad:start_app
(in /home/cstrom/repos/eee-code)
deploy@beta.eeecooks.com's password:
Could not chdir to home directory /home/deploy: No such file or directory
Initialized empty Git repository in /var/www/eeecooks/scm/repo/.git/
Switched to a new branch 'deployed-HEAD'
/var/www/eeecooks/scm
/var/www/eeecooks/scm
deploy@beta.eeecooks.com's password:
Could not chdir to home directory /home/deploy: No such file or directory
deploy@beta.eeecooks.com's password:
Could not chdir to home directory /home/deploy: No such file or directory
deploy@beta.eeecooks.com's password:
Could not chdir to home directory /home/deploy: No such file or directory
deploy@beta.eeecooks.com's password:
Could not chdir to home directory /home/deploy: No such file or directory
20090825024912 20090828024244 20090829014910
deploy@beta.eeecooks.com's password:
Could not chdir to home directory /home/deploy: No such file or directory
sh: line 0: cd: /var/www/eeecooks/releases/to: No such file or directory
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
/usr/lib/ruby/1.8/rake.rb:2353:in `raw_load_rakefile'
(See full trace by running task with --trace)
rake aborted!
execution failed with status 1: ssh deploy@beta.eeecooks.com cd /var/www/eeecooks/releases/to; rake RAILS_ENV=production db:migrate

(See full trace by running task with --trace)
There are several things wrong here, first I should be using a private SSH key so that I do not have to re-enter my password quite so much. Second, I need to create a /home/deploy directory—those errors are not harmful, but clutter up the output.

Then there is the actual failure. I do not have a releases/to sub-directory (hence the error). I need to run my couch migrations in the current directory. I also note the db: namespace for the migrate task (the migrate task above is in the top-level namespace.

To fix the namespace issue, I update the Rakefile to read:
namespace :db do
desc "Migrate the DB by reloading all design documents"
task :migrate => "couchdb:load_design_docs"
end
To get the db:migrate task to execute in the right directory, I have to dig in to the Vlad code a bit. The vlad:migrate task in version 2.0.0 is as follows:
  remote_task :migrate, :roles => :app do
break unless target_host == Rake::RemoteTask.hosts_for(:app).first

directory = case migrate_target.to_sym
when :current then current_path

when :latest then current_release
else raise ArgumentError, "unknown migration target #{migrate_target.inspect}"
end

run "cd #{directory}; #{rake_cmd} RAILS_ENV=#{rails_env} db:migrate #{migrate_args}"
end
The releases/to sub-directory was being chosen because migrate_target defaults to "latest", setting the directory local variable to the value of the second when, current_release. I want the current_path, so I need to set the migrate_target value to current in my config/deploy.rb:
set :scm, "git"
set :application, "eeecooks"
set :repository, "git://github.com/eee-c/eee-code.git"
set :deploy_to, "/var/www/#{application}"
set :user, 'deploy'
set :domain, "#{user}@beta.eeecooks.com"
set :god_command, 'sudo /usr/bin/god'
set :god_group, 'thin'
set :migrate_target, :current
I had to check the code, because the documentation describes the migrate_target as "Set this if you need to specify a particular migration ‘VERSION’ number. Defaults to “latest”." Looks like I need to submit a documentation patch. I will do that tomorrow.

For now, I would like to see if these changes allow me to deploy. The current code displays three unpublished meals:



Everything "above the fold" is unpublished. The first published meal should be "Star Wars: The Dinner". Once I see that, I will know that I have successfully deployed the latest code and uploaded the latest CouchDB design documents to the CouchDB server.

Soo...
cstrom@jaynestown:~/repos/eee-code$ rake vlad:update vlad:migrate vlad:start_app
(in /home/cstrom/repos/eee-code)
Initialized empty Git repository in /var/www/eeecooks/scm/repo/.git/
Switched to a new branch 'deployed-HEAD'
/var/www/eeecooks/scm
/var/www/eeecooks/scm
(in /var/www/eeecooks/releases/20090829020848)
Sending 'restart' command

The following watches were affected:
eee-thin-8000
eee-thin-8001
eee-thin-8002
eee-thin-8003
And do I have a "Star Wars: The Dinner" meal? Yes I do:



That is a great place to stop for the day. Tomorrow, I will submit a patch to the Vlad project and start on the next feature that is needed so that I can move my Sinatra / CouchDB version of EEE Cooks out of beta.

No comments:

Post a Comment