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:



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

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.



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

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:

  • the number of variables replaced

Raises:



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

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 { |rw| rw.missing_keys}.flatten.uniq.sort

  total
end

#saveObject



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

def save
  @rewriters.each { |rw| rw.save }
  true
end