Class: PoolParty::ChefResolver

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

Instance Attribute Summary

Attributes inherited from DependencyResolver

#properties_hash, #the_cloud

Instance Method Summary collapse

Methods inherited from DependencyResolver

compile, #initialize, permitted_resource_options, #permitted_resource_options, #tf

Constructor Details

This class inherits a constructor from PoolParty::DependencyResolver

Instance Method Details

#_compile(props = @properties_hash, tabs = 0, default_namespace = "poolparty") ⇒ Object



35
36
37
38
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 35

def _compile(props=@properties_hash, tabs=0, default_namespace="poolparty")
  cld_name = default_namespace
  comp(cld_name, props, tabs)
end

#base_dir(nm = "poolparty") ⇒ Object



67
68
69
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 67

def base_dir(nm="poolparty")
  @base_dir ||= "#{Default.tmp_path}/dr_configure/chef/cookbooks/#{nm}"
end

#before_filter_check_on_hash(hsh, nm) ⇒ Object

Check if the hash has content and that the content exists here. This is used to provide a check



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 157

def before_filter_check_on_hash(hsh, nm)
  if hsh.has_key?(:content)
    cont = hsh.delete(:content)
    temp_file = "#{base_dir}/templates/default/#{nm}.erb"
    
    ::FileUtils.mkdir_p(::File.dirname(temp_file)) unless ::File.directory? temp_file
    ::File.open(temp_file, "w+") {|f| f.print cont }
    
    hsh.merge!({:source => "#{nm}.erb"})
  end
  # 
  hsh.delete(:require) if hsh.has_key?(:require)
  hsh.delete(:name) # we don't need the names in the methods
  hsh
end

#build_base_recipe_directory(nm) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 52

def build_base_recipe_directory(nm)
  dputs "Making new #{base_dir}"
  ::FileUtils.mkdir_p "#{base_dir}"
  
  [ "recipes", "templates", "attributes" ].each do |bdir|
    ::FileUtils.mkdir_p "#{base_dir}/#{bdir}" #unless ::File.directory? "#{base_dir}/#{bdir}"
  end
  
  ::File.open("#{base_dir}/attributes/poolparty.rb", "w") do |f| 
    f << "poolparty Mash.new unless attribute?('poolparty')\n"
  end
  
  base_dir
end

#comp(cld_name, props, tabs) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 40

def comp(cld_name, props, tabs)
  
  default_recipe = [ 
    resources_to_string(props[:resources],tabs),
    services_to_string(props[:services],tabs)
  ].join("\n")

  ::File.open("#{base_dir}/recipes/default.rb", "w+") {|f| f << default_recipe }
  
  default_recipe
end

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

Compile and add to the zipper



26
27
28
29
30
31
32
33
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 26

def compile(props=@properties_hash, tabs=0, default_namespace="poolparty")
  base_dir(default_namespace)
  build_base_recipe_directory( default_namespace )
  
  # ::Suitcase::Zipper.add( base_dir, "chef/cookbooks")
  
  _compile(props, tabs, default_namespace)
end

#handle_actions(key, value) ⇒ Object

Handle ensures



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

def handle_actions(key,value)
  case key
  when :ensures
    value.nil? ? nil : "action :#{value}"
  else
    nil
  end
end

#handle_chef_types(ty) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 110

def handle_chef_types(ty)
  case ty.to_sym
  when :exec
    "execute"
  when :file
    "template"
  when :symlink
    "link"
  when :line_in_file
    "execute"
  when :chef_deploy_definition
    "deploy"
  else
    ty
  end
end

#handle_print_service(klassname, klassarray, tabs) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 140

def handle_print_service(klassname, klassarray, tabs)
  case klassname
  when nil
    nil
  else
    kname = klassname.to_s.gsub(/pool_party_/, '').gsub(/_class/, '')
    str = "\n#{tf(tabs)}# #{kname}\n"
    str << "#{tf(tabs)}"
    klassarray.each do |hsh|
      str << _compile(hsh,tabs+1, klassname)
    end        
    str << "#{tf(tabs)}"
  end
end

#handle_print_variable(varhash) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 95

def handle_print_variable(varhash)
  o = []
  if varhash[:namespace]
    o << ["\n#{varhash[:namespace]} Mash.new unless attribute?('#{varhash[:namespace]}')"]
    o << "#{varhash[:namespace]}[:#{varhash[:name]}] = #{to_option_string(varhash[:value])}\n"
  else
    o << "poolparty[:#{varhash[:name]}] = #{to_option_string(varhash[:value])}"
  end
  ::File.open("#{base_dir}/attributes/poolparty.rb", "a+") do |f| 
    f << o.join("\n")
    f << "\n"
  end
  ""
end

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

flush out the hash into something meaningful



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 128

def hash_flush_out(hash, pre="", post="") 
  hash.map do |k,v|
    if o = handle_actions(k,v)
      o
    else
      key = to_chef_key(k)
      res = to_option_string(v)
      (key.nil? || res.nil?) ? nil : "#{pre}#{key} #{res}#{post}"
    end
  end
end

#options_to_string(opts, tabs = 0) ⇒ Object



71
72
73
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 71

def options_to_string(opts,tabs=0)
  handle_print_variables(opts) if opts
end

#resources_to_string(opts, tabs = 0) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 75

def resources_to_string(opts,tabs=0)
  out = []        
  out << opts.map do |resource|
    case ty = resource.delete(:pp_type)
    when "variable"
      handle_print_variable(resource)
    when "chef_recipe"
      "#{tf(tabs)}include_recipe #{to_option_string(resource.name)}"
    when "chef_library"
      "#{tf(tabs)}require #{to_option_string("/etc/chef/lib/#{resource.name}")}"
    else
      real_type = handle_chef_types(ty)
      real_name = resource[:name]
      res = before_filter_check_on_hash(resource, real_name)          
      "#{tf(tabs)}#{real_type} \"#{real_name}\" do\n#{tf(tabs+1)}#{hash_flush_out(res).compact.join("\n#{tf(tabs+1)}")}\n#{tf(tabs)}end"
    end
  end      
  out.join("\n")
end

#services_to_string(opts, tabs = 0) ⇒ Object

Turn the services into strings using the handle_print_service method Here we can strip out non-meaningful chef services



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 175

def services_to_string(opts,tabs=0)
  if opts
    str = ""
    [:control_statements, :conditional].each do |k|
      opts.delete(k)
    end
    opts.map do |klassname, klasshash|
      str << handle_print_service(klassname, klasshash, tabs)
    end
    str
  end
end

#to_chef_key(key) ⇒ Object

Take the keys from the resource hash and turn them into chef-like meaningful keys. This is how helpers are created for chef



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 200

def to_chef_key(key)
  case key
  when :ensures
    nil
  when :reloads
    "notifies :reload,"
  when :calls
    "notifies :run,"
  when :stops
    "notifies :stop,"
  when :starts
    "notifies :start,"
  when :enable
    nil
  when :if_not
    "not_if"
  when :not_if
    "not_if"
  when :only_if
    "only_if"
  else
    "#{key}"        
  end
end

#to_option_string(obj) ⇒ Object

Resolve the value of the resource hash into a meaningful chef value. Resources are turned into resource strings here



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 227

def to_option_string(obj)
  case obj
  when PoolParty::Resources::Resource
    "resources(:#{handle_chef_types(obj.class.to_s.top_level_class.downcase.to_sym)} => \"#{obj.name}\")"
  when Fixnum
    "#{obj.to_i}"
  when String
    "\"#{obj}\""
  when Array
    # If we are sending a notifies with a second argument
    if obj[1] && [:immediately, :delayed].include?(obj[1])
      "#{to_option_string(obj[0])}, :#{obj[1]}"
    else
      "[ #{obj.map {|e| to_option_string(e) }.reject {|a| a.nil? || a.empty? }.join(", ")} ]"
    end        
  when nil
    nil
  when Symbol
    ":#{obj}"
  when Hash
    "#{obj.map {|k,v| ":#{k} => #{to_option_string(v)}" unless v == obj }.compact.join(",\n")}"
  else
    "#{obj}"
  end
end