class KijiRest::Server

Class that wraps the starting/stopping of a KijiREST server. assumes that this is running as a non-privileged user that has permissions to read/write to the location where KIJI_REST is installed

Constants

KIJI_REST_HOME

Public Class Methods

new(kiji_rest_home = KIJI_REST_HOME) click to toggle source
# File lib/kijirest/server.rb, line 31
def initialize(kiji_rest_home = KIJI_REST_HOME)
  @kiji_server_location = kiji_rest_home
  unless authorized_to_run?
    raise "#{ENV['USER']} not authorized to run KijiREST server!"
  end
  @http_port = 8080
  reload_configuration!
end

Public Instance Methods

new_client() click to toggle source

Returns a new client instance.

# File lib/kijirest/server.rb, line 102
def new_client
  Client.new("http://localhost:#{@http_port}")
end
start(zk_quorum = ".env", visible_instances = ["default"], wait_for_load = false) click to toggle source

Start a server given a zookeeper quorum and array of visible instances. This will modify the configuration.yml and launch the server. param: zk_quorum is the list of zookeeper servers. Default is .env param: visible_instances is an array of instance names that are to be visible by clients

of the REST server.

param: wait_for_load specifies whether or not to block until the server has come up. If the

server fails to come up after some period of time, an exception will be raised.
# File lib/kijirest/server.rb, line 47
def start(zk_quorum = ".env", visible_instances = ["default"], wait_for_load = false)
  if running?
    raise "KijiREST appears to be running."
  else
    #Update the configuration file to reflect the desired options
    dropwizard_config = YAML.load_file(qualify_file("/conf/configuration.yml"))

    #Do a bit of string checking so that we aren't adding kiji:// prefix twice.
    prefix="kiji://"
    prefix = "" if zk_quorum[0..6] == "kiji://"

    kiji_uri = "#{prefix}#{zk_quorum}"
    dropwizard_config["cluster"] = kiji_uri
    dropwizard_config["instances"] = visible_instances
    f = File.new(qualify_file("/conf/configuration.yml"), "w")
    f.puts(dropwizard_config.to_yaml)
    f.close
    #Now start the service
    launch_command = qualify_file("/bin/kiji-rest start")
    %x{#{launch_command}}
    if wait_for_load
      (1..20).each {|i|
        break if running?
        sleep 2
        }
      unless running?
        raise "KijiREST failed to start!"
      end
    end
  end
end
stop(wait_for_shutdown = false) click to toggle source

Stops the server optionally waiting for the server to shutdown param: wait_for_shutdown determines whether or not to wait for the server to have shutdown

before returning.
# File lib/kijirest/server.rb, line 82
def stop(wait_for_shutdown = false)
  pid_file = qualify_file("kiji-rest.pid")
  launch_command = qualify_file("/bin/kiji-rest stop")
  if File.exist?(pid_file)
    %x{#{launch_command}}
    if wait_for_shutdown
      (1..20).each {|i|
        break unless running?
        sleep 2
        }
       if running?
         raise "KijiREST failed to stop."
       end
    end
  else
    raise "KijiREST not running!"
  end
end

Private Instance Methods

app_running?() click to toggle source

Checks if the application is running by hitting a known URI

# File lib/kijirest/server.rb, line 125
def app_running?
  begin
    f = open("http://localhost:#{@http_port}/v1")
    f.close
    true
  rescue
    false
  end
end
authorized_to_run?() click to toggle source

Checks if this current user is authorized to run the server. A simple check is to ensure that the current user is the owner of a known file (conf/configuration.yml)

# File lib/kijirest/server.rb, line 120
def authorized_to_run?
  File.owned?(qualify_file("/conf/configuration.yml"))
end
pid_exists?() click to toggle source

Checks if the pid file exists. This will check for the pid file generated when the server is launched by a non-privileged user as well as the pid file generated when this server is launched by /sbin/service.

# File lib/kijirest/server.rb, line 138
def pid_exists?
  local_pid_file = qualify_file("kiji-rest.pid")
  global_pid_file = "/var/run/kiji-rest/kiji-rest.pid"
  File.exists?(local_pid_file) || File.exists?(global_pid_file)
end
qualify_file(filename) click to toggle source

Returns a fully qualified filename prefixed by the location of kiji_rest_server

# File lib/kijirest/server.rb, line 151
def qualify_file(filename)
  "#{@kiji_server_location}/#{filename}"
end
reload_configuration!() click to toggle source

Reloads the configuration.yml storing the configuration in a private member variable.

# File lib/kijirest/server.rb, line 110
def reload_configuration!
  @dropwizard_config = YAML.load_file(qualify_file("/conf/configuration.yml"))
  @http_port = 8080
  if @dropwizard_config.include?("http") && @dropwizard_config["http"].include?("port")
      @http_port = @dropwizard_config["http"]["port"]
  end
end
running?() click to toggle source

Checks if the server is running by checking the process file (kiji-rest.pid) or the global/root run /var/run/kiji-rest.pid

# File lib/kijirest/server.rb, line 146
def running?
  app_running? && pid_exists?
end