helm/bin/web

116 lines
2.4 KiB
Plaintext
Raw Normal View History

2013-03-06 19:11:19 -08:00
#!/usr/bin/env ruby
2013-03-21 09:32:20 -07:00
require 'colorize'
2013-03-06 19:11:19 -08:00
# A start-script for my local webserver. Mac-only and requires Homebrew
BASE = File.expand_path(File.dirname(File.dirname(__FILE__)))
2013-03-06 19:11:19 -08:00
PREFIX = `brew --prefix`.strip
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"
## Services ###
2013-03-21 09:32:20 -07:00
def cmd_exists(cmd)
return system("which #{cmd} 2>&1 >/dev/null")
end
SERVICES = {}
2013-03-21 09:32:20 -07:00
if cmd_exists("php-fpm")
SERVICES[:php] = {
2013-03-06 19:11:19 -08:00
: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"
}
end
2013-03-21 09:32:20 -07:00
if cmd_exists("nginx")
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"
}
end
2013-03-21 09:32:20 -07:00
if cmd_exists("memcached")
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")
SERVICES[:db] = {
:bin => "su #{`whoami`.strip} -c #{`which mysql.server`.strip} start",
: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-21 09:32:20 -07:00
puts " * #{key} is already running".red
2013-03-06 19:11:19 -08:00
else
2013-03-21 09:32:20 -07:00
puts " * Starting #{key}".green
`#{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-21 09:32:20 -07:00
puts " * Stopping #{key}".green
`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-21 09:32:20 -07:00
puts " * #{key} is not running".red
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}`
`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
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