-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-version.go
186 lines (153 loc) · 5.43 KB
/
service-version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package service
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-version"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
)
// Migration defines a target version and functions to upgrade and/or downgrade.
type Migration struct {
TargetVersion *version.Version
Up func(ctx context.Context) error
Down func(ctx context.Context) error
}
// ValidVersion creates a version.NewVersion ignoring the error.
func ValidVersion(v string) *version.Version {
obj, _ := version.NewVersion(v)
return obj
}
// FirstRun returns version "zero".
func FirstRun() *version.Version {
obj, _ := version.NewVersion("0.0.0")
return obj
}
// Latest retrieves current common Cells version.
func Latest() *version.Version {
return common.Version()
}
// LastKnownVersion looks on this server if there was a previous version of this service
func LastKnownVersion(serviceName string) (v *version.Version, e error) {
serviceDir, e := config.ServiceDataDir(serviceName)
if e != nil {
return nil, e
}
versionFile := filepath.Join(serviceDir, "version")
data, err := ioutil.ReadFile(versionFile)
if err != nil {
if os.IsNotExist(err) {
fake, _ := version.NewVersion("0.0.0")
return fake, nil
}
return nil, err
}
return version.NewVersion(strings.TrimSpace(string(data)))
}
// UpdateVersion writes the version string to file
func UpdateVersion(serviceName string, v *version.Version) error {
dir, err := config.ServiceDataDir(serviceName)
if err != nil {
return err
}
versionFile := filepath.Join(dir, "version")
return ioutil.WriteFile(versionFile, []byte(v.String()), 0755)
}
// UpdateServiceVersion applies migration(s) if necessary and stores new current version for future use.
func UpdateServiceVersion(s Service) error {
options := s.Options()
newVersion, _ := version.NewVersion(options.Version)
lastVersion, e := LastKnownVersion(options.Name)
if e != nil {
return e
}
writeVersion, err := ApplyMigrations(options.Context, lastVersion, newVersion, options.Migrations)
if writeVersion != nil {
if e := UpdateVersion(options.Name, writeVersion); e != nil {
log.Logger(options.Context).Error("could not write version file", zap.Error(e))
}
}
return err
}
// ApplyMigrations browse migrations upward on downward and apply them sequentially. It returns a version to be
// saved as the current valid version of the service, or nil if no changes were necessary. In specific case where
// current version is 0.0.0 (first run), it only applies first run migration (if any) and returns target version.
func ApplyMigrations(ctx context.Context, current *version.Version, target *version.Version, migrations []*Migration) (*version.Version, error) {
if target.Equal(current) {
return nil, nil
}
// Special case if we're in dev and moving from 0.2.0 to a dev
if strings.HasSuffix(target.String(), "-dev") && current.String() == "0.2.0" {
return target, nil
}
if migrations == nil {
return target, nil
}
// corner case of the fresh install, returns the current target version to be stored
if current.Equal(FirstRun()) {
m := migrations[0]
// Double check to insure we really only perform FirstRun initialisation
if !m.TargetVersion.Equal(FirstRun()) {
// no first run init, doing nothing
return target, nil
}
log.Logger(ctx).Debug(fmt.Sprintf("About to initialise service at version %s", target.String()))
err := m.Up(ctx)
if err != nil {
log.Logger(ctx).Error(fmt.Sprintf("could not initialise service at version %s", target.String()), zap.Error(err))
return current, err
}
return target, nil
}
log.Logger(ctx).Debug(fmt.Sprintf("About to perform migration from %s to %s", current.String(), target.String()))
if target.GreaterThan(current) {
var successVersion *version.Version
for _, migration := range migrations {
v := migration.TargetVersion
if migration.Up != nil && (current.String() == "0.0.0" || v.GreaterThan(current)) && (v.LessThan(target) || v.Equal(target)) {
if err := migration.Up(ctx); err != nil {
return successVersion, err
}
successVersion, _ = version.NewVersion(v.String())
}
}
}
if target.LessThan(current) {
var successVersion *version.Version
for i := len(migrations) - 1; i >= 0; i-- {
migration := migrations[i]
v := migration.TargetVersion
if migration.Down != nil && v.GreaterThan(target) && (v.LessThan(current) || v.Equal(current)) {
if err := migration.Down(ctx); err != nil {
return successVersion, err
}
successVersion, _ = version.NewVersion(v.String())
}
}
}
return target, nil
}