Skip to content

Commit

Permalink
new resource: 'conda_package', with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thmttch committed Jul 6, 2014
1 parent be18a53 commit 412e856
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 0.3.0

## 0.2.1

Fix incorrect checksums for Anaconda 2.0.1

## 0.2.0

Feature improvments:
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ include_recipe 'anaconda::default'

## Usage, recipes, and attributes

This cookbook only has one recipe: `anaconda::default`. Include
it in your runlist, and it will install the package as well as any necessary
dependencies.
The main recipe is `anaconda::default`. Include it in your runlist, and it will
install the package as well as any necessary dependencies.

The following are user-configurable attributes. Check
[attributes/default.rb](attributes/default.rb) for default values.
Expand All @@ -72,6 +71,10 @@ The following are user-configurable attributes. Check
- `owner`: the user who owns the install
- `group`: the group who owns the install

### `recipe[anaconda::shell-conveniences]`

Include this to have a

## Tests

Run the full test suite:
Expand Down
4 changes: 3 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ Vagrant.configure('2') do |config|
}

chef.run_list = [
'recipe[anaconda::default]',
#'recipe[anaconda::default]',
#'recipe[anaconda::shell-conveniences]',
'recipe[anaconda::package_tests]',
]
end
end
2 changes: 2 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# specific versions are installed _under_ this directory
default.anaconda.install_root = '/opt/anaconda'
default.anaconda.accept_license = 'no'
default.anaconda.install_logfile = ''

default.anaconda.owner = 'vagrant'
default.anaconda.group = 'vagrant'
default.anaconda.home = "/home/#{node.anaconda.owner}"
10 changes: 10 additions & 0 deletions libraries/matchers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
if defined?(ChefSpec)

def run_bash(resource_name)
ChefSpec::Matchers::ResourceMatcher.new('bash', :run, resource_name)
end

def install_conda_package(resource_name)
ChefSpec::Matchers::ResourceMatcher.new('anaconda_package', :install, resource_name)
end

def remove_conda_package(resource_name)
ChefSpec::Matchers::ResourceMatcher.new('anaconda_package', :remove, resource_name)
end

end
84 changes: 84 additions & 0 deletions providers/package.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
def whyrun_supported?
true
end

def cmd_conda
"#{node.anaconda.install_root}/#{node.anaconda.version}/bin/conda"
end

def is_installed?(package_name)
`"#{cmd_conda}" list`.include?(package_name)
end

def log_opts(node)
if node.anaconda.install_log
"2>&1 >#{node.anaconda.install_log}"
else
''
end
end

action :install do
r = new_resource
name = r.name
package_name = r.package_name || name

Chef::Log.info "installing conda package: #{package_name}"

cmd_opts = [
# --yes: automated install (don't ask)
"--yes",

r.env_name ? "--name #{r.env_name}" : '',
r.env_path_prefix ? "--name #{r.env_path_prefix}" : '',

r.channel ? "--channel #{r.channel}" : '',
r.override_channels ? '--override-channels' : '',

r.no_pin ? '--no-pin' : '',

r.force_install ? '--force' : '',

r.revision ? "--revision #{r.revision}" : '',
r.file ? "--file #{r.file}" : '',
r.unknown ? '--unknown' : '',
r.no_deps ? '--no_deps' : '',
r.mkdir ? '--mkdir' : '',
r.use_index_cache ? '--use-index-cache' : '',
r.use_local ? '--use-local' : '',
r.alt_hint ? '--alt-hint' : '',
].join(" ")

execute "conda_package_install_#{package_name}" do
command "#{cmd_conda} install #{package_name} #{cmd_opts} #{log_opts(node)}"
only_if { !is_installed?(package_name) || r.force_install }
end
end

action :remove do
r = new_resource
name = r.name
package_name = r.package_name || name

Chef::Log.info "removing conda package: #{package_name}"

cmd_opts = [
# --yes: automated install (don't ask)
"--yes",

r.env_name ? "--name #{r.env_name}" : '',
r.env_path_prefix ? "--name #{r.env_path_prefix}" : '',

r.channel ? "--channel #{r.channel}" : '',
r.override_channels ? '--override-channels' : '',

r.no_pin ? '--no-pin' : '',

r.all ? '--all' : '',
r.features ? '--features' : '',
].join(' ')

execute "conda_package_remove_#{package_name}" do
command "#{cmd_conda} remove #{package_name} #{cmd_opts} #{log_opts(node)}"
end
end
20 changes: 20 additions & 0 deletions recipes/package_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Cookbook Name:: anaconda
# Recipe:: package_tests
#
# Copyright (C) 2014 Matt Chu
#
# All rights reserved - Do Not Redistribute
#

log 'do NOT include this in your runlist! for testing only.' do
level :error
end

anaconda_package 'astroid' do
action :install
end

anaconda_package 'astroid' do
action :remove
end
17 changes: 17 additions & 0 deletions recipes/shell-conveniences.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Cookbook Name:: anaconda
# Recipe:: shell-conveniences
#
# Copyright (C) 2014 Matt Chu
#
# All rights reserved - Do Not Redistribute
#

template "#{node.anaconda.home}/source-me.sh" do
source 'source-me.sh.erb'
variables({
:install_root => node.anaconda.install_root,
:version => node.anaconda.version,
})
end

34 changes: 34 additions & 0 deletions resources/package.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
actions :install, :remove
default_action :install

attribute :name, :kind_of => String, :name_attribute => true

# attributes for both install and remove

attribute :package_name, :kind_of => String

attribute :env_name, :kind_of => String
attribute :env_path_prefix, :kind_of => String

attribute :channel, :kind_of => String
attribute :override_channels, :kind_of => [ TrueClass, FalseClass ]

attribute :no_pin, :kind_of => [ TrueClass, FalseClass ]

# attributes for install only

attribute :force_install, :kind_of => [ TrueClass, FalseClass ]

attribute :revision, :kind_of => String
attribute :file, :kind_of => String
attribute :unknown, :kind_of => [ TrueClass, FalseClass ]
attribute :no_deps, :kind_of => [ TrueClass, FalseClass ]
attribute :mkdir, :kind_of => [ TrueClass, FalseClass ]
attribute :use_index_cache, :kind_of => [ TrueClass, FalseClass ]
attribute :use_local, :kind_of => [ TrueClass, FalseClass ]
attribute :alt_hint, :kind_of => [ TrueClass, FalseClass ]

# attributes for remove only

attribute :all, :kind_of => [ TrueClass, FalseClass ]
attribute :features, :kind_of => [ TrueClass, FalseClass ]
13 changes: 13 additions & 0 deletions spec/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
expect(chef_run).to render_file(installer_config_path).with_content(/.*\n.*\n.*\n.*/)
end

it 'exposes the conda_package resource' do
chef_run.converge('recipe[anaconda::package_tests]')

expect(chef_run).to install_conda_package('astroid')
expect(chef_run).to remove_conda_package('astroid')
end

it 'provides a convenience shell script' do
chef_run.converge('recipe[anaconda::shell-conveniences]')

expect(chef_run).to create_template("#{chef_run.node.anaconda.home}/source-me.sh")
end

end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# https://github.com/sethvargo/chefspec#reporting
require 'chefspec'
ChefSpec::Coverage.start! do
end

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
Expand Down
3 changes: 3 additions & 0 deletions templates/default/source-me.sh.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

export PATH=<%= @install_root %>/<%= @version %>/bin:${PATH}
4 changes: 2 additions & 2 deletions test/integration/default/serverspec/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

describe 'general tests' do

it 'installs conda' do
command('/opt/anaconda/1.9.2/bin/conda --version').should return_stdout 'conda 3.4.1'
it 'installs conda 2.0.1' do
command('/opt/anaconda/2.0.1/bin/conda --version').should return_stdout 'conda 3.5.5'
end

end

0 comments on commit 412e856

Please sign in to comment.