Showing posts with label vlad. Show all posts
Showing posts with label vlad. Show all posts

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.

Thursday, August 27, 2009

Grotesquely Thin

‹prev | My Chain | next›

I was able to create a deploy user with limited privileges yesterday, but things quickly deteriorated after that. I was foiled in my attempts at enabling the deploy user to restart my thin cluster. As I quit in frustration, I was reasonably sure that the problem lay in the god configuration that I was using. Frustration is never a good guide, last night being yet another example of this.

The true source of the problem lay in the way that I was accessing individual servers in the thin cluster. The thin help text on the subject reads:
sh-3.2$ thin --help
Usage: thin [options] start|stop|restart|config|install
...
Cluster options:
-s, --servers NUM Number of servers to start
-o, --only NUM Send command to only one server of the cluster
-C, --config FILE Load options from config file
--all [DIR] Send command to each config files in DIR
...
The specific option that I got wrong was the the --only, which I assumed would be indexed from 1. Assumed, not verified, and I got burned (although honestly, who wants to start server number zero?). The relevant block of the god configuration reads:
DAEMON = ' '
CONFIG_PATH = '/etc/thin'
APP_ROOT = '/var/www/eee-code'

%w{8000 8001 8002 8003}.each do |port|
server_number = port.to_i - 7999
God.watch do |w|
w.name = "eee-thin-#{port}"
w.start = "#{DAEMON} start --all #{CONFIG_PATH} --only #{server_number}"
w.stop = "#{DAEMON} stop --all #{CONFIG_PATH} --only #{server_number}"
w.restart = "#{DAEMON} restart --all #{CONFIG_PATH} --only #{server_number}"
w.interval = 30.seconds # default
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
w.pid_file = File.join(APP_ROOT, "log/thin.#{port}.pid")

w.behavior(:clean_pid_file)
end
end
The server_number is calculated to be 1 (8000 - 7999) through 4, so starting eee-thin-8000 will start server #1 which actually runs on port 8001. As one might expect all manner of bad things happen when the monitoring software and the PIDs recorded on the filesystem disagree.

The real question is how does the server on port 8000 get started (because there is a server listening on port 8000). To answer that, I start the thin server manually:
sh-3.2$ sudo /var/lib/gems/1.8/bin/thin start --all /etc/thin --only 1
Starting server on 0.0.0.0:3001 ...
[start] /etc/thin/eee.yml ...
Starting server on 127.0.0.1:8000 ...
Starting server on 127.0.0.1:8001 ...
Starting server on 127.0.0.1:8002 ...
Starting server on 127.0.0.1:8003 ...
Wow. There is all sorts of wrong there. My /etc/thin/eee.yml (the only file in /etc/thin) has no mention of port 3001:
--- 
user: www-data
group: www-data
pid: log/thin.pid
log: log/thin.log
timeout: 60
address: 127.0.0.1
port: 8000
servers: 4
chdir: /var/www/eee-code
environment: production
daemonize: true
rackup: config.ru
Really, I have no explanation for that other than I am most likely abusing the --all, which is supposed to "Send command to each config files in DIR". Why this treats the only config file in the DIR differently that explicitly pointing to that config file, I cannot say. But when I use the config directly, it starts up correctly:
sh-3.2$ sudo /var/lib/gems/1.8/bin/thin start --config /etc/thin/eee.yml --only 1
Starting server on 127.0.0.1:8001 ...
It works, but I am definitely starting the second server, not the first one which would have started on port 8000. That is, the --only options is indexed from zero, not one.

Ultimately, I take my lessons learned and use this updated god configuration:
%w{8000 8001 8002 8003}.each do |port|
God.watch do |w|
server_number = port.to_i - 8000
pid_file = File.join(APP_ROOT, "log/thin.#{port}.pid")
w.name = "eee-thin-#{port}"
w.group = "thin"
w.interval = 30.seconds # default
w.start = "#{DAEMON} start --config #{CONFIG_PATH}/eee.yml --only #{server_number}"
w.stop = "#{DAEMON} stop --config #{CONFIG_PATH}/eee.yml --only #{server_number}"
w.restart = "#{DAEMON} restart --config #{CONFIG_PATH}/eee.yml --only #{server_number}"
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
w.pid_file = pid_file

w.behavior(:clean_pid_file)
end
end
After starting /etc/init.d/god, thin is now running:
sh-3.2$ sudo god status
couchdb: up
thin:
eee-thin-8000: up
eee-thin-8001: up
eee-thin-8002: up
eee-thin-8003: up
More importantly, I can now issue the stop/start commands with my Vlad / god rake tasks:
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
Which means...

I can finally deploy with Vlad. Except for one thing. My CouchDB "migrations" (loading updated views) are not hooked into Vlad yet. I will finish that off tomorrow and then move onto other features in need of deploying.

Tuesday, August 25, 2009

Thin Vlad

‹prev | My Chain | next›

I got Vlad and git mostly working last night. I still need to get Vlad working with thin. The thin gem includes a vlad.rb example that suggests installing the example in $GEM_HOME/gems/vlad-1.2.0/lib/vlad/thin.rb. I find the idea of modifying installed gems offensive, so I create a lib/vlad/thin.rb file inside of my Sinatra application instead with the contents of the thin example:
# Copied from thin/example/vlad.rake
require 'vlad'

namespace :vlad do
##
# Thin app server

set :thin_address, "127.0.0.1"
set :thin_command, 'thin'
set(:thin_conf) { "#{shared_path}/thin_cluster.conf" }
set :thin_environment, "production"
set :thin_group, nil
set :thin_log_file, nil
set :thin_pid_file, nil
set :thin_port, nil
set :thin_socket, nil
set :thin_prefix, nil
set :thin_servers, 2
set :thin_user, nil

desc "Prepares application servers for deployment. thin configuration is set via the thin_* variables.".cleanup

remote_task :setup_app, :roles => :app do

raise(ArgumentError, "Please provide either thin_socket or thin_port") if thin_port.nil? && thin_socket.nil?

cmd = [
"#{thin_command} config",
"-s #{thin_servers}",
("-S #{thin_socket}" if thin_socket),
"-e #{thin_environment}",
"-a #{thin_address}",
"-c #{current_path}",
"-C #{thin_conf}",
("-P #{thin_pid_file}" if thin_pid_file),
("-l #{thin_log_file}" if thin_log_file),
("--user #{thin_user}" if thin_user),
("--group #{thin_group}" if thin_group),
("--prefix #{thin_prefix}" if thin_prefix),
("-p #{thin_port}" if thin_port),
].compact.join ' '

run cmd
end

def thin(cmd) # :nodoc:
"#{thin_command} #{cmd} -C #{thin_conf}"
end

desc "Restart the app servers"

remote_task :start_app, :roles => :app do
run thin("restart -s #{thin_servers}")
end

desc "Stop the app servers"

remote_task :stop_app, :roles => :app do
run thin("stop -s #{thin_servers}")
end
end
I then modify my Rakefile so that the $LOAD_PATH includes the lib/vlad path when doing vlad-related things:
begin
require "vlad"
$: << "#{File.dirname(__FILE__)}/lib"
Vlad.load(:app => :thin, :scm => :git)
rescue LoadError
# do nothing
end
The Vlad load method does a require of the value of the :app attribute in the Vlad namespace ("vlad/thin" in the case of the above). Since the Sinatra application's vlad sub-directory is in the $LOAD_PATH, Vlad.load is now able to find it.

To configure this so that I can restart the running thin servers, I add the following :thin_* configuration parameters to my vlad config/deploy.rb file:
set :scm, "git"
set :application, "eeecooks"
set :repository, "git://github.com/eee-c/eee-code.git"
set :deploy_to, "/var/www/#{application}"
set :domain, "beta.eeecooks.com"
set :thin_command, 'sudo /var/lib/gems/1.8/bin/thin'
set :thin_conf, '/etc/thin/eee.yml'
set :thin_servers, 4
set :thin_port, 8000
The most important value in that configuration is the thin_command, which includes a sudo to ensure appropriate permissions on the running thin processes.

To verify that this works, I stop the servers via the appropriate Vlad rake task:
strom@jaynestown:~/repos/eee-code$ rake vlad:stop_app             # Stop the app servers
(in /home/cstrom/repos/eee-code)
cstrom@beta.eeecooks.com's password:
Stopping server on 127.0.0.1:8000 ...
Sending QUIT signal to process 3169 ...
>> Exiting!
Stopping server on 127.0.0.1:8001 ...
Sending QUIT signal to process 4157 ...
>> Exiting!
Stopping server on 127.0.0.1:8002 ...
Sending QUIT signal to process 4418 ...
Stopping server on 127.0.0.1:8003 ...
Sending QUIT signal to process 4129 ...
>> Exiting!
God restarts the servers for me, so that is a successful test and everything is still running.

I am not quite done with my Vlad work. Tomorrow, I need to ensure that the thin configuration is using the newly deployed-with-vlad code (they are currently two different directories). I also need to create a deploy user with limited priveleges (my user account that I am using has full sudo priveleges). There are likely a few other tweaks needing to be made as well, but I hope to be done with my Vlad setup tomorrow and have the latest updates running on the beta site.