class Ridley::Client

Constants

REQUIRED_OPTIONS

Attributes

chef_version[RW]
encrypted_data_bag_secret_path[RW]
options[R]
validator_client[RW]
validator_path[RW]

Public Class Methods

new(options = {}) click to toggle source

@option options [String] :server_url

URL to the Chef API

@option options [String] :client_name

name of the client used to authenticate with the Chef API

@option options [String] :client_key

filepath to the client's private key used to authenticate with the Chef API

@option options [String] :validator_client (nil) @option options [String] :validator_path (nil) @option options [String] :encrypted_data_bag_secret_path (nil) @option options [String] :chef_version

the version of Chef to use when bootstrapping

@option options [Hash] :params

URI query unencoded key/value pairs

@option options [Hash] :headers

unencoded HTTP header key/value pairs

@option options [Hash] :request

request options

@option options [Hash] :ssl

* :verify (Boolean) [true] set to false to disable SSL verification

@option options [URI, String, Hash] :proxy

URI, String, or Hash of HTTP proxy options

@option options [Integer] :pool_size (4)

size of the connection pool

@raise [Errors::ClientKeyFileNotFoundOrInvalid] if the option for :client_key does not contain

a file path pointing to a readable client key, or is a string containing a valid key
# File lib/ridley/client.rb, line 120
def initialize(options = {})
  @options = options.reverse_merge(
    pool_size: 4
  ).deep_symbolize_keys
  self.class.validate_options(@options)

  @chef_version     = @options[:chef_version]
  @validator_client = @options[:validator_client]

  if @options[:validator_path]
    @validator_path = File.expand_path(@options[:validator_path])
  end

  @options[:encrypted_data_bag_secret] ||= begin
    if @options[:encrypted_data_bag_secret_path]
      @encrypted_data_bag_secret_path = File.expand_path(@options[:encrypted_data_bag_secret_path])
    end

    encrypted_data_bag_secret
  end

  unless verify_client_key(@options[:client_key])
    @options[:client_key] = @options[:client_key].call if @options[:client_key].kind_of? Proc
    @options[:client_key] = File.expand_path(@options[:client_key])
    raise Errors::ClientKeyFileNotFoundOrInvalid, "client key is invalid or not found at: '#{@options[:client_key]}'" unless File.exist?(@options[:client_key]) && verify_client_key(::IO.read(@options[:client_key]))
  end

  @connection_registry   = Celluloid::Registry.new
  @resources_registry    = Celluloid::Registry.new
  @connection_supervisor = ConnectionSupervisor.new(@connection_registry, @options)
  @resources_supervisor  = ResourcesSupervisor.new(@resources_registry, @connection_registry, @options)
end
open(options = {}) { |client| ... } click to toggle source
# File lib/ridley/client.rb, line 36
def open(options = {}, &block)
  client = new(options)
  yield client
ensure
  client.terminate if client && client.alive?
end
validate_options(options) click to toggle source

@raise [ArgumentError]

@return [Boolean]

# File lib/ridley/client.rb, line 46
def validate_options(options)
  missing = (REQUIRED_OPTIONS - options.keys)

  if missing.any?
    missing.collect! { |opt| "'#{opt}'" }
    raise ArgumentError, "Missing required option(s): #{missing.join(', ')}"
  end

  missing_values = options.slice(*REQUIRED_OPTIONS).select { |key, value| !value.present? }
  if missing_values.any?
    values = missing_values.keys.collect { |opt| "'#{opt}'" }
    raise ArgumentError, "Missing value for required option(s): '#{values.join(', ')}'"
  end
end

Public Instance Methods

client() click to toggle source

@return [Ridley::ClientResource]

# File lib/ridley/client.rb, line 154
def client
  @resources_registry[:client_resource]
end
cookbook() click to toggle source

@return [Ridley::CookbookResource]

# File lib/ridley/client.rb, line 159
def cookbook
  @resources_registry[:cookbook_resource]
end
data_bag() click to toggle source

@return [Ridley::DataBagResource]

# File lib/ridley/client.rb, line 164
def data_bag
  @resources_registry[:data_bag_resource]
end
encrypted_data_bag_secret() click to toggle source

The encrypted data bag secret for this connection.

@raise [Ridley::Errors::EncryptedDataBagSecretNotFound]

@return [String, nil]

# File lib/ridley/client.rb, line 262
def encrypted_data_bag_secret
  return nil if encrypted_data_bag_secret_path.nil?

  ::IO.read(encrypted_data_bag_secret_path).chomp
rescue Errno::ENOENT => e
  raise Errors::EncryptedDataBagSecretNotFound, "Encrypted data bag secret provided but not found at '#{encrypted_data_bag_secret_path}'"
end
environment() click to toggle source

@return [Ridley::EnvironmentResource]

# File lib/ridley/client.rb, line 169
def environment
  @resources_registry[:environment_resource]
end
node() click to toggle source

@return [Ridley::NodeResource]

# File lib/ridley/client.rb, line 174
def node
  @resources_registry[:node_resource]
end
role() click to toggle source

@return [Ridley::RoleResource]

# File lib/ridley/client.rb, line 179
def role
  @resources_registry[:role_resource]
end
sandbox() click to toggle source

@return [Ridley::SandboxResource]

# File lib/ridley/client.rb, line 184
def sandbox
  @resources_registry[:sandbox_resource]
end
search_indexes() click to toggle source

Return an array of all possible search indexes for the including connection

@example

ridley = Ridley.new(...)
ridley.search_indexes #=>
  [:client, :environment, :node, :role, :"ridley-two", :"ridley-one"]

@return [Array<Symbol, String>]

# File lib/ridley/client.rb, line 218
def search_indexes
  @resources_registry[:search_resource].indexes
end
server_url() click to toggle source
# File lib/ridley/client.rb, line 270
def server_url
  self.url_prefix.to_s
end
universe() click to toggle source
# File lib/ridley/client.rb, line 251
def universe
  connection.send(:get, "universe").body
rescue Errors::HTTPError, Errors::ClientError => ex
  abort(ex)
end
user() click to toggle source

@return [Ridley::UserResource]

# File lib/ridley/client.rb, line 189
def user
  @resources_registry[:user_resource]
end

Private Instance Methods

connection() click to toggle source
# File lib/ridley/client.rb, line 283
def connection
  @connection_registry[:connection_pool]
end
finalize_callback() click to toggle source
# File lib/ridley/client.rb, line 287
def finalize_callback
  @connection_supervisor.async.terminate if @connection_supervisor
  @resources_supervisor.async.terminate if @resources_supervisor
end
verify_client_key(key) click to toggle source
# File lib/ridley/client.rb, line 276
def verify_client_key(key)
  OpenSSL::PKey::RSA.new(key)
  true
rescue
  false
end