2015-09-10 11:35:15 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
# 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
|
|
|
|
class OneGetPackage < Vulcano.resource(1)
|
|
|
|
name 'oneget'
|
|
|
|
|
|
|
|
def initialize(package_name)
|
|
|
|
@package_name = package_name
|
2015-10-02 08:31:46 +00:00
|
|
|
|
|
|
|
# verify that this resource is only supported on Windows
|
|
|
|
return skip_resource 'The `oneget` resource is not supported on your OS.' if vulcano.os[:family] != 'windows'
|
2015-09-10 11:35:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def info
|
2015-10-01 13:25:08 +00:00
|
|
|
return @info if defined?(@info)
|
2015-09-17 14:50:21 +00:00
|
|
|
|
|
|
|
@info = {}
|
|
|
|
@info[:type] = 'oneget'
|
2015-10-02 09:10:13 +00:00
|
|
|
@info[:installed] = false
|
2015-09-17 14:50:21 +00:00
|
|
|
|
2015-09-10 11:35:15 +00:00
|
|
|
cmd = vulcano.run_command("Get-Package -Name '#{@package_name}' | ConvertTo-Json")
|
|
|
|
# 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 14:50:21 +00:00
|
|
|
|
2015-09-10 11:35:15 +00:00
|
|
|
begin
|
|
|
|
pkgs = JSON.parse(cmd.stdout)
|
2015-10-02 09:10:13 +00:00
|
|
|
@info[:installed] = true
|
|
|
|
|
|
|
|
# sometimes we get multiple values
|
|
|
|
if pkgs.kind_of?(Array)
|
|
|
|
# select the first entry
|
|
|
|
pkgs = pkgs.first
|
|
|
|
end
|
2015-09-10 11:35:15 +00:00
|
|
|
rescue JSON::ParserError => _e
|
2015-09-17 14:50:21 +00:00
|
|
|
return @info
|
2015-09-10 11:35:15 +00:00
|
|
|
end
|
2015-09-17 14:50:21 +00:00
|
|
|
|
2015-10-02 09:10:13 +00:00
|
|
|
@info[:name] = pkgs['Name'] if pkgs.key?('Name')
|
|
|
|
@info[:version] = pkgs['Version'] if pkgs.key?('Version')
|
2015-09-17 14:50:21 +00:00
|
|
|
@info
|
2015-09-10 11:35:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def installed?
|
2015-09-17 18:51:06 +00:00
|
|
|
info[:installed] == true
|
2015-09-10 11:35:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
2015-09-17 14:50:21 +00:00
|
|
|
info[:version]
|
2015-09-10 11:35:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"oneget package #{@package_name}"
|
|
|
|
end
|
|
|
|
end
|