Class: Ridley::NodeObject

Inherits:
ChefObject show all
Defined in:
lib/ridley/chef_objects/node_object.rb

Instance Method Summary collapse

Methods inherited from ChefObject

#<=>, #==, chef_id, #chef_id, chef_json_class, chef_type, #eql?, #hash, #initialize, #inspect, #reload, #save, set_chef_id, set_chef_json_class, set_chef_type, #update

Constructor Details

This class inherits a constructor from Ridley::ChefObject

Instance Method Details

#chef_attributeshashie::Mash

A merged hash containing a deep merge of all of the attributes respecting the node attribute precedence level.

Returns:

  • (hashie::Mash)


37
38
39
# File 'lib/ridley/chef_objects/node_object.rb', line 37

def chef_attributes
  default.merge(normal.merge(override.merge(automatic)))
end

#cloud?Boolean

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

Returns:

  • (Boolean)


131
132
133
# File 'lib/ridley/chef_objects/node_object.rb', line 131

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)


124
125
126
# File 'lib/ridley/chef_objects/node_object.rb', line 124

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)


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

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)


138
139
140
# File 'lib/ridley/chef_objects/node_object.rb', line 138

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

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

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:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ridley/chef_objects/node_object.rb', line 165

def merge_data(options = {})
  new_run_list   = Array(options[:run_list])
  new_attributes = options[:attributes]

  unless new_run_list.empty?
    self.run_list = self.run_list | new_run_list
  end

  unless new_attributes.nil?
    self.normal = self.normal.deep_merge(new_attributes)
  end

  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)


98
99
100
# File 'lib/ridley/chef_objects/node_object.rb', line 98

def public_hostname
  self.cloud? ? self.automatic[:cloud][:public_hostname] || self.automatic[:fqdn] : 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)


109
110
111
# File 'lib/ridley/chef_objects/node_object.rb', line 109

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

#rackspace?Boolean

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

Returns:

  • (Boolean)


152
153
154
# File 'lib/ridley/chef_objects/node_object.rb', line 152

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)

    dotted path to key to be unset

  • value (Object)

Returns:

  • (Hashie::Mash)


58
59
60
61
# File 'lib/ridley/chef_objects/node_object.rb', line 58

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

#unset_chef_attribute(key) ⇒ Hashie::Mash

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

Examples:

unsetting and saving a node level normal attribute


obj = node.find("foonode")
obj.unset_chef_attribute("my_app.service_one.service_state")
obj.save

Parameters:

  • key (String)

    dotted path to key to be unset

Returns:

  • (Hashie::Mash)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ridley/chef_objects/node_object.rb', line 76

def unset_chef_attribute(key)
  keys = key.split(".")
  leaf_key = keys.pop
  attributes = keys.inject(self.normal) do |attributes, key|
    if attributes[key] && attributes[key].kind_of?(Hashie::Mash)
      attributes = attributes[key]
    else 
      return self.normal
    end
  end

  attributes.delete(leaf_key)
  return self.normal
end