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