Class: Chef::Provider::File

Inherits:
Chef::Provider show all
Extended by:
Deprecation::Warnings
Includes:
Deprecation::Provider::File, Mixin::Checksum, Mixin::EnforceOwnershipAndPermissions, Mixin::FileClass, Mixin::ShellOut, Util::Selinux
Defined in:
lib/chef/provider/file.rb,
lib/chef/provider/file/content.rb

Direct Known Subclasses

CookbookFile, Directory, RemoteFile, Template

Defined Under Namespace

Classes: Content

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#action, #current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Deprecation::Warnings

add_deprecation_warnings_for

Methods included from Deprecation::Provider::File

#backup, #compare_content, #deploy_tempfile, #diff_current, #diff_current_from_content, #is_binary?, #set_all_access_controls, #set_content, #setup_acl, #update_new_file_state

Methods included from Mixin::FileClass

#file_class

Methods included from Util::Selinux

#restore_security_context, #selinux_enabled?

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!

Methods included from Mixin::Checksum

#checksum

Methods included from Mixin::EnforceOwnershipAndPermissions

#access_controls, #enforce_ownership_and_permissions

Methods inherited from Chef::Provider

#action_nothing, #cleanup_after_converge, #converge_by, #cookbook_name, #events, #node, #process_resource_requirements, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, #whyrun_mode?

Methods included from DSL::Recipe

#method_missing

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

#initialize(new_resource, run_context) ⇒ File

Returns a new instance of File.



61
62
63
64
65
66
67
# File 'lib/chef/provider/file.rb', line 61

def initialize(new_resource, run_context)
  @content_class ||= Chef::Provider::File::Content
  if new_resource.respond_to?(:atomic_update)
    @deployment_strategy = Chef::FileContentManagement::Deploy.strategy(new_resource.atomic_update)
  end
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::DSL::Recipe

Instance Attribute Details

#deployment_strategyObject (readonly)

Returns the value of attribute deployment_strategy.



59
60
61
# File 'lib/chef/provider/file.rb', line 59

def deployment_strategy
  @deployment_strategy
end

Instance Method Details

#action_createObject



117
118
119
120
121
122
123
124
# File 'lib/chef/provider/file.rb', line 117

def action_create
  do_unlink
  do_create_file
  do_contents_changes
  do_acl_changes
  do_selinux
  load_resource_attributes_from_file(@new_resource)
end

#action_create_if_missingObject



126
127
128
129
130
131
132
# File 'lib/chef/provider/file.rb', line 126

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

#action_deleteObject



134
135
136
137
138
139
140
141
142
# File 'lib/chef/provider/file.rb', line 134

def action_delete
  if ::File.exists?(@new_resource.path)
    converge_by("delete file #{@new_resource.path}") do
      do_backup unless file_class.symlink?(@new_resource.path)
      ::File.delete(@new_resource.path)
      Chef::Log.info("#{@new_resource} deleted file at #{@new_resource.path}")
    end
  end
end

#action_touchObject



144
145
146
147
148
149
150
151
# File 'lib/chef/provider/file.rb', line 144

def action_touch
  action_create
  converge_by("update utime on file #{@new_resource.path}") do
    time = Time.now
    ::File.utime(time, time, @new_resource.path)
    Chef::Log.info("#{@new_resource} updated atime and mtime to #{time}")
  end
end

#define_resource_requirementsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chef/provider/file.rb', line 86

def define_resource_requirements
  # deep inside FAC we have to assert requirements, so call FACs hook to set that up
  access_controls.define_resource_requirements
  # Make sure the parent directory exists, otherwise fail.  For why-run assume it would have been created.
  requirements.assert(:create, :create_if_missing, :touch) do |a|
    parent_directory = ::File.dirname(@new_resource.path)
    a.assertion { ::File.directory?(parent_directory) }
    a.failure_message(Chef::Exceptions::EnclosingDirectoryDoesNotExist, "Parent directory #{parent_directory} does not exist.")
    a.whyrun("Assuming directory #{parent_directory} would have been created")
  end

  # Make sure the file is deletable if it exists, otherwise fail.
  if ::File.exists?(@new_resource.path)
    requirements.assert(:delete) do |a|
      a.assertion { ::File.writable?(@new_resource.path) }
      a.failure_message(Chef::Exceptions::InsufficientPermissions,"File #{@new_resource.path} exists but is not writable so it cannot be deleted")
    end
  end

  error, reason, whyrun_message = inspect_existing_fs_entry
  requirements.assert(:create) do |a|
    a.assertion { error.nil? }
    a.failure_message(error, reason)
    a.whyrun(whyrun_message)
    # Subsequent attempts to read the fs entry at the path (e.g., for
    # calculating checksums) could blow up, so give up trying to continue
    # why-running.
    a.block_action!
  end
end

#load_current_resourceObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/provider/file.rb', line 73

def load_current_resource
  # Let children resources override constructing the @current_resource
  @current_resource ||= Chef::Resource::File.new(@new_resource.name)
  @current_resource.path(@new_resource.path)
  if ::File.exists?(@current_resource.path) && ::File.file?(::File.realpath(@current_resource.path))
    if @action != :create_if_missing && @current_resource.respond_to?(:checksum)
      @current_resource.checksum(checksum(@current_resource.path))
    end
    load_resource_attributes_from_file(@current_resource)
  end
  @current_resource
end

Implementation components should follow symlinks when managing access control (e.g., use chmod instead of lchmod even if the path we’re managing is a symlink).

Returns:

  • (Boolean)


156
157
158
# File 'lib/chef/provider/file.rb', line 156

def manage_symlink_access?
  false
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/chef/provider/file.rb', line 69

def whyrun_supported?
  true
end