-
Notifications
You must be signed in to change notification settings - Fork 88
/
online.go
264 lines (237 loc) · 9.69 KB
/
online.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
package online
import (
"bufio"
"io"
"io/ioutil"
"os"
"time"
"github.com/pkg/errors"
apptypes "github.com/replicatedhq/kots/pkg/app/types"
kotsadmconfig "github.com/replicatedhq/kots/pkg/kotsadmconfig"
identity "github.com/replicatedhq/kots/pkg/kotsadmidentity"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/online/types"
"github.com/replicatedhq/kots/pkg/preflight"
"github.com/replicatedhq/kots/pkg/pull"
"github.com/replicatedhq/kots/pkg/render"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/store"
storetypes "github.com/replicatedhq/kots/pkg/store/types"
"github.com/replicatedhq/kots/pkg/supportbundle"
supportbundletypes "github.com/replicatedhq/kots/pkg/supportbundle/types"
"github.com/replicatedhq/kots/pkg/updatechecker"
"github.com/replicatedhq/kots/pkg/util"
"github.com/replicatedhq/kots/pkg/version"
"go.uber.org/zap"
)
type CreateOnlineAppOpts struct {
PendingApp *types.PendingApp
UpstreamURI string
IsAutomated bool
SkipPreflights bool
SkipCompatibilityCheck bool
}
func CreateAppFromOnline(opts CreateOnlineAppOpts) (_ *kotsutil.KotsKinds, finalError error) {
logger.Debug("creating app from online",
zap.String("upstreamURI", opts.UpstreamURI))
if err := store.GetStore().SetTaskStatus("online-install", "Uploading license...", "running"); err != nil {
return nil, 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("online-install"); err != nil {
logger.Error(err)
}
case <-finishedCh:
return
}
}
}()
var app *apptypes.App
defer func() {
if finalError == nil {
if err := store.GetStore().ClearTaskStatus("online-install"); err != nil {
logger.Error(errors.Wrap(err, "failed to clear install task status"))
}
if err := store.GetStore().SetAppInstallState(opts.PendingApp.ID, "installed"); err != nil {
logger.Error(errors.Wrap(err, "failed to set app status to installed"))
}
if err := updatechecker.Configure(app, app.UpdateCheckerSpec); err != nil {
logger.Error(errors.Wrap(err, "failed to configure update checker"))
}
} else {
if err := store.GetStore().SetTaskStatus("online-install", finalError.Error(), "failed"); err != nil {
logger.Error(errors.Wrap(err, "failed to set error on install task status"))
}
if err := store.GetStore().SetAppInstallState(opts.PendingApp.ID, "install_error"); err != nil {
logger.Error(errors.Wrap(err, "failed to set app status to error"))
}
}
}()
pipeReader, pipeWriter := io.Pipe()
go func() {
scanner := bufio.NewScanner(pipeReader)
for scanner.Scan() {
if err := store.GetStore().SetTaskStatus("online-install", scanner.Text(), "running"); err != nil {
logger.Error(err)
}
}
pipeReader.CloseWithError(scanner.Err())
}()
// put the license in a temp file
licenseFile, err := ioutil.TempFile("", "kotsadm")
if err != nil {
return nil, errors.Wrap(err, "failed to create tmp file for license")
}
defer os.RemoveAll(licenseFile.Name())
if err := ioutil.WriteFile(licenseFile.Name(), []byte(opts.PendingApp.LicenseData), 0644); err != nil {
return nil, errors.Wrap(err, "failed to write license tmp file")
}
// pull to a tmp dir
tmpRoot, err := ioutil.TempDir("", "kots")
if err != nil {
return nil, errors.Wrap(err, "failed to create tmp dir for pull")
}
defer os.RemoveAll(tmpRoot)
appNamespace := util.AppNamespace()
configValues, err := kotsadmconfig.ReadConfigValuesFromInClusterSecret()
if err != nil {
return nil, errors.Wrap(err, "failed to read config values from in cluster")
}
configFile := ""
if configValues != "" {
tmpFile, err := ioutil.TempFile("", "kots")
if err != nil {
return nil, errors.Wrap(err, "failed to create temp file for config values")
}
defer os.RemoveAll(tmpFile.Name())
if err := ioutil.WriteFile(tmpFile.Name(), []byte(configValues), 0644); err != nil {
return nil, errors.Wrap(err, "failed to write config values to temp file")
}
configFile = tmpFile.Name()
}
identityConfigFile, err := identity.InitAppIdentityConfig(opts.PendingApp.Slug)
if err != nil {
return nil, errors.Wrap(err, "failed to init identity config")
}
defer os.Remove(identityConfigFile)
// kots install --config-values (and other documented automation workflows) support
// a writing a config values file as a secret...
// if this secret exists, we automatically (blindly) use it as the config values
// for the application, and then delete it.
pullOptions := pull.PullOptions{
Downstreams: []string{"this-cluster"},
LicenseFile: licenseFile.Name(),
Namespace: appNamespace,
ExcludeKotsKinds: true,
RootDir: tmpRoot,
ExcludeAdminConsole: true,
CreateAppDir: false,
ConfigFile: configFile,
IdentityConfigFile: identityConfigFile,
ReportWriter: pipeWriter,
AppID: opts.PendingApp.ID,
AppSlug: opts.PendingApp.Slug,
AppSequence: 0,
AppVersionLabel: opts.PendingApp.VersionLabel,
ReportingInfo: reporting.GetReportingInfo(opts.PendingApp.ID),
SkipCompatibilityCheck: opts.SkipCompatibilityCheck,
}
if _, err := pull.Pull(opts.UpstreamURI, pullOptions); err != nil {
if errors.Cause(err) != pull.ErrConfigNeeded {
return nil, errors.Wrap(err, "failed to pull")
}
}
// Create the downstream
// copying this from typescript ...
// i'll leave this next line
// TODO: refactor this entire function to be testable, reliable and less monolithic
if err := store.GetStore().AddAppToAllDownstreams(opts.PendingApp.ID); err != nil {
return nil, errors.Wrap(err, "failed to add app to all downstreams")
}
if err := store.GetStore().SetAppIsAirgap(opts.PendingApp.ID, false); err != nil {
return nil, errors.Wrap(err, "failed to set app is not airgap")
}
newSequence, err := store.GetStore().CreateAppVersion(opts.PendingApp.ID, nil, tmpRoot, "Online Install", opts.SkipPreflights, &version.DownstreamGitOps{}, render.Renderer{})
if err != nil {
return nil, errors.Wrap(err, "failed to create new version")
}
app, err = store.GetStore().GetApp(opts.PendingApp.ID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get app %s", opts.PendingApp.ID)
}
troubleshootOpts := supportbundletypes.TroubleshootOptions{
InCluster: true,
}
_, err = supportbundle.CreateSupportBundleDependencies(app, newSequence, troubleshootOpts)
if err != nil {
return nil, errors.Wrap(err, "failed to create rendered support bundle spec")
}
kotsKinds, err := kotsutil.LoadKotsKinds(tmpRoot)
if err != nil {
return nil, errors.Wrap(err, "failed to load kotskinds from path")
}
hasStrictPreflights, err := store.GetStore().HasStrictPreflights(opts.PendingApp.ID, newSequence)
if err != nil {
return nil, errors.Wrap(err, "failed to check if app preflight has strict analyzers")
}
if hasStrictPreflights && opts.SkipPreflights {
logger.Warnf("preflights will not be skipped, strict preflights are set to %t", hasStrictPreflights)
}
if opts.IsAutomated && kotsKinds.IsConfigurable() {
// bypass the config screen if no configuration is required and it's an automated install
registrySettings, err := store.GetStore().GetRegistryDetailsForApp(opts.PendingApp.ID)
if err != nil {
return nil, errors.Wrap(err, "failed to get registry settings for app")
}
needsConfig, err := kotsadmconfig.NeedsConfiguration(opts.PendingApp.Slug, newSequence, false, kotsKinds, registrySettings)
if err != nil {
return nil, errors.Wrap(err, "failed to check if app needs configuration")
}
if !needsConfig {
if opts.SkipPreflights && !hasStrictPreflights {
if err := store.GetStore().SetDownstreamVersionStatus(opts.PendingApp.ID, newSequence, storetypes.VersionPending, ""); err != nil {
return nil, errors.Wrap(err, "failed to set downstream version status to pending")
}
if err := version.DeployVersion(opts.PendingApp.ID, newSequence); err != nil {
return nil, errors.Wrap(err, "failed to deploy version")
}
go func() {
if err := reporting.WaitAndReportPreflightChecks(opts.PendingApp.ID, newSequence, opts.SkipPreflights, opts.IsAutomated); err != nil {
logger.Debugf("failed to send preflights data to replicated app: %v", err)
}
}()
} else {
err := store.GetStore().SetDownstreamVersionStatus(opts.PendingApp.ID, newSequence, storetypes.VersionPendingPreflight, "")
if err != nil {
return nil, errors.Wrap(err, "failed to set downstream version status to 'pending preflight'")
}
}
}
}
if !opts.SkipPreflights || hasStrictPreflights {
if err := preflight.Run(opts.PendingApp.ID, opts.PendingApp.Slug, newSequence, false, tmpRoot); err != nil {
return nil, errors.Wrap(err, "failed to start preflights")
}
}
if !kotsKinds.IsConfigurable() && opts.SkipPreflights && !hasStrictPreflights {
if err := store.GetStore().SetDownstreamVersionStatus(opts.PendingApp.ID, newSequence, storetypes.VersionPending, ""); err != nil {
return nil, errors.Wrap(err, "failed to set downstream version status to pending")
}
// app is not configurable and preflights are skipped, so just deploy the app
if err := version.DeployVersion(opts.PendingApp.ID, newSequence); err != nil {
return nil, errors.Wrap(err, "failed to deploy version")
}
go func() {
if err := reporting.WaitAndReportPreflightChecks(opts.PendingApp.ID, newSequence, opts.SkipPreflights, opts.IsAutomated); err != nil {
logger.Debugf("failed to send preflights data to replicated app: %v", err)
}
}()
}
return kotsKinds, nil
}