Skip to content

Commit

Permalink
[sous-chefsGH-196] Add of whyrun support
Browse files Browse the repository at this point in the history
Ark resource wasn't supporting whyrun mode because of its overwritting
of resource attributes set by user recipes (especially the path attr).

By introducing new "internal" resource attribute ("_deploy_path" and
"_release_file"), which are set by and only by ark's resource internal
implementation, this resource is now whyrun compliant and constructs
involving `notifies :action, 'other_resource[name]', :before` are now
possible.

Signed-off-by: Christophe Sourisse <[email protected]>
  • Loading branch information
dosyfier committed Aug 25, 2019
1 parent 32d4978 commit 94501d1
Show file tree
Hide file tree
Showing 21 changed files with 347 additions and 202 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ matrix:
include:
- script:
- chef exec delivery local all
env: UNIT_AND_LINT=1
env:
- UNIT_AND_LINT=1
- CHEF_LICENSE=accept
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ Extract the archive to a specified path, does not create any symbolic links.

- Default: `5`

N.B. Some attributes, prefixed by '_', are reserved to ark resource internal implementation. Setting anyone of them wouldn't have any effect.

#### Examples

This example copies `ivy.tar.gz` to `/var/cache/chef/ivy-2.2.0.tar.gz`, unpacks its contents to `/usr/local/ivy-2.2.0/` -- stripping the leading directory, and symlinks `/usr/local/ivy` to `/usr/local/ivy-2.2.0`
Expand Down
1 change: 1 addition & 0 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ driver:

provisioner:
name: chef_zero
chef_license: accept
deprecations_as_errors: true

platforms:
Expand Down
20 changes: 11 additions & 9 deletions libraries/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,9 @@ def set_paths
new_resource.version = defaults.version
new_resource.owner = defaults.owner

# TODO: what happens when the path is already set --
# with the current logic we overwrite it
# if you are in windows we overwrite it
# otherwise we overwrite it with the root/name-version
new_resource.path = defaults.path
new_resource.release_file = defaults.release_file
# Calculate internal properties
new_resource._deploy_path = defaults.path
new_resource._release_file = defaults.release_file
end

def set_put_paths
Expand All @@ -65,13 +62,18 @@ def set_put_paths
# TODO: Should we be setting the prefix_root -
# as the prefix_root could be used in the path_with_version
# new_resource.prefix_root = default.prefix_root
new_resource.path = defaults.path_without_version
new_resource.release_file = defaults.release_file_without_version

# Calculate internal properties
new_resource._deploy_path = defaults.path_without_version
new_resource._release_file = defaults.release_file_without_version
end

def set_dump_paths
new_resource.extension = defaults.extension
new_resource.release_file = defaults.release_file_without_version

# Calculate internal properties
new_resource._deploy_path = new_resource.path
new_resource._release_file = defaults.release_file_without_version
end

def unpack_command
Expand Down
2 changes: 1 addition & 1 deletion libraries/general_owner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(resource)
attr_reader :resource

def command
"chown -R #{resource.owner}:#{resource.group} #{resource.path}"
"chown -R #{resource.owner}:#{resource.group} #{resource._deploy_path}"
end
end
end
10 changes: 5 additions & 5 deletions libraries/sevenzip_command_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ def unpack
end

def dump
sevenzip_command_builder(resource.path, 'e')
sevenzip_command_builder(resource._deploy_path, 'e')
end

def cherry_pick
"#{sevenzip_command_builder(resource.path, 'x')} -r #{resource.creates}"
"#{sevenzip_command_builder(resource._deploy_path, 'x')} -r #{resource.creates}"
end

def initialize(resource)
Expand All @@ -25,7 +25,7 @@ def node
end

def sevenzip_command
return sevenzip_command_builder(resource.path, 'x') if resource.strip_components <= 0
return sevenzip_command_builder(resource._deploy_path, 'x') if resource.strip_components <= 0

tmpdir = make_temp_directory.tr('/', '\\')
cmd = sevenzip_command_builder(tmpdir, 'x')
Expand All @@ -38,7 +38,7 @@ def sevenzip_command
currdir += "\\%#{count}"
end

cmd += "(\"#{ENV.fetch('SystemRoot')}\\System32\\robocopy\" \"#{currdir}\" \"#{resource.path}\" /s /e) ^& IF %ERRORLEVEL% LEQ 3 cmd /c exit 0"
cmd += "(\"#{ENV.fetch('SystemRoot')}\\System32\\robocopy\" \"#{currdir}\" \"#{resource._deploy_path}\" /s /e) ^& IF %ERRORLEVEL% LEQ 3 cmd /c exit 0"
end

def sevenzip_binary
Expand All @@ -57,7 +57,7 @@ def sevenzip_path_from_registry
end

def sevenzip_command_builder(dir, command)
"#{sevenzip_binary} #{command} \"#{resource.release_file}\"#{extension_is_tar} -o\"#{dir}\" -uy"
"#{sevenzip_binary} #{command} \"#{resource._release_file}\"#{extension_is_tar} -o\"#{dir}\" -uy"
end

def extension_is_tar
Expand Down
6 changes: 3 additions & 3 deletions libraries/tar_command_builder.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module Ark
class TarCommandBuilder
def unpack
"#{tar_binary} #{args} #{resource.release_file}#{strip_args}"
"#{tar_binary} #{args} #{resource._release_file}#{strip_args}"
end

def dump
"tar -mxf \"#{resource.release_file}\" -C \"#{resource.path}\""
"tar -mxf \"#{resource._release_file}\" -C \"#{resource._deploy_path}\""
end

def cherry_pick
"#{tar_binary} #{args} #{resource.release_file} -C #{resource.path} #{resource.creates}#{strip_args}"
"#{tar_binary} #{args} #{resource._release_file} -C #{resource._deploy_path} #{resource.creates}#{strip_args}"
end

def initialize(resource)
Expand Down
14 changes: 7 additions & 7 deletions libraries/unzip_command_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ def unpack
if resource.strip_components > 0
unzip_with_strip_components
else
"unzip -q -o #{resource.release_file} -d #{resource.path}"
"unzip -q -o #{resource._release_file} -d #{resource._deploy_path}"
end
end

def dump
"unzip -j -q -o \"#{resource.release_file}\" -d \"#{resource.path}\""
"unzip -j -q -o \"#{resource._release_file}\" -d \"#{resource._deploy_path}\""
end

def cherry_pick
cmd = "unzip -t #{resource.release_file} \"*/#{resource.creates}\" ; stat=$? ;"
cmd = "unzip -t #{resource._release_file} \"*/#{resource.creates}\" ; stat=$? ;"
cmd += 'if [ $stat -eq 11 ] ; then '
cmd += "unzip -j -o #{resource.release_file} \"#{resource.creates}\" -d #{resource.path} ;"
cmd += "unzip -j -o #{resource._release_file} \"#{resource.creates}\" -d #{resource._deploy_path} ;"
cmd += 'elif [ $stat -ne 0 ] ; then false ;'
cmd += 'else '
cmd += "unzip -j -o #{resource.release_file} \"*/#{resource.creates}\" -d #{resource.path} ;"
cmd += "unzip -j -o #{resource._release_file} \"*/#{resource.creates}\" -d #{resource._deploy_path} ;"
cmd += 'fi'
cmd
end
Expand All @@ -34,8 +34,8 @@ def initialize(resource)
def unzip_with_strip_components
tmpdir = make_temp_directory
strip_dir = '*/' * resource.strip_components
cmd = "unzip -q -o #{resource.release_file} -d #{tmpdir}"
cmd += " && rsync -a #{tmpdir}/#{strip_dir} #{resource.path}"
cmd = "unzip -q -o #{resource._release_file} -d #{tmpdir}"
cmd += " && rsync -a #{tmpdir}/#{strip_dir} #{resource._deploy_path}"
cmd += " && rm -rf #{tmpdir}"
cmd
end
Expand Down
2 changes: 1 addition & 1 deletion libraries/windows_owner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(resource)
attr_reader :resource

def command
"#{ENV.fetch('SystemRoot')}\\System32\\icacls \"#{resource.path}\\*\" /setowner \"#{resource.owner}\""
"#{ENV.fetch('SystemRoot')}\\System32\\icacls \"#{resource._deploy_path}\\*\" /setowner \"#{resource.owner}\""
end
end
end
Loading

0 comments on commit 94501d1

Please sign in to comment.