-
Notifications
You must be signed in to change notification settings - Fork 88
/
update.go
147 lines (120 loc) · 3.74 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
package handlers
import (
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/airgap"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/updatechecker"
"github.com/replicatedhq/kots/pkg/util"
)
type AppUpdateCheckRequest struct {
}
type AppUpdateCheckResponse struct {
AvailableUpdates int64 `json:"availableUpdates"`
CurrentAppSequence int64 `json:"currentAppSequence"`
}
func (h *Handler) AppUpdateCheck(w http.ResponseWriter, r *http.Request) {
appSlug := mux.Vars(r)["appSlug"]
foundApp, err := store.GetStore().GetAppFromSlug(appSlug)
if err != nil {
logger.Error(errors.Wrapf(err, "failed to get app for slug %q", appSlug))
w.WriteHeader(http.StatusNotFound)
return
}
deploy, _ := strconv.ParseBool(r.URL.Query().Get("deploy"))
skipPreflights, _ := strconv.ParseBool(r.URL.Query().Get("skipPreflights"))
isCLI, _ := strconv.ParseBool(r.URL.Query().Get("isCLI"))
contentType := strings.Split(r.Header.Get("Content-Type"), ";")[0]
contentType = strings.TrimSpace(contentType)
if contentType == "application/json" {
availableUpdates, err := updatechecker.CheckForUpdates(foundApp.ID, deploy, skipPreflights, isCLI)
if err != nil {
logger.Error(errors.Wrap(err, "failed to check for updates"))
w.WriteHeader(http.StatusInternalServerError)
cause := errors.Cause(err)
if _, ok := cause.(util.ActionableError); ok {
w.Write([]byte(cause.Error()))
}
return
}
appUpdateCheckResponse := AppUpdateCheckResponse{
AvailableUpdates: availableUpdates,
CurrentAppSequence: foundApp.CurrentSequence,
}
JSON(w, http.StatusOK, appUpdateCheckResponse)
return
}
if contentType == "multipart/form-data" {
if !foundApp.IsAirgap {
logger.Error(errors.New("not an airgap app"))
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Cannot update an online install using an airgap bundle"))
return
}
rootDir, err := ioutil.TempDir("", "kotsadm-airgap")
if err != nil {
logger.Error(errors.Wrap(err, "failed to create temp dir"))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer os.RemoveAll(rootDir)
formReader, err := r.MultipartReader()
if err != nil {
logger.Error(errors.Wrap(err, "failed to get multipart reader"))
w.WriteHeader(http.StatusInternalServerError)
return
}
for {
part, err := formReader.NextPart()
if err != nil {
if err == io.EOF {
break
}
logger.Error(errors.Wrap(err, "failed to get next part"))
w.WriteHeader(http.StatusInternalServerError)
return
}
fileName := filepath.Join(rootDir, part.FormName())
file, err := os.Create(fileName)
if err != nil {
logger.Error(errors.Wrap(err, "failed to create file"))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer file.Close()
_, err = io.Copy(file, part)
if err != nil {
logger.Error(errors.Wrap(err, "failed to copy part data"))
w.WriteHeader(http.StatusInternalServerError)
return
}
file.Close()
}
finishedChan := make(chan error)
defer close(finishedChan)
airgap.StartUpdateTaskMonitor(finishedChan)
err = airgap.UpdateAppFromPath(foundApp, rootDir, "", deploy, skipPreflights)
if err != nil {
finishedChan <- err
logger.Error(errors.Wrap(err, "failed to upgrde app"))
w.WriteHeader(http.StatusInternalServerError)
cause := errors.Cause(err)
if _, ok := cause.(util.ActionableError); ok {
w.Write([]byte(cause.Error()))
}
return
}
JSON(w, http.StatusOK, struct{}{})
return
}
logger.Error(errors.Errorf("unsupported content type: %s", r.Header.Get("Content-Type")))
w.WriteHeader(http.StatusBadRequest)
}