github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/upgrades/operations.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package upgrades
     5  
     6  import (
     7  	"github.com/juju/version/v2"
     8  
     9  	jujuversion "github.com/juju/juju/version"
    10  )
    11  
    12  // stateUpgradeOperations returns an ordered slice of sets of
    13  // state-based operations needed to upgrade Juju to particular
    14  // version. The slice is ordered by target version, so that the sets
    15  // of operations are executed in order from oldest version to most
    16  // recent.
    17  //
    18  // All state-based operations are run before API-based operations
    19  // (below).
    20  var stateUpgradeOperations = func() []Operation {
    21  	steps := []Operation{}
    22  	return steps
    23  }
    24  
    25  // upgradeOperations returns an ordered slice of sets of API-based
    26  // operations needed to upgrade Juju to particular version. As per the
    27  // state-based operations above, ordering is important.
    28  var upgradeOperations = func() []Operation {
    29  	steps := []Operation{}
    30  	return steps
    31  }
    32  
    33  type opsIterator struct {
    34  	from    version.Number
    35  	to      version.Number
    36  	allOps  []Operation
    37  	current int
    38  }
    39  
    40  func newStateUpgradeOpsIterator(from version.Number) *opsIterator {
    41  	return newOpsIterator(from, jujuversion.Current, stateUpgradeOperations())
    42  }
    43  
    44  func newUpgradeOpsIterator(from version.Number) *opsIterator {
    45  	return newOpsIterator(from, jujuversion.Current, upgradeOperations())
    46  }
    47  
    48  func newOpsIterator(from, to version.Number, ops []Operation) *opsIterator {
    49  	// If from is not known, it is 1.16.
    50  	if from == version.Zero {
    51  		from = version.MustParse("1.16.0")
    52  	}
    53  
    54  	// Clear the version tag of the target release to ensure that all
    55  	// upgrade steps for the release are run for alpha and beta
    56  	// releases.
    57  	// ...but only do this if the agent version has actually changed,
    58  	// lest we trigger upgrade mode unnecessarily for non-final
    59  	// versions.
    60  	if from.Compare(to) != 0 {
    61  		to.Tag = ""
    62  	}
    63  
    64  	return &opsIterator{
    65  		from:    from,
    66  		to:      to,
    67  		allOps:  ops,
    68  		current: -1,
    69  	}
    70  }
    71  
    72  func (it *opsIterator) Next() bool {
    73  	for {
    74  		it.current++
    75  		if it.current >= len(it.allOps) {
    76  			return false
    77  		}
    78  		targetVersion := it.allOps[it.current].TargetVersion()
    79  
    80  		// Do not run steps for versions of Juju earlier or same as we are upgrading from.
    81  		if targetVersion.Compare(it.from) <= 0 {
    82  			continue
    83  		}
    84  		// Do not run steps for versions of Juju later than we are upgrading to.
    85  		if targetVersion.Compare(it.to) > 0 {
    86  			continue
    87  		}
    88  		return true
    89  	}
    90  }
    91  
    92  func (it *opsIterator) Get() Operation {
    93  	return it.allOps[it.current]
    94  }