This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_handlers.go
122 lines (105 loc) · 3.38 KB
/
api_handlers.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
package main
import (
"github.com/Sirupsen/logrus"
"github.com/coreos/go-omaha/omaha"
"strconv"
)
const (
eventTypeDownload = 13
eventTypeArrive = 14
eventTypeApply = 3
eventTypeSuccess = 800
eventResultError = 0
eventResultOK = 1
eventResultDone = 2
)
// parse an 'app' tag of request and generate a corresponding 'app' tag of response
func handleApiApp(logContext *logrus.Entry, localUrl string, appRequest, appResponse *omaha.App) {
logContext = logContext.WithFields(logrus.Fields{
"machineId": appRequest.MachineID,
})
if appRequest.Id != coreOSAppID {
appResponse.Status = "error-unknownApplication"
} else {
appResponse.Status = "ok"
}
// <UpdateCheck> tag
if appRequest.UpdateCheck != nil {
logContext.Debug("Handling UpdateCheck")
ucResp := appResponse.AddUpdateCheck()
appVersion, err := parseVersionString(appRequest.Version)
if err != nil {
logContext.Errorf("Could not parse client's version string: %v", err.Error())
ucResp.Status = "error-invalidVersionString"
} else {
handleApiUpdateCheck(logContext, localUrl, appVersion, appRequest.Track, appRequest.UpdateCheck, ucResp)
}
}
// <ping> tag
if appRequest.Ping != nil {
// TODO register info from the ping
// response is always "ok" according to the specs
responsePing := appResponse.AddPing()
responsePing.Status = "ok"
}
// <Event> tag
handleApiEvents(logContext, appRequest.MachineID, appRequest.Events)
}
func handleApiEvents(logContext *logrus.Entry, client string, events []*omaha.Event) error {
for _, event := range events {
evType, err := strconv.Atoi(event.Type)
if err != nil {
return err
}
evResult, err := strconv.Atoi(event.Result)
if err != nil {
return err
}
err = db.LogEvent(client, evType, evResult)
if err != nil {
logContext.Error(err)
}
switch evType {
case eventTypeDownload:
logContext.Info("Client is downloading new version.")
case eventTypeArrive:
logContext.Info("Client finished download.")
case eventTypeApply:
switch evResult {
case eventResultOK:
logContext.Info("Client applied package.")
case eventResultError:
logContext.Info("Client errored during update.")
case eventResultDone:
logContext.Info("Client upgraded to current version.")
}
case eventTypeSuccess:
logContext.Info("Install success. Update completion prevented by instance.")
default:
logContext.Warn("Unknown event type %v.", evType)
}
}
return nil
}
// parse an 'UpdateCheck' tag of request and generate a corresponding 'UpdateCheck' tag of response
func handleApiUpdateCheck(logContext *logrus.Entry, localUrl string, appVersion payloadVersion, channel string, ucRequest, ucResp *omaha.UpdateCheck) {
payload, err := db.GetNewerPayload(appVersion, channel)
if err != nil {
logContext.Errorf("Failed checking for newer payload: %v", err.Error())
ucResp.Status = "error-internal"
} else {
if payload == nil {
logContext.Infof("Client already up-to-date")
ucResp.Status = "noupdate"
return
}
logContext.Infof("Found update to version '%v' (id %v)", payload.Version, payload.ID)
ucResp.Status = "ok"
ucResp.AddUrl(fileBE.GetUpdateURL(localUrl))
manifest := ucResp.AddManifest("1.0.2")
manifest.AddPackage(payload.SHA1, payload.ID, strconv.FormatInt(payload.Size, 10), true)
action := manifest.AddAction("postinstall")
action.Sha256 = payload.SHA256
action.DisablePayloadBackoff = true
}
}