Class: Inspec::Resources::GemPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resources/gem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_name, gem_binary = nil) ⇒ GemPackage

Returns a new instance of GemPackage.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/inspec/resources/gem.rb', line 19

def initialize(package_name, gem_binary = nil)
  @package_name = package_name
  @gem_binary = case gem_binary # TODO: no. this is not right
                when nil
                  "gem"
                when :chef
                  if inspec.os.windows?
                    # TODO: what about chef-dk or other installs?
                    'c:\opscode\chef\embedded\bin\gem.bat'
                  else
                    "/opt/chef/embedded/bin/gem"
                  end
                when :chef_server
                  "/opt/opscode/embedded/bin/gem"
                else
                  gem_binary
                end
  skip_resource "Unable to retrieve gem information" if info.empty?
end

Instance Attribute Details

#gem_binaryObject (readonly)

Returns the value of attribute gem_binary.



17
18
19
# File 'lib/inspec/resources/gem.rb', line 17

def gem_binary
  @gem_binary
end

Instance Method Details

#infoObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/inspec/resources/gem.rb', line 39

def info
  return @info if defined?(@info)

  require "inspec/resources/command"

  cmd = inspec.command("#{@gem_binary} list --local -a -q \^#{@package_name}\$")
  return {} unless cmd.exit_status == 0

  # extract package name and version
  # parses data like winrm (1.3.4, 1.3.3)
  params = /^\s*([^\(]*?)\s*\((.*?)\)\s*$/.match(cmd.stdout.chomp)
  @info = {
    installed: !params.nil?,
    type: "gem",
  }
  return @info unless @info[:installed]

  versions = params[2].split(",").map(&:strip)
  @info[:name] = params[1]
  @info[:version] = versions[0]
  @info[:versions] = versions
  @info
end

#installed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/inspec/resources/gem.rb', line 63

def installed?
  info[:installed] == true
end

#resource_idObject



80
81
82
# File 'lib/inspec/resources/gem.rb', line 80

def resource_id
  "#{@package_name}-#{version}"
end

#to_sObject



76
77
78
# File 'lib/inspec/resources/gem.rb', line 76

def to_s
  "gem package #{@package_name}"
end

#versionObject



67
68
69
# File 'lib/inspec/resources/gem.rb', line 67

def version
  info[:version]
end

#versionsObject

this returns an array of strings



72
73
74
# File 'lib/inspec/resources/gem.rb', line 72

def versions
  info[:versions]
end