Browse Source

move unicorn_rack to shell, bump version

master
Erik Hollensbe 10 years ago
parent
commit
a46aa5e0a8
  1. 67
      files/default/unicorn_rack
  2. 63
      files/default/unicorn_rack.sh
  3. 2
      metadata.rb
  4. 22
      recipes/default.rb

67
files/default/unicorn_rack

@ -1,67 +0,0 @@
#!/usr/bin/env ruby
require 'sys/proctable'
require 'etc'
require 'json'
$0 = File.expand_path($0)
ENV["PATH"] = "/usr/local/bin:/usr/local/bin:#{ENV["PATH"]}"
def get_apps
JSON.load(File.read("/etc/default/unicorn.json"))
end
pids = Sys::ProcTable.ps.select { |x| x.cmdline =~ /^unicorn/ && x.ppid == 1 }
app = ARGV[1]
if app
pid = pids.find { |x| x.cmdline =~ /#{Regexp.quote app}/ }
if pid
pids = [pid]
else
pids = []
end
end
case ARGV[0]
when "restart"
system("#{Gem.ruby} #{$0} stop #{app}")
system("#{Gem.ruby} #{$0} start #{app}")
when "reload"
pids.each do |pid|
puts "reloading '#{pid.cmdline}'"
Process.kill(:USR2, pid.pid)
sleep 1
Process.kill(:QUIT, pid.pid)
end
when "start"
apps = get_apps
if app
apps = { app => apps[app] }
end
unless apps.keys.size > 0
raise ArgumentError, "Could not find any applications; aborting"
end
apps.each do |key, value|
puts "Attempting to start '#{key}'"
fork do
user = Etc.getpwnam("rails")
Process.uid = user.uid
Process.gid = user.gid
Process.euid = user.uid
Process.egid = user.gid
Dir.chdir(key) do
exec("bundle exec '#{value["bin"]}' -c '#{value["config_path"]}' -E production -D")
end
end
end
when "stop"
pids.each do |pid|
puts "stopping '#{pid.cmdline}'"
Process.kill(:INT, pid.pid)
end
end

63
files/default/unicorn_rack.sh

@ -0,0 +1,63 @@
#!/bin/bash
CONFIG_PATHS=$(awk -F: '{ print $2 }' /etc/unicorn.conf)
CONFIG=$(cat /etc/unicorn.conf)
start_app() {
local path=$1
local config_path=$2
local bin=$3
local user=$4
cd $path
sudo -iu $user bundle exec "${bin}" -c "${config_path}" -E production -D
cd $OLDPWD
}
case "$1" in
restart)
$0 stop
$0 start
;;
reload)
for path in ${CONFIG_PATHS}
do
pkill -USR2 -f "unicorn master.*${path}"
echo "Waiting for new unicorns..."
sleep 5
pkill -QUIT -f "unicorn master \\(old\\).*${path}"
done
;;
start)
for line in ${CONFIG}
do
# I'm too dumb to use bash arrays properly
path=$(echo -n $line | awk -F: '{ print $1 }')
config_path=$(echo -n $line | awk -F: '{ print $2 }')
bin=$(echo -n $line | awk -F: '{ print $3 }')
user=$(echo -n $line | awk -F: '{ print $4 }')
if [ ! -z $2 ]
then
if [ $2 = $path ]
then
echo "${path} specified: starting it individually"
start_app $path $config_path $bin $user
fi
else
echo "Starting ${path}"
start_app $path $config_path $bin $user
fi
done
;;
stop)
for path in ${CONFIG_PATHS}
do
echo "Killing ${path} with INT signal"
pkill -INT -f "unicorn master.*${path}"
done
;;
esac

2
metadata.rb

@ -3,7 +3,7 @@ maintainer_email "erik+chef@hollensbe.org"
license "Apache 2.0"
description "Maintain applications running the unicorn server software"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "0.1.0"
version "0.2.0"
recipe "unicorn::default",
"Installs the unicorn_rack service and any applications definitions for the host"

22
recipes/default.rb

@ -1,11 +1,16 @@
require 'yajl'
user 'rails' do
shell "/bin/false"
action :create
end
gem_package "sys-proctable" do
action :install
end
cookbook_file "/etc/init.d/unicorn_rack" do
source 'unicorn_rack'
source 'unicorn_rack.sh'
mode 00750
owner "root"
group "root"
@ -21,7 +26,8 @@ host_applications = { }
filter_proc = proc do |x|
host_applications[x['path']] = {
"config_path" => x['config_path'],
"bin" => x['bin']
"bin" => x['bin'],
"user" => x['user'] || 'rails'
}
end
@ -29,7 +35,17 @@ search('applications', "hosts:#{node.name}").each(&filter_proc)
node["applications"].each(&filter_proc)
file "/etc/default/unicorn.json" do
content Yajl.dump(host_applications, :pretty => true)
action :delete
end
awk_friendly_mess =
host_applications.keys.map do |key|
ha = host_applications[key]
"#{key}:#{ha["config_path"]}:#{ha["bin"]}:#{ha["user"]}"
end.join("\n")
file '/etc/unicorn.conf' do
content awk_friendly_mess
mode 00640
owner "root"
group "root"

Loading…
Cancel
Save