Permalink
Browse files

Merge branch 'master' of git@git.topfunky.com:/var/repos/peepserver

  • Loading branch information...
2 parents e2dd719 + 757acda commit add54f1a2da634a57d9e9c5908dd9506beea6bd4 @topfunky topfunky committed Nov 5, 2008
View
@@ -1,3 +1,7 @@
+== 2008-11-02
+
+* Added peepcode:install:runit task.
+
== 2007-11-18
* First release.
View
@@ -1,10 +1,19 @@
-PeepServer
+PeepServer plugin for Capistrano.
+
by Geoffrey Grosenbach
- http://topfunky.com
+ Screencast: http://peepcode.com/products/capistrano-2
== DESCRIPTION
-Capistrano tasks for setting up and maintaining a CentOS server.
+Provides Capistrano deployment tasks for setting up and maintaining a CentOS server such those installed at RailsMachine (http://railsmachine.com).
+
+Some are highly customized for my current server (nginx, runit, thin, ruby-enterprise). You should customize it for your own situation.
Use at your own risk.
+== USAGE
+
+If you use the async-observer plugin and use this plugin to register a runit service for it, you'll also need to register the restart task in your own Capistrano deployment recipe file.
+
+ after "deploy:restart", "peepcode:runit:restart_async_observer"
+
View
@@ -31,6 +31,13 @@ def run_and_return(cmd)
end
return output.to_s
end
+
+ ##
+ # Return the path to a file inside the Peepserver templates directory.
+
+ def local_template_file_path(sub_path)
+ File.dirname(__FILE__) + "/templates/#{sub_path}"
+ end
end
@@ -100,6 +107,8 @@ def run_and_return(cmd)
git
nginx
+ runit
+ beanstalkd
memcached
munin
httperf
@@ -183,9 +192,7 @@ def run_and_return(cmd)
desc "Install nginx"
task :nginx do
-
- result = File.read(File.dirname(__FILE__) + "/templates/install-nginx.sh")
- put result, "src/install-nginx.sh"
+ upload local_template_file_path("install-nginx.sh"), "src/install-nginx.sh", :mode => 0755
cmd = [
"cd src",
@@ -195,15 +202,27 @@ def run_and_return(cmd)
run cmd
end
+ desc "Install runit task manager"
+ task :runit do
+ %w(install-runit.sh install-runit-for-user.sh).each do |filename|
+ upload local_template_file_path(filename), "src/#{filename}", :mode => 0755
+ end
+
+ sudo "src/install-runit.sh"
+ # netcat is used by some scripts
+ sudo "yum install nc -y"
+
+ run "src/install-runit-for-user.sh"
+ end
+
desc "Install memcached"
task :memcached do
# TODO Needs to run ldconfig after libevent is installed
run "echo '/usr/local/lib' > ~/src/memcached-i386.conf"
sudo "mv ~/src/memcached-i386.conf /etc/ld.so.conf.d/memcached-i386.conf"
sudo "/sbin/ldconfig"
- result = File.read(File.dirname(__FILE__) + "/templates/install-memcached-linux.sh")
- put result, "src/install-memcached-linux.sh"
+ upload local_template_file_path("install-memcached-linux.sh"), "src/install-memcached-linux.sh", :mode => 0755
cmd = [
"cd src",
@@ -254,8 +273,7 @@ def run_and_return(cmd)
# Reset
sudo "rm -f /etc/munin/plugins/*"
- # Upload
- put File.read(File.dirname(__FILE__) + "/templates/memcached_"), "/tmp/memcached_"
+ upload local_template_file_path("memcached_"), "/tmp/memcached_"
sudo "cp /tmp/memcached_ /usr/share/munin/plugins/memcached_"
sudo "chmod 755 /usr/share/munin/plugins/memcached_"
@@ -342,16 +360,18 @@ def run_and_return(cmd)
desc "Install beanstalk in-memory queue"
- task :beanstalk do
+ task :beanstalkd do
# TODO Bail unless make 3.81 is installed
cmd = [
"cd src",
- "git clone http://xph.us/src/beanstalkd.git",
- "cd beanstalkd",
+ "wget http://xph.us/software/beanstalkd/rel/beanstalkd-1.0.tar.gz",
+ "tar xfz beanstalkd-1.0.tar.gz",
+ "cd beanstalkd-1.0",
"/usr/local/bin/make",
"sudo cp beanstalkd /usr/local/bin/"
].join(" && ")
run cmd
+ # TODO Activate with runit
end
desc "Upgrade to Ruby 1.8.6 and newest RubyGems"
@@ -368,3 +388,5 @@ def run_and_return(cmd)
end
end
+
+# TODO sudo gem list | ruby -n -e 'puts $1 if ($_ =~ /(\S+) \(/)' | xargs sudo /opt/ruby-enterprise/bin/gem install
@@ -0,0 +1,77 @@
+
+class Capistrano::Configuration
+
+ ##
+ # Path to the directory with the rails-runit/service directory.
+
+ def shared_runit_service_path
+ File.join(shared_path, "rails-runit", "service")
+ end
+
+ def install_runit_service(service_name)
+ single_service_path = File.join(shared_runit_service_path, service_name)
+ run "mkdir -p #{single_service_path}"
+ upload local_template_file_path("runit/#{service_name}"), "#{single_service_path}/run", :mode => 0755
+ run "ln -s #{single_service_path} ~/service/"
+ end
+
+end
+
+namespace :peepcode do
+
+ namespace :runit do
+
+ desc "Install runit tasks for the current Rails app, using thin and Ruby enterprise.\nDo this before other peepcode:runit tasks."
+ task :rails do
+ cmd = [
+ "cd #{shared_path}",
+ "git clone git://github.com/purcell/rails-runit.git",
+ "cd rails-runit",
+ "ln -s /opt/ruby-enterprise/bin/thin thin",
+ "ln -s #{current_path} app"
+ ].join(" && ")
+ run cmd
+
+ # Add runit task for all thin ports
+ (mongrel_port...(mongrel_port + mongrel_servers)).each do |port_number|
+ run "cd #{shared_path}/rails-runit && ./add-thin #{port_number}"
+ run "ln -s #{shared_runit_service_path}/thin-#{port_number} ~/service"
+ end
+ end
+
+
+ desc "Install runit task for beanstalk"
+ task :beanstalkd do
+ install_runit_service("beanstalkd")
+ end
+
+ desc "Install beanstalk worker for async-observer plugin"
+ task :async_observer do
+ async_observer_service_path = File.join(shared_runit_service_path, "#{application}-async_observer")
+ run "mkdir -p #{async_observer_service_path}"
+
+ result = render_erb_template(File.dirname(__FILE__) + "/templates/runit/async_observer_worker.erb")
+ put result, "#{async_observer_service_path}/run", :mode => 0755
+ run "ln -s #{async_observer_service_path} ~/service/"
+
+ inform "Add this callback to your deploy.rb:\n\n\tafter 'deploy:restart', 'peepcode:runit:restart_async_observer'"
+ end
+
+ desc "Restart async-observer worker for this application"
+ task :restart_async_observer do
+ run "sv restart ~/service/#{application}-async_observer"
+ end
+
+ desc "Install runit task for memcache"
+ task :memcached do
+ install_runit_service("memcached")
+ end
+
+ desc "Dev task"
+ task :reset do
+ run "rm -rf #{shared_path}/rails-runit"
+ end
+
+ end
+
+end
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+##
+# Install the runit process launcher for a specific user.
+#
+# Should be run that user. Will also call sudo for some commands.
+#
+# Built for CentOS as found at RailsMachine hosting. http://railsmachine.com
+#
+# Author: Geoffrey Grosenbach http://peepcode.com
+# See also: http://www.sanityinc.com/articles/init-scripts-considered-harmful
+# And also: http://smarden.sunsite.dk/runit/install.html
+
+# Make global service directory for this user
+sudo mkdir -p /etc/sv/$USER
+
+# Make user-specific service directory
+mkdir ~/service
+
+# Copy run script for this user
+cat >/tmp/run <<EOT
+#!/bin/sh
+exec 2>&1
+exec chpst -u $USER /usr/local/bin/runsvdir $HOME/service 'log: ...........................................................................................................................................................................................................................................................................................................................................................................................................'
+EOT
+sudo mv /tmp/run /etc/sv/$USER/run
+sudo chmod +x /etc/sv/$USER/run
+
+# Activate service
+sudo ln -s /etc/sv/$USER /service/
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+##
+# Install the runit process launcher.
+#
+# Should be run as the root user.
+#
+# Built for CentOS as found at RailsMachine hosting. http://railsmachine.com
+#
+# Author: Geoffrey Grosenbach http://peepcode.com
+# Original: http://smarden.sunsite.dk/runit/install.html
+# See also: http://www.sanityinc.com/articles/init-scripts-considered-harmful
+
+mkdir /package
+chmod 1755 /package
+cd /package
+wget http://smarden.sunsite.dk/runit/runit-2.0.0.tar.gz
+tar xfz runit-2.0.0.tar.gz
+rm runit-2.0.0.tar.gz
+cd admin/runit-2.0.0
+./package/install
+
+# Install alongside traditional sysvinit and inittab
+install -m0750 /package/admin/runit/etc/2 /sbin/runsvdir-start
+mkdir -p /service
+mkdir -p /etc/sv
+
+cat >>/etc/inittab <<EOT
+SV:123456:respawn:/sbin/runsvdir-start
+EOT
+
+/sbin/init q
@@ -0,0 +1,11 @@
+# TODO Make erb and use app variables
+/var/log/nginx*.log /var/www/apps/nubyonrails.com/shared/log/nginx.*.log {
+ daily
+ rotate 7
+ compress
+ missingok
+ sharedscripts
+ postrotate
+ /etc/init.d/nginx relog
+ endscript
+}
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+exec 2>&1
+exec /opt/ruby-enterprise/bin/ruby <%= current_path %>/vendor/plugins/async-observer/bin/worker
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+exec 2>&1
+exec /usr/local/bin/beanstalkd -l 127.0.0.1 -p 11300
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+exec 2>&1
+exec /usr/local/bin/memcached -m 16 -l 127.0.0.1 -p 11211

0 comments on commit add54f1

Please sign in to comment.