Class: Inspec::Resources::Package

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

Instance Method Summary collapse

Constructor Details

#initialize(package_name, opts = {}) ⇒ Package

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/inspec/resources/package.rb', line 23

def initialize(package_name, opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  @package_name = package_name
  @name = @package_name
  @cache = nil
  # select package manager
  @pkgman = nil
  @latest_version = nil

  os = inspec.os
  if os.debian?
    @pkgman = Deb.new(inspec)
  elsif os.redhat? || %w{suse amazon fedora}.include?(os[:family])
    @pkgman = Rpm.new(inspec, opts)
  elsif ["arch"].include?(os[:name])
    @pkgman = Pacman.new(inspec)
  elsif ["darwin"].include?(os[:family])
    @pkgman = Brew.new(inspec)
  elsif os.windows?
    @pkgman = WindowsPkg.new(inspec)
  elsif ["aix"].include?(os[:family])
    @pkgman = BffPkg.new(inspec)
  elsif os.solaris?
    @pkgman = SolarisPkg.new(inspec)
  elsif ["hpux"].include?(os[:family])
    @pkgman = HpuxPkg.new(inspec)
  elsif ["alpine"].include?(os[:name])
    @pkgman = AlpinePkg.new(inspec)
  elsif ["freebsd"].include?(os[:name])
    @pkgman = FreebsdPkg.new(inspec)
  else
    raise Inspec::Exceptions::ResourceSkipped, "The `package` resource is not supported on your OS yet."
  end

  evaluate_missing_requirements
end

Instance Method Details

#held?(_provider = nil, _version = nil) ⇒ Boolean

returns true it the package is held (if the OS supports it)

Returns:

  • (Boolean)


74
75
76
# File 'lib/inspec/resources/package.rb', line 74

def held?(_provider = nil, _version = nil)
  info[:held] == true
end

#infoObject

returns the package description



79
80
81
82
83
84
85
86
87
# File 'lib/inspec/resources/package.rb', line 79

def info
  return @cache unless @cache.nil?
  # All `@pkgman.info` methods return `{}`. This matches that
  # behavior if `@pkgman` can't be determined, thus avoiding the
  # `undefined method 'info' for nil:NilClass` error
  return {} if @pkgman.nil?

  @pkgman.info(@package_name)
end

#installed?(_provider = nil, _version = nil) ⇒ Boolean

returns true if the package is installed

Returns:

  • (Boolean)


60
61
62
# File 'lib/inspec/resources/package.rb', line 60

def installed?(_provider = nil, _version = nil)
  info[:installed] == true
end

#latest?(_provider = nil, _version = nil) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
# File 'lib/inspec/resources/package.rb', line 64

def latest?(_provider = nil, _version = nil)
  os = inspec.os
  if os.solaris? || (%w{hpux aix}.include? os[:family])
    raise Inspec::Exceptions::ResourceSkipped, "The `be_latest` matcher is not supported on your OS yet."
  end

  (!info[:only_version_no].nil? && !latest_version.nil?) && (info[:only_version_no] == latest_version)
end

#latest_versionObject



95
96
97
# File 'lib/inspec/resources/package.rb', line 95

def latest_version
  @latest_version ||= ( @pkgman.latest_version(@package_name) || info[:latest_version] )
end

#resource_idObject



99
100
101
# File 'lib/inspec/resources/package.rb', line 99

def resource_id
  @package_name || "System Package"
end

#to_sObject



103
104
105
# File 'lib/inspec/resources/package.rb', line 103

def to_s
  "System Package #{@package_name}"
end

#versionObject

return the package version



90
91
92
93
# File 'lib/inspec/resources/package.rb', line 90

def version
  info = @pkgman.info(@package_name)
  info[:version]
end