-
Notifications
You must be signed in to change notification settings - Fork 90
/
update.go
290 lines (249 loc) · 9.63 KB
/
update.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package airgap
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/blang/semver"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
apptypes "github.com/replicatedhq/kots/pkg/app/types"
"github.com/replicatedhq/kots/pkg/cursor"
identity "github.com/replicatedhq/kots/pkg/kotsadmidentity"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/preflight"
"github.com/replicatedhq/kots/pkg/pull"
"github.com/replicatedhq/kots/pkg/store"
storetypes "github.com/replicatedhq/kots/pkg/store/types"
"github.com/replicatedhq/kots/pkg/util"
"github.com/replicatedhq/kots/pkg/version"
)
func StartUpdateTaskMonitor(finishedChan <-chan error) {
go func() {
var finalError error
defer func() {
if finalError == nil {
if err := store.GetStore().ClearTaskStatus("update-download"); err != nil {
logger.Error(errors.Wrap(err, "failed to clear update-download task status"))
}
} else {
errMsg := finalError.Error()
if cause, ok := errors.Cause(finalError).(util.ActionableError); ok {
errMsg = cause.Error()
}
if err := store.GetStore().SetTaskStatus("update-download", errMsg, "failed"); err != nil {
logger.Error(errors.Wrap(err, "failed to set error on update-download task status"))
}
}
}()
for {
select {
case <-time.After(time.Second):
if err := store.GetStore().UpdateTaskStatusTimestamp("update-download"); err != nil {
logger.Error(err)
}
case err := <-finishedChan:
finalError = err
return
}
}
}()
}
func UpdateAppFromAirgap(a *apptypes.App, airgapBundlePath string, deploy bool, skipPreflights bool, skipCompatibilityCheck bool) (finalError error) {
finishedChan := make(chan error)
defer close(finishedChan)
StartUpdateTaskMonitor(finishedChan)
defer func() {
finishedChan <- finalError
}()
if err := store.GetStore().SetTaskStatus("update-download", "Extracting files...", "running"); err != nil {
return errors.Wrap(err, "failed to set task status")
}
airgapRoot, err := extractAppMetaFromAirgapBundle(airgapBundlePath)
if err != nil {
return errors.Wrap(err, "failed to extract archive")
}
defer os.RemoveAll(airgapRoot)
err = UpdateAppFromPath(a, airgapRoot, airgapBundlePath, deploy, skipPreflights, skipCompatibilityCheck)
return errors.Wrap(err, "failed to update app")
}
func UpdateAppFromPath(a *apptypes.App, airgapRoot string, airgapBundlePath string, deploy bool, skipPreflights bool, skipCompatibilityCheck bool) error {
if err := store.GetStore().SetTaskStatus("update-download", "Processing package...", "running"); err != nil {
return errors.Wrap(err, "failed to set tasks status")
}
registrySettings, err := store.GetStore().GetRegistryDetailsForApp(a.ID)
if err != nil {
return errors.Wrap(err, "failed to get app registry settings")
}
airgap, err := pull.FindAirgapMetaInDir(airgapRoot)
if err != nil {
return errors.Wrap(err, "failed to parse license from file")
}
archiveDir, baseSequence, err := store.GetStore().GetAppVersionBaseArchive(a.ID, airgap.Spec.VersionLabel)
if err != nil {
return errors.Wrapf(err, "failed to get base archive dir for version %s", airgap.Spec.VersionLabel)
}
defer os.RemoveAll(archiveDir)
beforeKotsKinds, err := kotsutil.LoadKotsKindsFromPath(archiveDir)
if err != nil {
return errors.Wrap(err, "failed to load current kotskinds")
}
if beforeKotsKinds.License == nil {
err := errors.New("no license found in application")
return err
}
if err := store.GetStore().SetTaskStatus("update-download", "Processing app package...", "running"); err != nil {
return errors.Wrap(err, "failed to set task status")
}
appNamespace := util.AppNamespace()
if err := store.GetStore().SetTaskStatus("update-download", "Creating app version...", "running"); err != nil {
return errors.Wrap(err, "failed to set task status")
}
appSequence, err := store.GetStore().GetNextAppSequence(a.ID)
if err != nil {
return errors.Wrap(err, "failed to get new app sequence")
}
pipeReader, pipeWriter := io.Pipe()
go func() {
scanner := bufio.NewScanner(pipeReader)
for scanner.Scan() {
if err := store.GetStore().SetTaskStatus("update-download", scanner.Text(), "running"); err != nil {
logger.Error(errors.Wrap(err, "failed to update download status"))
}
}
pipeReader.CloseWithError(scanner.Err())
}()
// Using license from db instead of upstream bundle because the one in db has not been re-marshalled
license, err := pull.ParseLicenseFromBytes([]byte(a.License))
if err != nil {
return errors.Wrap(err, "failed parse license")
}
identityConfigFile := filepath.Join(archiveDir, "upstream", "userdata", "identityconfig.yaml")
if _, err := os.Stat(identityConfigFile); os.IsNotExist(err) {
file, err := identity.InitAppIdentityConfig(a.Slug, kotsv1beta1.Storage{})
if err != nil {
return errors.Wrap(err, "failed to init identity config")
}
identityConfigFile = file
defer os.Remove(identityConfigFile)
} else if err != nil {
return errors.Wrap(err, "failed to get stat identity config file")
}
pullOptions := pull.PullOptions{
LicenseObj: license,
Namespace: appNamespace,
ConfigFile: filepath.Join(archiveDir, "upstream", "userdata", "config.yaml"),
IdentityConfigFile: identityConfigFile,
AirgapRoot: airgapRoot,
AirgapBundle: airgapBundlePath,
InstallationFile: filepath.Join(archiveDir, "upstream", "userdata", "installation.yaml"),
UpdateCursor: beforeKotsKinds.Installation.Spec.UpdateCursor,
RootDir: archiveDir,
ExcludeKotsKinds: true,
ExcludeAdminConsole: true,
CreateAppDir: false,
ReportWriter: pipeWriter,
Silent: true,
RewriteImages: true,
RewriteImageOptions: pull.RewriteImageOptions{
ImageFiles: filepath.Join(airgapRoot, "images"),
Host: registrySettings.Hostname,
Namespace: registrySettings.Namespace,
Username: registrySettings.Username,
Password: registrySettings.Password,
IsReadOnly: registrySettings.IsReadOnly,
},
AppSlug: a.Slug,
AppSequence: appSequence,
SkipCompatibilityCheck: skipCompatibilityCheck,
}
if _, err := pull.Pull(fmt.Sprintf("replicated://%s", beforeKotsKinds.License.Spec.AppSlug), pullOptions); err != nil {
return errors.Wrap(err, "failed to pull")
}
afterKotsKinds, err := kotsutil.LoadKotsKindsFromPath(archiveDir)
if err != nil {
return errors.Wrap(err, "failed to read after kotskinds")
}
if err := canInstall(beforeKotsKinds, afterKotsKinds); err != nil {
return errors.Wrap(err, "cannot install")
}
// Create the app in the db
newSequence, err := store.GetStore().CreateAppVersion(a.ID, &baseSequence, archiveDir, "Airgap Update", skipPreflights, &version.DownstreamGitOps{})
if err != nil {
return errors.Wrap(err, "failed to create new version")
}
if !skipPreflights {
if err := preflight.Run(a.ID, a.Slug, newSequence, true, archiveDir); err != nil {
return errors.Wrap(err, "failed to start preflights")
}
}
if deploy {
downstreams, err := store.GetStore().ListDownstreamsForApp(a.ID)
if len(downstreams) == 0 {
return errors.Errorf("no downstreams found for app %q", a.Slug)
}
downstream := downstreams[0]
status, err := store.GetStore().GetStatusForVersion(a.ID, downstream.ClusterID, newSequence)
if err != nil {
return errors.Wrap(err, "failed to get update downstream status")
}
if status == storetypes.VersionPendingConfig {
return errors.Errorf("not deploying version %d because it's %s", newSequence, status)
}
if err := version.DeployVersion(a.ID, newSequence); err != nil {
return errors.Wrap(err, "failed to deploy app version")
}
}
return nil
}
func canInstall(beforeKotsKinds *kotsutil.KotsKinds, afterKotsKinds *kotsutil.KotsKinds) error {
var beforeSemver, afterSemver *semver.Version
if v, err := semver.ParseTolerant(beforeKotsKinds.Installation.Spec.VersionLabel); err == nil {
beforeSemver = &v
}
if v, err := semver.ParseTolerant(afterKotsKinds.Installation.Spec.VersionLabel); err == nil {
afterSemver = &v
}
isSameVersion := false
if beforeSemver != nil && afterSemver != nil {
// Allow uploading older versions if both have semvers because they can be sorted correctly.
if beforeSemver.EQ(*afterSemver) {
isSameVersion = true
}
} else if beforeSemver != nil {
// TODO: pass or fail?
} else if afterSemver != nil {
// TODO: pass or fail?
} else {
bc, err := cursor.NewCursor(beforeKotsKinds.Installation.Spec.UpdateCursor)
if err != nil {
return errors.Wrap(err, "failed to create bc")
}
ac, err := cursor.NewCursor(afterKotsKinds.Installation.Spec.UpdateCursor)
if err != nil {
return errors.Wrap(err, "failed to create ac")
}
if !bc.Comparable(ac) {
return errors.Errorf("cannot compare %q and %q", beforeKotsKinds.Installation.Spec.UpdateCursor, afterKotsKinds.Installation.Spec.UpdateCursor)
}
installChannelID := beforeKotsKinds.Installation.Spec.ChannelID
licenseChannelID := beforeKotsKinds.License.Spec.ChannelID
installChannelName := beforeKotsKinds.Installation.Spec.ChannelName
licenseChannelName := beforeKotsKinds.License.Spec.ChannelName
if (installChannelID != "" && licenseChannelID != "" && installChannelID == licenseChannelID) || (installChannelName == licenseChannelName) {
if bc.Equal(ac) {
isSameVersion = true
}
}
}
if isSameVersion {
return util.ActionableError{
NoRetry: true,
Message: fmt.Sprintf("Version %s (%s) cannot be installed again because it is already the current version", afterKotsKinds.Installation.Spec.VersionLabel, afterKotsKinds.Installation.Spec.UpdateCursor),
}
}
return nil
}