class Stubby::Master

Attributes

config[RW]
extensions[RW]

Public Class Methods

new(environments) click to toggle source
# File lib/stubby/master.rb, line 145
def initialize(environments)
  @extensions = {
    default:    Stubby::Extensions::Default.new,
    dns:        Stubby::Extensions::DNS::Server.new,
    http:       Stubby::Extensions::HTTP::Server.new,
    https:      Stubby::Extensions::HTTP::SSLServer.new,
    smtp:       Stubby::Extensions::SMTP::Server.new
  }

  @config = Api
  @config.environments = environments
  @config.master = self
end

Public Instance Methods

environment() click to toggle source
# File lib/stubby/master.rb, line 171
def environment
  @config.environment
end
environment=(environment) click to toggle source
# File lib/stubby/master.rb, line 159
def environment=(environment)
  @config.environment = environment
end
key(identifier) click to toggle source
# File lib/stubby/master.rb, line 163
def key(identifier)
  Digest::MD5.hexdigest(user_key + identifier)
end
restore!() click to toggle source
# File lib/stubby/master.rb, line 183
def restore!
  restore_extensions
end
run!(options={}) click to toggle source
# File lib/stubby/master.rb, line 175
def run!(options={})
  run_network do
    run_master do
      run_extensions
    end 
  end
end
stop!() click to toggle source
# File lib/stubby/master.rb, line 187
def stop!
  puts "Shutting down..."

  Api.stop!

  running.each do |process|
    Process.shutdown(process)
  end

  puts "Bye."
end
user_key() click to toggle source
# File lib/stubby/master.rb, line 167
def user_key
  @user_key ||= read_key
end

Private Instance Methods

assume_network_interface() click to toggle source
# File lib/stubby/master.rb, line 263
def assume_network_interface
  `ifconfig lo0 alias #{STUBBY_MASTER}`
end
generate_key() click to toggle source
# File lib/stubby/master.rb, line 207
def generate_key
  SecureRandom.hex(50).tap do |key|
    File.write(keyfile, key)
  end
end
keyfile() click to toggle source
# File lib/stubby/master.rb, line 213
def keyfile
  File.expand_path("~/.stubby/key")
end
read_key() click to toggle source
# File lib/stubby/master.rb, line 201
def read_key
  File.read(keyfile)
rescue
  generate_key
end
restore_extensions() click to toggle source
# File lib/stubby/master.rb, line 238
def restore_extensions
  @extensions.each do |name, plugin|
    plugin.restore!
  end
end
run_extensions() click to toggle source
# File lib/stubby/master.rb, line 232
def run_extensions
  running.each do |process|
    Process.waitpid(process)
  end
end
run_master() { || ... } click to toggle source
# File lib/stubby/master.rb, line 224
def run_master
  $0 = "stubby: master"

  Api.run! do |server|
    yield
  end
end
run_network() { || ... } click to toggle source
# File lib/stubby/master.rb, line 217
def run_network
  assume_network_interface
  yield
ensure
  unassume_network_interface
end
running() click to toggle source
# File lib/stubby/master.rb, line 244
def running
  @running ||= run_extensions
end
unassume_network_interface() click to toggle source
# File lib/stubby/master.rb, line 267
def unassume_network_interface
  `ifconfig lo0 -alias #{STUBBY_MASTER}`
end