Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move tar/7zip path logic out of attributes and into helpers #160

Merged
merged 8 commits into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 46 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

Expand All @@ -25,38 +28,48 @@ 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
- FreeBSD
- 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`.
Expand All @@ -69,74 +82,95 @@ 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.
- `version`: software version, defaults to `1`.
- `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/<name>-<version>` 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
Expand All @@ -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 <path>/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
Expand All @@ -173,7 +207,7 @@ Install Apache Ivy dependency resolution tool in <path>/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
Expand All @@ -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
Expand Down Expand Up @@ -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([[email protected]](mailto:[email protected]))
- Author: Bryan W. Berry ([[email protected]](mailto:[email protected]))
- Author: Denis Barishev ([[email protected]](mailto:[email protected]))
Expand Down
17 changes: 6 additions & 11 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
17 changes: 16 additions & 1 deletion libraries/sevenzip_command_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion libraries/tar_command_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions spec/fixtures/cookbooks/ark_spec/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apt_update 'update'
apt_update 'update' if platform_family?('debian')

include_recipe 'ark'

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -47,7 +47,7 @@
path '/usr/local/foo_dump'
creates 'foo1.txt'
owner 'foobarbaz'
group 'foobarbaz'
group 'foobarbaz_group'
action :dump
end

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/cookbooks/ark_spec/recipes/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
version '2'
prefix_root '/usr/local'
owner 'foobarbaz'
group 'foobarbaz'
group 'foobarbaz_group'
action :install
end
Loading