manager

package
v0.0.0-...-a1d8f0c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2021 License: LGPL-3.0 Imports: 14 Imported by: 6

Documentation

Overview

The manager package defines an interface which can carry out numerous package-management related operations on the local system and the respective implementations on apt and yum-based systems.

Index

Constants

This section is empty.

Variables

View Source
var (

	// Override for testing.
	Delay    = 10 * time.Second
	Attempts = 30
)
View Source
var CommandOutput = (*exec.Cmd).CombinedOutput

CommandOutput is cmd.Output. It was aliased for testing purposes.

View Source
var ProcessStateSys = (*os.ProcessState).Sys

ProcessStateSys is ps.Sys. It was aliased for testing purposes.

View Source
var RunCommand = func(command string, args ...string) (output string, err error) {
	cmd := exec.Command(command, args...)
	out, err := cmd.CombinedOutput()
	return string(out), err
}

RunCommand is helper function to execute the command and gather the output.

View Source
var RunCommandWithRetry = func(cmd string, getFatalError func(string) error) (output string, code int, _ error) {

	args := strings.Fields(cmd)
	if len(args) <= 1 {
		return "", 1, errors.New(fmt.Sprintf("too few arguments: expected at least 2, got %d", len(args)))
	}

	logger.Infof("Running: %s", cmd)

	// Retry operation 30 times, sleeping every 10 seconds between attempts.
	// This avoids failure in the case of something else having the dpkg lock
	// (e.g. a charm on the machine we're deploying containers to).
	var out []byte
	tryAgain := false
	err := retry.Call(retry.CallArgs{
		Clock:    clock.WallClock,
		Delay:    Delay,
		Attempts: Attempts,
		NotifyFunc: func(lastError error, attempt int) {
			logger.Infof("Retrying: %s", cmd)
		},
		IsFatalError: func(err error) bool {
			return !tryAgain
		},
		Func: func() error {
			tryAgain = false

			command := exec.Command(args[0], args[1:]...)

			var err error
			out, err = CommandOutput(command)

			if err == nil {
				return nil
			}

			exitError, ok := err.(*exec.ExitError)
			if !ok {
				return errors.Annotatef(err, "unexpected error type %T", err)
			}
			waitStatus, ok := ProcessStateSys(exitError.ProcessState).(exitStatuser)
			if !ok {
				return errors.Annotatef(err, "unexpected process state type %T", exitError.ProcessState.Sys())
			}

			code = waitStatus.ExitStatus()
			if code != 100 {
				return errors.Trace(err)
			}
			if getFatalError != nil {
				if fatalErr := getFatalError(string(out)); fatalErr != nil {
					return errors.Annotatef(fatalErr, "encountered fatal error")
				}
			}
			tryAgain = true
			return err
		},
	})

	if err != nil {
		logger.Errorf("packaging command failed: %v; cmd: %q; output: %s",
			err, cmd, string(out))
		return string(out), code, errors.Errorf("packaging command failed: %v", err)
	}

	return string(out), 0, nil
}

RunCommandWithRetry is a helper function which tries to execute the given command. It tries to do so for 30 times with a 10 second sleep between commands. It returns the output of the command, the exit code, and an error, if one occurs, logging along the way. It was aliased for testing purposes.

Functions

This section is empty.

Types

type PackageManager

type PackageManager interface {
	// InstallPrerequisite runs the command which installs the prerequisite
	// package which provides repository management functionalityes.
	InstallPrerequisite() error

	// Update runs the command to update the local package list.
	Update() error

	// Upgrade runs the command which issues an upgrade on all packages
	// with available newer versions.
	Upgrade() error

	// Install runs a *single* command that installs the given package(s).
	Install(packs ...string) error

	// Remove runs a *single* command that removes the given package(s).
	Remove(packs ...string) error

	// Purge runs the command that removes the given package(s) along
	// with any associated config files.
	Purge(packs ...string) error

	// Search runs the command that determines whether the given package is
	// available for installation from the currently configured repositories.
	Search(pack string) (bool, error)

	// IsInstalled runs the command which determines whether or not the
	// given package is currently installed on the system.
	IsInstalled(pack string) bool

	// AddRepository runs the command that adds a repository to the
	// list of available repositories.
	// NOTE: requires the prerequisite package whose installation command
	// is done by running InstallPrerequisite().
	AddRepository(repo string) error

	// RemoveRepository runs the command that removes a given
	// repository from the list of available repositories.
	// NOTE: requires the prerequisite package whose installation command
	// is done by running InstallPrerequisite().
	RemoveRepository(repo string) error

	// Cleanup runs the command that cleans up all orphaned packages,
	// left-over files and previously-cached packages.
	Cleanup() error

	// GetProxySettings returns the curretly-configured package manager proxy.
	GetProxySettings() (proxy.Settings, error)

	// SetProxy runs the commands to set the given proxy parameters for the
	// package management system.
	SetProxy(settings proxy.Settings) error
}

PackageManager is the interface which carries out various package-management related work.

func NewAptPackageManager

func NewAptPackageManager() PackageManager

NewAptPackageManager returns a PackageManager for apt-based systems.

func NewPackageManager

func NewPackageManager(series string) (PackageManager, error)

NewPackageManager returns the appropriate PackageManager implementation based on the provided series.

func NewYumPackageManager

func NewYumPackageManager() PackageManager

NewYumPackageManager returns a PackageManager for yum-based systems.

func NewZypperPackageManager

func NewZypperPackageManager() PackageManager

type Snap

type Snap struct {
	// contains filtered or unexported fields
}

Snap is the PackageManager implementation for snap-based systems.

func NewSnapPackageManager

func NewSnapPackageManager() *Snap

NewSnapPackageManager returns a PackageManager for snap-based systems.

func (*Snap) AddRepository

func (pm *Snap) AddRepository(repo string) error

AddRepository is defined on the PackageManager interface.

func (*Snap) ChangeChannel

func (snap *Snap) ChangeChannel(pack, channel string) error

ChangeChannel updates the tracked channel for an installed snap.

func (*Snap) Cleanup

func (pm *Snap) Cleanup() error

Cleanup is defined on the PackageManager interface.

func (*Snap) ConfigureStoreProxy

func (snap *Snap) ConfigureStoreProxy(assertions, storeID string) error

ConfigureStoreProxy sets up snapd to connect to the snap store proxy instance defined in the provided assertions using the provided store ID.

If snap also needs to use HTTP/HTTPS proxies to talk to the outside world, these need to be configured separately before invoking this method via a call to SetProxy.

func (*Snap) DisableStoreProxy

func (snap *Snap) DisableStoreProxy() error

DisableStoreProxy resets the snapd proxy store settings.

If snap was also configured to use HTTP/HTTPS proxies these must be reset separately via a call to SetProxy. call to SetProxy.

func (*Snap) GetProxySettings

func (snap *Snap) GetProxySettings() (proxy.Settings, error)

GetProxySettings is defined on the PackageManager interface.

func (*Snap) Install

func (snap *Snap) Install(packs ...string) error

Install is defined on the PackageManager interface.

func (*Snap) InstallPrerequisite

func (pm *Snap) InstallPrerequisite() error

InstallPrerequisite is defined on the PackageManager interface.

func (*Snap) InstalledChannel

func (snap *Snap) InstalledChannel(pack string) string

InstalledChannel returns the snap channel for an installed package.

func (*Snap) IsInstalled

func (snap *Snap) IsInstalled(pack string) bool

IsInstalled is defined on the PackageManager interface.

func (*Snap) Purge

func (pm *Snap) Purge(packs ...string) error

Purge is defined on the PackageManager interface.

func (*Snap) Remove

func (pm *Snap) Remove(packs ...string) error

Remove is defined on the PackageManager interface.

func (*Snap) RemoveRepository

func (pm *Snap) RemoveRepository(repo string) error

RemoveRepository is defined on the PackageManager interface.

func (*Snap) Search

func (snap *Snap) Search(pack string) (bool, error)

Search is defined on the PackageManager interface.

func (*Snap) SetProxy

func (pm *Snap) SetProxy(settings proxy.Settings) error

SetProxy is defined on the PackageManager interface.

func (*Snap) Update

func (pm *Snap) Update() error

Update is defined on the PackageManager interface.

func (*Snap) Upgrade

func (pm *Snap) Upgrade() error

Upgrade is defined on the PackageManager interface.

Directories

Path Synopsis
This package contains a mock implementation of the manager.PackageManager interface which always returns positive outcomes and a nil error.
This package contains a mock implementation of the manager.PackageManager interface which always returns positive outcomes and a nil error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL