Class: PoolParty::PuppetResolver

Inherits:
DependencyResolver show all
Defined in:
lib/poolparty/dependency_resolver/puppet_resolver.rb

Instance Attribute Summary

Attributes inherited from DependencyResolver

#properties_hash, #the_cloud

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DependencyResolver

permitted_resource_options, #permitted_resource_options, #tf

Constructor Details

#initialize(hsh = nil) ⇒ PuppetResolver

Returns a new instance of PuppetResolver.



14
15
16
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 14

def initialize(hsh=nil)
  super(hsh)
end

Class Method Details

.compile(props) ⇒ Object



18
19
20
21
22
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 18

def self.compile(props)
  "class poolparty {
    #{new(props).compile}
  }"
end

Instance Method Details

#compile(props = @properties_hash, tabs = 0) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 24

def compile(props=@properties_hash, tabs=0)
  [ 
    # options_to_string(props[:options],tabs),
    resources_to_string(props[:resources],tabs),
    services_to_string(props[:services],tabs)
  ].join("\n")
end

#handle_print_resource(res, type, tabs) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 167

def handle_print_resource(res, type, tabs)
  case type.to_s        
  when "line_in_file"
    "#{tf(tabs)}exec { \"#{res[:file]}_line_#{tabs}\": \n#{tf(tabs+1)}command => '#{PoolParty::Resources::LineInFile.command(res[:line], res[:file])}',\n#{tf(tabs+1)}path => '/usr/local/bin:$PATH'\n#{tf(tabs)}}"
  else
    klasstype = case type.to_s
    when "directory"
      "file"
    when "symlink"
      "file"
    else
      type
    end
    res.merge!(:klasstype => type.to_s)
    "#{tf(tabs)}#{klasstype} { \"#{res.delete(:name) }\": #{res.empty? ? "" : "\n#{tf(tabs+1)}#{hash_flush_out(res.reject {|k,v| !permitted_option?(type, k) }).reject {|s| s.nil? }.join(",\n#{tf(tabs+1)}")}"}\n#{tf(tabs)}}"
  end
end

#handle_print_service(klassname, klassarray, tabs) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 138

def handle_print_service(klassname, klassarray, tabs)
  case klassname.to_s
  when "conditional"
    # "#{tf(tabs)}case $#{klasshash[:options][:variable]} {#{klasshash[:services][:control_statements].map do |k,v|"\n#{tf(tabs+1)}#{k} : {#{compile(v.to_properties_hash, tabs+2)}#{tf(tabs+1)}\n#{tf(tabs)}}" end}}"
    
    str = ""        
    klassarray.each do |klasshash|
      # str << "\n#{tf(tabs+1)}#{compile(hsh,tabs+1)}"
      str << "#{tf(tabs)}case $#{klasshash[:options][:variable]} {"
      str << "#{klasshash[:services][:control_statements].map do |k,v|"\n#{tf(tabs+1)}#{k} : {#{compile(v.to_properties_hash, tabs+2)}#{tf(tabs+1)}\n#{tf(tabs)}}" end}"
    end        
    str << "#{tf(tabs)}}"
    
  else
    kname = klassname.to_s.gsub(/pool_party_/, '').gsub(/_class/, '')
    str = "\n#{tf(tabs)}# #{kname}\n"
    str << "#{tf(tabs)}class #{kname} {"
    klassarray.each do |hsh|
      str << "\n#{tf(tabs+1)}#{compile(hsh,tabs+1)}"
    end        
    str << "#{tf(tabs)}} include #{kname}"
    
  end
end

#handle_print_variable(name, value, tabs) ⇒ Object



163
164
165
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 163

def handle_print_variable(name, value, tabs)
  "$#{name} = #{to_option_string(value)}"
end

#hash_flush_out(hash, pre = "", post = "") ⇒ Object



91
92
93
94
95
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 91

def hash_flush_out(hash, pre="", post="")
  setup_hash_for_output(hash).map do |k,v|
    hash.empty? ? nil : "#{pre}#{k} => #{v}#{post}"
  end
end

#option_type(ns = []) ⇒ Object

This is the method we use to turn the options into a string to build the main puppet manifests



187
188
189
190
191
192
193
194
195
196
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 187

def option_type(ns=[])
  a_template = (self =~ /template/) == 0
  a_service = self =~ /^[A-Z][a-zA-Z]*\[[a-zA-Z0-9\-\.\"\'_\$\{\}\/]*\]/i
  a_function = self =~/(.)*\((.)*\)(.)*/
  if is_a?(PoolParty::Resources::Resource)
    self.to_s
  else
    (a_service || a_template || a_function) ? self : "'#{self}'"
  end    
end

#options_to_string(opts, tabs = 0) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 32

def options_to_string(opts,tabs=0)
  opts.map do |k,v| 
    res = to_option_string(v)
    next unless res && !res.empty?
    "#{tf(tabs)}$#{k} = #{res}"
  end.join("\n") if opts
end

#permitted_option?(ty, key) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 74

def permitted_option?(ty, key)
  true
  # if permitted_resource_options.has_key?(ty)
  #   permitted_resource_options[ty].include?(key) || true #permitted_resource_options[:global].include?(key)
  # else
  #   true
  # end
end

#resources_to_string(opts, tabs = 0) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 40

def resources_to_string(opts,tabs=0)
  out = []
  if opts
    if opts.has_key?(:variable)
      vars = opts.delete(:variable)
      out << vars.map do |res, arr|
        handle_print_resource(res, :variable, tabs)
      end
    end

    out << opts.map do |type, arr|
      arr.map do |res|
        handle_print_resource(res, type, tabs)
      end
    end
  end
  out.join("\n")
end

#services_to_string(opts, tabs = 0) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 83

def services_to_string(opts,tabs=0)
  if opts
    opts.map do |klassname, klasshash|
      handle_print_service(klassname, klasshash, tabs)
    end
  end
end

#setup_hash_for_output(hsh) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 97

def setup_hash_for_output(hsh)
  ty = hsh.delete(:klasstype)
  if hsh.has_key?(:ensures)
    hsh.delete(:ensures)
    hsh[:ensure] = case ty.to_s
    when "directory"
      "directory"
    when "symlink"
      hsh[:source]
    else
      "present"
    end
  end
  new_hsh ={}
  hsh.each do |k,v|
    new_hsh.merge!({k => to_option_string(v)})
  end
  new_hsh
end

#to_option_string(obj) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 117

def to_option_string(obj)
  case obj
  when PoolParty::Resources::Resource
    case obj
    when PoolParty::Resources::Directory
      "File[\"#{obj.name}\"]"
    else
      "#{obj.class.to_s.top_level_class.capitalize}[\"#{obj.name}\"]"
    end        
  when Fixnum
    "#{obj}"
  when String
    obj = obj
    obj =~ /generate\(/ ? "#{obj}" : "\"#{obj.safe_quote}\""
  when Array
    "[ #{obj.map {|e| to_option_string(e) }.reject {|a| a.nil? || a.empty? }.join(", ")} ]"
  else
    "#{obj}"
  end
end