Class: ConfigCurator::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/config_curator/collection.rb

Overview

Manages collections of units.

Examples:

Load a list of units and install them

Collection.new(manifest_path: 'manifest.yml').install

Constant Summary collapse

UNIT_TYPES =

Supported unit types.

%i(unit component config_file symlink)
UNIT_ATTRIBUTES =

The possible attributes specific to each unit type. This should not include generic attributes such as Unit#source and Unit#destination.

{
  unit: %i(hosts packages),
  component: %i(hosts packages fmode dmode owner group backend),
  config_file: %i(hosts packages fmode owner group),
  symlink: %i(hosts packages)
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest_path: nil, logger: nil) ⇒ Collection

Returns a new instance of Collection.



26
27
28
29
# File 'lib/config_curator/collection.rb', line 26

def initialize(manifest_path: nil, logger: nil)
  self.logger = logger unless logger.nil?
  load_manifest manifest_path unless manifest_path.nil?
end

Instance Attribute Details

#loggerLogger

Logger instance to use.

Returns:

  • (Logger)

    logger instance



33
34
35
# File 'lib/config_curator/collection.rb', line 33

def logger
  @logger
end

#manifestObject

Returns the value of attribute manifest.



24
25
26
# File 'lib/config_curator/collection.rb', line 24

def manifest
  @manifest
end

#unitsHash

Unit objects defined by the manifest and organized by type.

Returns:

  • (Hash)

    keys are pluralized unit types from UNIT_TYPES



49
50
51
# File 'lib/config_curator/collection.rb', line 49

def units
  @units
end

Instance Method Details

#create_unit(type, attributes: {}) ⇒ Unit

Creates a new unit object for the collection.

Parameters:

  • type (Symbol)

    a unit type in UNIT_TYPES

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

    attributes for the unit from UNIT_ATTRIBUTES

Returns:

  • (Unit)

    the unit object of the appropriate subclass



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/config_curator/collection.rb', line 93

def create_unit(type, attributes: {})
  "#{self.class.name.split('::').first}::#{type.to_s.camelize}".constantize
    .new(options: unit_options, logger: logger).tap do |unit|
    {src: :source, dst: :destination}.each do |k, v|
      unit.send "#{v}=".to_sym, attributes[k] unless attributes[k].nil?
    end

    UNIT_ATTRIBUTES[type].each do |v|
      unit.send "#{v}=".to_sym, defaults[v] unless defaults[v].nil?
      unit.send "#{v}=".to_sym, attributes[v] unless attributes[v].nil?
    end
  end
end

#installBoolean?

Installs all units from the manifest.

Returns:

  • (Boolean, nil)

    if units were installed or nil if fails mid-install



65
66
67
68
69
70
71
72
73
74
# File 'lib/config_curator/collection.rb', line 65

def install
  return false unless install? quiet: !(logger.level == Logger::DEBUG)

  UNIT_TYPES.each do |type|
    units[type.to_s.pluralize.to_sym].each do |unit|
      return nil unless install_unit(unit, type)
    end
  end
  true
end

#install?(quiet: false) ⇒ Boolean

Checks all units in the manifest for any detectable install issues.

Parameters:

  • quiet (Boolean) (defaults to: false)

    suppress some #logger output

Returns:

  • (Boolean)

    if units can be installed



79
80
81
82
83
84
85
86
87
# File 'lib/config_curator/collection.rb', line 79

def install?(quiet: false)
  result = true
  UNIT_TYPES.each do |type|
    units[type.to_s.pluralize.to_sym].each do |unit|
      result = install_unit?(unit, type, quiet) ? result : false
    end
  end
  result
end

#load_manifest(file) ⇒ Hash

Loads the manifest from file.

Parameters:

  • file (Hash)

    the yaml file to load

Returns:

  • (Hash)

    the loaded manifest



42
43
44
45
# File 'lib/config_curator/collection.rb', line 42

def load_manifest(file)
  self.manifest =
    ActiveSupport::HashWithIndifferentAccess.new YAML.load_file(file)
end