-
Notifications
You must be signed in to change notification settings - Fork 88
/
upload.go
213 lines (185 loc) · 6.72 KB
/
upload.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
package handlers
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/preflight"
"github.com/replicatedhq/kots/pkg/render"
"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"
)
type UploadExistingAppRequest struct {
Slug string `json:"slug"`
VersionLabel string `json:"versionLabel,omitempty"`
UpdateCursor string `json:"updateCursor,omitempty"`
Deploy bool `json:"deploy"`
SkipPreflights bool `json:"skipPreflights"`
}
type UploadResponse struct {
Slug string `json:"slug"`
}
// UploadExistingApp can be used to upload a multipart form file to the existing app
// This is used in the KOTS CLI when calling kots upload ...
// NOTE: this uses special kots token authorization
func (h *Handler) UploadExistingApp(w http.ResponseWriter, r *http.Request) {
if err := requireValidKOTSToken(w, r); err != nil {
logger.Error(errors.Wrap(err, "failed to get valid token"))
return
}
metadata := r.FormValue("metadata")
uploadExistingAppRequest := UploadExistingAppRequest{}
if err := json.NewDecoder(strings.NewReader(metadata)).Decode(&uploadExistingAppRequest); err != nil {
logger.Error(errors.Wrap(err, "failed to decode request"))
w.WriteHeader(http.StatusInternalServerError)
return
}
archive, _, err := r.FormFile("file")
if err != nil {
logger.Error(errors.Wrap(err, "failed to read file from request"))
w.WriteHeader(http.StatusInternalServerError)
return
}
tmpFile, err := ioutil.TempFile("", "kotsadm")
if err != nil {
logger.Error(errors.Wrap(err, "failed to create temp file"))
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = io.Copy(tmpFile, archive)
if err != nil {
logger.Error(errors.Wrap(err, "failed to copy file from request to temp file"))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer os.RemoveAll(tmpFile.Name())
archiveDir, err := version.ExtractArchiveToTempDirectory(tmpFile.Name())
if err != nil {
logger.Error(errors.Wrap(err, "failed to extract file"))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer os.RemoveAll(archiveDir)
// encrypt any plain text values
kotsKinds, err := kotsutil.LoadKotsKindsFromPath(archiveDir)
if err != nil {
logger.Error(errors.Wrap(err, "failed to load kotskinds"))
w.WriteHeader(http.StatusInternalServerError)
return
}
if kotsKinds.ConfigValues != nil {
if err := kotsKinds.EncryptConfigValues(); err != nil {
logger.Error(errors.Wrap(err, "failed to encrypt config values"))
w.WriteHeader(http.StatusInternalServerError)
return
}
updated, err := kotsKinds.Marshal("kots.io", "v1beta1", "ConfigValues")
if err != nil {
logger.Error(errors.Wrap(err, "failed to marshal config values"))
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := ioutil.WriteFile(filepath.Join(archiveDir, "upstream", "userdata", "config.yaml"), []byte(updated), 0644); err != nil {
logger.Error(errors.Wrap(err, "failed to write config values"))
w.WriteHeader(http.StatusInternalServerError)
return
}
}
a, err := store.GetStore().GetAppFromSlug(uploadExistingAppRequest.Slug)
if err != nil {
logger.Error(errors.Wrapf(err, "failed to get app for slug %q", uploadExistingAppRequest.Slug))
w.WriteHeader(http.StatusInternalServerError)
return
}
registrySettings, err := store.GetStore().GetRegistryDetailsForApp(a.ID)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get registry settings"))
w.WriteHeader(http.StatusInternalServerError)
return
}
downstreams, err := store.GetStore().ListDownstreamsForApp(a.ID)
if err != nil {
logger.Error(errors.Wrap(err, "failed to list downstreams"))
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(downstreams) == 0 {
logger.Errorf("no downstreams found for deploying %s", a.Slug)
w.WriteHeader(http.StatusInternalServerError)
return
}
nextAppSequence, err := store.GetStore().GetNextAppSequence(a.ID)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get next app sequence"))
w.WriteHeader(http.StatusInternalServerError)
return
}
err = render.RenderDir(archiveDir, a, downstreams, registrySettings, nextAppSequence)
if err != nil {
logger.Error(errors.Wrap(err, "failed to render app version"))
w.WriteHeader(http.StatusInternalServerError)
return
}
baseSequence, err := store.GetStore().GetAppVersionBaseSequence(a.ID, kotsKinds.Installation.Spec.VersionLabel)
if err != nil {
logger.Error(errors.Wrap(err, "failed to app version base sequence"))
w.WriteHeader(http.StatusInternalServerError)
return
}
newSequence, err := store.GetStore().CreateAppVersion(a.ID, &baseSequence, archiveDir, "KOTS Upload", uploadExistingAppRequest.SkipPreflights, &version.DownstreamGitOps{}, render.Renderer{})
if err != nil {
logger.Error(errors.Wrap(err, "failed to create app version"))
w.WriteHeader(http.StatusInternalServerError)
return
}
hasStrictPreflights, err := store.GetStore().HasStrictPreflights(a.ID, newSequence)
if err != nil {
logger.Error(errors.Wrap(err, "failed to check if app preflight has strict analyzers"))
w.WriteHeader(http.StatusInternalServerError)
return
}
if hasStrictPreflights && uploadExistingAppRequest.SkipPreflights {
logger.Warnf("preflights will not be skipped, strict preflights are set to %t", hasStrictPreflights)
}
if !uploadExistingAppRequest.SkipPreflights || hasStrictPreflights {
if err := preflight.Run(a.ID, a.Slug, newSequence, a.IsAirgap, archiveDir); err != nil {
logger.Error(errors.Wrap(err, "failed to get run preflights"))
w.WriteHeader(http.StatusInternalServerError)
return
}
}
if uploadExistingAppRequest.Deploy {
status, err := store.GetStore().GetStatusForVersion(a.ID, downstreams[0].ClusterID, newSequence)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get update downstream status"))
w.WriteHeader(http.StatusInternalServerError)
return
}
if status == storetypes.VersionPendingConfig {
logger.Error(errors.Errorf("not deploying version %d because it's %s", newSequence, status))
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := version.DeployVersion(a.ID, newSequence); err != nil {
logger.Error(errors.Wrap(err, "failed to deploy latest version"))
w.WriteHeader(http.StatusInternalServerError)
cause := errors.Cause(err)
if _, ok := cause.(util.ActionableError); ok {
w.Write([]byte(cause.Error()))
}
return
}
}
uploadResponse := UploadResponse{
Slug: a.Slug,
}
JSON(w, http.StatusOK, uploadResponse)
}