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