generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dir.go
214 lines (181 loc) · 6.91 KB
/
dir.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package releases
import (
"fmt"
"github.com/broadinstitute/thelma/internal/thelma/app/autoupdate/manifest"
"github.com/broadinstitute/thelma/internal/thelma/app/root"
"github.com/broadinstitute/thelma/internal/thelma/app/scratch"
"github.com/broadinstitute/thelma/internal/thelma/utils"
"github.com/broadinstitute/thelma/internal/thelma/utils/flock"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"golang.org/x/mod/semver"
"os"
"path"
"path/filepath"
"sort"
"time"
)
const currentSymlink = "current"
const lockFile = ".installer.lk"
// CurrentReleaseSymlink returns the path to the current release symlink at ~/.thelma/releases/current
func CurrentReleaseSymlink(root root.Root) string {
return path.Join(root.ReleasesDir(), currentSymlink)
}
// Dir represents the local directory where Thelma stores its releases ("~/.thelma/releases")
type Dir interface {
// CurrentVersion returns the currently installed version of Thelma
// Returns an error if the symlink does not exist, is broken, or another fs error occurs
CurrentVersion() (string, error)
// CurrentVersionMatches returns true if the currently installed version of
// Thelma matches the given version.
// Returns false if versions don't match, if there is no current version of Thelma (say,
// during a fresh install), or if another error is encountered identifying the current version
CurrentVersionMatches(version string) bool
// UpdateCurrentReleaseSymlink atomically updates the current release symlink at ~/.thelma/releases/current
// to point at a given installed release version
UpdateCurrentReleaseSymlink(version string) error
// CopyUnpackedArchive copies an unpacked release archive into the releases directory
CopyUnpackedArchive(unpackDir string) error
// WithInstallerLock executes a callback function while holding the release installer lock
WithInstallerLock(fn func() error) error
// CleanupOldReleases cleans up all but N most recent releases
CleanupOldReleases(keepReleases int) error
}
func NewDir(dir string, scratch scratch.Scratch) Dir {
return &releasesDir{
dir: dir,
scratch: scratch,
}
}
type releasesDir struct {
dir string
scratch scratch.Scratch
}
func (r *releasesDir) CurrentVersion() (string, error) {
symlink := r.currentSymlinkPath()
resolved, err := filepath.EvalSymlinks(symlink)
if err != nil {
return "", errors.Errorf("error resolving symlink %s: %v", symlink, err)
}
return path.Base(resolved), nil
}
func (r *releasesDir) CurrentVersionMatches(version string) bool {
currentVersion, err := r.CurrentVersion()
if err != nil {
log.Debug().Err(err).Msgf("error resolving current version symlink")
// failed to resolve current version; send false to indicate that we should attempt an upgrade anyway
// (this happens during bootstrapping when the current symlink does not exist yet)
return false
}
return currentVersion == version
}
func (r *releasesDir) UpdateCurrentReleaseSymlink(version string) error {
// following procedure described here:
// https://stackoverflow.com/a/58148921
// create tmp dir
tmpDir, err := r.scratch.Mkdir("releases")
if err != nil {
return errors.Errorf("error updating %s symbolic link: %v", r.currentSymlinkPath(), err)
}
releaseDir := r.pathForVersion(version)
// create a new symlink in tmp dir pointing at the release version directory
tmpLink := path.Join(tmpDir, "current.tmp")
if err = os.Symlink(releaseDir, tmpLink); err != nil {
return errors.Errorf("error updating %s symbolic link: %v", r.currentSymlinkPath(), err)
}
// then rename it on top of the existing symlink, which is atomic
if err = os.Rename(tmpLink, r.currentSymlinkPath()); err != nil {
return errors.Errorf("error updating %s symbolic link: %v", r.currentSymlinkPath(), err)
}
return nil
}
func (r *releasesDir) CopyUnpackedArchive(unpackDir string) error {
version, err := manifest.Version(unpackDir)
if err != nil {
return errors.Errorf("error installing %s to release directory: %v", unpackDir, err)
}
targetDir := r.pathForVersion(version)
exists, err := utils.FileExists(targetDir)
if err != nil {
return errors.Errorf("error installing %s to release directory: %v", unpackDir, err)
}
if exists {
log.Warn().Msgf("Release directory %s already exists; removing it", targetDir)
if err = os.RemoveAll(targetDir); err != nil {
return errors.Errorf("error removing existing release directory %s: %v", targetDir, err)
}
}
if err = os.Rename(unpackDir, targetDir); err != nil {
return errors.Errorf("error moving unpacked release archive %s to release directory %s: %v", unpackDir, targetDir, err)
}
return nil
}
func (r *releasesDir) CleanupOldReleases(keepReleases int) error {
entries, err := os.ReadDir(r.dir)
if err != nil {
return errors.Errorf("error cleaning up old Thelma releases: %v", err)
}
currentVersion, err := r.CurrentVersion()
if err != nil {
log.Debug().Err(err).Msgf("error resolving current version of Thelma")
}
var versions []string
for _, entry := range entries {
if entry.Name() == currentSymlink {
continue
}
if entry.Name() == currentVersion {
continue
}
if !entry.IsDir() {
log.Debug().Msgf("won't cleanup unexpected file in releases dir: %s", entry.Name())
continue
}
if !semver.IsValid(entry.Name()) {
log.Debug().Msgf("won't cleanup unexpected dir in releases dir: %s", entry.Name())
continue
}
versions = append(versions, entry.Name())
}
// sort versions in order of increasing semantic versions (older versions first)
sort.Slice(versions, func(i, j int) bool {
return semver.Compare(versions[i], versions[j]) < 0
})
if len(versions) <= keepReleases {
// no versions to delete
return nil
}
toDelete := len(versions) - keepReleases
for i := 0; i < toDelete; i++ {
version := versions[i]
targetDir := r.pathForVersion(version)
log.Info().Msgf("Deleting old Thelma release directory: %s", targetDir)
if err = os.RemoveAll(targetDir); err != nil {
return errors.Errorf("error cleaning up old Thelma releases: %v", err)
}
}
return nil
}
func (r *releasesDir) WithInstallerLock(fn func() error) error {
_lockFile := path.Join(r.dir, lockFile)
locker := flock.NewLocker(_lockFile, func(options *flock.Options) {
options.Timeout = 5 * time.Minute
options.RetryInterval = 10 * time.Second
})
return locker.WithLock(func() error {
// write pid to lock file after we obtain it, to help with debugging
pidStr := fmt.Sprintf("%d", os.Getpid())
if err := os.WriteFile(_lockFile, []byte(pidStr), 0644); err != nil {
return errors.Errorf("error updating lock file %s: %v", _lockFile, err)
}
return fn()
})
}
// currentSymlinkPath returns the full path to the current release symlink, eg. ~/.thelma/releases/current
func (r *releasesDir) currentSymlinkPath() string {
return path.Join(r.dir, currentSymlink)
}
// pathForVersion returns the full path to a given Thelma release, eg ~/.thelma/releases/v1.2.3
func (r *releasesDir) pathForVersion(version string) string {
return path.Join(r.dir, version)
}