chef 18.4.2 → 18.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -1
  3. data/chef.gemspec +7 -7
  4. data/lib/chef/application/client.rb +12 -0
  5. data/lib/chef/client.rb +10 -16
  6. data/lib/chef/compliance/runner.rb +10 -0
  7. data/lib/chef/cookbook/chefignore.rb +4 -1
  8. data/lib/chef/cookbook/cookbook_version_loader.rb +1 -1
  9. data/lib/chef/cookbook/synchronizer.rb +7 -1
  10. data/lib/chef/cookbook_manifest.rb +2 -2
  11. data/lib/chef/file_access_control/unix.rb +9 -9
  12. data/lib/chef/file_cache.rb +17 -2
  13. data/lib/chef/file_content_management/deploy/target_io.rb +29 -0
  14. data/lib/chef/file_content_management/deploy.rb +4 -1
  15. data/lib/chef/formatters/doc.rb +1 -1
  16. data/lib/chef/mixin/file_class.rb +3 -1
  17. data/lib/chef/mixin/get_source_from_package.rb +1 -1
  18. data/lib/chef/mixin/openssl_helper.rb +1 -1
  19. data/lib/chef/node/attribute.rb +3 -11
  20. data/lib/chef/node/immutable_collections.rb +15 -8
  21. data/lib/chef/node/mixin/state_tracking.rb +6 -3
  22. data/lib/chef/node.rb +1 -1
  23. data/lib/chef/policy_builder/policyfile.rb +8 -0
  24. data/lib/chef/provider/.gitkeep +0 -0
  25. data/lib/chef/provider/cookbook_file.rb +1 -1
  26. data/lib/chef/provider/cron.rb +1 -1
  27. data/lib/chef/provider/directory.rb +15 -15
  28. data/lib/chef/provider/file.rb +42 -29
  29. data/lib/chef/provider/git.rb +8 -8
  30. data/lib/chef/provider/group/aix.rb +1 -1
  31. data/lib/chef/provider/group/dscl.rb +1 -1
  32. data/lib/chef/provider/group/gpasswd.rb +2 -2
  33. data/lib/chef/provider/group/groupadd.rb +1 -1
  34. data/lib/chef/provider/group/groupmod.rb +2 -2
  35. data/lib/chef/provider/group/pw.rb +2 -2
  36. data/lib/chef/provider/group/solaris.rb +2 -2
  37. data/lib/chef/provider/group/usermod.rb +2 -2
  38. data/lib/chef/provider/group.rb +1 -1
  39. data/lib/chef/provider/http_request.rb +2 -3
  40. data/lib/chef/provider/ifconfig/aix.rb +1 -1
  41. data/lib/chef/provider/ifconfig/debian.rb +3 -3
  42. data/lib/chef/provider/ifconfig/redhat.rb +1 -1
  43. data/lib/chef/provider/ifconfig.rb +1 -1
  44. data/lib/chef/provider/link.rb +10 -10
  45. data/lib/chef/provider/mount/aix.rb +4 -4
  46. data/lib/chef/provider/mount/linux.rb +4 -4
  47. data/lib/chef/provider/mount/mount.rb +11 -11
  48. data/lib/chef/provider/package/apt.rb +2 -11
  49. data/lib/chef/provider/package/bff.rb +3 -3
  50. data/lib/chef/provider/package/chocolatey.rb +54 -24
  51. data/lib/chef/provider/package/dpkg.rb +3 -3
  52. data/lib/chef/provider/package/freebsd/base.rb +1 -1
  53. data/lib/chef/provider/package/habitat.rb +5 -3
  54. data/lib/chef/provider/package/ips.rb +2 -2
  55. data/lib/chef/provider/package/openbsd.rb +3 -2
  56. data/lib/chef/provider/package/pacman.rb +4 -4
  57. data/lib/chef/provider/package/paludis.rb +2 -2
  58. data/lib/chef/provider/package/portage.rb +1 -1
  59. data/lib/chef/provider/package/powershell.rb +1 -0
  60. data/lib/chef/provider/package/rpm.rb +2 -2
  61. data/lib/chef/provider/package/smartos.rb +2 -2
  62. data/lib/chef/provider/package/snap.rb +2 -1
  63. data/lib/chef/provider/package/snap_tm.rb +79 -0
  64. data/lib/chef/provider/package/solaris.rb +4 -4
  65. data/lib/chef/provider/package/zypper.rb +4 -5
  66. data/lib/chef/provider/package.rb +1 -1
  67. data/lib/chef/provider/remote_directory.rb +5 -5
  68. data/lib/chef/provider/remote_file/http.rb +2 -3
  69. data/lib/chef/provider/remote_file.rb +1 -1
  70. data/lib/chef/provider/route.rb +9 -9
  71. data/lib/chef/provider/service/aix.rb +1 -1
  72. data/lib/chef/provider/service/aixinit.rb +4 -4
  73. data/lib/chef/provider/service/arch.rb +6 -6
  74. data/lib/chef/provider/service/debian.rb +5 -5
  75. data/lib/chef/provider/service/freebsd.rb +7 -7
  76. data/lib/chef/provider/service/gentoo.rb +5 -5
  77. data/lib/chef/provider/service/init.rb +2 -2
  78. data/lib/chef/provider/service/insserv.rb +2 -2
  79. data/lib/chef/provider/service/invokercd.rb +1 -1
  80. data/lib/chef/provider/service/openbsd.rb +7 -7
  81. data/lib/chef/provider/service/redhat.rb +3 -3
  82. data/lib/chef/provider/service/solaris.rb +2 -2
  83. data/lib/chef/provider/service/systemd.rb +2 -2
  84. data/lib/chef/provider/service/upstart.rb +2 -2
  85. data/lib/chef/provider/service/windows.rb +0 -1
  86. data/lib/chef/provider/subversion.rb +8 -8
  87. data/lib/chef/provider/systemd_unit.rb +3 -3
  88. data/lib/chef/provider/template.rb +1 -1
  89. data/lib/chef/provider/user/aix.rb +3 -3
  90. data/lib/chef/provider/user/linux.rb +7 -2
  91. data/lib/chef/provider/user/pw.rb +3 -3
  92. data/lib/chef/provider/user/solaris.rb +7 -7
  93. data/lib/chef/provider/user.rb +7 -8
  94. data/lib/chef/provider/yum_repository.rb +1 -3
  95. data/lib/chef/provider/zypper_repository.rb +1 -1
  96. data/lib/chef/providers.rb +1 -0
  97. data/lib/chef/resource/.gitkeep +0 -0
  98. data/lib/chef/resource/alternatives.rb +2 -2
  99. data/lib/chef/resource/apt_preference.rb +1 -1
  100. data/lib/chef/resource/apt_repository.rb +7 -9
  101. data/lib/chef/resource/apt_update.rb +3 -3
  102. data/lib/chef/resource/bff_package.rb +1 -1
  103. data/lib/chef/resource/chef_client_config.rb +3 -2
  104. data/lib/chef/resource/chef_client_systemd_timer.rb +5 -0
  105. data/lib/chef/resource/chef_gem.rb +1 -1
  106. data/lib/chef/resource/chef_sleep.rb +1 -1
  107. data/lib/chef/resource/cookbook_file.rb +1 -1
  108. data/lib/chef/resource/cron/cron.rb +1 -1
  109. data/lib/chef/resource/cron/cron_d.rb +1 -1
  110. data/lib/chef/resource/cron_access.rb +1 -1
  111. data/lib/chef/resource/directory.rb +1 -1
  112. data/lib/chef/resource/dpkg_package.rb +1 -1
  113. data/lib/chef/resource/execute.rb +8 -6
  114. data/lib/chef/resource/file/verification/json.rb +1 -1
  115. data/lib/chef/resource/file/verification/systemd_unit.rb +1 -1
  116. data/lib/chef/resource/file/verification/yaml.rb +1 -1
  117. data/lib/chef/resource/file.rb +1 -1
  118. data/lib/chef/resource/freebsd_package.rb +2 -2
  119. data/lib/chef/resource/group.rb +1 -1
  120. data/lib/chef/resource/habitat/habitat_package.rb +1 -1
  121. data/lib/chef/resource/habitat/habitat_sup.rb +9 -9
  122. data/lib/chef/resource/habitat/habitat_sup_systemd.rb +2 -2
  123. data/lib/chef/resource/habitat_install.rb +5 -4
  124. data/lib/chef/resource/hostname.rb +11 -10
  125. data/lib/chef/resource/http_request.rb +1 -1
  126. data/lib/chef/resource/ifconfig.rb +1 -1
  127. data/lib/chef/resource/inspec_input.rb +3 -1
  128. data/lib/chef/resource/inspec_waiver.rb +1 -1
  129. data/lib/chef/resource/inspec_waiver_file_entry.rb +1 -1
  130. data/lib/chef/resource/ips_package.rb +2 -2
  131. data/lib/chef/resource/kernel_module.rb +2 -2
  132. data/lib/chef/resource/link.rb +1 -1
  133. data/lib/chef/resource/locale.rb +2 -2
  134. data/lib/chef/resource/mount.rb +1 -1
  135. data/lib/chef/resource/notify_group.rb +1 -1
  136. data/lib/chef/resource/ohai.rb +1 -1
  137. data/lib/chef/resource/ohai_hint.rb +1 -1
  138. data/lib/chef/resource/openbsd_package.rb +2 -2
  139. data/lib/chef/resource/package.rb +1 -1
  140. data/lib/chef/resource/pacman_package.rb +1 -1
  141. data/lib/chef/resource/paludis_package.rb +1 -1
  142. data/lib/chef/resource/portage_package.rb +1 -1
  143. data/lib/chef/resource/powershell_package.rb +4 -0
  144. data/lib/chef/resource/reboot.rb +1 -1
  145. data/lib/chef/resource/remote_directory.rb +1 -1
  146. data/lib/chef/resource/remote_file.rb +1 -1
  147. data/lib/chef/resource/rhsm_errata.rb +1 -1
  148. data/lib/chef/resource/rhsm_errata_level.rb +1 -1
  149. data/lib/chef/resource/rhsm_register.rb +1 -1
  150. data/lib/chef/resource/rhsm_repo.rb +3 -4
  151. data/lib/chef/resource/rhsm_subscription.rb +8 -9
  152. data/lib/chef/resource/route.rb +1 -1
  153. data/lib/chef/resource/rpm_package.rb +1 -1
  154. data/lib/chef/resource/scm/git.rb +1 -1
  155. data/lib/chef/resource/scm/subversion.rb +1 -1
  156. data/lib/chef/resource/selinux/common_helpers.rb +1 -1
  157. data/lib/chef/resource/selinux_boolean.rb +1 -1
  158. data/lib/chef/resource/selinux_fcontext.rb +3 -3
  159. data/lib/chef/resource/selinux_install.rb +1 -1
  160. data/lib/chef/resource/selinux_login.rb +1 -1
  161. data/lib/chef/resource/selinux_module.rb +5 -5
  162. data/lib/chef/resource/selinux_permissive.rb +2 -2
  163. data/lib/chef/resource/selinux_port.rb +2 -2
  164. data/lib/chef/resource/selinux_state.rb +2 -2
  165. data/lib/chef/resource/selinux_user.rb +1 -1
  166. data/lib/chef/resource/smartos_package.rb +2 -2
  167. data/lib/chef/resource/snap_package.rb +24 -1
  168. data/lib/chef/resource/solaris_package.rb +1 -1
  169. data/lib/chef/resource/ssh_known_hosts_entry.rb +1 -1
  170. data/lib/chef/resource/sudo.rb +5 -5
  171. data/lib/chef/resource/support/client.erb +1 -1
  172. data/lib/chef/resource/swap_file.rb +6 -6
  173. data/lib/chef/resource/sysctl.rb +6 -5
  174. data/lib/chef/resource/systemd_unit.rb +1 -1
  175. data/lib/chef/resource/template.rb +1 -1
  176. data/lib/chef/resource/timezone.rb +5 -5
  177. data/lib/chef/resource/user/aix_user.rb +2 -2
  178. data/lib/chef/resource/user/linux_user.rb +2 -2
  179. data/lib/chef/resource/user/pw_user.rb +2 -2
  180. data/lib/chef/resource/user/solaris_user.rb +2 -2
  181. data/lib/chef/resource/user_ulimit.rb +1 -1
  182. data/lib/chef/resource/yum_repository.rb +1 -1
  183. data/lib/chef/resource/zypper_package.rb +2 -2
  184. data/lib/chef/resource/zypper_repository.rb +2 -2
  185. data/lib/chef/run_lock.rb +3 -0
  186. data/lib/chef/scan_access_control.rb +6 -6
  187. data/lib/chef/target_io/dir.rb +12 -0
  188. data/lib/chef/target_io/etc.rb +16 -0
  189. data/lib/chef/target_io/file.rb +12 -0
  190. data/lib/chef/target_io/fileutils.rb +12 -0
  191. data/lib/chef/target_io/http.rb +22 -0
  192. data/lib/chef/target_io/io.rb +12 -0
  193. data/lib/chef/target_io/shadow.rb +44 -0
  194. data/lib/chef/target_io/train/dir.rb +69 -0
  195. data/lib/chef/target_io/train/etc.rb +112 -0
  196. data/lib/chef/target_io/train/file.rb +219 -0
  197. data/lib/chef/target_io/train/fileutils.rb +220 -0
  198. data/lib/chef/target_io/train/http.rb +117 -0
  199. data/lib/chef/target_io/train/io.rb +13 -0
  200. data/lib/chef/target_io/train/shadow.rb +52 -0
  201. data/lib/chef/target_io/train_compat.rb +7 -0
  202. data/lib/chef/target_io.rb +9 -0
  203. data/lib/chef/util/backup.rb +1 -1
  204. data/lib/chef/util/diff.rb +14 -1
  205. data/lib/chef/util/file_edit.rb +4 -4
  206. data/lib/chef/version.rb +1 -1
  207. data/lib/chef.rb +2 -0
  208. data/spec/functional/resource/remote_file_spec.rb +1 -1
  209. data/spec/integration/client/fips_spec.rb +11 -2
  210. data/spec/integration/client/open_ssl_spec.rb +20 -0
  211. data/spec/spec_helper.rb +3 -1
  212. data/spec/support/platform_helpers.rb +20 -7
  213. data/spec/unit/client_spec.rb +0 -16
  214. data/spec/unit/file_cache_spec.rb +64 -0
  215. data/spec/unit/mixin/openssl_helper_spec.rb +6 -1
  216. data/spec/unit/provider/apt_repository_spec.rb +1 -1
  217. data/spec/unit/provider/package/chocolatey_spec.rb +17 -12
  218. data/spec/unit/provider/package/windows_spec.rb +5 -5
  219. data/spec/unit/provider/package/zypper_spec.rb +0 -10
  220. data/spec/unit/provider/route_spec.rb +6 -4
  221. data/spec/unit/resource/rhsm_repo_spec.rb +1 -0
  222. data/spec/unit/resource/rhsm_subscription_spec.rb +2 -0
  223. metadata +31 -10
@@ -27,12 +27,12 @@ class Chef
27
27
  class Provider
28
28
  class Directory < Chef::Provider::File
29
29
 
30
- provides :directory
30
+ provides :directory, target_mode: true
31
31
 
32
32
  def load_current_resource
33
33
  @current_resource = Chef::Resource::Directory.new(new_resource.name)
34
34
  current_resource.path(new_resource.path)
35
- if ::File.exist?(current_resource.path) && @action != :create_if_missing
35
+ if ::TargetIO::File.exist?(current_resource.path) && @action != :create_if_missing
36
36
  load_resource_attributes_from_file(current_resource)
37
37
  end
38
38
  current_resource
@@ -50,15 +50,15 @@ class Chef
50
50
  if new_resource.recursive
51
51
  does_parent_exist = lambda do |base_dir|
52
52
  base_dir = ::File.dirname(base_dir)
53
- if ::File.exist?(base_dir)
54
- ::File.directory?(base_dir)
53
+ if ::TargetIO::File.exist?(base_dir)
54
+ ::TargetIO::File.directory?(base_dir)
55
55
  else
56
56
  does_parent_exist.call(base_dir)
57
57
  end
58
58
  end
59
59
  does_parent_exist.call(new_resource.path)
60
60
  else
61
- ::File.directory?(parent_directory)
61
+ ::TargetIO::File.directory?(parent_directory)
62
62
  end
63
63
  end
64
64
  a.failure_message(Chef::Exceptions::EnclosingDirectoryDoesNotExist, "Parent directory #{parent_directory} does not exist, cannot create #{new_resource.path}")
@@ -73,7 +73,7 @@ class Chef
73
73
  # make sure we have write permissions to that directory
74
74
  is_parent_writable = lambda do |base_dir|
75
75
  base_dir = ::File.dirname(base_dir)
76
- if ::File.exist?(base_dir)
76
+ if ::TargetIO::File.exist?(base_dir)
77
77
  if Chef::FileAccessControl.writable?(base_dir)
78
78
  true
79
79
  elsif Chef::Util::PathHelper.is_sip_path?(base_dir, node)
@@ -89,7 +89,7 @@ class Chef
89
89
  else
90
90
  # in why run mode & parent directory does not exist no permissions check is required
91
91
  # If not in why run, permissions must be valid and we rely on prior assertion that dir exists
92
- if !whyrun_mode? || ::File.exist?(parent_directory)
92
+ if !whyrun_mode? || ::TargetIO::File.exist?(parent_directory)
93
93
  if Chef::FileAccessControl.writable?(parent_directory)
94
94
  true
95
95
  elsif Chef::Util::PathHelper.is_sip_path?(parent_directory, node)
@@ -108,8 +108,8 @@ class Chef
108
108
 
109
109
  requirements.assert(:delete) do |a|
110
110
  a.assertion do
111
- if ::File.exist?(new_resource.path)
112
- ::File.directory?(new_resource.path) && Chef::FileAccessControl.writable?(new_resource.path)
111
+ if ::TargetIO::File.exist?(new_resource.path)
112
+ ::TargetIO::File.directory?(new_resource.path) && Chef::FileAccessControl.writable?(new_resource.path)
113
113
  else
114
114
  true
115
115
  end
@@ -122,12 +122,12 @@ class Chef
122
122
  end
123
123
 
124
124
  action :create, description: "Create a directory. If a directory already exists (but does not match), update that directory to match." do
125
- unless ::File.exist?(new_resource.path)
125
+ unless ::TargetIO::File.exist?(new_resource.path)
126
126
  converge_by("create new directory #{new_resource.path}") do
127
127
  if new_resource.recursive == true
128
- ::FileUtils.mkdir_p(new_resource.path)
128
+ ::TargetIO::FileUtils.mkdir_p(new_resource.path)
129
129
  else
130
- ::Dir.mkdir(new_resource.path)
130
+ ::TargetIO::Dir.mkdir(new_resource.path)
131
131
  end
132
132
  logger.info("#{new_resource} created directory #{new_resource.path}")
133
133
  end
@@ -138,15 +138,15 @@ class Chef
138
138
  end
139
139
 
140
140
  action :delete, description: "Delete a directory." do
141
- if ::File.exist?(new_resource.path)
141
+ if ::TargetIO::File.exist?(new_resource.path)
142
142
  converge_by("delete existing directory #{new_resource.path}") do
143
143
  if new_resource.recursive == true
144
144
  # we don't use rm_rf here because it masks all errors, including
145
145
  # IO errors or permission errors that would prevent the deletion
146
- FileUtils.rm_r(new_resource.path)
146
+ ::TargetIO::FileUtils.rm_r(new_resource.path)
147
147
  logger.info("#{new_resource} deleted #{new_resource.path} recursively")
148
148
  else
149
- ::Dir.delete(new_resource.path)
149
+ ::TargetIO::Dir.delete(new_resource.path)
150
150
  logger.info("#{new_resource} deleted #{new_resource.path}")
151
151
  end
152
152
  end
@@ -29,6 +29,7 @@ require_relative "../mixin/file_class"
29
29
  require_relative "../mixin/enforce_ownership_and_permissions"
30
30
  require_relative "../resource/file/verification/json"
31
31
  require_relative "../resource/file/verification/yaml"
32
+ require_relative "../target_io"
32
33
  require_relative "../util/backup"
33
34
  require_relative "../util/diff"
34
35
  require_relative "../util/selinux"
@@ -53,7 +54,7 @@ class Chef
53
54
  include Chef::Util::Selinux
54
55
  include Chef::Mixin::FileClass
55
56
 
56
- provides :file
57
+ provides :file, target_mode: true
57
58
 
58
59
  attr_reader :deployment_strategy
59
60
 
@@ -75,7 +76,7 @@ class Chef
75
76
 
76
77
  # true if there is a non-file thing in the way that we need to unlink first
77
78
  @needs_unlinking =
78
- if ::File.exist?(new_resource.path)
79
+ if ::TargetIO::File.exist?(new_resource.path)
79
80
  if managing_symlink?
80
81
  !symlink_to_real_file?(new_resource.path)
81
82
  else
@@ -86,7 +87,7 @@ class Chef
86
87
  end
87
88
 
88
89
  # true if we are going to be creating a new file
89
- @needs_creating = !::File.exist?(new_resource.path) || needs_unlinking?
90
+ @needs_creating = !::TargetIO::File.exist?(new_resource.path) || needs_unlinking?
90
91
 
91
92
  # Let children resources override constructing the current_resource
92
93
  @current_resource ||= Chef::Resource::File.new(new_resource.name)
@@ -96,7 +97,11 @@ class Chef
96
97
  # we are updating an existing file
97
98
  if managing_content?
98
99
  logger.trace("#{new_resource} checksumming file at #{new_resource.path}.")
99
- current_resource.checksum(checksum(current_resource.path))
100
+
101
+ resource_to_check = current_resource.path
102
+ resource_to_check = ::TargetIO::File.open(current_resource.path) if ChefConfig::Config.target_mode?
103
+
104
+ current_resource.checksum(checksum(resource_to_check))
100
105
  else
101
106
  # if the file does not exist or is not a file, then the checksum is invalid/pointless
102
107
  current_resource.checksum(nil)
@@ -114,15 +119,15 @@ class Chef
114
119
  # Make sure the parent directory exists, otherwise fail. For why-run assume it would have been created.
115
120
  requirements.assert(:create, :create_if_missing, :touch) do |a|
116
121
  parent_directory = ::File.dirname(new_resource.path)
117
- a.assertion { ::File.directory?(parent_directory) }
122
+ a.assertion { ::TargetIO::File.directory?(parent_directory) }
118
123
  a.failure_message(Chef::Exceptions::EnclosingDirectoryDoesNotExist, "Parent directory #{parent_directory} does not exist.")
119
124
  a.whyrun("Assuming directory #{parent_directory} would have been created")
120
125
  end
121
126
 
122
127
  # Make sure the file is deletable if it exists, otherwise fail.
123
- if ::File.exist?(new_resource.path)
128
+ if ::TargetIO::File.exist?(new_resource.path)
124
129
  requirements.assert(:delete) do |a|
125
- a.assertion { ::File.writable?(new_resource.path) }
130
+ a.assertion { ::TargetIO::File.writable?(new_resource.path) }
126
131
  a.failure_message(Chef::Exceptions::InsufficientPermissions, "File #{new_resource.path} exists but is not writable so it cannot be deleted")
127
132
  end
128
133
  end
@@ -151,7 +156,7 @@ class Chef
151
156
  end
152
157
 
153
158
  action :create_if_missing do
154
- unless ::File.exist?(new_resource.path)
159
+ unless ::TargetIO::File.exist?(new_resource.path)
155
160
  action_create
156
161
  else
157
162
  logger.debug("#{new_resource} exists at #{new_resource.path} taking no action.")
@@ -159,10 +164,10 @@ class Chef
159
164
  end
160
165
 
161
166
  action :delete do
162
- if ::File.exist?(new_resource.path)
167
+ if ::TargetIO::File.exist?(new_resource.path)
163
168
  converge_by("delete file #{new_resource.path}") do
164
169
  do_backup unless file_class.symlink?(new_resource.path)
165
- ::File.delete(new_resource.path)
170
+ ::TargetIO::File.delete(new_resource.path)
166
171
  logger.info("#{new_resource} deleted file at #{new_resource.path}")
167
172
  end
168
173
  end
@@ -172,7 +177,7 @@ class Chef
172
177
  action_create
173
178
  converge_by("update utime on file #{new_resource.path}") do
174
179
  time = Time.now
175
- ::File.utime(time, time, new_resource.path)
180
+ ::TargetIO::File.utime(time, time, new_resource.path)
176
181
  logger.info("#{new_resource} updated atime and mtime to #{time}")
177
182
  end
178
183
  end
@@ -254,7 +259,7 @@ class Chef
254
259
  # If any of the above apply, returns a 3-tuple of Exception class,
255
260
  # exception message, whyrun message; otherwise returns a 3-tuple of nil.
256
261
  def verify_symlink_sanity(path)
257
- real_path = ::File.realpath(path)
262
+ real_path = ::TargetIO::File.realpath(path)
258
263
  if real_file?(real_path)
259
264
  [nil, nil, nil]
260
265
  else
@@ -283,15 +288,15 @@ class Chef
283
288
 
284
289
  def file_type_string(path)
285
290
  case
286
- when ::File.blockdev?(path)
291
+ when ::TargetIO::File.blockdev?(path)
287
292
  "block device"
288
- when ::File.chardev?(path)
293
+ when ::TargetIO::File.chardev?(path)
289
294
  "char device"
290
- when ::File.directory?(path)
295
+ when ::TargetIO::File.directory?(path)
291
296
  "directory"
292
- when ::File.pipe?(path)
297
+ when ::TargetIO::File.pipe?(path)
293
298
  "pipe"
294
- when ::File.socket?(path)
299
+ when ::TargetIO::File.socket?(path)
295
300
  "socket"
296
301
  when file_class.symlink?(path)
297
302
  "symlink"
@@ -301,12 +306,12 @@ class Chef
301
306
  end
302
307
 
303
308
  def real_file?(path)
304
- !file_class.symlink?(path) && ::File.file?(path)
309
+ !file_class.symlink?(path) && ::TargetIO::File.file?(path)
305
310
  end
306
311
 
307
312
  # like real_file? that follows (sane) symlinks
308
313
  def symlink_to_real_file?(path)
309
- real_file?(::File.realpath(path))
314
+ real_file?(::TargetIO::File.realpath(path))
310
315
  rescue Errno::ELOOP, Errno::ENOENT
311
316
  false
312
317
  end
@@ -314,15 +319,15 @@ class Chef
314
319
  # Similar to File.exist?, but also returns true in the case that the
315
320
  # named file is a broken symlink.
316
321
  def l_exist?(path)
317
- ::File.exist?(path) || file_class.symlink?(path)
322
+ ::TargetIO::File.exist?(path) || file_class.symlink?(path)
318
323
  end
319
324
 
320
325
  def unlink(path)
321
326
  # Directories can not be unlinked. Remove them using FileUtils.
322
- if ::File.directory?(path)
323
- FileUtils.rm_rf(path)
327
+ if ::TargetIO::File.directory?(path)
328
+ TargetIO::FileUtils.rm_rf(path)
324
329
  else
325
- ::File.unlink(path)
330
+ ::TargetIO::File.unlink(path)
326
331
  end
327
332
  end
328
333
 
@@ -344,8 +349,8 @@ class Chef
344
349
  new_resource.verify.each do |v|
345
350
  unless v.verify(tempfile.path)
346
351
  backupfile = "#{Chef::Config[:file_cache_path]}/failed_validations/#{::File.basename(tempfile.path)}"
347
- FileUtils.mkdir_p ::File.dirname(backupfile)
348
- FileUtils.cp tempfile.path, backupfile
352
+ TargetIO::FileUtils.mkdir_p ::File.dirname(backupfile)
353
+ TargetIO::FileUtils.cp tempfile.path, backupfile
349
354
  raise Chef::Exceptions::ValidationFailed.new "Proposed content for #{new_resource.path} failed verification #{new_resource.sensitive ? "[sensitive]" : "#{v}\n#{v.output}"}\nTemporary file moved to #{backupfile}"
350
355
  end
351
356
  end
@@ -374,6 +379,11 @@ class Chef
374
379
  end
375
380
 
376
381
  def do_backup(file = nil)
382
+ if ChefConfig::Config.target_mode?
383
+ Chef::Log.info("#{@new_resource} backup skipped in Target Mode")
384
+ return
385
+ end
386
+
377
387
  Chef::Util::Backup.new(new_resource, file).backup!
378
388
  end
379
389
 
@@ -383,11 +393,14 @@ class Chef
383
393
 
384
394
  def update_file_contents
385
395
  do_backup unless needs_creating?
386
- deployment_strategy.deploy(tempfile.path, ::File.realpath(new_resource.path).force_encoding(Chef::Config[:ruby_encoding]))
396
+ deployment_strategy.deploy(tempfile.path, ::TargetIO::File.realpath(new_resource.path).force_encoding(Chef::Config[:ruby_encoding]))
387
397
  logger.info("#{new_resource} updated file contents #{new_resource.path}")
388
398
  if managing_content?
389
399
  # save final checksum for reporting.
390
- new_resource.final_checksum = checksum(new_resource.path)
400
+ resource_to_check = new_resource.path
401
+ resource_to_check = ::TargetIO::File.open(resource_to_check) if ChefConfig::Config.target_mode?
402
+
403
+ new_resource.final_checksum = checksum(resource_to_check)
391
404
  end
392
405
  end
393
406
 
@@ -400,7 +413,7 @@ class Chef
400
413
  end
401
414
 
402
415
  # the file? on the next line suppresses the case in why-run when we have a not-file here that would have otherwise been removed
403
- if ::File.file?(new_resource.path) && contents_changed?
416
+ if ::TargetIO::File.file?(new_resource.path) && contents_changed?
404
417
  description = [ "update content in file #{new_resource.path} from \
405
418
  #{short_cksum(current_resource.checksum)} to #{short_cksum(tempfile_checksum)}" ]
406
419
 
@@ -432,7 +445,7 @@ class Chef
432
445
  if resource_updated? && Chef::Config[:enable_selinux_file_permission_fixup]
433
446
  if selinux_enabled?
434
447
  converge_by("restore selinux security context") do
435
- restore_security_context(::File.realpath(new_resource.path), recursive)
448
+ restore_security_context(::TargetIO::File.realpath(new_resource.path), recursive)
436
449
  end
437
450
  else
438
451
  logger.trace "selinux utilities can not be found. Skipping selinux permission fixup."
@@ -26,7 +26,7 @@ class Chef
26
26
  class Git < Chef::Provider
27
27
 
28
28
  extend Forwardable
29
- provides :git
29
+ provides :git, target_mode: true
30
30
 
31
31
  GIT_VERSION_PATTERN = Regexp.compile("git version (\\d+\\.\\d+.\\d+)")
32
32
 
@@ -58,7 +58,7 @@ class Chef
58
58
  # Parent directory of the target must exist.
59
59
  requirements.assert(:checkout, :sync) do |a|
60
60
  dirname = ::File.dirname(cwd)
61
- a.assertion { ::File.directory?(dirname) }
61
+ a.assertion { ::TargetIO::File.directory?(dirname) }
62
62
  a.whyrun("Directory #{dirname} does not exist, this run will fail unless it has been previously created. Assuming it would have been created.")
63
63
  a.failure_message(Chef::Exceptions::MissingParentDirectory,
64
64
  "Cannot clone #{new_resource} to #{cwd}, the enclosing directory #{dirname} does not exist")
@@ -101,7 +101,7 @@ class Chef
101
101
  action :export do
102
102
  action_checkout
103
103
  converge_by("complete the export by removing #{cwd}.git after checkout") do
104
- FileUtils.rm_rf(::File.join(cwd, ".git"))
104
+ TargetIO::FileUtils.rm_rf(::File.join(cwd, ".git"))
105
105
  end
106
106
  end
107
107
 
@@ -138,16 +138,16 @@ class Chef
138
138
  end
139
139
 
140
140
  def existing_git_clone?
141
- ::File.exist?(::File.join(cwd, ".git"))
141
+ ::TargetIO::File.exist?(::File.join(cwd, ".git"))
142
142
  end
143
143
 
144
144
  def target_dir_non_existent_or_empty?
145
- !::File.exist?(cwd) || Dir.entries(cwd).sort == [".", ".."]
145
+ !::TargetIO::File.exist?(cwd) || TargetIO::Dir.entries(cwd).sort == [".", ".."]
146
146
  end
147
147
 
148
148
  def find_current_revision
149
149
  logger.trace("#{new_resource} finding current git revision")
150
- if ::File.exist?(::File.join(cwd, ".git"))
150
+ if ::TargetIO::File.exist?(::File.join(cwd, ".git"))
151
151
  # 128 is returned when we're not in a git repo. this is fine
152
152
  result = git("rev-parse", "HEAD", cwd: cwd, returns: [0, 128]).stdout.strip
153
153
  end
@@ -413,9 +413,9 @@ class Chef
413
413
  require "etc" unless defined?(Etc)
414
414
  case user
415
415
  when Integer
416
- Etc.getpwuid(user).dir
416
+ TargetIO::Etc.getpwuid(user).dir
417
417
  else
418
- Etc.getpwnam(user.to_s).dir
418
+ TargetIO::Etc.getpwnam(user.to_s).dir
419
419
  end
420
420
  end
421
421
  end
@@ -22,7 +22,7 @@ class Chef
22
22
  class Provider
23
23
  class Group
24
24
  class Aix < Chef::Provider::Group::Groupadd
25
- provides :group, platform: "aix"
25
+ provides :group, platform: "aix", target_mode: true
26
26
 
27
27
  def required_binaries
28
28
  [ "/usr/bin/mkgroup",
@@ -21,7 +21,7 @@ class Chef
21
21
  class Group
22
22
  class Dscl < Chef::Provider::Group
23
23
 
24
- provides :group, os: "darwin"
24
+ provides :group, os: "darwin", target_mode: true
25
25
 
26
26
  def dscl(*args)
27
27
  argdup = args.dup
@@ -22,7 +22,7 @@ class Chef
22
22
  class Provider
23
23
  class Group
24
24
  class Gpasswd < Chef::Provider::Group::Groupadd
25
- provides :group
25
+ provides :group, target_mode: true
26
26
 
27
27
  def load_current_resource
28
28
  super
@@ -31,7 +31,7 @@ class Chef
31
31
  def define_resource_requirements
32
32
  super
33
33
  requirements.assert(:all_actions) do |a|
34
- a.assertion { ::File.exist?("/usr/bin/gpasswd") }
34
+ a.assertion { ::TargetIO::File.exist?("/usr/bin/gpasswd") }
35
35
  a.failure_message Chef::Exceptions::Group, "Could not find binary /usr/bin/gpasswd for #{new_resource}"
36
36
  # No whyrun alternative: this component should be available in the base install of any given system that uses it
37
37
  end
@@ -35,7 +35,7 @@ class Chef
35
35
  super
36
36
  required_binaries.each do |required_binary|
37
37
  requirements.assert(:all_actions) do |a|
38
- a.assertion { ::File.exist?(required_binary) }
38
+ a.assertion { ::TargetIO::File.exist?(required_binary) }
39
39
  a.failure_message Chef::Exceptions::Group, "Could not find binary #{required_binary} for #{new_resource}"
40
40
  # No whyrun alternative: this component should be available in the base install of any given system that uses it
41
41
  end
@@ -21,12 +21,12 @@ class Chef
21
21
  class Group
22
22
  class Groupmod < Chef::Provider::Group
23
23
 
24
- provides :group, os: "netbsd"
24
+ provides :group, os: "netbsd", target_mode: true
25
25
 
26
26
  def load_current_resource
27
27
  super
28
28
  %w{group user}.each do |binary|
29
- raise Chef::Exceptions::Group, "Could not find binary /usr/sbin/#{binary} for #{new_resource}" unless ::File.exist?("/usr/sbin/#{binary}")
29
+ raise Chef::Exceptions::Group, "Could not find binary /usr/sbin/#{binary} for #{new_resource}" unless ::TargetIO::File.exist?("/usr/sbin/#{binary}")
30
30
  end
31
31
  end
32
32
 
@@ -20,7 +20,7 @@ class Chef
20
20
  class Provider
21
21
  class Group
22
22
  class Pw < Chef::Provider::Group
23
- provides :group, platform: "freebsd"
23
+ provides :group, platform: "freebsd", target_mode: true
24
24
 
25
25
  def load_current_resource
26
26
  super
@@ -30,7 +30,7 @@ class Chef
30
30
  super
31
31
 
32
32
  requirements.assert(:all_actions) do |a|
33
- a.assertion { ::File.exist?("/usr/sbin/pw") }
33
+ a.assertion { ::TargetIO::File.exist?("/usr/sbin/pw") }
34
34
  a.failure_message Chef::Exceptions::Group, "Could not find binary /usr/sbin/pw for #{new_resource}"
35
35
  # No whyrun alternative: this component should be available in the base install of any given system that uses it
36
36
  end
@@ -25,7 +25,7 @@ class Chef
25
25
 
26
26
  # this provides line is setup to only catch the solaris2 platform, but
27
27
  # NOT other platforms in the Solaris platform_family. (See usermod provider.)
28
- provides :group, platform: "solaris2"
28
+ provides :group, platform: "solaris2", target_mode: true
29
29
 
30
30
  def load_current_resource
31
31
  super
@@ -35,7 +35,7 @@ class Chef
35
35
  super
36
36
 
37
37
  requirements.assert(:all_actions) do |a|
38
- a.assertion { ::File.exist?("/usr/sbin/usermod") && ::File.exist?("/usr/sbin/groupmod") }
38
+ a.assertion { ::TargetIO::File.exist?("/usr/sbin/usermod") && ::TargetIO::File.exist?("/usr/sbin/groupmod") }
39
39
  a.failure_message Chef::Exceptions::Group, "Could not find binary /usr/sbin/usermod or /usr/sbin/groupmod for #{new_resource}"
40
40
  # No whyrun alternative: this component should be available in the base install of any given system that uses it
41
41
  end
@@ -23,7 +23,7 @@ class Chef
23
23
  class Group
24
24
  class Usermod < Chef::Provider::Group::Groupadd
25
25
 
26
- provides :group, os: %w{openbsd solaris2}
26
+ provides :group, os: %w{openbsd solaris2}, target_mode: true
27
27
 
28
28
  def load_current_resource
29
29
  super
@@ -33,7 +33,7 @@ class Chef
33
33
  super
34
34
 
35
35
  requirements.assert(:all_actions) do |a|
36
- a.assertion { ::File.exist?("/usr/sbin/usermod") }
36
+ a.assertion { ::TargetIO::File.exist?("/usr/sbin/usermod") }
37
37
  a.failure_message Chef::Exceptions::Group, "Could not find binary /usr/sbin/usermod for #{new_resource}"
38
38
  # No whyrun alternative: this component should be available in the base install of any given system that uses it
39
39
  end
@@ -36,7 +36,7 @@ class Chef
36
36
 
37
37
  group_info = nil
38
38
  begin
39
- group_info = Etc.getgrnam(new_resource.group_name)
39
+ group_info = TargetIO::Etc.getgrnam(new_resource.group_name)
40
40
  rescue ArgumentError
41
41
  @group_exists = false
42
42
  logger.trace("#{new_resource} group does not exist")
@@ -23,12 +23,12 @@ class Chef
23
23
  class Provider
24
24
  class HttpRequest < Chef::Provider
25
25
 
26
- provides :http_request
26
+ provides :http_request, target_mode: true
27
27
 
28
28
  attr_writer :http
29
29
 
30
30
  def http
31
- @http ||= Chef::HTTP::Simple.new(new_resource.url)
31
+ @http ||= TargetIO::HTTP.new(new_resource.url)
32
32
  end
33
33
 
34
34
  def load_current_resource; end
@@ -60,7 +60,6 @@ class Chef
60
60
  new_resource.headers
61
61
  )
62
62
  logger.info("#{new_resource} GET to #{new_resource.url} successful")
63
- logger.trace("#{new_resource} GET request response: #{body}")
64
63
  end
65
64
  end
66
65
 
@@ -22,7 +22,7 @@ class Chef
22
22
  class Provider
23
23
  class Ifconfig
24
24
  class Aix < Chef::Provider::Ifconfig
25
- provides :ifconfig, platform: "aix"
25
+ provides :ifconfig, platform: "aix", target_mode: true
26
26
 
27
27
  def load_current_resource
28
28
  @current_resource = Chef::Resource::Ifconfig.new(new_resource.name)
@@ -23,7 +23,7 @@ class Chef
23
23
  class Provider
24
24
  class Ifconfig
25
25
  class Debian < Chef::Provider::Ifconfig
26
- provides :ifconfig, platform_family: %w{debian}
26
+ provides :ifconfig, platform_family: %w{debian}, target_mode: true
27
27
 
28
28
  INTERFACES_FILE = "/etc/network/interfaces".freeze
29
29
  INTERFACES_DOT_D_DIR = "/etc/network/interfaces.d".freeze
@@ -81,7 +81,7 @@ iface <%= new_resource.device %> <%= new_resource.family %> static
81
81
 
82
82
  def enforce_interfaces_dot_d_sanity
83
83
  # on ubuntu 18.04+ there's no interfaces file and it uses interfaces.d by default
84
- return if ::File.directory?(INTERFACES_DOT_D_DIR) && !::File.exist?(INTERFACES_FILE)
84
+ return if ::TargetIO::File.directory?(INTERFACES_DOT_D_DIR) && !::TargetIO::File.exist?(INTERFACES_FILE)
85
85
 
86
86
  # create /etc/network/interfaces.d via dir if it's missing
87
87
  directory INTERFACES_DOT_D_DIR
@@ -90,7 +90,7 @@ iface <%= new_resource.device %> <%= new_resource.family %> static
90
90
  interfaces_dot_d_for_regexp = INTERFACES_DOT_D_DIR.gsub(/\./, "\\.") # escape dots for the regexp
91
91
  regexp = %r{^\s*source\s+#{interfaces_dot_d_for_regexp}/\*\s*$}
92
92
 
93
- return if ::File.exist?(INTERFACES_FILE) && regexp.match(IO.read(INTERFACES_FILE))
93
+ return if ::TargetIO::File.exist?(INTERFACES_FILE) && regexp.match(IO.read(INTERFACES_FILE))
94
94
 
95
95
  converge_by("modifying #{INTERFACES_FILE} to source #{INTERFACES_DOT_D_DIR}") do
96
96
  conf = Chef::Util::FileEdit.new(INTERFACES_FILE)
@@ -22,7 +22,7 @@ class Chef
22
22
  class Provider
23
23
  class Ifconfig
24
24
  class Redhat < Chef::Provider::Ifconfig
25
- provides :ifconfig, platform_family: "fedora_derived"
25
+ provides :ifconfig, platform_family: "fedora_derived", target_mode: true
26
26
 
27
27
  def initialize(new_resource, run_context)
28
28
  super(new_resource, run_context)
@@ -31,7 +31,7 @@ class Chef
31
31
  # device 'eth1'
32
32
  # end
33
33
  class Ifconfig < Chef::Provider
34
- provides :ifconfig
34
+ provides :ifconfig, target_mode: true
35
35
 
36
36
  attr_accessor :config_template
37
37
  attr_accessor :config_path
@@ -28,7 +28,7 @@ class Chef
28
28
  class Provider
29
29
  class Link < Chef::Provider
30
30
 
31
- provides :link
31
+ provides :link, target_mode: true
32
32
 
33
33
  include Chef::Mixin::EnforceOwnershipAndPermissions
34
34
  include Chef::Mixin::FileClass
@@ -43,8 +43,8 @@ class Chef
43
43
  )
44
44
  else
45
45
  current_resource.link_type(:hard)
46
- if ::File.exist?(current_resource.target_file)
47
- if ::File.exist?(new_resource.to) &&
46
+ if ::TargetIO::File.exist?(current_resource.target_file)
47
+ if ::TargetIO::File.exist?(new_resource.to) &&
48
48
  file_class.stat(current_resource.target_file).ino ==
49
49
  file_class.stat(new_resource.to).ino
50
50
  current_resource.to(canonicalize(new_resource.to))
@@ -92,14 +92,14 @@ class Chef
92
92
  # However if the new symlink will point to a file and the current symlink is pointing at a
93
93
  # directory we want to throw an exception and calling ::File.unlink on the directory symlink
94
94
  # will throw the correct ones.
95
- if ChefUtils.windows? && ::File.directory?(new_resource.to) &&
96
- ::File.directory?(current_resource.target_file)
95
+ if ChefUtils.windows? && ::TargetIO::File.directory?(new_resource.to) &&
96
+ ::TargetIO::File.directory?(current_resource.target_file)
97
97
  converge_by("unlink existing windows symlink to dir at #{new_resource.target_file}") do
98
- ::Dir.unlink(new_resource.target_file)
98
+ ::TargetIO::Dir.unlink(new_resource.target_file)
99
99
  end
100
100
  else
101
101
  converge_by("unlink existing symlink to file at #{new_resource.target_file}") do
102
- ::File.unlink(new_resource.target_file)
102
+ ::TargetIO::File.unlink(new_resource.target_file)
103
103
  end
104
104
  end
105
105
  end
@@ -134,14 +134,14 @@ class Chef
134
134
 
135
135
  action :delete do
136
136
  if current_resource.to # Exists
137
- if ChefUtils.windows? && ::File.directory?(current_resource.target_file)
137
+ if ChefUtils.windows? && ::TargetIO::File.directory?(current_resource.target_file)
138
138
  converge_by("delete link to dir at #{new_resource.target_file}") do
139
- ::Dir.delete(new_resource.target_file)
139
+ ::TargetIO::Dir.delete(new_resource.target_file)
140
140
  logger.info("#{new_resource} deleted")
141
141
  end
142
142
  else
143
143
  converge_by("delete link to file at #{new_resource.target_file}") do
144
- ::File.delete(new_resource.target_file)
144
+ ::TargetIO::File.delete(new_resource.target_file)
145
145
  logger.info("#{new_resource} deleted")
146
146
  end
147
147
  end