Class: Ridley::Client

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

Overview

Examples:

connection = Ridley::Client.new
connection.role.all

connection.role.find("reset") => Ridley::RoleResource.find(connection, "reset")

instantiating new resources

connection = Ridley::Connection.new
connection.role.new(name: "hello") => <#Ridley::RoleResource: @name="hello">

New instances of resources can be instantiated by calling new on the Ridley::Context. These messages
will be send to the Chef resource's class in Ridley and can be treated as a normal Ruby object. Each
instantiated object will have the connection information contained within so you can do things like
save a role after changing it's attributes.

r = connection.role.new(name: "new-role")
r.name => "new-role"
r.name = "other-name"
r.save

connection.role.find("new-role") => <#Ridley::RoleResource: @name="new-role">

Author:

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

  • :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


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ridley/client.rb', line 106

def initialize(options = {})
  log.info { "Ridley starting..." }
  @options = options.reverse_merge(
    ssh: Hash.new
  ).deep_symbolize_keys
  self.class.validate_options(@options)

  @ssh              = @options[:ssh]
  @validator_client = @options[:validator_client]

  @options[:client_key] = File.expand_path(@options[:client_key])

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

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

  unless @options[:client_key].present? && File.exist?(@options[:client_key])
    raise Errors::ClientKeyFileNotFound, "client key not found at: '#{@options[:client_key]}'"
  end

  super(Celluloid::Registry.new)
  pool(Ridley::Connection, size: 4, args: [
    @options[:server_url],
    @options[:client_name],
    @options[:client_key],
    @options.slice(*Connection::VALID_OPTIONS)
  ], as: :connection_pool)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)


241
242
243
244
245
246
247
248
249
250
251
# File 'lib/ridley/client.rb', line 241

def method_missing(method, *args, &block)
  if block_given?
    @self_before_instance_eval ||= eval("self", block.binding)
  end

  if @self_before_instance_eval.nil?
    super
  end

  @self_before_instance_eval.send(method, *args, &block)
end

Instance Attribute Details

#encrypted_data_bag_secret_pathObject

Returns the value of attribute encrypted_data_bag_secret_path


78
79
80
# File 'lib/ridley/client.rb', line 78

def encrypted_data_bag_secret_path
  @encrypted_data_bag_secret_path
end

#optionsObject (readonly)

Returns the value of attribute options


74
75
76
# File 'lib/ridley/client.rb', line 74

def options
  @options
end

#sshObject

Returns the value of attribute ssh


79
80
81
# File 'lib/ridley/client.rb', line 79

def ssh
  @ssh
end

#validator_clientObject

Returns the value of attribute validator_client


76
77
78
# File 'lib/ridley/client.rb', line 76

def validator_client
  @validator_client
end

#validator_pathObject

Returns the value of attribute validator_path


77
78
79
# File 'lib/ridley/client.rb', line 77

def validator_path
  @validator_path
end

Class Method Details

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


27
28
29
30
31
32
# File 'lib/ridley/client.rb', line 27

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

.validate_options(options) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ridley/client.rb', line 37

def validate_options(options)
  missing = (REQUIRED_OPTIONS - options.keys)

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

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

Instance Method Details

#clientRidley::ChainLink

Returns:


140
141
142
# File 'lib/ridley/client.rb', line 140

def client
  ChainLink.new(Actor.current, Ridley::ClientResource)
end

#connectionObject


233
234
235
# File 'lib/ridley/client.rb', line 233

def connection
  registry[:connection_pool]
end

#cookbookRidley::ChainLink

Returns:


145
146
147
# File 'lib/ridley/client.rb', line 145

def cookbook
  ChainLink.new(Actor.current, Ridley::CookbookResource)
end

#data_bagRidley::ChainLink

Returns:


150
151
152
# File 'lib/ridley/client.rb', line 150

def data_bag
  ChainLink.new(Actor.current, Ridley::DataBagResource)
end

#encrypted_data_bag_secretString?

The encrypted data bag secret for this connection.

Returns:

  • (String, nil)

Raises:


207
208
209
210
211
212
213
# File 'lib/ridley/client.rb', line 207

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::ChainLink

Returns:


155
156
157
# File 'lib/ridley/client.rb', line 155

def environment
  ChainLink.new(Actor.current, Ridley::EnvironmentResource)
end

#evaluate(&block) ⇒ Object Also known as: sync


219
220
221
222
223
224
225
226
# File 'lib/ridley/client.rb', line 219

def evaluate(&block)
  unless block_given?
    raise LocalJumpError, "no block given (yield)"
  end

  @self_before_instance_eval = eval("self", block.binding)
  instance_eval(&block)
end

#finalizeObject


229
230
231
# File 'lib/ridley/client.rb', line 229

def finalize
  connection.terminate if connection.alive?
end

#nodeRidley::ChainLink

Returns:


160
161
162
# File 'lib/ridley/client.rb', line 160

def node
  ChainLink.new(Actor.current, Ridley::NodeResource)
end

#roleRidley::ChainLink

Returns:


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

def role
  ChainLink.new(Actor.current, Ridley::RoleResource)
end

#sandboxRidley::ChainLink

Returns:


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

def sandbox
  ChainLink.new(Actor.current, Ridley::SandboxResource)
end

#search(index, query = nil, options = {}) ⇒ Hash

Creates an runs a new Ridley::Search

Parameters:

  • index (String, Symbol)
  • query (String, nil) (defaults to: nil)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sort (String)
  • :rows (Integer)
  • :start (Integer)

Returns:

  • (Hash)

See Also:


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

def search(index, query = nil, options = {})
  Ridley::Search.new(Actor.current, index, query, options).run
end

#search_indexesArray<Symbol, String>

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

Examples:

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

Returns:

  • (Array<Symbol, String>)

198
199
200
# File 'lib/ridley/client.rb', line 198

def search_indexes
  Ridley::Search.indexes(Actor.current)
end

#server_urlObject


215
216
217
# File 'lib/ridley/client.rb', line 215

def server_url
  self.url_prefix.to_s
end