Class: Chef::Provider::CookbookFile

Inherits:
File show all
Defined in:
lib/chef/provider/cookbook_file.rb

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

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 included from Mixin::Checksum

#checksum

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chef/provider/cookbook_file.rb', line 35

def action_create
  if file_cache_location && content_stale?
    Chef::Log.debug("content of file #{@new_resource.path} requires update")
    backup_new_resource
    Tempfile.open(::File.basename(@new_resource.name)) do |staging_file|
      Chef::Log.debug("staging #{file_cache_location} to #{staging_file.path}")
      staging_file.close
      stage_file_to_tmpdir(staging_file.path)
      FileUtils.mv(staging_file.path, @new_resource.path)
    end
    @new_resource.updated_by_last_action(true)
  else
    set_all_access_controls(@new_resource.path)
  end
  @new_resource.updated_by_last_action?
end

#action_create_if_missingObject



52
53
54
55
56
57
58
# File 'lib/chef/provider/cookbook_file.rb', line 52

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

#backup_new_resourceObject



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

def backup_new_resource
  if ::File.exists?(@new_resource.path)
    Chef::Log.info "Backing up current file at #{@new_resource.path}"
    backup @new_resource.path
  end
end

#content_stale?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/chef/provider/cookbook_file.rb', line 94

def content_stale?
  ( ! ::File.exist?(@new_resource.path)) || ( ! compare_content)
end

#file_cache_locationObject



60
61
62
63
64
65
# File 'lib/chef/provider/cookbook_file.rb', line 60

def file_cache_location
  @file_cache_location ||= begin
    cookbook = run_context.cookbook_collection[resource_cookbook]
    cookbook.preferred_filename_on_disk_location(node, :files, @new_resource.source, @new_resource.path)
  end
end

#load_current_resourceObject



27
28
29
30
31
32
# File 'lib/chef/provider/cookbook_file.rb', line 27

def load_current_resource
  @current_resource = Chef::Resource::CookbookFile.new(@new_resource.name)
  @new_resource.path.gsub!(/\\/, "/") # for Windows
  @current_resource.path(@new_resource.path)
  @current_resource
end

#resource_cookbookObject

Determine the cookbook to get the file from. If new resource sets an explicit cookbook, use it, otherwise fall back to the implicit cookbook i.e., the cookbook the resource was declared in.



70
71
72
# File 'lib/chef/provider/cookbook_file.rb', line 70

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

#set_all_access_controls(file) ⇒ Object



81
82
83
84
85
# File 'lib/chef/provider/cookbook_file.rb', line 81

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

#stage_file_to_tmpdir(staging_file_location) ⇒ Object

Copy the file from the cookbook cache to a temporary location and then set its file access control settings.



76
77
78
79
# File 'lib/chef/provider/cookbook_file.rb', line 76

def stage_file_to_tmpdir(staging_file_location)
  FileUtils.cp(file_cache_location, staging_file_location)
  set_all_access_controls(staging_file_location)
end