class Stubby::CLI::Application

Public Instance Methods

env(name=nil) click to toggle source
# File lib/stubby/cli/application.rb, line 89
def env(name=nil)
  unless master_running?
    puts "[ERROR]: Stubby must be running to run 'environment'"
    return
  end

  puts HTTPI.post("http://#{STUBBY_MASTER}:9000/environment.json", environment: name).body
end
halt() click to toggle source
# File lib/stubby/cli/application.rb, line 54
def halt
  if master_running?
    stop
  else
    restore
  end
end
restore() click to toggle source
# File lib/stubby/cli/application.rb, line 74
def restore
  if master_running?
    puts "[ERROR] Stubby needs to be shut down first"
    exit
  end

  master = Stubby::Master.new({})
  master.restore!
end
start(environment="development") click to toggle source
# File lib/stubby/cli/application.rb, line 26
def start(environment="development")
  stubfile = File.expand_path(options[:stubfile])

  unless File.exists?(stubfile)
    puts "[ERROR]: Stubfile.json not found!"
    return
  end

  unless permissions?
    puts "[ERROR]: ATM I need to be run with sudo..."
    return
  end

  if master_running?
    puts "[ERROR]: Stubby's already running!"
    return
  end

  environments = MultiJson.load(File.read(stubfile))

  File.write(pidfile, Process.pid)

  master = Stubby::Master.new(environments)
  master.environment = environment
  master.run!
end
status() click to toggle source
# File lib/stubby/cli/application.rb, line 117
def status
  if master_running?
    environment = MultiJson.load(HTTPI.get("http://#{STUBBY_MASTER}:9000/environment.json").body)["environment"]
    activated = MultiJson.load(HTTPI.get("http://#{STUBBY_MASTER}:9000/stubs/activated.json").body)
    puts MultiJson.dump({ "rules" => activated, "environment" => environment }, pretty: true)
  else
    puts MultiJson.dump(status: "error", message: "Stubby currently not running")
  end
end
stop() click to toggle source
# File lib/stubby/cli/application.rb, line 63
def stop
  if master_running?
    Process.kill("INT", File.read(pidfile).to_i)

    while master_running?
      puts "." and sleep 1
    end 
  end
end

Private Instance Methods

master_running?() click to toggle source
# File lib/stubby/cli/application.rb, line 136
def master_running?
  Process.kill(0, File.read(pidfile).to_i)
rescue
  false
end
permissions?() click to toggle source
# File lib/stubby/cli/application.rb, line 142
def permissions?
  `whoami`.strip == "root"
end
pidfile() click to toggle source
# File lib/stubby/cli/application.rb, line 128
def pidfile
  @pidfile ||= (
    home = File.expand_path("~/.stubby")
    FileUtils.mkdir_p(home) unless File.exists?(home)
    File.join(home, "pid")
  )
end