-
Notifications
You must be signed in to change notification settings - Fork 4
/
supervisorv2.go
127 lines (116 loc) · 3.07 KB
/
supervisorv2.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
package balena
import (
"bytes"
"context"
"fmt"
"net/http"
)
const supervisorv2BasePath = "v2"
// SupervisorService handles communication with the supervisor related methods of the
// Balena API.
type SupervisorV2Service struct {
service
apiKey string
deviceUUID string
appID string
local bool
}
func (s *SupervisorV2Service) RestartServiceByName(ctx context.Context, name string) error {
type request struct {
ServiceName string `json:"serviceName"`
}
req, err := s.newRequest(
ctx,
http.MethodPost,
supervisorv2BasePath+"/applications/"+s.appID+"/restart-service",
&request{ServiceName: name},
)
if err != nil {
return fmt.Errorf("unable to create restart service request: %v", err)
}
buf := &bytes.Buffer{}
err = s.client.Do(req, buf)
if err != nil {
return fmt.Errorf("unable to restart service %s: %v", name, err)
}
return nil
}
func (s *SupervisorV2Service) StopServiceByName(ctx context.Context, name string) error {
type request struct {
ServiceName string `json:"serviceName"`
}
req, err := s.newRequest(
ctx,
http.MethodPost,
supervisorv2BasePath+"/applications/"+s.appID+"/stop-service",
&request{ServiceName: name},
)
if err != nil {
return fmt.Errorf("unable to create stop service request: %v", err)
}
buf := &bytes.Buffer{}
err = s.client.Do(req, buf)
if err != nil {
return fmt.Errorf("unable to stop service %s: %v", name, err)
}
return nil
}
type ApplicationState struct {
Services map[string]ServiceState `json:"services,omitempty"`
}
type ServiceState struct {
Status string `json:"status,omitempty"`
ReleaseID int64 `json:"releaseId,omitempty"`
DownloadProgress interface{} `json:"download_progress,omitempty"`
}
type SvAppStateResp struct {
// Local likely always contains only one single entry where the key is the
// Application ID
Local map[string]ApplicationState `json:"local,omitempty"`
Dependent interface{} `json:"dependent,omitempty"`
Commit string `json:"commit,omitempty"`
}
func (s *SupervisorV2Service) ApplicationState(ctx context.Context) (*SvAppStateResp, error) {
req, err := s.newRequest(
ctx,
http.MethodGet,
supervisorv2BasePath+"/applications/"+s.appID+"/state",
nil,
)
if err != nil {
return nil, fmt.Errorf("unable to create application state request: %v", err)
}
resp := &SvAppStateResp{}
err = s.client.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("unable to fetch application state: %v", err)
}
return resp, nil
}
type CloudRequest struct {
UUID string `json:"uuid,omitempty"`
Method string `json:"method,omitempty"`
Data interface{} `json:"data,omitempty"`
}
func (s *SupervisorV2Service) newRequest(
ctx context.Context,
method string,
urlStr string,
body interface{},
) (*http.Request, error) {
u := urlStr
m := method
q := fmt.Sprintf("apikey=%s", s.apiKey)
b := body
if !s.local {
u = "supervisor/" + u
m = http.MethodPost
q = ""
b = &CloudRequest{
UUID: s.deviceUUID,
Method: method,
Data: body,
}
}
return s.client.NewRequest(ctx, m, u, q, b)
}