Class: Template::Create

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

Instance Method Summary collapse

Constructor Details

#initialize(profile_path, new_template_path) ⇒ Create

include Extras



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/template.rb', line 10

def initialize(profile_path, new_template_path)
  # Parse options
  params,files = parseopts(ARGV)

  # Log it
  @log   = Logger.new(STDOUT) 
  @log.level = params[:logger_level] || Logger::INFO
  if params[:logger_level] == Logger::DEBUG
    debug("Logger set to DEBUG")
  else
    info("Logger set to INFO")
  end
  
  # Init some class vars
  @new_template_path   = files[1] 
  @profile_path    = files[0]
  @keys      = Hash.new 
  @data_groups   = Hash.new 
  @data_groups["unknown"] = @current_group = []
  @cwd = File.expand_path File.dirname(__FILE__)
  

  # Kick back to stdout 
  debug("In init definition")
  info("Creating new template for #{@profile_path}")
  info("New template path #{@new_template_path}")

  # Run it 
  parse()
  debug(@data_groups)
  write()
end

Instance Method Details

#debug(message) ⇒ Object



129
130
131
# File 'lib/template.rb', line 129

def debug(message)
  @log.debug(message)
end

#error(message) ⇒ Object



124
125
126
127
# File 'lib/template.rb', line 124

def error(message)
  @log.error(message)
  abort 
end

#info(message) ⇒ Object



137
138
139
# File 'lib/template.rb', line 137

def info(message)
  @log.info(message)
end

#parseObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/template.rb', line 60

def parse
  info("Parsing #{@profile_path}")
  profile = File.open(@profile_path)

  # For each data grouop, parse the file
  profile.each_line do |line| 
    catch :unknown_group do 
      # If the line matches the data group, init a new array for the keys in that data group
      if line.match(/\#\[/)
        debug("Data group found on line: #{line}")
        data_group = line.split('[').last.delete(']').chomp
        info("Creating data group: #{data_group}") 
        @data_groups[data_group] = @current_group = []
        debug("Data groups now include: #{@data_groups}")

      # For all other lines, parse it for keys
      elsif line.match(/hiera\(/)
        debug("Current data group being added to: #{@current_group}")
        debug("Data item found: #{line}")
        data = line.split("(").last.chomp
        data.delete! (")")
        data.delete! ("\"")
        data.delete! ("'")
        data.delete! (",")
        
        @current_group << data
        
        info("Adding #{data}")
        debug("Adding #{data} to #{@current_group} hash")
      end
    end
  end
end

#parseopts(argv) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/template.rb', line 43

def parseopts(argv)
  params = {}
  parser = OptionParser.new 

  parser.on("-D") { 
    params[:logger_level] = Logger::DEBUG
  }

  parser.on("-s") { 
    params[:squeeze_extra_newlines] = true               
  }

  files = parser.parse(ARGV)

  [params,files]
end

#verify(path) ⇒ Object



118
119
120
121
122
# File 'lib/template.rb', line 118

def verify(path)
  unless path.absolute?
    error("Path #{path} must be absolute")
  end
end

#warn(message) ⇒ Object



133
134
135
# File 'lib/template.rb', line 133

def warn(message)
  @log.warn(message)
end

#writeObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/template.rb', line 94

def write
  # Create dotdir if needed:
  unless Dir.exists?("#{Dir.home}/.hiera-template")
    Dir.mkdir(File.join(Dir.home, '.hiera-template'), 0755)
  end
  unless Dir.exists?("#{Dir.home}/.hiera-template/templates")
    Dir.mkdir("#{Dir.home}/.hiera-template/templates", 0755)
  end

  temp_path = "#{Dir.home}/.hiera-template/templates"
  profile_name = @profile_path.split("/").last.split(".").first

  @data_groups.each do |k,v|
    write_hash = Hash.new
    v.each do |key|
      write_hash[key] = nil
    end
    info("Writing keys to new template #{temp_path}/#{profile_name}-#{k}-template.yaml")
    template = File.open("#{temp_path}/#{profile_name}-#{k}-template.yaml", "w")
    template.write(write_hash.to_yaml)
    template.close
  end
end