-
Notifications
You must be signed in to change notification settings - Fork 88
/
update_checker_spec.go
132 lines (113 loc) · 4.98 KB
/
update_checker_spec.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
package handlers
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/pkg/errors"
apptypes "github.com/replicatedhq/kots/pkg/app/types"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/updatechecker"
cron "github.com/robfig/cron/v3"
)
type SetAutomaticUpdatesConfigRequest struct {
UpdateCheckerSpec string `json:"updateCheckerSpec"`
AutoDeploy apptypes.AutoDeploy `json:"autoDeploy"`
}
type SetAutomaticUpdatesConfigResponse struct {
Error string `json:"error"`
}
type GetAutomaticUpdatesConfigResponse struct {
UpdateCheckerSpec string `json:"updateCheckerSpec"`
AutoDeploy apptypes.AutoDeploy `json:"autoDeploy"`
Error string `json:"error"`
}
func (h *Handler) SetAutomaticUpdatesConfig(w http.ResponseWriter, r *http.Request) {
updateCheckerSpecResponse := &SetAutomaticUpdatesConfigResponse{}
configureAutomaticUpdatesRequest := SetAutomaticUpdatesConfigRequest{}
if err := json.NewDecoder(r.Body).Decode(&configureAutomaticUpdatesRequest); err != nil {
updateCheckerSpecResponse.Error = "failed to decode request body"
logger.Error(errors.Wrap(err, updateCheckerSpecResponse.Error))
JSON(w, http.StatusBadRequest, updateCheckerSpecResponse)
return
}
foundApp, err := store.GetStore().GetAppFromSlug(mux.Vars(r)["appSlug"])
if err != nil {
updateCheckerSpecResponse.Error = "failed to get app from slug"
logger.Error(errors.Wrap(err, updateCheckerSpecResponse.Error))
JSON(w, http.StatusInternalServerError, updateCheckerSpecResponse)
return
}
license, err := kotsutil.LoadLicenseFromBytes([]byte(foundApp.License))
if err != nil {
updateCheckerSpecResponse.Error = "failed to get license from app"
logger.Error(errors.Wrap(err, updateCheckerSpecResponse.Error))
JSON(w, http.StatusInternalServerError, updateCheckerSpecResponse)
return
}
// Check if the deploy update configuration is valid based on app channel
if license.Spec.IsSemverRequired {
if configureAutomaticUpdatesRequest.AutoDeploy == apptypes.AutoDeploySequence {
updateCheckerSpecResponse.Error = "automatic updates based on sequence type are not supported for semantic versioning apps"
JSON(w, http.StatusUnprocessableEntity, updateCheckerSpecResponse)
return
}
} else {
if configureAutomaticUpdatesRequest.AutoDeploy != apptypes.AutoDeployDisabled && configureAutomaticUpdatesRequest.AutoDeploy != apptypes.AutoDeploySequence {
updateCheckerSpecResponse.Error = "automatic updates based on semantic versioning are not supported for non-semantic versioning apps"
JSON(w, http.StatusUnprocessableEntity, updateCheckerSpecResponse)
return
}
}
if foundApp.IsAirgap {
updateCheckerSpecResponse.Error = "airgap scheduled update checks are not supported"
logger.Error(errors.New(updateCheckerSpecResponse.Error))
JSON(w, http.StatusBadRequest, updateCheckerSpecResponse)
return
}
// validate cron spec
cronSpec := configureAutomaticUpdatesRequest.UpdateCheckerSpec
if cronSpec != "@never" && cronSpec != "@default" {
_, err := cron.ParseStandard(cronSpec)
if err != nil {
updateCheckerSpecResponse.Error = "failed to parse cron spec"
logger.Error(errors.Wrap(err, updateCheckerSpecResponse.Error))
JSON(w, http.StatusInternalServerError, updateCheckerSpecResponse)
return
}
}
if err := store.GetStore().SetUpdateCheckerSpec(foundApp.ID, cronSpec); err != nil {
updateCheckerSpecResponse.Error = "failed to set update checker spec"
logger.Error(errors.Wrap(err, updateCheckerSpecResponse.Error))
JSON(w, http.StatusInternalServerError, updateCheckerSpecResponse)
return
}
if err := store.GetStore().SetAutoDeploy(foundApp.ID, configureAutomaticUpdatesRequest.AutoDeploy); err != nil {
updateCheckerSpecResponse.Error = "failed to set auto deploy"
logger.Error(errors.Wrap(err, updateCheckerSpecResponse.Error))
JSON(w, http.StatusInternalServerError, updateCheckerSpecResponse)
return
}
// reconfigure update checker for the app
if err := updatechecker.Configure(foundApp, cronSpec); err != nil {
updateCheckerSpecResponse.Error = "failed to reconfigure update checker cron job"
logger.Error(errors.Wrap(err, updateCheckerSpecResponse.Error))
JSON(w, http.StatusInternalServerError, updateCheckerSpecResponse)
return
}
JSON(w, http.StatusNoContent, "")
}
func (h *Handler) GetAutomaticUpdatesConfig(w http.ResponseWriter, r *http.Request) {
getCheckerSpecResponse := &GetAutomaticUpdatesConfigResponse{}
foundApp, err := store.GetStore().GetAppFromSlug(mux.Vars(r)["appSlug"])
if err != nil {
getCheckerSpecResponse.Error = "failed to get app from slug"
logger.Error(errors.Wrap(err, getCheckerSpecResponse.Error))
JSON(w, http.StatusInternalServerError, getCheckerSpecResponse)
return
}
getCheckerSpecResponse.UpdateCheckerSpec = foundApp.UpdateCheckerSpec
getCheckerSpecResponse.AutoDeploy = foundApp.AutoDeploy
JSON(w, http.StatusOK, getCheckerSpecResponse)
}