Class: Chef::Provider::File

Inherits:
Chef::Provider show all
Includes:
Mixin::Checksum, Mixin::EnforceOwnershipAndPermissions, Mixin::FileClass, 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

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#action, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods included from Mixin::FileClass

#file_class

Methods included from Util::Selinux

#restore_security_context, #selinux_enabled?

Methods included from Mixin::ShellOut

apply_default_env, maybe_add_timeout, #shell_out, #shell_out!

Methods included from Mixin::Checksum

#checksum, #short_cksum

Methods included from Mixin::EnforceOwnershipAndPermissions

#access_controls, #enforce_ownership_and_permissions

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #description, #events, include_resource_dsl?, include_resource_dsl_module, #introduced, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from Mixin::PowershellExec

#powershell_exec

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #resources, #with_run_context

Constructor Details

#initialize(new_resource, run_context) ⇒ File

Returns a new instance of File.



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

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

Instance Attribute Details

#deployment_strategyObject (readonly)

Returns the value of attribute deployment_strategy.



56
57
58
# File 'lib/chef/provider/file.rb', line 56

def deployment_strategy
  @deployment_strategy
end

Returns the value of attribute managing_symlink.



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

def managing_symlink
  @managing_symlink
end

#needs_creatingObject

Returns the value of attribute needs_creating.



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

def needs_creating
  @needs_creating
end

#needs_unlinkingObject

Returns the value of attribute needs_unlinking.



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

def needs_unlinking
  @needs_unlinking
end

Instance Method Details

#action_createObject



140
141
142
143
144
145
146
147
148
149
# File 'lib/chef/provider/file.rb', line 140

def action_create
  do_generate_content
  do_validate_content
  do_unlink
  do_create_file
  do_contents_changes
  do_acl_changes
  do_selinux
  load_resource_attributes_from_file(new_resource) unless Chef::Config[:why_run]
end

#action_create_if_missingObject



151
152
153
154
155
156
157
# File 'lib/chef/provider/file.rb', line 151

def action_create_if_missing
  unless ::File.exist?(new_resource.path)
    action_create
  else
    logger.trace("#{new_resource} exists at #{new_resource.path} taking no action.")
  end
end

#action_deleteObject



159
160
161
162
163
164
165
166
167
# File 'lib/chef/provider/file.rb', line 159

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)
      logger.info("#{new_resource} deleted file at #{new_resource.path}")
    end
  end
end

#action_touchObject



169
170
171
172
173
174
175
176
# File 'lib/chef/provider/file.rb', line 169

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)
    logger.info("#{new_resource} updated atime and mtime to #{time}")
  end
end

#define_resource_requirementsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chef/provider/file.rb', line 108

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.exist?(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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/provider/file.rb', line 70

def load_current_resource
  # true if there is a symlink and we need to manage what it points at
  @managing_symlink = file_class.symlink?(new_resource.path) && ( new_resource.manage_symlink_source || new_resource.manage_symlink_source.nil? )

  # true if there is a non-file thing in the way that we need to unlink first
  @needs_unlinking =
    if ::File.exist?(new_resource.path)
      if managing_symlink?
        !symlink_to_real_file?(new_resource.path)
      else
        !real_file?(new_resource.path)
      end
    else
      false
    end

  # true if we are going to be creating a new file
  @needs_creating = !::File.exist?(new_resource.path) || needs_unlinking?

  # Let children resources override constructing the current_resource
  @current_resource ||= Chef::Resource::File.new(new_resource.name)
  current_resource.path(new_resource.path)

  unless needs_creating?
    # we are updating an existing file
    if managing_content?
      logger.trace("#{new_resource} checksumming file at #{new_resource.path}.")
      current_resource.checksum(checksum(current_resource.path))
    else
      # if the file does not exist or is not a file, then the checksum is invalid/pointless
      current_resource.checksum(nil)
    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)


181
182
183
# File 'lib/chef/provider/file.rb', line 181

def manage_symlink_access?
  false
end