-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
machine.go
244 lines (197 loc) · 6.79 KB
/
machine.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package keygen
import (
"context"
"time"
"github.com/keygen-sh/jsonapi-go"
)
type HeartbeatStatusCode string
const (
HeartbeatStatusCodeNotStarted HeartbeatStatusCode = "NOT_STARTED"
HeartbeatStatusCodeAlive HeartbeatStatusCode = "ALIVE"
HeartbeatStatusCodeDead HeartbeatStatusCode = "DEAD"
HeartbeatStatusCodeResurrected HeartbeatStatusCode = "RESURRECTED"
)
type machine struct {
ID string `json:"-"`
Type string `json:"-"`
Fingerprint string `json:"fingerprint"`
Hostname string `json:"hostname"`
Platform string `json:"platform"`
Cores int `json:"cores"`
LicenseID string `json:"-"`
Components Components `json:"-"`
}
// GetID implements the jsonapi.MarshalResourceIdentifier interface.
func (m machine) GetID() string {
return m.ID
}
// GetType implements the jsonapi.MarshalResourceIdentifier interface.
func (m machine) GetType() string {
return "machines"
}
// GetData implements the jsonapi.MarshalData interface.
func (m machine) GetData() interface{} {
return m
}
// GetRelationships implements jsonapi.MarshalRelationships interface.
func (m machine) GetRelationships() map[string]interface{} {
relationships := make(map[string]interface{})
if len(m.Components) > 0 {
relationships["components"] = m.Components
}
relationships["license"] = jsonapi.ResourceObjectIdentifier{
Type: "licenses",
ID: m.LicenseID,
}
return relationships
}
// Machine represents a Keygen machine object.
type Machine struct {
ID string `json:"-"`
Type string `json:"-"`
Name string `json:"name"`
Fingerprint string `json:"fingerprint"`
Hostname string `json:"hostname"`
Platform string `json:"platform"`
IP string `json:"ip"`
Cores int `json:"cores"`
RequireHeartbeat bool `json:"requireHeartbeat"`
HeartbeatStatus HeartbeatStatusCode `json:"heartbeatStatus"`
HeartbeatDuration int `json:"heartbeatDuration"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Metadata map[string]interface{} `json:"metadata"`
LicenseID string `json:"-"`
components []Component `json:"-"`
}
// GetID implements the jsonapi.MarshalResourceIdentifier interface.
func (m Machine) GetID() string {
return m.ID
}
// GetType implements the jsonapi.MarshalResourceIdentifier interface.
func (m Machine) GetType() string {
return "machines"
}
// GetData implements the jsonapi.MarshalData interface.
func (m Machine) GetData() interface{} {
// Transform public machine to private machine to only send a subset of attrs
return machine{
Fingerprint: m.Fingerprint,
Hostname: m.Hostname,
Platform: m.Platform,
Cores: m.Cores,
LicenseID: m.LicenseID,
Components: m.components,
}
}
// SetID implements the jsonapi.UnmarshalResourceIdentifier interface.
func (m *Machine) SetID(id string) error {
m.ID = id
return nil
}
// SetType implements the jsonapi.UnmarshalResourceIdentifier interface.
func (m *Machine) SetType(t string) error {
m.Type = t
return nil
}
// SetData implements the jsonapi.UnmarshalData interface.
func (m *Machine) SetData(to func(target interface{}) error) error {
return to(m)
}
// Machines represents an array of machine objects.
type Machines []Machine
// SetData implements the jsonapi.UnmarshalData interface.
func (m *Machines) SetData(to func(target interface{}) error) error {
return to(m)
}
// Deactivate performs a machine deactivation for the current Machine. An error
// will be returned if the machine deactivation fails.
func (m *Machine) Deactivate(ctx context.Context) error {
client := NewClient()
if _, err := client.Delete(ctx, "machines/"+m.ID, nil, nil); err != nil {
return err
}
return nil
}
// Monitor performs, on a loop, a machine hearbeat ping for the current Machine. An
// error channel will be returned, where any ping errors will be emitted. Pings are
// sent according to the machine's required heartbeat window, minus 30 seconds to
// account for any network lag. Panics if a heartbeat ping fails after first ping.
func (m *Machine) Monitor(ctx context.Context) error {
if err := m.ping(ctx); err != nil {
return err
}
go func() {
t := (time.Duration(m.HeartbeatDuration) * time.Second) - (30 * time.Second)
for range time.Tick(t) {
if err := m.ping(ctx); err != nil {
panic(err)
}
}
}()
return nil
}
// Checkout generates an encrypted machine file. Returns a MachineFile.
func (m *Machine) Checkout(ctx context.Context, options ...CheckoutOption) (*MachineFile, error) {
client := NewClient()
license := &License{}
lic := &MachineFile{}
opts := CheckoutOptions{Encrypt: true, Include: "license,license.entitlements"}
for _, opt := range options {
if err := opt(&opts); err != nil {
return nil, err
}
}
if _, err := client.Get(ctx, "me", nil, license); err != nil {
return nil, err
}
if _, err := client.Post(ctx, "machines/"+m.ID+"/actions/check-out", opts, lic); err != nil {
return nil, err
}
return lic, nil
}
// Components lists up to 100 components for the machine.
func (m *Machine) Components(ctx context.Context) (Components, error) {
client := NewClient()
components := Components{}
if _, err := client.Get(ctx, "machines/"+m.ID+"/components", querystring{Limit: 100}, &components); err != nil {
return nil, err
}
return components, nil
}
// Spawn creates a new process for a machine, identified by the provided pid. If
// successful, the new Process will be returned. When unsuccessful, as error
// will be returned, e.g. ErrProcessLimitExceeded. Automatically starts a loop
// that sends heartbeat pings according to the process's Interval. Panics if a
// heartbeat ping fails after first ping.
func (m *Machine) Spawn(ctx context.Context, pid string) (*Process, error) {
client := NewClient()
params := &Process{
Pid: pid,
MachineID: m.ID,
}
process := &Process{}
if _, err := client.Post(ctx, "processes", params, process); err != nil {
return nil, err
}
if err := process.monitor(ctx); err != nil {
return process, err
}
return process, nil
}
// Processes lists up to 100 processes for the machine.
func (m *Machine) Processes(ctx context.Context) (Processes, error) {
client := NewClient()
processes := Processes{}
if _, err := client.Get(ctx, "machines/"+m.ID+"/processes", querystring{Limit: 100}, &processes); err != nil {
return nil, err
}
return processes, nil
}
func (m *Machine) ping(ctx context.Context) error {
client := NewClient()
if _, err := client.Post(ctx, "machines/"+m.ID+"/actions/ping", nil, m); err != nil {
return err
}
return nil
}