#!/usr/bin/env ruby # A start-script for my local webserver. Mac-only and requires Homebrew PREFIX = `brew --prefix`.strip CONF = "#{PREFIX}/dev/conf" PID = "#{PREFIX}/var/run" WWW = "#{PREFIX}/dev/www" 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" }, :nginx => { :bin => `which nginx`.strip, :opts => "-c '#{CONF}/nginx/nginx.conf'", :pid => "#{PID}/nginx.pid" } } ## 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)" 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