class Inspec::Resources::AptRepository

Constants

HTTP_URL_RE

TODO: remove this. just see if it is valid w/ URI.parse

Public Class Methods

new(ppa_name) click to toggle source
# File lib/inspec/resources/apt.rb, line 41
def initialize(ppa_name)
  @deb_url = nil
  # check if the os is ubuntu or debian
  if inspec.os.debian?
    @deb_url = determine_ppa_url(ppa_name)
  else
    # this resource is only supported on ubuntu and debian
    skip_resource "The `apt` resource is not supported on your OS yet."
  end
end

Public Instance Methods

enabled?() click to toggle source
# File lib/inspec/resources/apt.rb, line 56
def enabled?
  return false if find_repo.count == 0

  actives = find_repo.map { |repo| repo[:active] }
  actives = actives.uniq
  actives.size == 1 && actives[0] = true
end
exists?() click to toggle source
# File lib/inspec/resources/apt.rb, line 52
def exists?
  find_repo.count > 0
end
to_s() click to toggle source
# File lib/inspec/resources/apt.rb, line 64
def to_s
  "Apt Repository #{@deb_url}"
end

Private Instance Methods

determine_ppa_url(ppa_url) click to toggle source

resolves ppa urls @see bazaar.launchpad.net/~ubuntu-core-dev/software-properties/main/view/head:/softwareproperties/ppa.py

# File lib/inspec/resources/apt.rb, line 118
def determine_ppa_url(ppa_url)
  # verify if we have the url already, then just return
  return ppa_url if ppa_url =~ HTTP_URL_RE

  # otherwise start generating the ppa url

  # special care if the name stats with :
  ppa_url = ppa_url.split(":")[1] if ppa_url.start_with?("ppa:")

  # parse ppa owner and repo
  ppa_owner, ppa_repo = ppa_url.split("/")
  ppa_repo = "ppa" if ppa_repo.nil?

  # construct new ppa url and return it
  format("http://ppa.launchpad.net/%s/%s/ubuntu", ppa_owner, ppa_repo)
end
find_repo() click to toggle source
# File lib/inspec/resources/apt.rb, line 70
def find_repo
  read_debs.select { |repo| repo[:url] == @deb_url && repo[:type] == "deb" }
end
read_debs() click to toggle source
# File lib/inspec/resources/apt.rb, line 77
def read_debs
  return @repo_cache if defined?(@repo_cache)

  # load all lists
  cmd = inspec.command("find /etc/apt/ -name \"*.list\" -exec sh -c 'cat {} || echo -n' \\;")

  # @see https://help.ubuntu.com/community/Repositories/CommandLine#Explanation_of_the_Repository_Format
  @repo_cache = cmd.stdout.lines.map do |raw_line|
    # detect if the repo is commented out
    line = raw_line.gsub(/^(#\s*)*/, "")
    active = raw_line == line

    # formats:
    # deb                          "http://archive.ubuntu.com/ubuntu/" wily main restricted ...
    # deb                          http://archive.ubuntu.com/ubuntu/ wily main restricted ...
    # deb [trusted=yes]            http://archive.ubuntu.com/ubuntu/ wily main restricted ...
    # deb [arch=amd64 trusted=yes] http://archive.ubuntu.com/ubuntu/ wily main restricted ...
    # deb cdrom:[Ubuntu 15.10 _Wily Werewolf_ - Release amd64 (20151021)]/ wily main restricted ...

    words = line.sub(/^(deb|deb-src)\s+\[.+?\]/, '\1').split
    type, url, distro, *components = words
    url = url.delete('"') if url

    next if words[1] && words[1].start_with?("cdrom:") # skip unsupported apt-cdrom repos
    next if components.empty?
    next unless URI::HTTP === URI.parse(url)
    next unless %w{deb deb-src}.include? type

    # map data
    {
      type: type,
      url: url,
      distro: distro,
      components: components,
      active: active,
    }
  end.compact
end