2013-03-06 19:11:19 -08:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2013-03-22 00:46:49 -07:00
|
|
|
if ENV['USER'] == "root"
|
|
|
|
puts "Must not be run as root!"
|
|
|
|
exit 1
|
|
|
|
end
|
2013-03-21 09:32:20 -07:00
|
|
|
|
2013-03-06 19:11:19 -08:00
|
|
|
# A start-script for my local webserver. Mac-only and requires Homebrew
|
|
|
|
|
2013-03-17 21:35:04 -07:00
|
|
|
BASE = File.expand_path(File.dirname(File.dirname(__FILE__)))
|
2013-03-06 19:11:19 -08:00
|
|
|
PREFIX = `brew --prefix`.strip
|
|
|
|
|
2013-03-17 21:35:04 -07:00
|
|
|
CONF = "#{BASE}/conf"
|
2013-03-06 19:11:19 -08:00
|
|
|
PID = "#{PREFIX}/var/run"
|
2013-03-21 09:32:20 -07:00
|
|
|
WWW = "#{PREFIX}/www"
|
2013-03-17 21:35:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
## Services ###
|
|
|
|
|
2013-03-21 09:32:20 -07:00
|
|
|
def cmd_exists(cmd)
|
|
|
|
return system("which #{cmd} 2>&1 >/dev/null")
|
|
|
|
end
|
|
|
|
|
2013-03-17 21:35:04 -07:00
|
|
|
SERVICES = {}
|
2013-03-21 09:32:20 -07:00
|
|
|
if cmd_exists("php-fpm")
|
2013-03-17 21:35:04 -07:00
|
|
|
SERVICES[:php] = {
|
2013-03-22 00:46:49 -07:00
|
|
|
:bin => "sudo #{`which php-fpm`.strip}",
|
2013-03-08 21:18:59 -08:00
|
|
|
:opts => "-p '/usr/local/var' -c '#{CONF}/php/php.ini' --fpm-config '#{CONF}/php/php-fpm.conf' --pid '#{PID}/php-fpm-pid'",
|
2013-03-06 19:11:19 -08:00
|
|
|
:pid => "#{PID}/php-fpm.pid"
|
2013-03-17 21:35:04 -07:00
|
|
|
}
|
|
|
|
end
|
2013-03-21 09:32:20 -07:00
|
|
|
if cmd_exists("nginx")
|
2013-03-17 21:35:04 -07:00
|
|
|
SERVICES[:nginx] = {
|
2013-03-21 09:32:20 -07:00
|
|
|
:bin => "sudo #{`which nginx`.strip}",
|
2013-03-06 19:11:19 -08:00
|
|
|
:opts => "-c '#{CONF}/nginx/nginx.conf'",
|
|
|
|
:pid => "#{PID}/nginx.pid"
|
|
|
|
}
|
2013-03-17 21:35:04 -07:00
|
|
|
end
|
2013-03-21 09:32:20 -07:00
|
|
|
if cmd_exists("memcached")
|
2013-03-17 21:35:04 -07:00
|
|
|
SERVICES[:memcached] = {
|
|
|
|
:bin => `which memcached`.strip,
|
|
|
|
:opts => "-d -P '#{PID}/memcached.pid' -u #{`whoami`.strip}",
|
|
|
|
:pid => "#{PID}/memcached.pid"
|
|
|
|
}
|
|
|
|
end
|
2013-03-21 09:32:20 -07:00
|
|
|
if cmd_exists("mysql.server")
|
2013-03-17 21:35:04 -07:00
|
|
|
SERVICES[:db] = {
|
2013-03-22 00:46:49 -07:00
|
|
|
:bin => "#{`which mysql.server`.strip} start",
|
2013-03-17 21:35:04 -07:00
|
|
|
:opts => "",
|
|
|
|
:pid => "#{PREFIX}/var/mysql/#{`hostname`}.pid"
|
|
|
|
}
|
|
|
|
end
|
2013-03-06 19:11:19 -08:00
|
|
|
|
|
|
|
|
|
|
|
## Functions ##
|
|
|
|
|
|
|
|
def start
|
|
|
|
SERVICES.each_pair do |key,opts|
|
|
|
|
if File.exist?(opts[:pid])
|
2013-03-22 00:46:49 -07:00
|
|
|
puts " * #{key} is already running"
|
2013-03-06 19:11:19 -08:00
|
|
|
else
|
2013-03-22 00:46:49 -07:00
|
|
|
puts " * Starting #{key}"
|
2013-03-08 21:18:59 -08:00
|
|
|
`#{opts[:bin]} #{opts[:opts]}`
|
2013-03-06 19:11:19 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
SERVICES.each_pair do |key,opts|
|
|
|
|
if File.exist?(opts[:pid])
|
2013-03-22 00:46:49 -07:00
|
|
|
puts " * Stopping #{key}"
|
2013-03-21 09:32:20 -07:00
|
|
|
`sudo kill #{`cat #{opts[:pid]}`.strip}`
|
|
|
|
`sudo rm #{opts[:pid]} >/dev/null 2>&1`
|
2013-03-06 19:11:19 -08:00
|
|
|
else
|
2013-03-22 00:46:49 -07:00
|
|
|
puts " * #{key} is not running"
|
2013-03-06 19:11:19 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def restart
|
|
|
|
stop
|
|
|
|
start
|
|
|
|
end
|
|
|
|
|
|
|
|
# Change the local webserver's root to the current directory
|
|
|
|
def chdir
|
2013-03-21 09:32:20 -07:00
|
|
|
`rm -f #{WWW}`
|
2013-03-08 21:18:59 -08:00
|
|
|
`ln -sf "#{`pwd`.strip}" "#{WWW}"`
|
2013-03-06 19:11:19 -08:00
|
|
|
|
|
|
|
restart
|
|
|
|
end
|
|
|
|
|
2013-03-21 09:32:20 -07:00
|
|
|
def usage
|
2013-03-17 21:35:04 -07:00
|
|
|
puts "Usage: web (start|stop|restart|chdir)"
|
2013-03-06 19:11:19 -08:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
## Bootstrap ##
|
|
|
|
|
|
|
|
if ARGV.length == 0
|
2013-03-21 09:32:20 -07:00
|
|
|
usage
|
2013-03-06 19:11:19 -08:00
|
|
|
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
|
2013-03-21 09:32:20 -07:00
|
|
|
usage
|
2013-03-06 19:11:19 -08:00
|
|
|
end
|
|
|
|
end
|