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