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.

No comments:

Post a Comment