Class: Chef::Provider::Template

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

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #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_create_if_missing, #action_delete, #action_touch, #backup, #compare_content, #deploy_tempfile, #diff_current, #diff_current_from_content, #is_binary?, #load_current_resource_attrs, #set_all_access_controls, #set_content, #setup_acl, #update_new_file_state, #whyrun_supported?

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cleanup_after_converge, #converge, #cookbook_name, #events, #initialize, #node, #process_resource_requirements, #requirements, #resource_collection, #run_action, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::EnforceOwnershipAndPermissions

#access_controls, #enforce_ownership_and_permissions

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

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

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/provider/template.rb', line 49

def action_create
  render_with_context(template_location) do |rendered_template|
    rendered(rendered_template)
    update = ::File.exist?(@new_resource.path)
    if update && content_matches?
      Chef::Log.debug("#{@new_resource} content has not changed.")
      set_all_access_controls
    else
      description = [] 
      action_message = update ? "update #{@current_resource} from #{short_cksum(@current_resource.checksum)} to #{short_cksum(@new_resource.checksum)}" :
        "create #{@new_resource}"
      description << action_message
      description << diff_current(rendered_template.path)
      converge_by(description) do
        backup
        FileUtils.mv(rendered_template.path, @new_resource.path)
        Chef::Log.info("#{@new_resource} updated content")
        access_controls.set_all!
        stat = ::File.stat(@new_resource.path)

        # template depends on the checksum not changing, and updates it
        # itself later in the code, so we cannot set it here, as we do with
        # all other < File child provider classes
        @new_resource.owner(stat.uid)
        @new_resource.mode(stat.mode & 07777)
        @new_resource.group(stat.gid)
      end
    end
  end  
end

#content_matches?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/chef/provider/template.rb', line 102

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

#define_resource_requirementsObject



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

def define_resource_requirements
  super

  requirements.assert(:create, :create_if_missing) do |a| 
    a.assertion { ::File::exist?(template_location) } 
    a.failure_message "Template source #{template_location} could not be found."
    a.whyrun "Template source #{template_location} does not exist. Assuming it would have been created."
    a.block_action!
  end
end

#load_current_resourceObject



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

def load_current_resource
  @current_resource = Chef::Resource::Template.new(@new_resource.name)
  super
end

#rendered(rendered_template) ⇒ Object



96
97
98
99
100
# File 'lib/chef/provider/template.rb', line 96

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



92
93
94
# File 'lib/chef/provider/template.rb', line 92

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

#template_locationObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/provider/template.rb', line 81

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