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



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

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



61
62
63
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 61

def base_dir(nm="poolparty")
  @base_dir ||= "#{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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 153

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)
  hsh.delete(:requires)
  hsh.delete(:name) # we don't need the names in the methods
  hsh
end

#build_base_recipe_directory(nm) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 47

def build_base_recipe_directory(nm)
  ::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



39
40
41
42
43
44
45
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 39

def comp(cld_name, props, tabs)
  
  default_recipe = resources_to_string(props[:resources],tabs)
  ::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
# 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



171
172
173
174
175
176
177
178
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 171

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



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

def handle_print_service(plugin, tabs)
  if plugin && plugin.has_key?(:resources) && !plugin[:resources].empty?
    kname = plugin.delete(:name).to_s.gsub(/pool_party_/, '').gsub(/_class/, '')
    str = "\n#{tf(tabs)}# #{kname}\n"
    str << "#{tf(tabs)}"
    str << _compile(plugin,tabs+1, kname)
    str << "#{tf(tabs)}"
  end
  str
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
  nil
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



69
70
71
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 69

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

#resources_to_string(opts, tabs = 0) ⇒ Object



73
74
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 73

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}")}"
    when "plugin"
      handle_print_service(resource, tabs)
    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.compact.join("\n")
end

#tmp_pathObject



65
66
67
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 65

def tmp_path
  the_cloud ? the_cloud.tmp_path : "#{Default.tmp_path}/pool/cloud"
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

  • reloads - todo



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 184

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/poolparty/dependency_resolver/chef_resolver.rb', line 212

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
    case obj
    when /^\d{3}$/
      "0#{obj.to_i}"
    else
      "#{obj.to_i}"
    end        
  when String
    case obj
    when /^\d{4}$/
      "#{obj}"
    when /^\d{3}$/
      "0#{obj}"
    else
      "\"#{obj}\""
    end
  when Proc
    obj.call # eh
  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