class Enc::Runner

Public Class Methods

new(config) click to toggle source
# File lib/enc/runner.rb, line 5
def initialize(config)
  @config = config
  @cache = get_cache
end

Public Instance Methods

build(hostname) click to toggle source
# File lib/enc/runner.rb, line 10
def build(hostname)
  asset, from_collins = get_asset(hostname)
  # Do not rewrite the cache unless the asset comes from Collins.
  @cache.write(hostname, asset) if from_collins
  Enc::Builder.new(asset)
end

Protected Instance Methods

bail(msg, exception) click to toggle source
# File lib/enc/runner.rb, line 56
def bail(msg, exception)
  logger.fatal("#{msg}: #{exception.message}")
  logger.debug("Backtrace: #{exception.backtrace}")
  exit(2)
end
get_asset(hostname) click to toggle source
# File lib/enc/runner.rb, line 27
def get_asset(hostname)
  # It's important to handle any errors here so we know when to fall back to the cache.
  begin
    connection = Enc::CollinsHelper::Connection.new(@config)
    return connection.api.safe_find_exact(:hostname => hostname, :details => true), true
  rescue Enc::CollinsHelper::Api::CannotConnect,
    Collins::AuthenticationError,
    Collins::RequestError
    logger.error('Failed to connect to Collins. Attempting to read from cache')
  # If the asset does not exist in Collins, it's important to save an empty asset in the cache that way in the case
  # Collins is down, we will still return a valid, empty, response to puppet.
  rescue Enc::CollinsHelper::Api::NoAssets
    logger.info("Did not find any assets with hostname #{hostname}, saving an empty asset to the cache")
    return Enc::CollinsHelper::Node::NodeAsset.new, true
  rescue Enc::CollinsHelper::Api::AssetNotValid => e
    logger.error("Asset with hostname #{hostname} is not valid: #{e}, saving an empty asset to the cache")
    return Enc::CollinsHelper::Node::NodeAsset.new, true
  rescue Enc::CollinsHelper::Api::AssetNotConfigured => e
    bail("Asset with hostname #{hostname} is configured to use the ENC but does not have all the required tags", e)
  rescue Enc::CollinsHelper::Api::TooManyAssets => e
    bail("Too many assets with hostname #{hostname}", e)
  end
  begin
    return @cache.read(hostname), false
  rescue Enc::Cache::CacheDoesNotExist => e
    bail('Could not find asset anywhere', e)
  end
end
get_cache() click to toggle source
# File lib/enc/runner.rb, line 19
def get_cache
  begin
    return Enc::Cache::NodeCache.new(@config)
  rescue Enc::Cache::UnableToCreateCacheDirectory => e
    bail('Could access cache directory', e)
  end
end