Class: Ridley::NodeResource

Inherits:
Resource show all
Defined in:
lib/ridley/resources/node_resource.rb

Overview

Author:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#<=>, #==, all, #chef_id, chef_id, chef_json_class, chef_type, create, delete, delete_all, #eql?, find, find!, #hash, #initialize, #reload, resource_path, #save, set_chef_id, set_chef_json_class, set_chef_type, set_resource_path, #to_s, update, #update

Constructor Details

This class inherits a constructor from Ridley::Resource

Class Method Details

.bootstrap(client, nodes, options = {}) ⇒ SSH::ResponseSet

Parameters:

  • client (Ridley::Client)
  • nodes (Array<String>, String)
  • ssh (Hash)
    • :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

Options Hash (options):

  • :validator_client (String)
  • :validator_path (String)

    filepath to the validator used to bootstrap the node (required)

  • :bootstrap_proxy (String)

    URL to a proxy server to bootstrap through (default: nil)

  • :encrypted_data_bag_secret_path (String)

    filepath on your host machine to your organizations encrypted data bag secret (default: nil)

  • :hints (Hash)

    a hash of Ohai hints to place on the bootstrapped node (default: Hash.new)

  • :attributes (Hash)

    a hash of attributes to use in the first Chef run (default: Hash.new)

  • :run_list (Array)

    an initial run list to bootstrap with (default: Array.new)

  • :chef_version (String)

    version of Chef to install on the node (default: CHEF_VERSION)

  • :environment (String)

    environment to join the node to (default: '_default')

  • :sudo (Boolean)

    bootstrap with sudo (default: true)

  • :template (String)

    bootstrap template to use (default: omnibus)

Returns:


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

def bootstrap(client, *args)
  options = args.extract_options!

  default_options = {
    server_url: client.server_url,
    validator_path: client.validator_path,
    validator_client: client.validator_client,
    encrypted_data_bag_secret_path: client.encrypted_data_bag_secret_path,
    ssh: client.ssh
  }

  options = default_options.merge(options)

  Bootstrapper.new(*args, options).run
end

.merge_data(client, target, options = {}) ⇒ Ridley::NodeResource

Merges the given data with the the data of the target node on the remote

Parameters:

  • client (Ridley::Client)
  • target (Ridley::NodeResource, String)

    node or identifier of the node to merge

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

    a customizable set of options

Options Hash (options):

  • :run_list (Array)

    run list items to merge

  • :attributes (Hash)

    attributes of normal precedence to merge

Returns:

Raises:


67
68
69
# File 'lib/ridley/resources/node_resource.rb', line 67

def merge_data(client, target, options = {})
  find!(client, target).merge_data(options)
end

Instance Method Details

#chef_client(options = {}) ⇒ SSH::Response

Run Chef-Client on the instantiated node

Parameters:

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

    a hash of options to pass to SSH.start

Returns:


200
201
202
203
204
205
206
207
# File 'lib/ridley/resources/node_resource.rb', line 200

def chef_client(options = {})
  options = client.ssh.merge(options)

  Ridley.log.debug "Running Chef Client on: #{self.public_hostname}"
  Ridley::SSH.start(self, options) do |ssh|
    ssh.run("sudo chef-client").first
  end
end

#cloud?Boolean

Returns true if the node is identified as a cloud node.

Returns:

  • (Boolean)

169
170
171
# File 'lib/ridley/resources/node_resource.rb', line 169

def cloud?
  self.automatic.has_key?(:cloud)
end

#cloud_providernil, String

Returns the cloud provider of the instantiated node. If the node is not identified as a cloud node, then nil is returned.

Examples:

node_1.cloud_provider => "eucalyptus"
node_2.cloud_provider => "ec2"
node_3.cloud_provider => "rackspace"
node_4.cloud_provider => nil

Returns:

  • (nil, String)

162
163
164
# File 'lib/ridley/resources/node_resource.rb', line 162

def cloud_provider
  self.cloud? ? self.automatic[:cloud][:provider] : nil      
end

#ec2?Boolean

Returns true if the node is identified as a cloud node using the ec2 provider.

Returns:

  • (Boolean)

183
184
185
# File 'lib/ridley/resources/node_resource.rb', line 183

def ec2?
  self.cloud_provider == "ec2"
end

#eucalyptus?Boolean

Returns true if the node is identified as a cloud node using the eucalyptus provider.

Returns:

  • (Boolean)

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

def eucalyptus?
  self.cloud_provider == "eucalyptus"
end

#merge_data(options = {}) ⇒ Ridley::NodeResource

Merges the instaniated nodes data with the given data and updates the remote with the merged results

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :run_list (Array)

    run list items to merge

  • :attributes (Hash)

    attributes of normal precedence to merge

Returns:


243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/ridley/resources/node_resource.rb', line 243

def merge_data(options = {})
  unless options[:run_list].nil?
    self.run_list = (self.run_list + Array(options[:run_list])).uniq
  end

  unless options[:attributes].nil?
    self.normal = self.normal.deep_merge(options[:attributes])
  end

  self.update
  self
end

#public_hostnameString

Returns the public hostname of the instantiated node. This hostname should be used for public communications to the node.

Examples:

node.public_hostname => "reset.riotgames.com"

Returns:

  • (String)

136
137
138
# File 'lib/ridley/resources/node_resource.rb', line 136

def public_hostname
  self.cloud? ? self.automatic[:cloud][:public_hostname] : self.automatic[:fqdn]
end

#public_ipv4String Also known as: public_ipaddress

Returns the public IPv4 address of the instantiated node. This ip address should be used for public communications to the node.

Examples:

node.public_ipv4 => "10.33.33.1"

Returns:

  • (String)

147
148
149
# File 'lib/ridley/resources/node_resource.rb', line 147

def public_ipv4
  self.cloud? ? self.automatic[:cloud][:public_ipv4] : self.automatic[:ipaddress]
end

#put_secret(options = {}) ⇒ SSH::Response?

Put the client's encrypted data bag secret onto the instantiated node. If no encrypted data bag key path is set on the resource's client then nil will be returned

Parameters:

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

    a hash of options to pass to SSH.start

Returns:


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ridley/resources/node_resource.rb', line 217

def put_secret(options = {})
  if client.encrypted_data_bag_secret_path.nil? ||
    !File.exists?(client.encrypted_data_bag_secret_path)

    return nil
  end

  options = client.ssh.merge(options)
  secret  = File.read(client.encrypted_data_bag_secret_path).chomp
  command = "echo '#{secret}' > /etc/chef/encrypted_data_bag_secret; chmod 0600 /etc/chef/encrypted_data_bag_secret"

  Ridley.log.debug "Writing Encrypted Data Bag Secret to: #{self.public_hostname}"
  Ridley::SSH.start(self, options) do |ssh|
    ssh.run(command).first
  end
end

#rackspace?Boolean

Returns true if the node is identified as a cloud node using the rackspace provider.

Returns:

  • (Boolean)

190
191
192
# File 'lib/ridley/resources/node_resource.rb', line 190

def rackspace?
  self.cloud_provider == "rackspace"
end

#set_chef_attribute(key, value) ⇒ Hashie::Mash

Note:

It is not possible to set any other attribute level on a node and have it persist after a Chef Run. This is because all other attribute levels are truncated at the start of a Chef Run.

Set a node level normal attribute given the dotted path representation of the Chef attribute and value.

Examples:

setting and saving a node level normal attribute


obj = node.find("jwinsor-1")
obj.set_chef_attribute("my_app.billing.enabled", false)
obj.save

Parameters:

  • key (String)
  • value (Object)

Returns:

  • (Hashie::Mash)

124
125
126
127
# File 'lib/ridley/resources/node_resource.rb', line 124

def set_chef_attribute(key, value)
  attr_hash = Hashie::Mash.from_dotted_path(key, value)
  self.normal = self.normal.deep_merge(attr_hash)
end