module Smith

FIXME. This needs to be either fixed up or removed. It dumps the output into the log at the moment which isn't the place for it. It should also be a smithctl command rather than an agency command.

This is a meta-command. It doesn't implement anything it simply sends messages to one or many existing commands to do the work.

Constants

VERSION

Public Class Methods

acl_cache_directory() click to toggle source

Return the acl cache path.

# File lib/smith.rb, line 69
def acl_cache_directory
  cache_directory.join('acl').tap do |path|
    check_path(path, true)
  end
end
acl_directories() click to toggle source
# File lib/smith.rb, line 60
def acl_directories
  config.agency.acl_directories
end
agent_directories() click to toggle source
# File lib/smith.rb, line 51
def agent_directories
  config.agency.agent_directories
end
cache_directory() click to toggle source
# File lib/smith.rb, line 64
def cache_directory
  Utils.check_and_create_directory(config.agency.cache_directory)
end
compile_acls() click to toggle source
# File lib/smith.rb, line 75
def compile_acls
  @compiler = ACLCompiler.new
  @compiler.compile
end
config() click to toggle source
# File lib/smith.rb, line 35
def config
  Config.get
end
config_path() click to toggle source
# File lib/smith.rb, line 39
def config_path
  Smith.config.path
end
connection() click to toggle source
# File lib/smith.rb, line 26
def connection
  raise RuntimeError, "You must run this in a Smith.start block" if @connection.nil?
  @connection
end
environment() click to toggle source
# File lib/smith.rb, line 31
def environment
  ENV['SMITH_ENV'] || 'development'
end
hostname() click to toggle source

Convenience method to get the hostname

# File lib/smith.rb, line 56
def hostname
  Socket.gethostname
end
lib_directories() click to toggle source
# File lib/smith.rb, line 47
def lib_directories
  config.agency.lib_directories
end
root_path() click to toggle source
# File lib/smith.rb, line 43
def root_path
  Pathname.new(__FILE__).dirname.parent.expand_path
end
running?() click to toggle source
# File lib/smith.rb, line 80
def running?
  EM.reactor_running?
end
shutdown_hook(&block) click to toggle source
# File lib/smith.rb, line 136
def shutdown_hook(&block)
  EM.add_shutdown_hook(&block)
end
start(opts={}, &block) click to toggle source
# File lib/smith.rb, line 84
def start(opts={}, &block)

  if EM.epoll? && Smith.config.eventmachine.epoll
    logger.debug { "Using epoll for I/O event notification." }
    EM.epoll
  end

  if EM.kqueue? && Smith.config.eventmachine.kqueue
    logger.debug { "Using kqueue for I/O event notification." }
    EM.kqueue
  end

  connection_settings = {
    :on_tcp_connection_failure => method(:tcp_connection_failure_handler),
    :on_possible_authentication_failure => method(:authentication_failure_handler)
  }

  AMQP.start(config.amqp.broker.uri.to_s, connection_settings) do |connection|
    @connection = connection

    connection.on_connection do
      logger.info { "Connected to: AMQP Broker: #{broker_identifier(connection)}" } unless opts[:quiet]
    end

    # FIXME This should go in the config.
    reconnection_delay = 5

    connection.on_tcp_connection_loss do |connection, settings|
      logger.info { "Reconnecting to AMQP Broker: #{broker_identifier(connection)} in #{reconnection_delay}s" }
      connection.reconnect(false, reconnection_delay)
    end

    connection.after_recovery do |connection|
      logger.info { "Connection with AMQP Broker restored: #{broker_identifier(connection)}" } unless opts[:quiet]
    end

    connection.on_error do |connection, connection_close|
      # If the broker is gracefully shutdown we get a 320. Log a nice message.
      if connection_close.reply_code == 320
        logger.warn { "AMQP Broker shutdown: #{broker_identifier(connection)}" }
      else
        logger.warn {  connection_close.reply_text }
      end
    end

    # This will be the last thing run by the reactor.
    shutdown_hook { logger.debug { "Reactor Stopped" } }

    block.call
  end
end
stop(immediately=false, &blk) click to toggle source
# File lib/smith.rb, line 140
def stop(immediately=false, &blk)
  shutdown_hook(&blk) if blk

  if running?
    if immediately
      EM.next_tick do
        @connection.close { EM.stop_event_loop }
      end
    else
      EM.add_timer(1) do
        @connection.close { EM.stop_event_loop }
      end
    end
  else
    logger.fatal { "Eventmachine is not running, exiting with prejudice" }
    exit!
  end
end

Private Class Methods

authentication_failure_handler(settings) click to toggle source
# File lib/smith.rb, line 174
def authentication_failure_handler(settings)
  # Only display the following settings.
  s = settings.select { |k,v| [:user, :pass, :vhost, :host].include?(k) }

  logger.fatal { "Authentication failure." }
  logger.info { "Details:" }
  s.each do |k,v|
    logger.info { " Setting: %-7s%s" %  [k, v] }
  end
  EM.stop
end
broker_identifier(connection) click to toggle source
# File lib/smith.rb, line 186
def broker_identifier(connection)
  broker = connection.broker.properties
  "amqp://#{connection.broker_endpoint}, (#{broker['product']}/v#{broker['version']}, #{broker['cluster_name']})"
end
check_path(path, create=false) click to toggle source
# File lib/smith.rb, line 191
def check_path(path, create=false)
  unless path.exist?
    error_message = "Path does not exist: #{path}"
    if create
      logger.info { "#{error_message}. Creating" }
      path.mkpath
    else
      logger.warn { error_message }
    end
  end
end
tcp_connection_failure_handler(settings) click to toggle source
# File lib/smith.rb, line 161
def tcp_connection_failure_handler(settings)
  # Only display the following settings.
  s = settings.select { |k,v| ([:user, :pass, :vhost, :host, :port, :ssl].include?(k)) }

  logger.fatal { "Cannot connect to the AMQP server." }
  logger.fatal { "Is the server running and are the connection details correct?" }
  logger.info { "Details:" }
  s.each do |k,v|
    logger.info { " Setting: %-7s%s" %  [k, v] }
  end
  EM.stop
end