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.



8
9
10
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 8

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

Class Method Details

.compile(props) ⇒ Object



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

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

Instance Method Details

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



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

def compile(props=@properties_hash, tabs=0)
  resources_to_string(props[:resources],tabs)
end

#handle_print_resource(res, type, tabs) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 133

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, plugin, tabs) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 121

def handle_print_service(klassname, plugin, tabs)
  kname = klassname.to_s.gsub(/pool_party_/, '').gsub(/_class/, '')
  str = "\n#{tf(tabs)}# #{kname}\n"
  str << "#{tf(tabs)}class #{kname} {"
  str << "\n#{tf(tabs+1)}#{compile(plugin,tabs+1)}"
  str << "#{tf(tabs)}} include #{kname}"        
end

#handle_print_variable(name, value, tabs) ⇒ Object



129
130
131
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 129

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

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



70
71
72
73
74
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 70

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 153

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



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

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)


66
67
68
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 66

def permitted_option?(ty, key)
  true
end

#resources_to_string(opts, tabs = 0) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 30

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

#setup_hash_for_output(hsh) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/poolparty/dependency_resolver/puppet_resolver.rb', line 76

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

  if hsh.has_key?(:requires)
    hsh[:require] = hsh.delete(:requires)
  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



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

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