~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/upgrades/operations.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

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