Commit cd06b3a4 authored by Tim Smith's avatar Tim Smith
Browse files

Remove the resource since chocolatey_package is built in with 12.7+


Signed-off-by: default avatarTim Smith <tsmith@chef.io>
parent 4b0b0386
source 'https://supermarket.chef.io'
metadata
group :integration do
cookbook 'chocolatey_specs', path: './spec/cookbooks/chocolatey_specs'
cookbook 'chocolatey_test', path: './test/cookbooks/chocolatey_test'
end
[![Cookbook Version](https://img.shields.io/cookbook/v/chocolatey.svg)](https://supermarket.getchef.com/cookbooks/chocolatey) [![Build Status](http://img.shields.io/travis/chocolatey/chocolatey-cookbook/master.svg)](https://travis-ci.org/chocolatey/chocolatey-cookbook)
# chocolatey Cookbook
# Description
[![Cookbook Version](https://img.shields.io/cookbook/v/chocolatey.svg)](https://supermarket.getchef.com/cookbooks/chocolatey) [![Build Status](http://img.shields.io/travis/chocolatey/chocolatey-cookbook/master.svg)](https://travis-ci.org/chocolatey/chocolatey-cookbook)
Install Chocolatey with the default recipe and manage packages with a handy resource/provider.
# Requirements
## Requirements
## Platform:
### Platform
- Windows
- Chef 11.6 or greater
## Cookbooks:
### Chef
- windows (~> 1.38)
- 12.7 or greater
# Notes
## Notes
As of Chocolatey version [0.9.8.24](https://github.com/chocolatey/chocolatey/blob/master/CHANGELOG.md#09824-july-3-2014) the install directory for Chocolatey has changed from `C:\Chocolatey` to `C:\ProgramData\Chocolatey`.
More information can be gotten from the [Chocolateywiki](https://github.com/chocolatey/chocolatey/wiki/DefaultChocolateyInstallReasoning).
# Attributes
## Attributes
All attributes below are pre-pended with `node['chocolatey']`
......@@ -34,67 +33,11 @@ Attribute | Description
`['install_vars']['chocolateyVersion']` | Version of Chocolatey to install, e.g. '0.9.9.11' | String | nil (download latest version)
`['install_vars']['chocolateyDownloadUrl']` | Chocolatey .nupkg file URL. Use this if you host an internal copy of the chocolatey.nupkg | String | <https://chocolatey.org/api/v2/package/chocolatey> (download from chocolatey.org)
# Recipes
- chocolatey::default
# Resources
- [chocolatey](#chocolatey)
## chocolatey
**Note:** On chef client versions 12.7.0 and greater, Use the `chocolatey_package` resource instead.
### Actions
- install: Install a chocolatey package (default)
- remove: Uninstall a chocolatey package
- upgrade: Update a chocolatey package
### Resource Properties
- package: package to manage (default name)
- source: The source to find the package(s) to install
- version: The version of the package to use.
- args: arguments to the installation.
- options: Hash of additional options to be sent to `choco.exe`
# Examples
```ruby
include_recipe 'chocolatey'
%w{sysinternals 7zip notepadplusplus GoogleChrome Console2}.each do |pack|
chocolatey pack
end
%w{bash openssh grep}.each do |pack|
chocolatey pack do
source 'cygwin'
end
end
chocolatey 'git.install' do
options ({ 'params' => "'/GitOnlyOnPath'" })
end
chocolatey 'wireshark' do
version '1.12.6'
action :install
end
chocolatey "some_private_secure_package" do
source "https://some.proget/feed"
options ({'u' => 'username', 'p' => 'password'})
end
chocolatey 'DotNet4.5'
## Recipes
chocolatey 'PowerShell'
```
- chocolatey::default - installs Chocolatey
# License and Maintainer
## License and Maintainer
Maintainer:: Guilhem Lettron ([guilhem@lettron.fr](mailto:guilhem@lettron.fr))
......
#
# Cookbook Name:: chocolatey
# Library:: matchers
#
# These can be used when testing for chefspec.
# Example:
# Recipe:
# chocolatey 'cool_package'
# ChefSpec:
# expect(chef_run).to install_chocolatey('cool_package')
#
if defined?(ChefSpec)
ChefSpec.define_matcher(:chocolatey)
def install_chocolatey(name)
ChefSpec::Matchers::ResourceMatcher.new(:chocolatey, :install, name)
end
def upgrade_chocolatey(name)
ChefSpec::Matchers::ResourceMatcher.new(:chocolatey, :upgrade, name)
end
def remove_chocolatey(name)
ChefSpec::Matchers::ResourceMatcher.new(:chocolatey, :remove, name)
end
end
#
# Provider:: chocolatey
# Author:: Guilhem Lettron <guilhem.lettron@youscribe.com>
#
# Copyright 20012, Societe Publica.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Prepending with '::' to prevent breaks in older versions of chef
include ::Chocolatey::Helpers
use_inline_resources
# Support whyrun
def whyrun_supported?
true
end
def load_current_resource
@current_resource = Chef::Resource::Chocolatey.new(@new_resource.name)
@current_resource.name(@new_resource.name)
@current_resource.version(@new_resource.version)
@current_resource.source(@new_resource.source) unless @new_resource.source.nil?
@current_resource.args(@new_resource.args) unless @new_resource.args.nil?
@current_resource.options(@new_resource.options)
@current_resource.package(@new_resource.package)
@current_resource.exists = package_exists?(@current_resource.package, @current_resource.version)
# @current_resource.installed = true if package_installed?(@current_resource.package)
end
action :install do
if @current_resource.exists
Chef::Log.info "#{@current_resource.package} already installed - nothing to do."
else
if @current_resource.version
install_version(@current_resource.package, @current_resource.version)
else
install(@current_resource.package)
end
adjust_path(@current_resource.package)
end
end
def adjust_path(name)
ruby_block "track-path-#{name}" do
block { ENV['PATH'] = environment_path(ENV['PATH']) }
end
end
action :upgrade do
package_name = @current_resource.package
if upgradeable?(package_name)
upgrade(package_name)
else
Chef::Log.info("Package #{@current_resource} already to latest version")
end
end
action :remove do
if @current_resource.exists
package_name = @current_resource.package
converge_by("uninstall package #{package_name}") do
execute "uninstall package #{package_name}" do
command "#{chocolatey_executable} uninstall -y #{package_name}"
end
end
else
Chef::Log.info "#{@current_resource} not installed - nothing to do."
end
end
def cmd_args
output = ''
output += " --source #{@current_resource.source}" if @current_resource.source
output += " --installargs '#{@current_resource.args}'" unless @current_resource.args.to_s.empty?
@current_resource.options.each do |k, v|
output += " -#{k}"
output += " #{v}" if v
end
output
end
def package_installed?(name)
package_exists?(name, nil)
end
def package_exists?(name, version)
cmd = Mixlib::ShellOut.new("#{chocolatey_executable} list -l -r #{name}")
cmd.run_command
software = cmd.stdout.split("\r\n").each_with_object({}) do |s, h|
v, k = s.split('|')
h[String(v).strip.downcase] = String(k).strip.downcase
h
end
if version
installed = software[name.downcase]
installed && installed.downcase.casecmp(version.downcase) == 0
else
!software[name.downcase].nil?
end
end
def upgradeable?(name)
return false unless @current_resource.exists
unless package_installed?(name)
Chef::Log.debug("Package isn't installed... we can upgrade it!")
return true
end
Chef::Log.debug("Checking to see if this chocolatey package is installed/upgradable: '#{name}'")
cmd = Mixlib::ShellOut.new("#{chocolatey_executable} upgrade -r --noop #{cmd_args} #{name}")
cmd.run_command
result = cmd.stdout.chomp
package_name, current_version, updated_version, is_pinned = result.downcase.split('|')
raise "Wrong package name #{name} != #{package_name}" if package_name != name
current_version != updated_version && is_pinned != 'true'
end
def install(name)
execute "install package #{name}" do
command "#{chocolatey_executable} install -y #{cmd_args} #{name}"
end
end
def upgrade(name)
execute "updating #{name} to latest" do
command "#{chocolatey_executable} upgrade -y #{cmd_args} #{name}"
end
end
def install_version(name, version)
execute "install package #{name} version #{version}" do
command "#{chocolatey_executable} install -y -version #{version} #{cmd_args} #{name}"
end
end
actions :install, :remove, :upgrade
default_action :install
attribute :package, kind_of: String, name_attribute: true
attribute :source, kind_of: [String, nil], default: nil
attribute :version, kind_of: [String]
attribute :args, kind_of: [String, nil], default: nil
attribute :options, kind_of: Hash, default: {}
attr_accessor :exists, :upgradeable
name 'chocolatey_specs'
maintainer 'Maintainer'
maintainer_email 'Maintainer@example.com'
license 'Apache 2.0'
description 'Used with ChefSpec to test chocolatey'
version '0.1.0'
depends 'chocolatey'
chocolatey 'new_package' do
version '0.1.0'
action :install
end
chocolatey 'existing_package' do
version '0.2.0'
action :upgrade
end
chocolatey 'trash_package' do
action :remove
end
require 'chefspec'
require 'chefspec/berkshelf'
RSpec.describe 'chocolatey_specs::default' do
let(:chef_run) do
ChefSpec::SoloRunner.new(
platform: 'windows', version: '2012R2'
).converge(described_recipe)
end
it 'installs a package' do
expect(chef_run).to install_chocolatey('new_package').with(version: '0.1.0')
end
it 'upgrades a package' do
expect(chef_run).to upgrade_chocolatey('existing_package').with(version: '0.2.0')
end
it 'removes a package' do
expect(chef_run).to remove_chocolatey('trash_package')
end
end
name 'chocolatey_test'
maintainer 'Maintainer'
maintainer_email 'Maintainer@example.com'
license 'Apache 2.0'
description 'Used with Test-Kitchen and ServerSpec to test chocolatey'
version '0.1.0'
depends 'chocolatey'
chocolatey 'git.install' do
options('params' => "'/GitOnlyOnPath'")
end
git File.join(ENV['TEMP'], 'chocolatey-cookbook') do
repository 'https://github.com/chocolatey/chocolatey-cookbook'
revision 'master'
action :sync
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment