Parent

Included Modules

Class/Module Index [+]

Quicksearch

Object

Public Class Methods

instances() click to toggle source

instances sub command (builds the installed packages list)

# File portupgrade_pkgng.rb, line 33
def self.instances
  Puppet.debug "portupgrade.rb Building packages list from installed ports"

  # regex to match output from pkg_info
  regex = %{^(\S+)-([^-\s]+):(\S+)$}
  # Corresponding field names
  fields = [:portname, :ensure, :portorigin]
  # define Temporary hash used, packages array of hashes
  hash = Hash.new
  packages = []

  # exec command
  cmdline = ["info","-aoq"]
  begin
    output = pkg(*cmdline)
  rescue Puppet::ExecutionFailure
    raise Puppet::Error.new(output)
    return nil
  end

  # split output and match it and populate temp hash
  output.split("\n").each { |data|
    # reset hash to nil for each line
    hash.clear
    if match = regex.match(data)
      # Output matched regex
      fields.zip(match.captures) { |field, value|
        hash[field] = value
      }

      # populate the actual :name field from the :portorigin
      # Set :provider to this object name
      hash[:name] = hash[:portorigin]
      hash[:provider] = self.name

      # Add to the full packages listing
      packages << new(hash)
    else
      # unrecognised output from pkg_info
      Puppet.debug "portupgrade.Instances() - unable to match output: #{data}"
    end
  }

  # return the packages array of hashes
  return packages
end

Public Instance Methods

install() click to toggle source

Installation sub command

# File portupgrade_pkgng.rb, line 82
def install
  Puppet.debug "portupgrade.install() - Installation call on #{@resource[:name]}"
  # -M: yes, we're a batch, so don't ask any questions
  cmdline = ["-M BATCH=yes", @resource[:name]]

  # FIXME: it's possible that portinstall prompts for data so locks up.
  begin
    output = portinstall(*cmdline)
  rescue Puppet::ExecutionFailure
    raise Puppet::Error.new(output)
  end

  if output =~ /\*\* No such /
    raise Puppet::ExecutionFailure, "Could not find package #{@resource[:name]}"
  end

  # No return code required, so do nil to be clean
  return nil
end
latest() click to toggle source

Latest subcommand (returns the latest version available, or current version if installed is latest)

# File portupgrade_pkgng.rb, line 104
def latest
  Puppet.debug "portupgrade.latest() - Latest check called on #{@resource[:name]}"
  # search for latest version available, or return current version.
  # cmdline = "portversion -v <portorigin>", returns "<portname> <code> <stuff>"
  # or "** No matching package found: <portname>"
  cmdline = ['version','-ve', @resource[:name]]

  begin
    output = pkg(*cmdline)
  rescue Puppet::ExecutionFailure
    raise Puppet::Error.new(output)
  end

  # Check: output format.
  if output =~ /^\S+-([^-\s]+)\s+(\S)\s+(.*)/
    installedversion = $1
    comparison = $2
    otherdata = $3

    # Only return a new version number when it's clear that there is a new version
    # all others return the current version so no unexpected 'upgrades' occur.
    case comparison
    when "=", ">"
      Puppet.debug "portupgrade.latest() - Installed package is latest (#{installedversion})"
      return installedversion
    when "<"
      # "portpkg-1.7_5 < needs updating (port has 1.14)"
      # "portpkg-1.7_5 < needs updating (port has 1.14) (=> 'newport/pkg')
      if otherdata =~ /\(port has (\S+)\)/
        newversion = $1
        Puppet.debug "portupgrade.latest() - Installed version needs updating to (#{newversion})"
        return newversion
      else
        Puppet.debug "portupgrade.latest() - Unable to determine new version from (#{otherdata})"
        return installedversion
      end
    when "?", "!", "#"
      Puppet.debug "portupgrade.latest() - Comparison Error reported from portversion (#{output})"
      return installedversion
    else
      Puppet.debug "portupgrade.latest() - Unknown code from portversion output (#{output})"
      return installedversion
    end

  else
    # error: output not parsed correctly, error out with nil.
    # Seriously - this section should never be called in a perfect world.
    # as verification that the port is installed has already happened in query.
    if output =~ /^\*\* No matching package /
      raise Puppet::ExecutionFailure, "Could not find package #{@resource[:name]}"
    else
      # Any other error (dump output to log)
      raise Puppet::ExecutionFailure, "Unexpected output from portversion: #{output}"
    end

    # Just in case we still are running, return nil
    return nil
  end

  # At this point normal operation has finished and we shouldn't have been called.
  # Error out and let the admin deal with it.
  raise Puppet::Error, "portversion.latest() - fatal error with portversion: #{output}"
  return nil
end
query() click to toggle source
Query subcommand - return a hash of details if exists, or nil if it doesn't.

Used to make sure the package is installed

# File portupgrade_pkgng.rb, line 172
def query
  Puppet.debug "portupgrade.query() - Called on #{@resource[:name]}"

  cmdline = ['info', '-qo', @resource[:name]]
  begin
    output = pkg(*cmdline)
  rescue Puppet::ExecutionFailure
    raise Puppet::Error.new(output)
  end

  # Check: if output isn't in the right format, return nil
  if output =~ /^(\S+)-([^-\s]+)/
    # Fill in the details
    hash = Hash.new
    hash[:portorigin] = self.name
    hash[:portname]   = $1
    hash[:ensure]     = $2

    # If more details are required, then we can do another pkg_info
    # query here and parse out that output and add to the hash
    # return the hash to the caller
    return hash
  else
    Puppet.debug "portupgrade.query() - package (#{@resource[:name]}) not installed"
    return nil
  end
end
uninstall() click to toggle source

Uninstall command

# File portupgrade_pkgng.rb, line 202
def uninstall
  Puppet.debug "portupgrade.uninstall() - called on #{@resource[:name]}"
  # Get full package name from port origin to uninstall with
  cmdline = ['pkg','-qo', @resource[:name]]
  begin
    output = pkg(*cmdline)
  rescue Puppet::ExecutionFailure
    raise Puppet::Error.new(output)
  end

  if output =~ /^(\S+)/
    # output matches, so uninstall it
    portuninstall $1
  end
end
update() click to toggle source

Update/upgrade command

# File portupgrade_pkgng.rb, line 220
def update
  Puppet.debug "portupgrade.update() - called on (#{@resource[:name]})"

  cmdline = ['info','-qo', @resource[:name]]
  begin
    output = pkg(*cmdline)
  rescue Puppet::ExecutionFailure
    raise Puppet::Error.new(output)
  end

  if output =~ /^(\S+)/
    # output matches, so upgrade the software
    cmdline = ["-M BATCH=yes", $1]
    begin
      output = portupgrade(*cmdline)
    rescue Puppet::ExecutionFailure
      raise Puppet::Error.new(output)
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.