diff options
author | Tim Smith <tsmith@chef.io> | 2018-10-28 17:21:39 -0700 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2018-10-28 17:21:39 -0700 |
commit | 5f91346f94c9418e8eea7cb524d2a75618f8759d (patch) | |
tree | fc8037c9d71bf3b987ffd37d4c1cb6d87dbd5314 /lib | |
parent | 088a32b9adb00053d752713605be41b1bbb3e2f3 (diff) | |
download | chef-5f91346f94c9418e8eea7cb524d2a75618f8759d.tar.gz |
Move more functionality to helper methods and properties
More passphrase_cmd and software_license_agreement over to helper methods that we can potentially test later on.
Improve the requires EULA error message
Use a default value for volumes_dir that sets it to the value of the app property instead of creating a variable later.
Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/resource/dmg_package.rb | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/lib/chef/resource/dmg_package.rb b/lib/chef/resource/dmg_package.rb index 76eb8dd193..7719dda96a 100644 --- a/lib/chef/resource/dmg_package.rb +++ b/lib/chef/resource/dmg_package.rb @@ -47,7 +47,8 @@ class Chef description: "The sha256 checksum of the dmg to download." property :volumes_dir, String, - description: "The Directory under /Volumes where the dmg is mounted as not all dmgs are mounted into a /Volumes location matching the name of the dmg." + description: "The Directory under /Volumes where the dmg is mounted as not all dmgs are mounted into a /Volumes location matching the name of the dmg.", + default: lazy { |r| r.app } property :dmg_name, String, description: "The name of the dmg if it is not the same as app, or if the name has spaces.", @@ -92,8 +93,6 @@ class Chef description "Installs the application." if current_resource.nil? - volumes_dir = new_resource.volumes_dir ? new_resource.volumes_dir : new_resource.app - if new_resource.source remote_file dmg_file do source new_resource.source @@ -102,30 +101,27 @@ class Chef end end - passphrase_cmd = new_resource.dmg_passphrase ? "-passphrase #{new_resource.dmg_passphrase}" : "" ruby_block "attach #{dmg_file}" do block do - # example hdiutil imageinfo output: http://rubular.com/r/0xvOaA6d8B - software_license_agreement = /Software License Agreement: true/.match?(shell_out!("/usr/bin/hdiutil imageinfo #{passphrase_cmd} '#{dmg_file}'").stdout) - raise "Requires EULA Acceptance; add 'accept_eula true' to dmg_package resource" if software_license_agreement && !new_resource.accept_eula - accept_eula_cmd = new_resource.accept_eula ? "echo Y | PAGER=true" : "" - shell_out!("#{accept_eula_cmd} /usr/bin/hdiutil attach #{passphrase_cmd} '#{dmg_file}' -nobrowse -mountpoint '/Volumes/#{volumes_dir}' -quiet") + raise "This DMG package requires EULA acceptance. Add 'accept_eula true' to dmg_package resource to accept the EULA during installation." if software_license_agreement? && !new_resource.accept_eula + accept_eula_cmd = new_resource.accept_eula ? "echo Y | echo Y | PAGER=true " : "" + shell_out!("#{accept_eula_cmd} /usr/bin/hdiutil attach #{passphrase_cmd} '#{dmg_file}' -nobrowse -mountpoint '/Volumes/#{new_resource.volumes_dir}' -quiet", env: { "PAGER" => "true" }) end not_if "/usr/bin/hdiutil info #{passphrase_cmd} | grep -q 'image-path.*#{dmg_file}'" end case new_resource.type when "app" - execute "rsync --force --recursive --links --perms --executability --owner --group --times '/Volumes/#{volumes_dir}/#{new_resource.app}.app' '#{new_resource.destination}'" do + execute "rsync --force --recursive --links --perms --executability --owner --group --times '/Volumes/#{new_resource.volumes_dir}/#{new_resource.app}.app' '#{new_resource.destination}'" do user new_resource.owner if new_resource.owner end file "#{new_resource.destination}/#{new_resource.app}.app/Contents/MacOS/#{new_resource.app}" do - mode "755" + mode "0755" ignore_failure true end when "mpkg", "pkg" - install_cmd = "installation_file=$(ls '/Volumes/#{volumes_dir}' | grep '.#{new_resource.type}$') && sudo installer -pkg \"/Volumes/#{volumes_dir}/$installation_file\" -target /" + install_cmd = "installation_file=$(ls '/Volumes/#{new_resource.volumes_dir}' | grep '.#{new_resource.type}$') && sudo installer -pkg \"/Volumes/#{new_resource.volumes_dir}/$installation_file\" -target /" install_cmd += " -allowUntrusted" if new_resource.allow_untrusted execute install_cmd do @@ -134,7 +130,7 @@ class Chef end end - execute "/usr/bin/hdiutil detach '/Volumes/#{volumes_dir}' || /usr/bin/hdiutil detach '/Volumes/#{volumes_dir}' -force" + execute "/usr/bin/hdiutil detach '/Volumes/#{new_resource.volumes_dir}' || /usr/bin/hdiutil detach '/Volumes/#{new_resource.volumes_dir}' -force" end end @@ -149,6 +145,17 @@ class Chef end end end + + # @return [String] the hdiutil flag for handling DMGs with a password + def passphrase_cmd + new_resource.dmg_passphrase ? "-passphrase #{new_resource.dmg_passphrase}" : "" + end + + # @return [Boolean] does the DMG require a software license agreement + def software_license_agreement? + # example hdiutil imageinfo output: http://rubular.com/r/0xvOaA6d8B + /Software License Agreement: true/.match?(shell_out!("/usr/bin/hdiutil imageinfo #{passphrase_cmd} '#{dmg_file}'").stdout) + end end end end |