class Inspec::Resources::Rpm

RHEL family

def info(package_name)

def info(package_name)
  rpm_cmd = rpm_command(package_name)
  cmd = inspec.command(rpm_cmd)
  # CentOS does not return an error code if the package is not installed,
  # therefore we need to check for emptyness
  return {} if cmd.exit_status.to_i != 0 || cmd.stdout.chomp.empty?
  params = SimpleConfig.new(
    cmd.stdout.chomp,
    assignment_regex: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
    multiple_values: false,
  ).params
  # On some (all?) systems, the linebreak before the vendor line is missing
  if params['Version'] =~ /\s*Vendor:/
    v = params['Version'].split(' ')[0]
  else
    v = params['Version']
  end
  # On some (all?) systems, the linebreak before the build line is missing
  if params['Release'] =~ /\s*Build Date:/
    r = params['Release'].split(' ')[0]
  else
    r = params['Release']
  end
  {
    name: params['Name'],
    installed: true,
    version: "#{v}-#{r}",
    type: 'rpm',
  }
end

def initialize(inspec, opts)

def initialize(inspec, opts)
  super(inspec)
  @dbpath = opts.fetch(:rpm_dbpath, nil)
end

def missing_requirements

def missing_requirements
  missing_requirements = []
  unless @dbpath.nil? || inspec.directory(@dbpath).directory?
    missing_requirements << "RPMDB #{@dbpath} does not exist"
  end
  missing_requirements
end

def rpm_command(package_name)

def rpm_command(package_name)
  cmd = ''
  cmd += 'rpm -qi'
  cmd += " --dbpath #{@dbpath}" if @dbpath
  cmd += ' ' + package_name
  cmd
end