Instantly share code, notes, and snippets.

Embed
What would you like to do?
Service task for Mac OS X
class Service < Thor
desc "apache", "Apache"
def apache(action)
service action do |s|
s.down { %x[sudo apachectl stop] }
s.up { %x[sudo apachectl start] }
end
end
desc "memcached", "Memcached"
def memcached(action)
launchctl_service action, '/Users/sobstel/Library/LaunchAgents/com.danga.memcached.plist'
end
desc "mongo", "MongoDB"
def mongo(action)
launchctl_service action, '/Users/sobstel/Library/LaunchAgents/org.mongodb.mongod.plist'
end
desc "mysql", "MySQL"
def mysql(action)
launchctl_service action, '/Users/sobstel/Library/LaunchAgents/com.mysql.mysqld.plist'
end
desc "pow", "Pow (pow.cx)"
def pow(action)
launchctl_service action, '/Users/sobstel/Library/LaunchAgents/cx.pow.powd.plist'
end
desc "redis", "Redis"
def redis(action)
launchctl_service action, '/Users/sobstel/Library/LaunchAgents/io.redis.redis-server.plist'
end
private
def launchctl_service(action, path)
service action do |s|
s.down { %x[launchctl unload -w #{path}] }
s.up { %x[launchctl load -w #{path}] }
end
end
def service(action)
yield(ServiceHandler.new(action))
end
class ServiceHandler
def initialize(action)
@action = action.to_sym
end
def call_method?(method)
case method
when :down
[:down, :stop, :unload, :restart].include?(@action)
when :up
[:up, :start, :load, :restart].include?(@action)
else
false
end
end
def method_missing(m, *args, &block)
if call_method?(m)
output = block.call
status_code = $?.to_i
puts output unless output.empty?
puts "Action '#{@action}' failed (exit code: #{status_code})" if status_code > 0
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment