Class: Chef::Provider::Template

Inherits:
File show all
Includes:
Mixin::Checksum, Mixin::Template
Defined in:
lib/chef/provider/template.rb

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::Template

#render_template

Methods included from Mixin::Checksum

#checksum

Methods inherited from File

#action_delete, #action_touch, #backup, #compare_content, #compare_group, #compare_mode, #compare_owner, #set_content, #set_group, #set_mode, #set_owner

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cookbook_name, #initialize, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

This class inherits a constructor from Chef::Provider

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Method Details

#action_createObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chef/provider/template.rb', line 38

def action_create
  render_with_context(template_location) do |rendered_template|
    rendered(rendered_template)
    if ::File.exist?(@new_resource.path) && content_matches?
      Chef::Log.debug("#{@new_resource} content has not changed.")
      set_all_access_controls(@new_resource.path)
    else
      backup
      set_all_access_controls(rendered_template.path)
      FileUtils.mv(rendered_template.path, @new_resource.path)
      Chef::Log.info("#{@new_resource} updated content")
      @new_resource.updated_by_last_action(true)
    end
  end
end

#action_create_if_missingObject



54
55
56
57
58
59
60
# File 'lib/chef/provider/template.rb', line 54

def action_create_if_missing
  if ::File.exists?(@new_resource.path)
    Chef::Log.debug("#{@new_resource} exists - taking no action")
  else
    action_create
  end
end

#content_matches?Boolean



83
84
85
# File 'lib/chef/provider/template.rb', line 83

def content_matches?
  @current_resource.checksum == @new_resource.checksum
end

#load_current_resourceObject



33
34
35
36
# File 'lib/chef/provider/template.rb', line 33

def load_current_resource
  super
  @current_resource.checksum(checksum(@current_resource.path)) if ::File.exist?(@current_resource.path)
end

#rendered(rendered_template) ⇒ Object



77
78
79
80
81
# File 'lib/chef/provider/template.rb', line 77

def rendered(rendered_template)
  @new_resource.checksum(checksum(rendered_template.path))
  Chef::Log.debug("Current content's checksum:  #{@current_resource.checksum}")
  Chef::Log.debug("Rendered content's checksum: #{@new_resource.checksum}")
end

#resource_cookbookObject



73
74
75
# File 'lib/chef/provider/template.rb', line 73

def resource_cookbook
  @new_resource.cookbook || @new_resource.cookbook_name
end

#set_all_access_controls(file) ⇒ Object



87
88
89
90
91
# File 'lib/chef/provider/template.rb', line 87

def set_all_access_controls(file)
  access_controls = Chef::FileAccessControl.new(@new_resource, file)
  access_controls.set_all
  @new_resource.updated_by_last_action(access_controls.modified?)
end

#template_locationObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/provider/template.rb', line 62

def template_location
  @template_file_cache_location ||= begin
    if @new_resource.local
      @new_resource.source
    else
      cookbook = run_context.cookbook_collection[resource_cookbook]
      cookbook.preferred_filename_on_disk_location(node, :templates, @new_resource.source)
    end
  end
end