class Chook::Server

The chook server is a basic sinatra server running on the engine of your choice.

the server

the server app

the server

see server.rb

see server.rb

see server.rb

see server.rb

see server.rb

see server.rb

Constants

DEFAULT_CONCURRENCY
DEFAULT_PORT
DEFAULT_SESSION_EXPIRE
DEFAULT_SSL_PORT
HANDLE_EVENT_ROUTE

Public Class Methods

admin_user_pw() click to toggle source
# File lib/chook/server/auth.rb, line 122
def self.admin_user_pw
  @admin_user_pw ||= pw_from_conf Chook.config.admin_pw
end
humanize_secs(secs) click to toggle source

Very handy! lifted from stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails

# File lib/chook/server.rb, line 110
def self.humanize_secs(secs)
  [[60, :second], [60, :minute], [24, :hour], [7, :day], [52.179, :week], [1_000_000, :year]].map do |count, name|
    next unless secs.positive?

    secs, n = secs.divmod(count)
    n = n.to_i
    "#{n} #{n == 1 ? name : (name.to_s + 's')}"
  end.compact.reverse.join(' ')
end
prep_to_run() click to toggle source
# File lib/chook/server.rb, line 73
def self.prep_to_run
  @start_time = Time.now
  log_level ||= Chook.config.log_level
  @log_level = Chook::Procs::STRING_TO_LOG_LEVEL.call log_level

  configure do
    set :logger, Log.startup(@log_level)
    set :server, :thin
    set :bind, '0.0.0.0'
    set :port, Chook.config.port
    set :show_exceptions, :after_handler if development?
    set :root, "#{File.dirname __FILE__}/server"
    enable :static
    enable :sessions
    set :sessions, expire_after: Chook.config.admin_session_expires if Chook.config.admin_user
    if Chook.config.concurrency
      set :threaded, true
    else
      enable :lock
    end
  end # configure

  Chook::HandledEvent::Handlers.load_handlers
end
pw_from_command(cmd) click to toggle source
# File lib/chook/server/auth.rb, line 138
def self.pw_from_command(cmd)
  cmd = cmd.chomp '|'
  output = `#{cmd} 2>&1`.chomp
  raise "Can't get password from #{setting}: #{output}" unless $CHILD_STATUS.exitstatus.zero?
  output
end
pw_from_conf(setting) click to toggle source
# File lib/chook/server/auth.rb, line 126
def self.pw_from_conf(setting)
  return '' unless setting

  # if the path ends with a pipe, its a command that will
  # return the desired password, so remove the pipe,
  # execute it, and return stdout from it.
  return pw_from_command(setting) if setting.end_with? '|'

  # otherwise its a file path, and read the pw from the contents
  pw_from_file(setting)
end
pw_from_file(file) click to toggle source
# File lib/chook/server/auth.rb, line 145
def self.pw_from_file(file)
  file = Pathname.new file
  return nil unless file.file?
  stat = file.stat
  mode = format('%o', stat.mode)
  raise "Password file #{setting} has insecure mode, must be 0600." unless mode.end_with?('0600')
  raise "Password file #{setting} has insecure owner, must be owned by UID #{Process.euid}." unless stat.owned?
  # chomping an empty string removes all trailing \n's and \r\n's
  file.read.chomp('')
end
run!(log_level: nil) click to toggle source

Run the server

Calls superclass method
# File lib/chook/server.rb, line 55
def self.run!(log_level: nil)
  prep_to_run

  if Chook.config.use_ssl
    super do |server|
      server.ssl = true
      server.ssl_options = {
        cert_chain_file: Chook.config.ssl_cert_path.to_s,
        private_key_file: Chook.config.ssl_private_key_path.to_s,
        verify_peer: false
      }
    end # super do

  else # no ssl
    super
  end # if use ssl
end
starttime() click to toggle source
# File lib/chook/server.rb, line 98
def self.starttime
  @start_time
end
uptime() click to toggle source
# File lib/chook/server.rb, line 102
def self.uptime
  @start_time ? "#{humanize_secs(Time.now - @start_time)} ago" : 'Not Running'
end
webhooks_user_pw() click to toggle source

Learn the webhook or admin passwords from config. so we can authenticate them from the browser and the JSS

This is at the Server level, since we only need read it once per server startup, so we store it in a server instance var.

# File lib/chook/server/auth.rb, line 118
def self.webhooks_user_pw
  @webhooks_user_pw ||= pw_from_conf Chook.config.webhooks_user_pw
end