Dev set up

This commit is contained in:
2019-07-01 10:15:24 -04:00
parent da1a3d5491
commit 23a4e98668
340 changed files with 23143 additions and 41 deletions

View File

@ -0,0 +1,112 @@
require_relative 'platform_specific_builders'
require_relative 'resource_deprecations'
require_relative 'resource_defaults'
require_relative 'sevenzip_command_builder'
require_relative 'unzip_command_builder'
require_relative 'tar_command_builder'
require_relative 'general_owner'
require_relative 'windows_owner'
module Ark
module ProviderHelpers
extend ::Ark::PlatformSpecificBuilders
generates_archive_commands_for :seven_zip,
when_the: -> { node['platform_family'] == 'windows' },
with_klass: ::Ark::SevenZipCommandBuilder
generates_archive_commands_for :unzip,
when_the: -> { new_resource.extension =~ /zip|war|jar/ },
with_klass: ::Ark::UnzipCommandBuilder
generates_archive_commands_for :tar,
when_the: -> { true },
with_klass: ::Ark::TarCommandBuilder
generates_owner_commands_for :windows,
when_the: -> { node['platform_family'] == 'windows' },
with_klass: ::Ark::WindowsOwner
generates_owner_commands_for :all_other_platforms,
when_the: -> { true },
with_klass: ::Ark::GeneralOwner
def deprecations
::Ark::ResourceDeprecations.on(new_resource)
end
def show_deprecations
deprecations.each { |message| Chef::Log.warn("DEPRECATED: #{message}") }
end
def defaults
@resource_defaults ||= ::Ark::ResourceDefaults.new(new_resource)
end
def set_paths
new_resource.extension = defaults.extension
new_resource.prefix_bin = defaults.prefix_bin
new_resource.prefix_root = defaults.prefix_root
new_resource.home_dir = defaults.home_dir
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
end
def set_put_paths
new_resource.extension = defaults.extension
# 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
end
def set_dump_paths
new_resource.extension = defaults.extension
new_resource.release_file = defaults.release_file_without_version
end
def unpack_command
archive_application.unpack
end
def dump_command
archive_application.dump
end
def cherry_pick_command
archive_application.cherry_pick
end
def unzip_command
archive_application.unpack
end
def owner_command
owner_builder_klass.new(new_resource).command
end
private
def archive_application
@archive_application ||= archive_builder_klass.new(new_resource)
end
def archive_builder_klass
new_resource.extension ||= defaults.extension
Ark::ProviderHelpers.archive_command_generators.find { |condition, _klass| instance_exec(&condition) }.last
end
def owner_builder_klass
Ark::ProviderHelpers.owner_command_generators.find { |condition, _klass| instance_exec(&condition) }.last
end
end
end

View File

@ -0,0 +1,13 @@
module Ark
class GeneralOwner
def initialize(resource)
@resource = resource
end
attr_reader :resource
def command
"chown -R #{resource.owner}:#{resource.group} #{resource.path}"
end
end
end

View File

@ -0,0 +1,23 @@
module Ark
module PlatformSpecificBuilders
def generates_archive_commands_for(_name, options)
condition = options[:when_the]
builder = options[:with_klass]
archive_command_generators.push [condition, builder]
end
def archive_command_generators
@archive_command_generators ||= []
end
def generates_owner_commands_for(_name, options)
condition = options[:when_the]
builder = options[:with_klass]
owner_command_generators.push [condition, builder]
end
def owner_command_generators
@owner_command_generators ||= []
end
end
end

View File

@ -0,0 +1,119 @@
module Ark
class ResourceDefaults
def extension
resource.extension || generate_extension_from_url(resource.url.clone)
end
def prefix_bin
resource.prefix_bin || prefix_bin_from_node_in_run_context
end
def prefix_root
resource.prefix_root || prefix_root_from_node_in_run_context
end
def home_dir
if resource.home_dir.nil? || resource.home_dir.empty?
prefix_home = resource.prefix_home || prefix_home_from_node_in_run_context
::File.join(prefix_home, resource.name)
else
resource.home_dir
end
end
def version
resource.version || default_version
end
def path
if windows?
resource.win_install_dir
else
::File.join(resource.prefix_root, "#{resource.name}-#{resource.version}")
end
end
def owner
resource.owner || default_owner
end
def windows?
node_in_run_context['platform_family'] == 'windows'
end
def path_without_version
partial_path = resource.path || prefix_root_from_node_in_run_context
::File.join(partial_path, resource.name)
end
def release_file
release_filename = "#{resource.name}-#{resource.version}.#{resource.extension}"
::File.join(file_cache_path, release_filename)
end
def release_file_without_version
release_filename = "#{resource.name}.#{resource.extension}"
::File.join(file_cache_path, release_filename)
end
def initialize(resource)
@resource = resource
end
private
attr_reader :resource
def generate_extension_from_url(url)
# purge any trailing redirect
url =~ %r{^https?:\/\/.*(.bin|bz2|gz|jar|tbz|tgz|txz|war|xz|zip|7z)(\/.*\/)}
url.gsub!(Regexp.last_match(2), '') unless Regexp.last_match(2).nil?
# remove trailing query string
release_basename = ::File.basename(url.gsub(/\?.*\z/, '')).gsub(/-bin\b/, '')
# (\?.*)? accounts for a trailing querystring
Chef::Log.debug("DEBUG: release_basename is #{release_basename}")
release_basename =~ /^(.+?)\.(jar|tar\.bz2|tar\.gz|tar\.xz|tbz|tgz|txz|war|zip|tar|7z)(\?.*)?/
Chef::Log.debug("DEBUG: file_extension is #{Regexp.last_match(2)}")
Regexp.last_match(2)
end
def prefix_bin_from_node_in_run_context
node_in_run_context['ark']['prefix_bin']
end
def prefix_root_from_node_in_run_context
node_in_run_context['ark']['prefix_root']
end
def prefix_home_from_node_in_run_context
node_in_run_context['ark']['prefix_home']
end
def default_version
'1'
end
def default_owner
if windows?
wmi_property_from_query(:name, "select * from Win32_UserAccount where sid like 'S-1-5-21-%-500' and LocalAccount=True")
else
'root'
end
end
def wmi_property_from_query(wmi_property, wmi_query)
@wmi = ::WIN32OLE.connect('winmgmts://')
result = @wmi.ExecQuery(wmi_query)
return nil unless result.each.count > 0
result.each.next.send(wmi_property)
end
def file_cache_path
Chef::Config[:file_cache_path]
end
def node_in_run_context
resource.run_context.node
end
end
end

View File

@ -0,0 +1,33 @@
module Ark
class ResourceDeprecations
def self.on(resource)
new(resource).warnings
end
def initialize(resource)
@resource = resource
end
attr_reader :resource
def warnings
applicable_deprecrations.map { |_, message| message }
end
def applicable_deprecrations
deprecations.select { |condition, _| send(condition) }
end
def deprecations
{ strip_leading_dir_feature: strip_leading_dir_feature_message }
end
def strip_leading_dir_feature
[true, false].include?(resource.strip_leading_dir)
end
def strip_leading_dir_feature_message
'strip_leading_dir attribute was deprecated. Use strip_components instead.'
end
end
end

View File

@ -0,0 +1,78 @@
module Ark
class SevenZipCommandBuilder
def unpack
sevenzip_command
end
def dump
sevenzip_command_builder(resource.path, 'e')
end
def cherry_pick
"#{sevenzip_command_builder(resource.path, 'x')} -r #{resource.creates}"
end
def initialize(resource)
@resource = resource
end
private
attr_reader :resource
def node
resource.run_context.node
end
def sevenzip_command
if resource.strip_components <= 0
return sevenzip_command_builder(resource.path, 'x')
end
tmpdir = make_temp_directory.tr('/', '\\')
cmd = sevenzip_command_builder(tmpdir, 'x')
cmd += ' && '
currdir = tmpdir
1.upto(resource.strip_components).each do |count|
cmd += "for /f %#{count} in ('dir /ad /b \"#{currdir}\"') do "
currdir += "\\%#{count}"
end
cmd += "(\"#{ENV.fetch('SystemRoot')}\\System32\\robocopy\" \"#{currdir}\" \"#{resource.path}\" /s /e) ^& IF %ERRORLEVEL% LEQ 3 cmd /c exit 0"
end
def sevenzip_binary
@tar_binary ||= "\"#{(node['ark']['sevenzip_binary'] || sevenzip_path_from_registry)}\""
end
def sevenzip_path_from_registry
begin
basepath = ::Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe').read_s('Path')
# users like pretty errors
rescue ::Win32::Registry::Error
raise 'Failed to find the path of 7zip binary by searching checking HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe\Path. Make sure to install 7zip before using this resource. If 7zip is installed and you still receive this message you can also specify the 7zip binary path by setting node["ark"]["sevenzip_binary"]'
end
"#{basepath}7z.exe"
end
def sevenzip_command_builder(dir, command)
"#{sevenzip_binary} #{command} \"#{resource.release_file}\"#{extension_is_tar} -o\"#{dir}\" -uy"
end
def extension_is_tar
if resource.extension =~ /tar.gz|tgz|tar.bz2|tbz|tar.xz|txz/
" -so | #{sevenzip_binary} x -aoa -si -ttar"
else
' -aoa' # force overwrite, Fixes #164
end
end
def make_temp_directory
require 'tmpdir'
Dir.mktmpdir
end
end
end

View File

@ -0,0 +1,56 @@
module Ark
class TarCommandBuilder
def unpack
"#{tar_binary} #{args} #{resource.release_file}#{strip_args}"
end
def dump
"tar -mxf \"#{resource.release_file}\" -C \"#{resource.path}\""
end
def cherry_pick
"#{tar_binary} #{args} #{resource.release_file} -C #{resource.path} #{resource.creates}#{strip_args}"
end
def initialize(resource)
@resource = resource
end
private
attr_reader :resource
def node
resource.run_context.node
end
def tar_binary
@tar_binary ||= node['ark']['tar'] || case node['platform_family']
when 'mac_os_x', 'freebsd'
'/usr/bin/tar'
when 'smartos'
'/bin/gtar'
else
'/bin/tar'
end
end
def args
case resource.extension
when /^(tar)$/ then 'xf'
when /^(tar.gz|tgz)$/ then 'xzf'
when /^(tar.bz2|tbz)$/ then 'xjf'
when /^(tar.xz|txz)$/ then 'xJf'
else raise unsupported_extension
end
end
def strip_args
resource.strip_components > 0 ? " --strip-components=#{resource.strip_components}" : ''
end
def unsupported_extension
"Don't know how to expand #{resource.url} (extension: #{resource.extension})"
end
end
end

View File

@ -0,0 +1,48 @@
module Ark
class UnzipCommandBuilder
def unpack
if resource.strip_components > 0
unzip_with_strip_components
else
"unzip -q -o #{resource.release_file} -d #{resource.path}"
end
end
def dump
"unzip -j -q -o \"#{resource.release_file}\" -d \"#{resource.path}\""
end
def cherry_pick
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 += 'elif [ $stat -ne 0 ] ; then false ;'
cmd += 'else '
cmd += "unzip -j -o #{resource.release_file} \"*/#{resource.creates}\" -d #{resource.path} ;"
cmd += 'fi'
cmd
end
def initialize(resource)
@resource = resource
end
private
attr_reader :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 += " && rm -rf #{tmpdir}"
cmd
end
def make_temp_directory
require 'tmpdir'
Dir.mktmpdir
end
end
end

View File

@ -0,0 +1,13 @@
module Ark
class WindowsOwner
def initialize(resource)
@resource = resource
end
attr_reader :resource
def command
"#{ENV.fetch('SystemRoot')}\\System32\\icacls \"#{resource.path}\\*\" /setowner \"#{resource.owner}\""
end
end
end