From 613d215370354fabd42decea7675742580147bc9 Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Tue, 7 Jun 2011 17:06:06 -0700 Subject: [PATCH 1/5] Load the geocoder database location from the env first, then argv. This is not optimal, because it makes sense to have the command-line over-ride the environment, however when we run this under unicorn, argv[0] is set to something that is *not* a database location. --- lib/geocoder/us/rest.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/geocoder/us/rest.rb b/lib/geocoder/us/rest.rb index 4881c7f..dd90f70 100644 --- a/lib/geocoder/us/rest.rb +++ b/lib/geocoder/us/rest.rb @@ -3,7 +3,7 @@ require 'json' require 'timeout' -@@db = Geocoder::US::Database.new(ARGV[0] || ENV["GEOCODER_DB"]) +@@db = Geocoder::US::Database.new(ENV["GEOCODER_DB"] || ARGV[0]) set :port, 8081 get '/geocode' do @@ -43,12 +43,12 @@ :features => features }.to_json rescue JSON::GeneratorError - { + { :type => "FeatureCollection", :error => "JSON::GeneratorError", :features => [] }.to_json - end + end else status 400 "parameter 'q' is missing" From 434f9d3aa40e71a20b5d2cf9b8dad1802282957a Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Tue, 7 Jun 2011 17:27:41 -0700 Subject: [PATCH 2/5] Remove cruft. --- conf/geocoder-us/thin.yml | 8 ------- debian/README.source | 9 -------- debian/geocoder-us.postinst | 43 ------------------------------------- debian/geocoder-us.prerm | 24 --------------------- 4 files changed, 84 deletions(-) delete mode 100644 conf/geocoder-us/thin.yml delete mode 100644 debian/README.source delete mode 100644 debian/geocoder-us.postinst delete mode 100644 debian/geocoder-us.prerm diff --git a/conf/geocoder-us/thin.yml b/conf/geocoder-us/thin.yml deleted file mode 100644 index e644c33..0000000 --- a/conf/geocoder-us/thin.yml +++ /dev/null @@ -1,8 +0,0 @@ -environment: production -chdir: /var/lib/geocoder-us -user: www-data -group: www-data -port: 8081 -pid: /var/run/geocoder-us/geocoder-us.pid -log: /var/log/geocoder-us/geocoder-us.log -tag: geocoder diff --git a/debian/README.source b/debian/README.source deleted file mode 100644 index 722dd34..0000000 --- a/debian/README.source +++ /dev/null @@ -1,9 +0,0 @@ -geocoder-us for Debian ----------------------- - - - - - - diff --git a/debian/geocoder-us.postinst b/debian/geocoder-us.postinst deleted file mode 100644 index 0739a29..0000000 --- a/debian/geocoder-us.postinst +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/sh -# postinst script for #PACKAGE# -# -# see: dh_installdeb(1) - -set -e - -# summary of how this script can be called: -# * `configure' -# * `abort-upgrade' -# * `abort-remove' `in-favour' -# -# * `abort-remove' -# * `abort-deconfigure' `in-favour' -# `removing' -# -# for details, see http://www.debian.org/doc/debian-policy/ or -# the debian-policy package - - -case "$1" in - configure) - # just make sure that /usr/bin/thin can write its PID file and logs - chown www-data /var/run/geocoder-us - chown www-data /var/log/geocoder-us - start geocoder-us || /bin/true - ;; - - abort-upgrade|abort-remove|abort-deconfigure) - ;; - - *) - echo "postinst called with unknown argument \`$1'" >&2 - exit 1 - ;; -esac - -# dh_installdeb will replace this with shell code automatically -# generated by other debhelper scripts. - -#DEBHELPER# - -exit 0 diff --git a/debian/geocoder-us.prerm b/debian/geocoder-us.prerm deleted file mode 100644 index 80186a9..0000000 --- a/debian/geocoder-us.prerm +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh - -set -e - -case "$1" in - remove|deconfigure) - stop geocoder-us || true - ;; - upgrade) - ;; - failed-upgrade) - ;; - *) - echo "prerm called with unknown argument \`$1'" >&2 - exit 0 - ;; -esac - -# dh_installdeb will replace this with shell code automatically -# generated by other debhelper scripts. - - - -exit 0 From 953fd30c85674d68988c4a3bf4378af4f0b10989 Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Tue, 7 Jun 2011 17:28:23 -0700 Subject: [PATCH 3/5] Use unicorn to run the geocoder. --- conf/geocoder-us/unicorn.rb | 11 +++++++++++ conf/init/geocoder-us.conf | 14 ++------------ debian/control | 2 +- debian/default | 6 ++++++ 4 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 conf/geocoder-us/unicorn.rb create mode 100644 debian/default diff --git a/conf/geocoder-us/unicorn.rb b/conf/geocoder-us/unicorn.rb new file mode 100644 index 0000000..d99ec89 --- /dev/null +++ b/conf/geocoder-us/unicorn.rb @@ -0,0 +1,11 @@ +worker_processes 4 +listen "/var/run/geocoder-us/unicorn.sock", :backlog => 64 +pid "/var/run/geocoder-us/unicorn.pid" +stderr_path "/var/log/geocoder-us/geocoder-err.log" +stdout_path "/var/log/geocoder-us/geocoder-out.log" + +# Have each process listen on a local port for debugging purposes. +after_fork do |server, worker| + addr = "127.0.0.1:#{40000 + worker.nr}" + server.listen(addr, :tries => 1, :delay => 5, :tcp_nopush => true) +end diff --git a/conf/init/geocoder-us.conf b/conf/init/geocoder-us.conf index 4d047de..e53f8a1 100644 --- a/conf/init/geocoder-us.conf +++ b/conf/init/geocoder-us.conf @@ -10,16 +10,6 @@ stop on runlevel [!2345] respawn script - cd /tmp - ruby /usr/lib/ruby/1.8/geocoder/us/rest.rb /mnt/geocoder-us/geocoder.db -end script - -post-start script - mkdir -p /var/run/geocoder-us - PID=`status geocoder-us | egrep -oi '([0-9]+)$' | head -n1` - echo $PID > /var/run/geocoder-us/geocoder-us.pid -end script - -post-stop script - rm -f /var/run/geocoder-us/geocoder-us.pid + . /etc/default/geocoder-us + unicorn -c /etc/geocoder-us/unicorn.rb -D /etc/geocoder-us/geocoder.ru end script diff --git a/debian/control b/debian/control index 4f3fd14..a20c5c6 100644 --- a/debian/control +++ b/debian/control @@ -9,6 +9,6 @@ Homepage: http://github.com/simplegeo/geocoder/ Package: geocoder-us Architecture: any -Depends: ${misc:Depends}, ${shlibs:Depends}, ruby1.8, libsqlite3-ruby (>= 1.3.0), libsinatra-ruby, libjson-ruby, thin +Depends: ${misc:Depends}, ${shlibs:Depends}, ruby1.8, libsqlite3-ruby (>= 1.3.0), libsinatra-ruby, libjson-ruby, unicorn Description: A US address geocoder. A US address geocoder. Requires a suitable database. diff --git a/debian/default b/debian/default new file mode 100644 index 0000000..13ecc25 --- /dev/null +++ b/debian/default @@ -0,0 +1,6 @@ +# Defaults for geocoder-us upstart job +# sourced by /etc/init/geocoder-us.conf +# installed at /etc/default/geocoder-us by maintainer scripts + +# Set the location of the geocoder database. +export GEOCODER_DB="/var/lib/geocoder-us/geocoder.db" From a12cf39d09128761a6b644c1756060a9a4b26948 Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Tue, 7 Jun 2011 17:31:21 -0700 Subject: [PATCH 4/5] Don't run as root. --- conf/geocoder-us/unicorn.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/conf/geocoder-us/unicorn.rb b/conf/geocoder-us/unicorn.rb index d99ec89..51d4245 100644 --- a/conf/geocoder-us/unicorn.rb +++ b/conf/geocoder-us/unicorn.rb @@ -1,4 +1,5 @@ worker_processes 4 +user "www-data", "www-data" listen "/var/run/geocoder-us/unicorn.sock", :backlog => 64 pid "/var/run/geocoder-us/unicorn.pid" stderr_path "/var/log/geocoder-us/geocoder-err.log" From 701b08f387c161d5b46c0ec622e64f8440446747 Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Tue, 7 Jun 2011 17:34:05 -0700 Subject: [PATCH 5/5] Reverting "Remove cruft." Resurrect these files I shouldn't have deleted. This reverts commit 434f9d3aa40e71a20b5d2cf9b8dad1802282957a. --- debian/geocoder-us.postinst | 43 +++++++++++++++++++++++++++++++++++++ debian/geocoder-us.prerm | 24 +++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 debian/geocoder-us.postinst create mode 100644 debian/geocoder-us.prerm diff --git a/debian/geocoder-us.postinst b/debian/geocoder-us.postinst new file mode 100644 index 0000000..0739a29 --- /dev/null +++ b/debian/geocoder-us.postinst @@ -0,0 +1,43 @@ +#!/bin/sh +# postinst script for #PACKAGE# +# +# see: dh_installdeb(1) + +set -e + +# summary of how this script can be called: +# * `configure' +# * `abort-upgrade' +# * `abort-remove' `in-favour' +# +# * `abort-remove' +# * `abort-deconfigure' `in-favour' +# `removing' +# +# for details, see http://www.debian.org/doc/debian-policy/ or +# the debian-policy package + + +case "$1" in + configure) + # just make sure that /usr/bin/thin can write its PID file and logs + chown www-data /var/run/geocoder-us + chown www-data /var/log/geocoder-us + start geocoder-us || /bin/true + ;; + + abort-upgrade|abort-remove|abort-deconfigure) + ;; + + *) + echo "postinst called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + +#DEBHELPER# + +exit 0 diff --git a/debian/geocoder-us.prerm b/debian/geocoder-us.prerm new file mode 100644 index 0000000..80186a9 --- /dev/null +++ b/debian/geocoder-us.prerm @@ -0,0 +1,24 @@ +#!/bin/sh + +set -e + +case "$1" in + remove|deconfigure) + stop geocoder-us || true + ;; + upgrade) + ;; + failed-upgrade) + ;; + *) + echo "prerm called with unknown argument \`$1'" >&2 + exit 0 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + + + +exit 0