diff --git a/.kitchen.yml b/.kitchen.yml index 6c925141..fb319ec9 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -18,11 +18,32 @@ platforms: run_list: yum::dnf_yum_compat - name: opensuse-13.2 - name: opensuse-leap-42.1 + - name: sles-12-sp1 + driver: + box: chef/sles-12-sp1-x86_64 # private box + - name: solaris-11.3 + driver: + box: chef/solaris-11.3 # private box - name: ubuntu-12.04 - name: ubuntu-14.04 - name: ubuntu-16.04 + - name: windows-2008r2 + driver: + box: chef/windows-server-2008r2-standard # private box + - name: windows-2012r2 + driver: + box: chef/windows-server-2012r2-standard # private box suites: - name: default run_list: - recipe[ark_spec] + excludes: + - windows-2008r2 + - windows-2012r2 +- name: windows + run_list: + - recipe[ark_spec::windows] + includes: + - windows-2008r2 + - windows-2012r2 diff --git a/README.md b/README.md index a966df8e..0383fa89 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # ark cookbook + [![Build Status](https://travis-ci.org/chef-cookbooks/ark.svg?branch=master)](https://travis-ci.org/chef-cookbooks/ark) [![Cookbook Version](https://img.shields.io/cookbook/v/ark.svg)](https://supermarket.chef.io/cookbooks/ark) ## Overview + This cookbook provides `ark`, a resource for managing software archives. It manages the fetch-unpack-configure-build-install process common to installing software from source, or from binary distributions that are not fully fledged OS packages. This cookbook started its life as a modified version of Infochimp's install_from cookbook. It has since been heavily refactored and extended to meet different use cases. @@ -15,8 +17,9 @@ end ``` The provider will: + - fetch it to to `/var/cache/chef/` -- unpack it to the default path (`/usr/local/pig-0.8.0`) +- unpack it to the default path (`/usr/local/pig-0.8.0`) - create a symlink for `:home_dir` (`/usr/local/pig`) pointing to path - add specified binary commands to the environment `PATH` variable @@ -25,7 +28,9 @@ By default, the ark will not run again if the `:path` is not empty. Ark provides At this time ark only handles files available from URLs using the [remote_file](http://docs.chef.io/resource_remote_file.html) provider. It does handle local files using the `file://` protocol. ## Requirements + ### Platforms + - Debian/Ubuntu - RHEL/CentOS/Scientific/Oracle - Fedora @@ -33,30 +38,38 @@ At this time ark only handles files available from URLs using the [remote_file]( - SmartOS - Mac OS X - openSUSE / SUSE Linux Enterprises +- Windows Should work on common Unix/Linux systems with typical userland utilities like tar, gzip, etc. May require the installation of build tools for compiling from source, but that installation is outside the scope of this cookbook. ### Chef + - Chef 12.1+ ### Cookbooks + - build-essential - seven_zip - windows ## Attributes + Customize the attributes to suit site specific conventions and defaults. -- `node['ark']['apache_mirror']` - if the URL is an apache mirror, use the attribute as the default. -- `node['ark']['prefix_root']` - default base location if the `prefix_root` is not passed into the resource. -- `node['ark']['prefix_bin']` - default binary location if the `prefix_bin` is not passed into the resource. -- `node['ark']['prefix_home']` - default home location if the `prefix_home` is not passed into the resource. -- `node['ark']['win_install_dir']` - directory where the files will be put on windows -- `node['ark']['package_dependencies']` - prerequisite system packages that need to be installed to support ark. + +- `node['ark']['apache_mirror']` - if the URL is an apache mirror, use the attribute as the default. default: `http://apache.mirrors.tds.net` +- `node['ark']['prefix_root']` - default base location if the `prefix_root` is not passed into the resource. default: `/usr/local` +- `node['ark']['prefix_bin']` - default binary location if the `prefix_bin` is not passed into the resource. default: `/usr/local/bin` +- `node['ark']['prefix_home']` - default home location if the `prefix_home` is not passed into the resource. default: `/usr/local` +- `node['ark']['package_dependencies']` - prerequisite system packages that need to be installed to support ark. default: varies based on platform +- `node['ark']['tar']` - allows overriding the default path to the tar binary, which varies based on platform +- `node['ark']['sevenzip_binary']` - allows overriding the default path to the 7zip binary, which is determined based on registry key value ## Resources + - `ark` - does the extract/build/configure dance ### Actions + - `:install`: extracts the file and creates a 'friendly' symbolic link to the extracted directory path - `:configure`: configure ahead of the install action - `:install_with_make`: extracts the archive to a path, runs `make`, and `make install`. @@ -69,34 +82,43 @@ Customize the attributes to suit site specific conventions and defaults. - `:setup_py_install`: runs the command "python setup.py install" in the extracted directory ### :cherry_pick + Extract a specified file from an archive and places in specified path. #### Relevant Attribute Parameters for :cherry_pick + - `path`: directory to place file in. - `creates`: specific file to cherry-pick. ### :dump + Strips all directories from the archive and dumps the contained files into a specified path. NOTE: This currently only works for zip archives #### Attribute Parameters for :dump + - `path`: path to dump files to. - `mode`: file mode for `app_home`, as an integer. + - Example: `0775` - `creates`: if you are appending files to a given directory, ark needs a condition to test whether the file has already been extracted. You can specify with creates, a file whose existence indicates the ark has previously been extracted and does not need to be extracted again. ### :put + Extract the archive to a specified path, does not create any symbolic links. #### Attribute Parameters for :put + - `path`: path to extract to. + - Default: `/usr/local` - `append_env_path`: boolean, if true, append the `./bin` directory of the extracted directory to the global `PATH` variable for all users. ### Attribute Parameters + - `name`: name of the package, defaults to the resource name. - `url`: url for tarball, `.tar.gz`, `.bin` (oracle-specific), `.war`, and `.zip` currently supported. Also supports special syntax - `:name:version:apache_mirror:` that will auto-magically construct download url from the apache mirrors site. @@ -104,39 +126,51 @@ Extract the archive to a specified path, does not create any symbolic links. - `mode`: file mode for `app_home`, is an integer. - `prefix_root`: default `prefix_root`, for use with `:install*` actions. - `prefix_home`: default directory prefix for a friendly symlink to the path. + - Example: `/usr/local/maven` -> `/usr/local/maven-2.2.1` - `prefix_bin`: default directory to place a symlink to a binary command. + - Example: `/opt/bin/mvn` -> `/opt/maven-2.2.1/bin/mvn`, where the `prefix_bin` is `/opt/bin` - `path`: path to extract the ark to. The `:install*` actions overwrite any user-provided values for `:path`. + - Default: `/usr/local/-` for the `:install`, `:install_with_make` actions - `home_dir`: symbolic link to the path `:prefix_root/:name-:version`, does not apply to `:dump`, `:put`, or `:cherry_pick` actions. + - Default: `:prefix_root/:name` - `has_binaries`: array of binary commands to symlink into `/usr/local/bin/`, you must specify the relative path. + - Example: `[ 'bin/java', 'bin/javaws' ]` - `append_env_path`: boolean, similar to `has_binaries` but less granular. If true, append the `./bin` directory of the extracted directory to. the `PATH` environment variable for all users, by placing a file in `/etc/profile.d/`. The commands are symbolically linked into `/usr/bin/*`. This option provides more granularity than the boolean option. + - Example: `mvn`, `java`, `javac`, etc. - `environment`: hash of environment variables to pass to invoked shell commands like `tar`, `unzip`, `configure`, and `make`. + - `strip_components`: number of components in path to strip when extracting archive. With default value of `1`, ark strips the leading directory from an archive, which is the default for both `unzip` and `tar` commands. + - `autoconf_opts`: an array of command line options for use with the GNU `autoconf` script. + - Example: `[ '--include=/opt/local/include', '--force' ]` - `make_opts`: an array of command line options for use with `make`. + - Example: `[ '--warn-undefined-variables', '--load-average=2' ]` - `owner`: owner of extracted directory. + - Default: `root` - `backup`: The number of backups to be kept in /var/chef/backup (for UNIX- and Linux-based platforms) or C:/chef/backup (for the Microsoft Windows platform). Set to false to prevent backups from being kept. - - Default: `5` + - Default: `5` #### 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` ```ruby @@ -163,7 +197,7 @@ This example copies `jdk-7u2-linux-x64.tar.gz` to `/var/cache/chef/jdk-7.2.tar.g end ``` -Install Apache Ivy dependency resolution tool in /resource_name in this case `/usr/local/ivy`, do not symlink, and strip any leading directory if one exists in the tarball: +Install Apache Ivy dependency resolution tool in `/resource_name` in this case `/usr/local/ivy`, do not symlink, and strip any leading directory if one exists in the tarball: ```ruby ark "ivy" do @@ -173,7 +207,7 @@ Install Apache Ivy dependency resolution tool in /resource_name in this ca end ``` -Install Apache Ivy dependency resolution tool in /home/foobar/ivy, strip any leading directory if one exists, don't keep backup copies of ivy.tar.gz: +Install Apache Ivy dependency resolution tool in `/home/foobar/ivy`, strip any leading directory if one exists, don't keep backup copies of `ivy.tar.gz`: ```ruby ark "ivy" do @@ -185,7 +219,7 @@ Install Apache Ivy dependency resolution tool in /home/foobar/ivy, strip any lea end ``` - Strip all directories and dump files into path specified by the path attribute. You must specify the `creates` attribute in order to keep the extraction from running every time. The directory path will be created if it doesn't already exist: +Strip all directories and dump files into path specified by the path attribute. You must specify the `creates` attribute in order to keep the extraction from running every time. The directory path will be created if it doesn't already exist: ```ruby ark "my_jars" do @@ -234,6 +268,7 @@ You can also pass multiple actions to ark and supply the file extension in case ``` ## License & Authors + - Author: Philip (flip) Kromer - Infochimps, Inc([coders@infochimps.com](mailto:coders@infochimps.com)) - Author: Bryan W. Berry ([bryan.berry@gmail.com](mailto:bryan.berry@gmail.com)) - Author: Denis Barishev ([denis.barishev@gmail.com](mailto:denis.barishev@gmail.com)) diff --git a/attributes/default.rb b/attributes/default.rb index 945e1d96..0f44dbd1 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -20,17 +20,12 @@ default['ark']['prefix_root'] = '/usr/local' default['ark']['prefix_bin'] = '/usr/local/bin' default['ark']['prefix_home'] = '/usr/local' -default['ark']['tar'] = case node['platform_family'] - when 'windows' - "\"#{::Win32::Registry::HKEY_LOCAL_MACHINE.open( - 'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe', ::Win32::Registry::KEY_READ).read_s('Path')}\\7z.exe\"" - when 'mac_os_x', 'freebsd' - '/usr/bin/tar' - when 'smartos' - '/bin/gtar' - else - '/bin/tar' - end + +# the default path will be determined based on platform, but can be overridden here +default['ark']['tar'] = nil + +# the default path will be determined from the registry, but you may override here +default['ark']['sevenzip_binary'] = nil pkgs = %w(libtool autoconf) unless platform_family?('mac_os_x') pkgs += %w(make) unless platform_family?('mac_os_x', 'freebsd') diff --git a/libraries/sevenzip_command_builder.rb b/libraries/sevenzip_command_builder.rb index 48e62952..8d8a9934 100644 --- a/libraries/sevenzip_command_builder.rb +++ b/libraries/sevenzip_command_builder.rb @@ -20,6 +20,10 @@ def initialize(resource) 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') @@ -40,7 +44,18 @@ def sevenzip_command end def sevenzip_binary - resource.run_context.node['ark']['tar'] + @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) diff --git a/libraries/tar_command_builder.rb b/libraries/tar_command_builder.rb index 00ed522c..291dbb97 100644 --- a/libraries/tar_command_builder.rb +++ b/libraries/tar_command_builder.rb @@ -25,7 +25,14 @@ def node end def tar_binary - resource.run_context.node['ark']['tar'] + @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 diff --git a/spec/fixtures/cookbooks/ark_spec/recipes/default.rb b/spec/fixtures/cookbooks/ark_spec/recipes/default.rb index 75c8f474..b02c96d9 100644 --- a/spec/fixtures/cookbooks/ark_spec/recipes/default.rb +++ b/spec/fixtures/cookbooks/ark_spec/recipes/default.rb @@ -1,4 +1,4 @@ -apt_update 'update' +apt_update 'update' if platform_family?('debian') include_recipe 'ark' @@ -12,10 +12,10 @@ action :nothing end -group 'foobarbaz' +group 'foobarbaz_group' user 'foobarbaz' do - group 'foobarbaz' + group 'foobarbaz_group' end directory '/opt/bin' do @@ -28,7 +28,7 @@ version '2' prefix_root '/usr/local' owner 'foobarbaz' - group 'foobarbaz' + group 'foobarbaz_group' has_binaries ['bin/do_foo', 'bin/do_more_foo'] action :install end @@ -37,7 +37,7 @@ url 'https://github.com/burtlo/ark/raw/master/files/default/foo.tar.gz' checksum '5996e676f17457c823d86f1605eaa44ca8a81e70d6a0e5f8e45b51e62e0c52e8' owner 'foobarbaz' - group 'foobarbaz' + group 'foobarbaz_group' action :put end @@ -47,7 +47,7 @@ path '/usr/local/foo_dump' creates 'foo1.txt' owner 'foobarbaz' - group 'foobarbaz' + group 'foobarbaz_group' action :dump end @@ -56,7 +56,7 @@ checksum '5996e676f17457c823d86f1605eaa44ca8a81e70d6a0e5f8e45b51e62e0c52e8' path '/usr/local/foo_cherry_pick' owner 'foobarbaz' - group 'foobarbaz' + group 'foobarbaz_group' creates 'foo_sub/foo1.txt' action :cherry_pick end @@ -108,7 +108,7 @@ prefix_home '/opt' prefix_bin '/opt/bin' owner 'foobarbaz' - group 'foobarbaz' + group 'foobarbaz_group' has_binaries ['bin/do_foo'] action :install end diff --git a/spec/fixtures/cookbooks/ark_spec/recipes/install.rb b/spec/fixtures/cookbooks/ark_spec/recipes/install.rb index 37ca9b41..06c89b63 100644 --- a/spec/fixtures/cookbooks/ark_spec/recipes/install.rb +++ b/spec/fixtures/cookbooks/ark_spec/recipes/install.rb @@ -5,6 +5,6 @@ version '2' prefix_root '/usr/local' owner 'foobarbaz' - group 'foobarbaz' + group 'foobarbaz_group' action :install end diff --git a/spec/fixtures/cookbooks/ark_spec/recipes/windows.rb b/spec/fixtures/cookbooks/ark_spec/recipes/windows.rb new file mode 100644 index 00000000..ab60bcca --- /dev/null +++ b/spec/fixtures/cookbooks/ark_spec/recipes/windows.rb @@ -0,0 +1,18 @@ +include_recipe 'ark' + +group 'foobar' + +user 'foobarbaz' do + group 'foobarbaz' +end + +ark 'foo' do + url 'https://github.com/burtlo/ark/raw/master/files/default/foo.tar.gz' + checksum '5996e676f17457c823d86f1605eaa44ca8a81e70d6a0e5f8e45b51e62e0c52e8' + version '2' + win_install_dir 'C:\dir' + owner 'foobarbaz' + group 'foobar' + has_binaries ['bin\do_foo', 'bin\do_more_foo'] + action :install +end diff --git a/spec/libraries/default_spec.rb b/spec/libraries/default_spec.rb index e70dd843..020ced5a 100644 --- a/spec/libraries/default_spec.rb +++ b/spec/libraries/default_spec.rb @@ -121,6 +121,8 @@ end describe '#cherry_pick_command' do + include_context 'seven zip installed' + context "when the node's platform_family is windows" do it 'generates a 7-zip command' do with_node_attributes(platform_family: 'windows') @@ -132,7 +134,7 @@ run_context: double(node: { 'ark' => { 'tar' => 'sevenzip_command' } }) ) - expect(cherry_pick_command).to eq('sevenzip_command e "/resource/release_file" -o"/resource/path" -uy -r /resource/creates') + expect(cherry_pick_command).to eq('C:\\Program Files\\7-Zip7z.exe e "/resource/release_file" -o"/resource/path" -uy -r /resource/creates') end end diff --git a/spec/recipes/default_spec.rb b/spec/recipes/default_spec.rb index 495ab1b8..09a663af 100644 --- a/spec/recipes/default_spec.rb +++ b/spec/recipes/default_spec.rb @@ -29,9 +29,5 @@ it 'prefix home' do expect(default_cookbook_attribute('prefix_home')).to eq '/usr/local' end - - it 'tar binary' do - expect(default_cookbook_attribute('tar')).to eq '/bin/tar' - end end end diff --git a/spec/recipes/suse/default_spec.rb b/spec/recipes/suse/default_spec.rb index dcf4c5a5..38fb25b6 100644 --- a/spec/recipes/suse/default_spec.rb +++ b/spec/recipes/suse/default_spec.rb @@ -6,6 +6,6 @@ def node_attributes end it 'installs core packages' do - expect(chef_run).to install_package(%w(libtool autoconf make unzip rsync gcc)) + expect(chef_run).to install_package(%w(libtool autoconf make unzip rsync gcc xz bzip2 tar)) end end diff --git a/spec/recipes/windows/default_spec.rb b/spec/recipes/windows/default_spec.rb index 6bdf2608..fe7f6486 100644 --- a/spec/recipes/windows/default_spec.rb +++ b/spec/recipes/windows/default_spec.rb @@ -1,8 +1,6 @@ require 'spec_helper' describe_recipe 'ark::default' do - include_context 'seven zip installed' - def node_attributes { platform: 'windows', version: '2012R2' } end @@ -10,10 +8,4 @@ def node_attributes it 'does include the 7-zip recipe' do expect(chef_run).to include_recipe('seven_zip') end - - context 'sets default attributes' do - it 'tar binary' do - expect(default_cookbook_attribute('tar')).to eq %("C:\\Program Files\\7-Zip\\7z.exe") - end - end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 855127b4..272855d3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -114,7 +114,7 @@ def recipe_name fake_hkey_local_machine = double('fake_hkey_local_machine') seven_zip_win32_registry = double('seven_zip_registry') allow(seven_zip_win32_registry).to receive(:read_s).with('Path').and_return('C:\\Program Files\\7-Zip') - allow(fake_hkey_local_machine).to receive(:open).with('SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe', ::Win32::Registry::KEY_READ).and_return(seven_zip_win32_registry) + allow(fake_hkey_local_machine).to receive(:open).with('SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe').and_return(seven_zip_win32_registry) fake_hkey_local_machine end @@ -125,7 +125,7 @@ class Registry end end end - stub_const('::Win32::Registry::KEY_READ', double('win32_registry_key_read')) unless defined? ::Win32::Registry::KEY_READ stub_const('::Win32::Registry::HKEY_LOCAL_MACHINE', fake_hkey_local_machine) + stub_const('::Win32::Registry::Error', double('win32_registry_error')) unless defined? ::Win32::Registry::Error end end