-
Notifications
You must be signed in to change notification settings - Fork 88
/
upstream.go
171 lines (148 loc) · 5.26 KB
/
upstream.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
package upstream
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/crypto"
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"
kotspull "github.com/replicatedhq/kots/pkg/pull"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
"github.com/replicatedhq/kots/pkg/version"
)
func DownloadUpdate(appID string, archiveDir string, toCursor string, skipPreflights bool) (sequence int64, finalError error) {
if err := store.GetStore().SetTaskStatus("update-download", "Fetching update...", "running"); err != nil {
return 0, errors.Wrap(err, "failed to set task status")
}
finishedCh := make(chan struct{})
defer close(finishedCh)
go func() {
for {
select {
case <-time.After(time.Second):
if err := store.GetStore().UpdateTaskStatusTimestamp("update-download"); err != nil {
logger.Error(err)
}
case <-finishedCh:
return
}
}
}()
defer func() {
if finalError == nil {
if err := store.GetStore().ClearTaskStatus("update-download"); err != nil {
logger.Error(err)
}
} else {
if err := store.GetStore().SetTaskStatus("update-download", finalError.Error(), "failed"); err != nil {
logger.Error(err)
}
}
}()
beforeKotsKinds, err := kotsutil.LoadKotsKindsFromPath(archiveDir)
if err != nil {
return 0, errors.Wrap(err, "failed to read kots kinds before update")
}
beforeCursor := beforeKotsKinds.Installation.Spec.UpdateCursor
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(err)
}
}
pipeReader.CloseWithError(scanner.Err())
}()
a, err := store.GetStore().GetApp(appID)
if err != nil {
return 0, errors.Wrap(err, "failed to get app")
}
downstreams, err := store.GetStore().ListDownstreamsForApp(a.ID)
if err != nil {
return 0, errors.Wrap(err, "failed to list downstreams for app")
}
downstreamNames := []string{}
for _, d := range downstreams {
downstreamNames = append(downstreamNames, d.Name)
}
appNamespace := util.AppNamespace()
appSequence, err := version.GetNextAppSequence(a.ID, &a.CurrentSequence)
if err != nil {
return 0, errors.Wrap(err, "failed to get new app sequence")
}
latestLicense, err := store.GetStore().GetLatestLicenseForApp(a.ID)
if err != nil {
return 0, errors.Wrap(err, "failed to get latest 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{}, crypto.AESCipher{})
if err != nil {
return 0, errors.Wrap(err, "failed to init identity config")
}
identityConfigFile = file
defer os.Remove(identityConfigFile)
} else if err != nil {
return 0, errors.Wrap(err, "failed to get stat identity config file")
}
registrySettings, err := store.GetStore().GetRegistryDetailsForApp(appID)
if err != nil {
return 0, errors.Wrap(err, "failed to get registry settings")
}
pullOptions := kotspull.PullOptions{
LicenseObj: latestLicense,
Namespace: appNamespace,
ConfigFile: filepath.Join(archiveDir, "upstream", "userdata", "config.yaml"),
IdentityConfigFile: identityConfigFile,
InstallationFile: filepath.Join(archiveDir, "upstream", "userdata", "installation.yaml"),
UpdateCursor: toCursor,
RootDir: archiveDir,
Downstreams: downstreamNames,
ExcludeKotsKinds: true,
ExcludeAdminConsole: true,
CreateAppDir: false,
ReportWriter: pipeWriter,
AppSlug: a.Slug,
AppSequence: appSequence,
IsGitOps: a.IsGitOps,
ReportingInfo: reporting.GetReportingInfo(a.ID),
RewriteImages: registrySettings.IsValid(),
RewriteImageOptions: kotspull.RewriteImageOptions{
Host: registrySettings.Hostname,
Namespace: registrySettings.Namespace,
Username: registrySettings.Username,
Password: registrySettings.Password,
IsReadOnly: registrySettings.IsReadOnly,
},
}
if _, err := kotspull.Pull(fmt.Sprintf("replicated://%s", beforeKotsKinds.License.Spec.AppSlug), pullOptions); err != nil {
return 0, errors.Wrap(err, "failed to pull")
}
afterKotsKinds, err := kotsutil.LoadKotsKindsFromPath(archiveDir)
if err != nil {
return 0, errors.Wrap(err, "failed to read kots kinds after update")
}
if afterKotsKinds.Installation.Spec.UpdateCursor == beforeCursor {
return 0, nil // ?
}
newSequence, err := store.GetStore().CreateAppVersion(a.ID, &a.CurrentSequence, archiveDir, "Upstream Update", skipPreflights, &version.DownstreamGitOps{})
if err != nil {
return 0, errors.Wrap(err, "failed to create version")
}
if !skipPreflights {
if err := preflight.Run(appID, a.Slug, newSequence, a.IsAirgap, archiveDir); err != nil {
return 0, errors.Wrap(err, "failed to run preflights")
}
}
return newSequence, nil
}