github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  		upgradeToVersion{
    35  			version.MustParse("1.24.0"),
    36  			stateStepsFor124(),
    37  		},
    38  	}
    39  	return steps
    40  }
    41  
    42  // upgradeOperations returns an ordered slice of sets of API-based
    43  // operations needed to upgrade Juju to particular version. As per the
    44  // state-based operations above, ordering is important.
    45  var upgradeOperations = func() []Operation {
    46  	steps := []Operation{
    47  		upgradeToVersion{
    48  			version.MustParse("1.18.0"),
    49  			stepsFor118(),
    50  		},
    51  		upgradeToVersion{
    52  			version.MustParse("1.22.0"),
    53  			stepsFor122(),
    54  		},
    55  		upgradeToVersion{
    56  			version.MustParse("1.23.0"),
    57  			stepsFor123(),
    58  		},
    59  	}
    60  	return steps
    61  }
    62  
    63  type opsIterator struct {
    64  	from    version.Number
    65  	to      version.Number
    66  	allOps  []Operation
    67  	current int
    68  }
    69  
    70  func newStateUpgradeOpsIterator(from version.Number) *opsIterator {
    71  	return newOpsIterator(from, version.Current.Number, stateUpgradeOperations())
    72  }
    73  
    74  func newUpgradeOpsIterator(from version.Number) *opsIterator {
    75  	return newOpsIterator(from, version.Current.Number, upgradeOperations())
    76  }
    77  
    78  func newOpsIterator(from, to version.Number, ops []Operation) *opsIterator {
    79  	// If from is not known, it is 1.16.
    80  	if from == version.Zero {
    81  		from = version.MustParse("1.16.0")
    82  	}
    83  
    84  	// Clear the version tag of the target release to ensure that all
    85  	// upgrade steps for the release are run for alpha and beta
    86  	// releases.
    87  	// ...but only do this if the agent version has actually changed,
    88  	// lest we trigger upgrade mode unnecessarily for non-final
    89  	// versions.
    90  	if from.Compare(to) != 0 {
    91  		to.Tag = ""
    92  	}
    93  
    94  	return &opsIterator{
    95  		from:    from,
    96  		to:      to,
    97  		allOps:  ops,
    98  		current: -1,
    99  	}
   100  }
   101  
   102  func (it *opsIterator) Next() bool {
   103  	for {
   104  		it.current++
   105  		if it.current >= len(it.allOps) {
   106  			return false
   107  		}
   108  		targetVersion := it.allOps[it.current].TargetVersion()
   109  
   110  		// Do not run steps for versions of Juju earlier or same as we are upgrading from.
   111  		if targetVersion.Compare(it.from) <= 0 {
   112  			continue
   113  		}
   114  		// Do not run steps for versions of Juju later than we are upgrading to.
   115  		if targetVersion.Compare(it.to) > 0 {
   116  			continue
   117  		}
   118  		return true
   119  	}
   120  }
   121  
   122  func (it *opsIterator) Get() Operation {
   123  	return it.allOps[it.current]
   124  }