helm/bin/web
2013-03-21 12:32:20 -04:00

116 lines
2.4 KiB
Ruby
Executable file

#!/usr/bin/env ruby
require 'colorize'
# 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}/www"
## Services ###
def cmd_exists(cmd)
return system("which #{cmd} 2>&1 >/dev/null")
end
SERVICES = {}
if cmd_exists("php-fpm")
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 cmd_exists("nginx")
SERVICES[:nginx] = {
:bin => "sudo #{`which nginx`.strip}",
:opts => "-c '#{CONF}/nginx/nginx.conf'",
:pid => "#{PID}/nginx.pid"
}
end
if cmd_exists("memcached")
SERVICES[:memcached] = {
:bin => `which memcached`.strip,
:opts => "-d -P '#{PID}/memcached.pid' -u #{`whoami`.strip}",
:pid => "#{PID}/memcached.pid"
}
end
if cmd_exists("mysql.server")
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 " * #{key} is already running".red
else
puts " * Starting #{key}".green
`#{opts[:bin]} #{opts[:opts]}`
end
end
end
def stop
SERVICES.each_pair do |key,opts|
if File.exist?(opts[:pid])
puts " * Stopping #{key}".green
`sudo kill #{`cat #{opts[:pid]}`.strip}`
`sudo rm #{opts[:pid]} >/dev/null 2>&1`
else
puts " * #{key} is not running".red
end
end
end
def restart
stop
start
end
# Change the local webserver's root to the current directory
def chdir
`rm -f #{WWW}`
`ln -sf "#{`pwd`.strip}" "#{WWW}"`
restart
end
def usage
puts "Usage: web (start|stop|restart|chdir)"
exit
end
## Bootstrap ##
if ARGV.length == 0
usage
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
usage
end
end