Class: Ridley::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Celluloid, Logging
Defined in:
lib/ridley/client.rb

Defined Under Namespace

Classes: ConnectionSupervisor, ResourcesSupervisor

Constant Summary collapse

REQUIRED_OPTIONS =
[
  :server_url,
  :client_name,
  :client_key
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger, #logger, set_logger

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :server_url (String)

    URL to the Chef API

  • :client_name (String)

    name of the client used to authenticate with the Chef API

  • :client_key (String)

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

  • :validator_client (String) — default: nil
  • :validator_path (String) — default: nil
  • :encrypted_data_bag_secret_path (String) — default: nil
  • :ssh (Hash) — default: Hash.new
    • :user (String) a shell user that will login to each node and perform the bootstrap command on (required)

    • :password (String) the password for the shell user that will perform the bootstrap

    • :keys (Array, String) an array of keys (or a single key) to authenticate the ssh user with instead of a password

    • :timeout (Float) [5.0] timeout value for SSH bootstrap

    • :sudo (Boolean) [true] bootstrap with sudo

  • :winrm (Hash) — default: Hash.new
    • :user (String) a user that will login to each node and perform the bootstrap command on (required)

    • :password (String) the password for the user that will perform the bootstrap

    • :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985)

  • :chef_version (String)

    the version of Chef to use when bootstrapping

  • :params (Hash)

    URI query unencoded key/value pairs

  • :headers (Hash)

    unencoded HTTP header key/value pairs

  • :request (Hash)

    request options

  • :ssl (Hash)
    • :verify (Boolean) [true] set to false to disable SSL verification

  • :proxy (URI, String, Hash)

    URI, String, or Hash of HTTP proxy options

  • :pool_size (Integer) — default: 4

    size of the connection pool

Raises:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ridley/client.rb', line 129

def initialize(options = {})
  @options = options.reverse_merge(
    ssh: Hash.new,
    winrm: Hash.new,
    pool_size: 4
  ).deep_symbolize_keys
  self.class.validate_options(@options)

  @ssh              = @options[:ssh]
  @winrm            = @options[:winrm]
  @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] = 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

Instance Attribute Details

#chef_versionObject

Returns the value of attribute chef_version.



91
92
93
# File 'lib/ridley/client.rb', line 91

def chef_version
  @chef_version
end

#encrypted_data_bag_secret_pathObject

Returns the value of attribute encrypted_data_bag_secret_path.



88
89
90
# File 'lib/ridley/client.rb', line 88

def encrypted_data_bag_secret_path
  @encrypted_data_bag_secret_path
end

#optionsObject (readonly)

Returns the value of attribute options.



84
85
86
# File 'lib/ridley/client.rb', line 84

def options
  @options
end

#sshObject

Returns the value of attribute ssh.



89
90
91
# File 'lib/ridley/client.rb', line 89

def ssh
  @ssh
end

#validator_clientObject

Returns the value of attribute validator_client.



86
87
88
# File 'lib/ridley/client.rb', line 86

def validator_client
  @validator_client
end

#validator_pathObject

Returns the value of attribute validator_path.



87
88
89
# File 'lib/ridley/client.rb', line 87

def validator_path
  @validator_path
end

#winrmObject

Returns the value of attribute winrm.



90
91
92
# File 'lib/ridley/client.rb', line 90

def winrm
  @winrm
end

Class Method Details

.open(options = {}, &block) ⇒ Object



33
34
35
36
37
38
# File 'lib/ridley/client.rb', line 33

def open(options = {}, &block)
  client = new(options)
  yield client
ensure
  client.terminate if client && client.alive?
end

.validate_options(options) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ridley/client.rb', line 43

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

Instance Method Details

#clientRidley::ClientResource



166
167
168
# File 'lib/ridley/client.rb', line 166

def client
  @resources_registry[:client_resource]
end

#cookbookRidley::CookbookResource



171
172
173
# File 'lib/ridley/client.rb', line 171

def cookbook
  @resources_registry[:cookbook_resource]
end

#data_bagRidley::DataBagResource



176
177
178
# File 'lib/ridley/client.rb', line 176

def data_bag
  @resources_registry[:data_bag_resource]
end

#encrypted_data_bag_secretString?

The encrypted data bag secret for this connection.

Returns:

  • (String, nil)

Raises:



263
264
265
266
267
268
269
# File 'lib/ridley/client.rb', line 263

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

#environmentRidley::EnvironmentResource



181
182
183
# File 'lib/ridley/client.rb', line 181

def environment
  @resources_registry[:environment_resource]
end

#nodeRidley::NodeResource



186
187
188
# File 'lib/ridley/client.rb', line 186

def node
  @resources_registry[:node_resource]
end

#partial_search(index, query = nil, attributes = [], options = {}) ⇒ Array<ChefObject>, Hash

Perform a partial search the Chef Server. Partial objects or a smaller hash will be returned resulting in a faster response for larger response sets. Specify the attributes you want returned with the attributes parameter.

Examples:

ridley = Ridley.new(...)
ridley.partial_search(:node, "chef_environment:RESET", [ 'ipaddress', 'some.application.setting' ]) #=>
  [
    #<Ridley::NodeObject: chef_id:"reset.riotgames.com" normal:
      { "ipaddress" => "192.168.1.1", "some" => { "application" => { "setting" => "value" } } } ...>
  ]

Parameters:

  • index (#to_sym, #to_s)
  • query (#to_s) (defaults to: nil)
  • attributes (Array) (defaults to: [])

    an array of strings in dotted hash notation representing the attributes to return

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sort (String)

    a sort string such as ‘name DESC’

  • :rows (Integer)

    how many rows to return

  • :start (Integer)

    the result number to start from

Returns:



254
255
256
# File 'lib/ridley/client.rb', line 254

def partial_search(index, query = nil, attributes = [], options = {})
  @resources_registry[:search_resource].partial(index, query, Array(attributes), @resources_registry, options)
end

#roleRidley::RoleResource



191
192
193
# File 'lib/ridley/client.rb', line 191

def role
  @resources_registry[:role_resource]
end

#sandboxRidley::SandboxResource



196
197
198
# File 'lib/ridley/client.rb', line 196

def sandbox
  @resources_registry[:sandbox_resource]
end

#search(index, query = nil, options = {}) ⇒ Array<ChefObject>, Hash

Perform a search the Chef Server

Parameters:

  • index (#to_sym, #to_s)
  • query (#to_s) (defaults to: nil)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sort (String)

    a sort string such as ‘name DESC’

  • :rows (Integer)

    how many rows to return

  • :start (Integer)

    the result number to start from

Returns:



213
214
215
# File 'lib/ridley/client.rb', line 213

def search(index, query = nil, options = {})
  @resources_registry[:search_resource].run(index, query, @resources_registry, options)
end

#search_indexesArray<Symbol, String>

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

Examples:

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

Returns:

  • (Array<Symbol, String>)


225
226
227
# File 'lib/ridley/client.rb', line 225

def search_indexes
  @resources_registry[:search_resource].indexes
end

#server_urlObject



271
272
273
# File 'lib/ridley/client.rb', line 271

def server_url
  self.url_prefix.to_s
end