Class: CMDB::Rewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdb/rewriter.rb

Overview

Tool that visits every file in a hierarchy and rewrites its references to CMDB inputs.

References look like <<name.of.key>> and may be replaced with a scalar value or an array depending on the type of the input value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_dir) ⇒ Rewriter

Create a new shim to rewrite config files in a chosen dir and all subdirs. config_dir is transformed into an absolute path (if it isn’t already) before any rewrite operations occur.

Parameters:

  • config_dir (String, Pathname)


18
19
20
21
# File 'lib/cmdb/rewriter.rb', line 18

def initialize(config_dir)
  @dir = File.expand_path(config_dir)
  @rewriters = []
end

Instance Attribute Details

#missing_keysObject (readonly)

Returns the value of attribute missing_keys.



11
12
13
# File 'lib/cmdb/rewriter.rb', line 11

def missing_keys
  @missing_keys
end

Instance Method Details

#rewrite(cmdb) ⇒ Integer

Substitute CMDB input values into config files whenever a replacement token is encountered.

Parameters:

Returns:

  • (Integer)

    the number of variables replaced

Raises:

  • (Errno::ENOENT)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cmdb/rewriter.rb', line 27

def rewrite(cmdb)
  raise Errno::ENOENT.new(@dir) unless File.directory?(@dir)

  visit(@dir)
  total = 0
  @rewriters.each { |rw| total += rw.rewrite(cmdb) }

  @missing_keys = @rewriters.map(&:missing_keys).flatten.uniq.sort

  total
end

#saveObject



39
40
41
42
# File 'lib/cmdb/rewriter.rb', line 39

def save
  @rewriters.each(&:save)
  true
end