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_ATTRIBUTESS =

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),
  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.



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

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

Instance Attribute Details

#loggerLogger

Logger instance to use.

Returns:

  • (Logger)

    logger instance



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

def logger
  @logger
end

#manifestObject

Returns the value of attribute manifest.



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

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_ATTRIBUTESS

Returns:

  • (Unit)

    the unit object of the appropriate subclass



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/config_curator/collection.rb', line 112

def create_unit type, attributes: {}
  options = {}
  i(root package_tool).each do |k|
    options[k] = manifest[k] unless manifest[k].nil?
  end if manifest

  "#{self.class.name.split('::').first}::#{type.to_s.camelize}".constantize
  .new(options: 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_ATTRIBUTESS[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 when fails mid-install



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/config_curator/collection.rb', line 66

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

  UNIT_TYPES.each do |t|
    type_name = t.to_s.humanize capitalize: false

    units[t.to_s.pluralize.to_sym].each do |unit|
      begin
        if unit.install
          logger.info { "Installed #{type_name}: #{unit.source} ⇨ #{unit.destination_path}" }
        end
      rescue Unit::InstallFailed => e
        logger.fatal { "Halting install! Install attempt failed for #{type_name}: #{e}" }
        return nil
      end
    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



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

def install? quiet: false
  result = true
  UNIT_TYPES.each do |t|
    type_name = t.to_s.humanize capitalize: false

    units[t.to_s.pluralize.to_sym].each do |unit|
      begin
        if unit.install?
          logger.info { "Testing install for #{type_name}: #{unit.source} ⇨ #{unit.destination_path}" }
        end unless quiet
      rescue Unit::InstallFailed => e
        result = false
        logger.error { "Cannot install #{type_name}: #{e}" }
      end
    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



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

def load_manifest file
  self.manifest = YAML.load_file file
end