Class: Enc::CollinsHelper::Node::NodeAsset

Inherits:
Collins::Asset
  • Object
show all
Defined in:
lib/enc/collins_helper/node/node_asset.rb

Constant Summary collapse

VALID_ENC_FLAGS =

Note: Do not include the logging module here. If the logging module loads a file handle, the Marshal.dump will fail horribly. This class is meant to be used as a resource.

%w(1 true yes y on)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_json(json_hash, bare_asset = false) ⇒ Object

Raises:

  • (Collins::CollinsError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/enc/collins_helper/node/node_asset.rb', line 16

def from_json(json_hash, bare_asset=false)
  raise Collins::CollinsError, 'Invalid JSON specified for Asset.from_json' if
      (json_hash.nil? || !json_hash.is_a?(Hash))
  json = deep_copy_hash(json_hash)
  json = json['data'] && json['data'] || json
  if bare_asset or !json.include?('ASSET')
    asset = NodeAsset.new json
  else
    asset = NodeAsset.new json.delete('ASSET')
  end
  asset.send('ipmi='.to_sym, Collins::Ipmi.from_json(json.delete('IPMI')))
  asset.send('addresses='.to_sym, Collins::Address.from_json(json.delete('ADDRESSES')))
  asset.send('power='.to_sym, Collins::Power.from_json(json.delete('POWER')))
  asset.send('location=', json.delete('LOCATION'))
  asset.send('extras=', json)
  asset
end

Instance Method Details

#get_datacenterObject



77
78
79
80
81
82
83
84
# File 'lib/enc/collins_helper/node/node_asset.rb', line 77

def get_datacenter
  begin
    value = get_safe_attribute('PUPPET_DATACENTER')
  rescue KeyError
    raise NoDatacenter, 'The node does not have a puppet_datacenter tag set'
  end
  value
end

#get_deployment_environmentObject



68
69
70
71
72
73
74
75
# File 'lib/enc/collins_helper/node/node_asset.rb', line 68

def get_deployment_environment
  begin
    value = get_safe_attribute('ENVIRONMENT')
  rescue KeyError
    raise NoDeploymentEnvironment, 'The node does not have an environment tag set'
  end
  value
end

#get_flattened_attributesObject

We need to take all the attributes and make them more human-readable. TODO: Make it not so ugly.



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/enc/collins_helper/node/node_asset.rb', line 109

def get_flattened_attributes
  flattened_attributes = Hash.new
  Hash[extract(extras, 'ATTRIBS').inject({}){ |hash, (k, v)| hash.merge(k.to_i => v) }.
    sort_by {|k,_| k} ].each { |_,hash| hash.each {
      |k,v| flattened_attributes.has_key?(k) && (flattened_attributes[k].is_a?(Array) &&
        (flattened_attributes[k] << (v)) ||
        (flattened_attributes[k] = [flattened_attributes[k], v])) ||
        flattened_attributes[k] = v
    }
  }
  flattened_attributes
end

#get_hostnameObject



59
60
61
62
63
64
65
66
# File 'lib/enc/collins_helper/node/node_asset.rb', line 59

def get_hostname
  begin
    value = get_safe_attribute('HOSTNAME')
  rescue KeyError
    raise NoHostname, 'The node does not have a hostname tag set'
  end
  value
end

#get_multi_attribute(attribute) ⇒ Object



133
134
135
# File 'lib/enc/collins_helper/node/node_asset.rb', line 133

def get_multi_attribute(attribute)
  get_multi_attribute_with_index(attribute).values
end

#get_multi_attribute_with_index(attribute) ⇒ Object



128
129
130
131
# File 'lib/enc/collins_helper/node/node_asset.rb', line 128

def get_multi_attribute_with_index(attribute)
  Hash[extract(extras, 'ATTRIBS').inject({}){ |hash, (k, v)| hash.merge(k.to_i => v[attribute]) if
      v.has_key?(attribute) }.sort_by {|k,_| k} ]
end

#get_puppet_environmentObject



86
87
88
89
90
91
92
93
# File 'lib/enc/collins_helper/node/node_asset.rb', line 86

def get_puppet_environment
  begin
    value = get_safe_attribute('PUPPET_ENVIRONMENT')
  rescue KeyError
    value = 'production'
  end
  value
end

#get_rolesObject



95
96
97
# File 'lib/enc/collins_helper/node/node_asset.rb', line 95

def get_roles
  get_multi_attribute('CONFIGURATION_ROLES')
end

#get_roles_as_stringObject



99
100
101
# File 'lib/enc/collins_helper/node/node_asset.rb', line 99

def get_roles_as_string
  get_roles.join(',')
end

#get_roles_by_indexObject



103
104
105
# File 'lib/enc/collins_helper/node/node_asset.rb', line 103

def get_roles_by_index
  get_multi_attribute_with_index('CONFIGURATION_ROLES')
end

#get_safe_attribute(attribute) ⇒ Object

Raises:

  • (KeyError)


122
123
124
125
126
# File 'lib/enc/collins_helper/node/node_asset.rb', line 122

def get_safe_attribute(attribute)
  value = get_attribute(attribute)
  raise KeyError unless value
  value
end

#has_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/enc/collins_helper/node/node_asset.rb', line 50

def has_attribute?(attribute)
  begin
    get_safe_attribute(attribute)
  rescue KeyError
    return false
  end
  true
end

#is_valid?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/enc/collins_helper/node/node_asset.rb', line 35

def is_valid?
  begin
    get_hostname
    get_deployment_environment
    get_datacenter
  rescue NoHostname, NoDeploymentEnvironment, NoDatacenter
    return false
  end
  true
end

#uses_enc?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/enc/collins_helper/node/node_asset.rb', line 46

def uses_enc?
  has_attribute?('PUPPET_ENC') && VALID_ENC_FLAGS.include?(get_attribute('PUPPET_ENC').to_s.downcase)
end