#!/usr/bin/env ruby # A start-script for my local webserver. Mac-only and requires Homebrew BASE = File.expand_path(File.dirname(File.dirname(__FILE__))) PREFIX = `brew --prefix`.strip CONF = "#{BASE}/conf" PID = "#{PREFIX}/var/run" WWW = "#{PREFIX}/dev/www" ## Services ### SERVICES = {} if system("which php-fpm 2>&1 >/dev/null") SERVICES[:php] = { :bin => `which php-fpm`.strip, :opts => "-p '/usr/local/var' -c '#{CONF}/php/php.ini' --fpm-config '#{CONF}/php/php-fpm.conf' --pid '#{PID}/php-fpm-pid'", :pid => "#{PID}/php-fpm.pid" } end if system("which nginx 2>&1 >/dev/null") SERVICES[:nginx] = { :bin => `which nginx`.strip, :opts => "-c '#{CONF}/nginx/nginx.conf'", :pid => "#{PID}/nginx.pid" } end if system("which memcached 2>&1 >/dev/null") SERVICES[:memcached] = { :bin => `which memcached`.strip, :opts => "-d -P '#{PID}/memcached.pid' -u #{`whoami`.strip}", :pid => "#{PID}/memcached.pid" } end if system("which mysql.server 2>&1 >/dev/null") SERVICES[:db] = { :bin => "su #{`whoami`.strip} -c #{`which mysql.server`.strip} start", :opts => "", :pid => "#{PREFIX}/var/mysql/#{`hostname`}.pid" } end ## Functions ## def start SERVICES.each_pair do |key,opts| if File.exist?(opts[:pid]) puts "[WARNING] #{key} is already running." else puts "[NOTICE] Starting #{key} ..." `#{opts[:bin]} #{opts[:opts]}` end end end def stop SERVICES.each_pair do |key,opts| if File.exist?(opts[:pid]) puts "[NOTICE] Stopping #{key} ..." `kill #{`cat #{opts[:pid]}`.strip}` `rm #{opts[:pid]} >/dev/null 2>&1` else puts "[WARNING] #{key} is not running." end end end def restart stop start end # Change the local webserver's root to the current directory def chdir if File.directory?(WWW) `rm -r #{WWW}` end `ln -sf "#{`pwd`.strip}" "#{WWW}"` restart end def help puts "Usage: web (start|stop|restart|chdir)" exit end ## Bootstrap ## if `id -u` != "0\n" puts "Must be run as root!" exit end if ARGV.length == 0 help else case ARGV[0] when "start" puts "Starting web services" start when "stop" puts "Stopping web services" stop when "restart" restart when "chdir" chdir else help end end