-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
222 lines (178 loc) · 5.3 KB
/
api.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
package updatehub
import (
"encoding/json"
"fmt"
"net/url"
"github.com/parnurzeal/gorequest"
)
type ProbeResponse interface{}
const (
Updating = "updating"
NoUpdate = "no_update"
TryAgain = "try_again"
)
type APIState string
const (
Park = "park"
EntryPoint = "entry_point"
Poll = "poll"
Validation = "validation"
Download = "download"
Install = "install"
Reboot = "reboot"
DirectDownload = "direct_download"
PrepareLocalInstall = "prepare_local_install"
Error = "error"
)
type Client struct {
}
type Metadata struct {
Metadata string `json:"metadata"`
}
type Network struct {
ServerAddress string `json:"server_address"`
ListenSocket string `json:"listen_socket"`
}
type Polling struct {
Interval string `json:"interval"`
Enabled bool `json:"enabled"`
}
type Storage struct {
ReadOnly bool `json:"read_only"`
RuntimeSettings string `json:"runtime_settings"`
}
type Update struct {
DownloadDir string `json:"download_dir"`
SupportedInstallModes []string `json:"supported_install_modes"`
}
type ServerAddress struct {
Custom string `json:"custom"`
}
type DeviceAttributes struct {
Attr1 string `json:"attr1"`
Attr2 string `json:"attr2"`
}
type DeviceIdentity struct {
ID1 string `json:"id1"`
ID2 string `json:"id2"`
}
type Firmware struct {
DeviceAttributes DeviceAttributes `json:"device_attributes"`
DeviceIdentity DeviceIdentity `json:"device_identity"`
Hardware string `json:"hardware"`
PubKey string `json:"pub_key"`
Version string `json:"version"`
}
type UpdatePackage struct {
AppliedPackageUid string `json:"applied_package_uid"`
UpdgradeToInstallation string `json:"upgrade_to_installation"`
}
type Settings struct {
Firmware Metadata `json:"firmware"`
Network Network `json:"network"`
Polling Polling `json:"polling"`
Storage Storage `json:"storage"`
Update Update `json:"update"`
}
type RuntimeSettings struct {
Path string `json:"path"`
Persistent bool `json:"persistent"`
Polling PollingLog `json:"polling"`
Update UpdatePackage `json:"update"`
}
type PollingLog struct {
Last string `json:"last"`
Now bool `json:"now"`
Retries int64 `json:"retries"`
ServerAddress ServerAddress `json:"server_address"`
}
type AgentInfo struct {
Config Settings `json:"config"`
Firmware Firmware `json:"firmware"`
RuntimeSettings RuntimeSettings `json:"runtime_settings"`
State APIState `json:"state"`
Version string `json:"version"`
}
type Entry struct {
Data interface{} `json:"data"`
Level string `json:"level"`
Message string `json:"message"`
Time string `json:"time"`
}
type Log struct {
Entries []Entry `json:"entries"`
}
// NewClient instantiates a new updatehub agent client
func NewClient() *Client {
return &Client{}
}
// Probe server address for update
func (c *Client) Probe(serverAddress string) (*ProbeResponse, error) {
var probe ProbeResponse
var req struct {
ServerAddress string `json:"custom_server"`
}
req.ServerAddress = serverAddress
response, err := processRequest(string("/probe"), &probe, req, "POST")
return response.(*ProbeResponse), err
}
// GetInfo get updatehub agent general information
func (c *Client) GetInfo() (*AgentInfo, error) {
response, err := processRequest(string("/info"), &AgentInfo{}, nil, "GET")
return response.(*AgentInfo), err
}
// GetLogs get updatehub agent log entries
func (c *Client) GetLogs() (*Log, error) {
response, err := processRequest(string("/log"), &Log{}, nil, "GET")
return response.(*Log), err
}
// RemoteInstall trigger the installation of a package from a direct URL
func (c *Client) RemoteInstall(serverAddress string) (*APIState, error) {
var state APIState
var req struct {
URL string `json:"url"`
}
req.URL = serverAddress
response, err := processRequest(string("/remote_install"), &state, req, "POST")
return response.(*APIState), err
}
// LocalInstall trigger the installation of a local package
func (c *Client) LocalInstall(filePath string) (*APIState, error) {
var state APIState
var req struct {
FilePath string `json:"file"`
}
req.FilePath = filePath
response, err := processRequest(string("/local_install"), &state, req, "POST")
return response.(*APIState), err
}
func (c *Client) AbortDownload() (*APIState, error) {
var state APIState
response, err := processRequest(string("/update/download/abort"), &state, nil, "POST")
return response.(*APIState), err
}
func processRequest(url string, responseStruct interface{}, req interface{}, method string) (interface{}, error) {
var body []byte
var errs []error
switch method {
case "GET":
_, body, errs = gorequest.New().Get(buildURL(url)).EndStruct(&responseStruct)
case "POST":
_, body, errs = gorequest.New().Post(buildURL(url)).Send(req).EndStruct(&responseStruct)
}
if len(errs) > 0 {
return nil, errs[0]
}
err := json.Unmarshal([]byte(body), &responseStruct)
if err != nil {
return nil, err
}
return responseStruct, nil
}
func buildURL(path string) string {
u, err := url.Parse("localhost:8080")
if err != nil {
panic(err)
}
return fmt.Sprintf("http://%s%s", u, path)
}