Class: MotherBrain::Manifest

Inherits:
Hash
  • Object
show all
Defined in:
lib/mb/manifest.rb

Direct Known Subclasses

Bootstrap::Manifest, Provisioner::Manifest

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = Hash.new) ⇒ Manifest

Returns a new instance of Manifest.

Parameters:

  • attributes (Hash) (defaults to: Hash.new)

    (Hash.new)



38
39
40
41
42
# File 'lib/mb/manifest.rb', line 38

def initialize(attributes = Hash.new)
  if attributes && attributes.any?
    from_hash(attributes)
  end
end

Instance Attribute Details

#pathObject

return [String]



35
36
37
# File 'lib/mb/manifest.rb', line 35

def path
  @path
end

Class Method Details

.from_file(path) ⇒ Manifest

Parameters:

  • path (#to_s)

Returns:

Raises:



9
10
11
12
13
14
15
16
17
# File 'lib/mb/manifest.rb', line 9

def from_file(path)
  path = File.expand_path(path.to_s)
  data = File.read(path)
  obj = new.from_json(data)
  obj.path = path
  obj
rescue Errno::ENOENT
  raise ManifestNotFound, "No manifest found at: '#{path}'"
end

.from_hash(data) ⇒ Manifest

Parameters:

  • data (Hash)

Returns:



29
30
31
# File 'lib/mb/manifest.rb', line 29

def from_hash(data)
  new.from_hash(data)
end

.from_json(data) ⇒ Manifest

Parameters:

  • data (#to_s)

Returns:



22
23
24
# File 'lib/mb/manifest.rb', line 22

def from_json(data)
  new.from_json(data)
end

Instance Method Details

#from_hash(hash) ⇒ Manifest

Parameters:

  • hash (Hash)

Returns:



60
61
62
63
64
# File 'lib/mb/manifest.rb', line 60

def from_hash(hash)
  mass_assign(hash)

  self
end

#from_json(json, options = {}) ⇒ Manifest

Parameters:

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

    @see MultiJson.decode

Returns:

Raises:



51
52
53
54
55
# File 'lib/mb/manifest.rb', line 51

def from_json(json, options = {})
  from_hash(MultiJson.decode(json, options))
rescue MultiJson::DecodeError => error
  raise InvalidManifest, error
end

#node_countInteger

Returns the number of nodes expected to be created by this manifest regardless of type

Returns:

  • (Integer)


99
100
101
102
103
# File 'lib/mb/manifest.rb', line 99

def node_count
  node_groups.reduce(0) { |total, node|
    total + (node[:count] || 1)
  }
end

#node_groupsArray

Returns:

  • (Array)


87
88
89
# File 'lib/mb/manifest.rb', line 87

def node_groups
  self[:nodes] || []
end

#optionsHash

Returns:

  • (Hash)


92
93
94
# File 'lib/mb/manifest.rb', line 92

def options
  self[:options] || {}
end

#save(path = nil) ⇒ Manifest

Parameters:

  • path (String) (defaults to: nil)

Returns:

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mb/manifest.rb', line 71

def save(path = nil)
  self.path = path || self.path

  unless path.present?
    raise InternalError, "Cannot save manifest without a destination. Set the 'path' attribute on your object."
  end

  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'w+') do |file|
    file.write(MultiJson.dump(self, pretty: true))
  end

  self
end