Skip to content

Instantly share code, notes, and snippets.

@jpetazzo
Created February 10, 2012 11:00
Show Gist options
  • Save jpetazzo/1788779 to your computer and use it in GitHub Desktop.
Save jpetazzo/1788779 to your computer and use it in GitHub Desktop.
all-in-one ruby + memcached + jetty
Use this like the "ruby" service: it will install dependencies from Gemfile, and look for a Rails/Rack environment to serve it with passenger.
However, it will also run:
- a local memcached, on port 11211
- a local jetty configured to deploy WAR files, with servlets & JSP support, on port 8080
Place WAR files in the webapps directory as in this example.
If you need to add worker processes, add them to dotcloud.yml, in the processes section.
#!/usr/bin/env bash
shopt -s extglob
name=ruby
passenger_install_dir="$HOME/passenger"
nginx_install_dir="$HOME/nginx"
msg() {
echo -e "\033[1;32m-->\033[0m $0:" $*
}
die() {
msg $*
exit 1
}
move_to_approot() {
[ -n "$SERVICE_APPROOT" ] && cd $SERVICE_APPROOT
}
upgrade_rvm() {
msg "upgrading rvm"
rvm get latest
rvm reload
. "/usr/local/rvm/scripts/rvm"
}
install_ruby() {
local ruby_version=$SERVICE_CONFIG_RUBY_VERSION
if [ -n $ruby_version ] ; then
msg "installing ruby $ruby_version"
rvm install $ruby_version
rvm use $ruby_version --default
fi
}
install_nginx_passenger() {
local passenger_url="http://rubyforge.org/frs/download.php/75548/passenger-3.0.11.tar.gz"
msg "Passsenger install directory: $passenger_install_dir"
# install nginx/passenger requirements
if [ ! -d $passenger_install_dir ] ; then
msg "fetch and installing Nginx/Passenger"
mkdir -p $passenger_install_dir
wget -O - $passenger_url | tar -C $passenger_install_dir --strip-components=1 -zxf -
[ $? -eq 0 ] || die "can't fetch Passenger"
msg "installing rack"
gem install --no-ri --no-rdoc rack
else
msg "Passenger already installed"
fi
msg "Nginx install directory: $nginx_install_dir"
# install nginx
if [ ! -d $nginx_install_dir ] ; then
mkdir -p $nginx_install_dir
export CFLAGS="-O3 -pipe"
$passenger_install_dir/bin/passenger-install-nginx-module \
--auto \
--prefix=$nginx_install_dir \
--auto-download \
--extra-configure-flags=" \
--with-http_addition_module \
--with-http_dav_module \
--with-http_geoip_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_xslt_module \
--with-ipv6 \
--with-sha1=/usr/include/openssl \
--with-md5=/usr/include/openssl"
[ $? -eq 0 ] || die "Nginx install failed"
rm $nginx_install_dir/conf/*.default
else
msg "Nginx already installed"
fi
# update nginx configuration file
# XXX: PORT_WWW is missing in the environment at build time
sed > $nginx_install_dir/conf/nginx.conf < nginx.conf.in \
-e "s/@PORT_WWW@/${PORT_WWW:-42800}/g" \
-e "s#@PASSENGER_ROOT@#$passenger_install_dir#g" \
-e "s/@RACK_ENV@/${SERVICE_CONFIG_RACK_ENV:-production}/g"
}
install_gems() {
if [ -f Gemfile ]; then
msg "Installing dependencies from Gemfile"
gem install --no-ri --no-rdoc bundler
bundle install
else
msg "No Gemfile found, not running \`bundle install'"
fi
}
install_application() {
cat >> profile << EOF
export PATH="$nginx_install_dir/sbin:$passenger_install_dir/bin:$PATH"
EOF
mv profile ~/
mv passenger-kill-stuck-workers $passenger_install_dir/bin/
# Use ~/code and ~/current like the regular Ruby service for better compatibility
msg "installing application to ~/current/"
rsync -aH --delete --exclude "data" * ~/current/
}
move_to_approot
upgrade_rvm
install_ruby
install_nginx_passenger # could be replaced by something else
install_gems
install_application
allinone:
type: custom
buildscript: builder
systempackages:
- libpcre3-dev # needed for the Nginx rewrite module
- sphinxsearch
- jetty
- libjetty-extra-java # needed for servlet support
- libevent-1.4-2 # needed for memcached binary
processes:
nginx: nginx
watcher: passenger-kill-stuck-workers
memcached: /home/dotcloud/current/memcached
jetty: /home/dotcloud/current/jetty
# Could be moved to environment to make them mutable after the initial build
config:
# This is directly used by "rvm install"
ruby_version: 1.9.2
rack_env: production
#!/bin/bash
rm -rf ~/jetty
cp -rL /usr/share/jetty ~/jetty
cd ~/jetty
rmdir ~/jetty/logs
ln -s /var/log/supervisor logs
cp /usr/lib/jvm/java-6-openjdk/lib/tools.jar lib
cp -a ~/current/webapps/. webapps
exec java -jar start.jar
daemon off;
worker_processes 1;
error_log /dev/stderr notice;
events {
worker_connections 1024;
}
http {
passenger_root @PASSENGER_ROOT@;
# note that we use default here (i.e: what has been set with:
# rvm use <ruby_version> --default)
passenger_ruby /usr/local/rvm/wrappers/default/ruby;
passenger_friendly_error_pages off;
include mime.types;
default_type application/octet-stream;
log_format combined-realip '$remote_addr ($http_x_real_ip) - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
access_log /dev/stdout combined-realip;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen @PORT_WWW@ default;
server_name localhost;
if (-f /home/dotcloud/current/maintenance) {
return 503;
}
root /home/dotcloud/current/public;
passenger_enabled on;
rack_env @RACK_ENV@;
passenger_set_cgi_param SERVER_NAME $http_host;
error_page 404 @404;
error_page 500 @500;
error_page 502 @502;
error_page 503 @503;
error_page 504 @504;
location @404 {
rewrite ^ /404.html;
}
location @500 {
rewrite ^ /500.html;
}
location @502 {
rewrite ^ /502.html;
}
location @503 {
rewrite ^ /503.html;
}
location @504 {
rewrite ^ /504.html;
}
include /home/dotcloud/current/*nginx.conf;
}
}
#!/usr/bin/env ruby
# Phusion Passenger - http://www.modrails.com/
# Copyright (c) 2010 Phusion
#
# "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 2012, Louis Opter <louis@dotcloud.com>, create a script inspired by
# passenger-status that kill stuck Ruby processes
source_root = File.expand_path(File.dirname(__FILE__) + "/..")
$LOAD_PATH.unshift("#{source_root}/lib")
require 'phusion_passenger'
require 'phusion_passenger/platform_info'
require 'phusion_passenger/admin_tools/server_instance'
include PhusionPassenger::AdminTools
# Each check_interval seconds this daemon inspects the Ruby processes launched
# by Passenger. If a Ruby process didn't handled more request than at the last
# check this daemon sigkill it.
#
# Note: the first processes will be killed after 2 * CHECK_INTERVAL seconds.
$CHECK_INTERVAL = 60
def watch_processes(server_instance)
server_instance.connect(:passenger_status) do
last = {}
while 1 do
current = {}
# Inspect each Rack process in each Passenger group
server_instance.groups.each do |group|
current[group.name] = {}
group.processes.each do |process|
# We don't care about idle processes
if process.sessions > 0
# Check if we know this process already and if it is stuck
if last.has_key?(group.name) and last[group.name].has_key?(process.pid) \
and last[group.name][process.pid] == process.processed
puts "Killing stuck process #{process.pid} (processed #{process.processed} requests)"
Process.kill("KILL", process.pid)
else
# Refresh its status in our hash
current[group.name][process.pid] = process.processed
end
end
end
end
last = current
sleep $CHECK_INTERVAL
end
end
end
def run
if ARGV.empty?
while 1 do
server_instances = ServerInstance.list
if server_instances.empty?
STDERR.puts("ERROR: Phusion Passenger doesn't seem to be running, retrying in #{$CHECK_INTERVAL}s.")
sleep $CHECK_INTERVAL
else
break
end
end
if server_instances.size == 1
watch_processes(server_instances.first)
else
puts "It appears that multiple Passenger instances are running. Please select a"
puts "specific one by running:"
puts
puts " passenger-kill-stuck-workers <PID>"
puts
puts "The following Passenger instances are running:"
server_instances.each do |instance|
puts " PID: #{instance.pid}"
end
exit 1
end
else
server_instance = ServerInstance.for_pid(ARGV[0].to_i)
watch_processes(server_instance)
end
end
run
#!/bin/bash
[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment