github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/upgradeseries/upgrader.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package upgradeseries
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/errors"
    10  
    11  	"github.com/juju/juju/core/paths"
    12  	"github.com/juju/juju/service"
    13  )
    14  
    15  //go:generate go run go.uber.org/mock/mockgen -package mocks -destination mocks/servicemanager_mock.go github.com/juju/juju/service SystemdServiceManager
    16  
    17  // Upgrader describes methods required to perform file-system manipulation in
    18  // preparation for upgrading the host Ubuntu version.
    19  type Upgrader interface {
    20  	PerformUpgrade() error
    21  }
    22  
    23  // upgrader implements the Upgrader interface for a specific (from/to) upgrade
    24  // of the host Ubuntu version, via the systemd service manager.
    25  type upgrader struct {
    26  	logger Logger
    27  
    28  	// jujuCurrentSeries is what Juju thinks the
    29  	// current series of the machine is.
    30  	jujuCurrentSeries string
    31  
    32  	// fromSeries is the actual current series,
    33  	// determined directly from the machine.
    34  	fromSeries string
    35  
    36  	toSeries string
    37  
    38  	machineAgent string
    39  	unitAgents   []string
    40  
    41  	manager service.SystemdServiceManager
    42  }
    43  
    44  // NewUpgrader uses the input function to determine the series that should be
    45  // supported, and returns a reference to a new Upgrader that supports it.
    46  func NewUpgrader(
    47  	currentSeries, toSeries string, manager service.SystemdServiceManager, logger Logger,
    48  ) (Upgrader, error) {
    49  	fromSeries, err := hostSeries()
    50  	if err != nil {
    51  		return nil, errors.Trace(err)
    52  	}
    53  	return &upgrader{
    54  		logger:            logger,
    55  		jujuCurrentSeries: currentSeries,
    56  		fromSeries:        fromSeries,
    57  		toSeries:          toSeries,
    58  		manager:           manager,
    59  	}, nil
    60  }
    61  
    62  // PerformUpgrade writes Juju binaries and service files that allow the machine
    63  // and unit agents to run on the target version of Ubuntu.
    64  func (u *upgrader) PerformUpgrade() error {
    65  	return u.populateAgents()
    66  }
    67  
    68  // populateAgents discovers and sets the names of the machine and unit agents.
    69  // If there are any other agents determined, a warning is logged to notify that
    70  // they are being skipped from the upgrade process.
    71  func (u *upgrader) populateAgents() (err error) {
    72  	var unknown []string
    73  	u.machineAgent, u.unitAgents, unknown, err = u.manager.FindAgents(paths.NixDataDir)
    74  	if err != nil {
    75  		return errors.Trace(err)
    76  	}
    77  	if len(unknown) > 0 {
    78  		u.logger.Warningf("skipping agents not of type machine or unit: %s", strings.Join(unknown, ", "))
    79  	}
    80  	return nil
    81  }