2015-09-10 12:35:15 +01:00
# encoding: utf-8
2015-10-02 14:55:13 +02:00
# author: Christoph Hartmann
2015-10-06 18:55:44 +02:00
# author: Dominik Richter
2015-09-10 12:35:15 +01:00
# This resource talks with OneGet (https://github.com/OneGet/oneget)
# Its part of Windows Management Framework 5.0 and part of Windows 10
#
# Usage:
# describe oneget('zoomit') do
# it { should be_installed }
# end
2015-10-26 04:04:18 +01:00
class OneGetPackage < Inspec . resource ( 1 )
2015-09-10 12:35:15 +01:00
name 'oneget'
2015-11-27 14:02:38 +01:00
desc 'Use the oneget InSpec audit resource to test if the named package and/or package version is installed on the system. This resource uses OneGet, which is part of the Windows Management Framework 5.0 and Windows 10. This resource uses the Get-Package cmdlet to return all of the package names in the OneGet repository.'
example "
describe oneget ( 'zoomit' ) do
it { should be_installed }
end
"
2015-09-10 12:35:15 +01:00
def initialize ( package_name )
@package_name = package_name
2015-10-02 10:31:46 +02:00
# verify that this resource is only supported on Windows
2015-10-26 04:04:18 +01:00
return skip_resource 'The `oneget` resource is not supported on your OS.' if inspec . os [ :family ] != 'windows'
2015-09-10 12:35:15 +01:00
end
def info
2015-10-01 15:25:08 +02:00
return @info if defined? ( @info )
2015-09-17 16:50:21 +02:00
@info = { }
@info [ :type ] = 'oneget'
2015-10-02 11:10:13 +02:00
@info [ :installed ] = false
2015-09-17 16:50:21 +02:00
2015-10-26 04:04:18 +01:00
cmd = inspec . command ( " Get-Package -Name ' #{ @package_name } ' | ConvertTo-Json " )
2015-09-10 12:35:15 +01:00
# cannot rely on exit code for now, successful command returns exit code 1
# return nil if cmd.exit_status != 0
# try to parse json
2015-09-17 16:50:21 +02:00
2015-09-10 12:35:15 +01:00
begin
pkgs = JSON . parse ( cmd . stdout )
2015-10-02 11:10:13 +02:00
@info [ :installed ] = true
# sometimes we get multiple values
2015-10-03 14:02:34 +02:00
if pkgs . is_a? ( Array )
2015-10-02 11:10:13 +02:00
# select the first entry
pkgs = pkgs . first
end
2015-09-10 12:35:15 +01:00
rescue JSON :: ParserError = > _e
2015-09-17 16:50:21 +02:00
return @info
2015-09-10 12:35:15 +01:00
end
2015-09-17 16:50:21 +02:00
2015-10-02 11:10:13 +02:00
@info [ :name ] = pkgs [ 'Name' ] if pkgs . key? ( 'Name' )
@info [ :version ] = pkgs [ 'Version' ] if pkgs . key? ( 'Version' )
2015-09-17 16:50:21 +02:00
@info
2015-09-10 12:35:15 +01:00
end
def installed?
2015-09-17 20:51:06 +02:00
info [ :installed ] == true
2015-09-10 12:35:15 +01:00
end
def version
2015-09-17 16:50:21 +02:00
info [ :version ]
2015-09-10 12:35:15 +01:00
end
def to_s
2015-10-12 13:01:58 +02:00
" OneGet Package #{ @package_name } "
2015-09-10 12:35:15 +01:00
end
end