Skip to content

Instantly share code, notes, and snippets.

@eventualbuddha
Created July 29, 2010 14:35
Show Gist options
  • Save eventualbuddha/498267 to your computer and use it in GitHub Desktop.
Save eventualbuddha/498267 to your computer and use it in GitHub Desktop.
Screen sessions + databases on a per-project basis, isolated in separate volumes
#!/usr/bin/env ruby
require 'optparse'
begin
require 'open4'
rescue LoadError => ex
begin
require 'rubygems'
require 'open4'
rescue LoadError
raise ex
end
end
class DbServer
class Error < RuntimeError; end
attr_accessor :datadir
def initialize(datadir)
self.datadir = datadir
end
def start
@pid = Open4.bg(start_command, :stdout => $stdout, :stderr => $stderr).pid
end
def stop
Open4.maim(@pid)
end
def running?
begin
Process.kill(0, @pid)
return true
rescue Errno::ESRCH
return false
end
end
end
class MySQL < DbServer
def installdb
system 'which -s mysql_install_db'
raise DbServer::Error, "Unable to find mysql_install_db. Please make sure it's in your PATH before trying again." unless $?.success?
system 'mysql_install_db', "--datadir=#{datadir}"
raise DbServer::Error, "There was a problem creating the database files at #{datadir}." unless $?.success?
end
def start_command
"mysqld --datadir=#{datadir}"
end
end
class DbCLI
attr_reader :args
def initialize(args)
@args = args
parser.parse!(args)
end
def run
begin
case args.first
when 'install'
server.installdb
when 'run', nil
trap(:INT) { server.stop; exit }
server.start
sleep 1 while server.running?
else
$stderr.puts "#{$0}: error: unknown subcommand #{args.first.inspect}"
exit(-1)
end
rescue DbServer::Error => ex
$stderr.puts ex.message
exit(-1)
end
end
def server
@server ||= begin
if not @datadir
puts parser
exit 1
end
MySQL.new(@datadir)
end
end
def self.run(args)
new(args).run
end
private
def parser
@parser ||= OptionParser.new do |opts|
opts.banner = "Usage: #{$0} (run|install) [options]"
opts.on('-p', '--project=NAME', 'which project to start the database with (required)') do |name|
@datadir = "/Volumes/#{name}/Data/MySQL/5.0"
end
opts.on('-D', '--datadir=PATH', 'which directory to use for the data') do |datadir|
@datadir = datadir
end
opts.on('-h', '--help', 'show this message') do
puts opts
exit
end
end
end
end
DbCLI.run(ARGV)
#!/usr/bin/env ruby -w
require 'pathname'
this = File.basename($0)
name = ARGV.shift
volumes = Pathname.glob('/Volumes/*')
volume_names = volumes.map{|v| v.basename.to_s }.join(', ')
if name.nil? || name.empty?
abort "#{this}: please provide one of #{volume_names}"
end
if closest_match = volumes.select {|vol| vol.basename.to_s.downcase.index(name.downcase) == 0 }.min
screenrc = closest_match + '.screenrc'
if screenrc.exist?
args = ARGV.map {|a| %{"#{a}"} }.join(' ')
exec %{screen -U -A -S #{closest_match.basename} -c #{screenrc} -xRR #{args}}
else
abort "#{this}: no .screenrc found in #{closest_match}"
end
else
abort "#{this}: no volume found matching #{name}, please provide one of #{volume_names}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment