forked from pydio/cells
/
service-version.go
155 lines (127 loc) · 4.17 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
/*
* 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"
"io/ioutil"
"os"
"path/filepath"
"github.com/hashicorp/go-version"
"go.uber.org/zap"
"strings"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
)
type Migration struct {
TargetVersion *version.Version
Up func(ctx context.Context) error
Down func(ctx context.Context) error
}
// Create a version.NewVersion ignoring the error
func ValidVersion(v string) *version.Version {
obj, _ := version.NewVersion(v)
return obj
}
func FirstRun() *version.Version {
obj, _ := version.NewVersion("0.0.0")
return obj
}
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")
if data, err := ioutil.ReadFile(versionFile); err != nil {
if os.IsNotExist(err) {
fake, _ := version.NewVersion("0.0.0")
return fake, nil
} else {
return nil, err
}
} else {
return version.NewVersion(strings.TrimSpace(string(data)))
}
}
// UpdateVersion writes the version string to file
func UpdateVersion(serviceName string, v *version.Version) error {
if dir, err := config.ServiceDataDir(serviceName); err == nil {
versionFile := filepath.Join(dir, "version")
return ioutil.WriteFile(versionFile, []byte(v.String()), 0755)
} else {
return err
}
}
func UpdateServiceVersion(s Service) error {
options := s.Options()
newVersion, _ := version.NewVersion(options.Version)
if lastVersion, e := LastKnownVersion(options.Name); e == nil {
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
} else {
return e
}
}
// 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
func ApplyMigrations(ctx context.Context, current *version.Version, target *version.Version, migrations []*Migration) (*version.Version, error) {
if target.Equal(current) {
return nil, nil
}
if migrations == nil {
return target, nil
}
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
}