helm/bin/web

95 lines
1.8 KiB
Plaintext
Raw Normal View History

2013-03-06 19:11:19 -08:00
#!/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'",
2013-03-06 19:11:19 -08:00
: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."
2013-03-06 19:11:19 -08:00
else
puts "[NOTICE] Starting #{key} ..."
`#{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])
puts "[NOTICE] Stopping #{key} ..."
2013-03-06 19:11:19 -08:00
`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}"`
2013-03-06 19:11:19 -08:00
restart
end
def help
2013-03-16 14:51:40 -07:00
puts "Usage: web (start|stop|restart)"
2013-03-06 19:11:19 -08:00
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