Repository navigation
Expand file tree
/
Copy pathinstance.go
More file actions
332 lines (293 loc) · 10.7 KB
/
Copy pathinstance.go
File metadata and controls
332 lines (293 loc) · 10.7 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package v1
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/alecthomas/units"
"github.com/brevdev/cloud/internal/collections"
v1 "github.com/brevdev/cloud/v1"
openapi "github.com/brevdev/cloud/v1/providers/lambdalabs/gen/lambdalabs"
)
const lambdaLabsTimeNameFormat = "2006-01-02-15-04-05Z07-00"
// CreateInstance creates a new instance in Lambda Labs
// Supported via: POST /api/v1/instance-operations/launch
func (c *LambdaLabsClient) CreateInstance(ctx context.Context, attrs v1.CreateInstanceAttrs) (*v1.Instance, error) {
keyPairName := attrs.RefID
if attrs.KeyPairName != nil {
keyPairName = *attrs.KeyPairName
}
if attrs.PublicKey != "" {
request := openapi.AddSSHKeyRequest{
Name: keyPairName,
PublicKey: &attrs.PublicKey,
}
keyPairResp, err := c.addSSHKey(ctx, request)
if err != nil && !strings.Contains(err.Error(), "name must be unique") {
return nil, fmt.Errorf("failed to add SSH key: %w", err)
}
keyPairName = keyPairResp.Data.Name
}
if keyPairName == "" {
return nil, errors.New("keyPairName is required if public key not provided")
}
location := attrs.Location
if location == "" {
location = c.location
}
quantity := int32(1)
request := openapi.LaunchInstanceRequest{
RegionName: location,
InstanceTypeName: attrs.InstanceType,
SshKeyNames: []string{keyPairName},
Quantity: &quantity,
FileSystemNames: []string{},
}
name := fmt.Sprintf("%s--%s", c.GetReferenceID(), time.Now().UTC().Format(lambdaLabsTimeNameFormat))
if len(name) > 64 {
return nil, errors.New("name is too long")
}
request.Name = *openapi.NewNullableString(&name)
resp, err := c.launchInstance(ctx, request)
if err != nil {
return nil, fmt.Errorf("failed to launch instance: %w", handleErrToCloudErr(err))
}
if len(resp.Data.InstanceIds) != 1 {
return nil, fmt.Errorf("expected 1 instance ID, got %d", len(resp.Data.InstanceIds))
}
instanceID := v1.CloudProviderInstanceID(resp.Data.InstanceIds[0])
return c.GetInstance(ctx, instanceID)
}
// GetInstance retrieves an instance by ID
// Supported via: GET /api/v1/instances/{id}
func (c *LambdaLabsClient) GetInstance(ctx context.Context, instanceID v1.CloudProviderInstanceID) (*v1.Instance, error) {
resp, err := c.getInstance(ctx, string(instanceID))
if err != nil {
return nil, fmt.Errorf("failed to get instance: %w", err)
}
return convertLambdaLabsInstanceToV1Instance(resp.Data), nil
}
// TerminateInstance terminates an instance
// Supported via: POST /api/v1/instance-operations/terminate
func (c *LambdaLabsClient) TerminateInstance(ctx context.Context, instanceID v1.CloudProviderInstanceID) error {
request := openapi.TerminateInstanceRequest{
InstanceIds: []string{string(instanceID)},
}
// TODO: since "createInstance" also creates an SSH key, we should consider cleaning up the key pair here
_, err := c.terminateInstance(ctx, request)
if err != nil {
return fmt.Errorf("failed to terminate instance: %w", err)
}
return nil
}
// ListInstances lists all instances
// Supported via: GET /api/v1/instances
func (c *LambdaLabsClient) ListInstances(ctx context.Context, _ v1.ListInstancesArgs) ([]v1.Instance, error) {
resp, err := c.listInstances(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list instances: %w", err)
}
instances := make([]v1.Instance, len(resp.Data))
for i, llInstance := range resp.Data {
instances[i] = *convertLambdaLabsInstanceToV1Instance(llInstance)
}
return instances, nil
}
// RebootInstance reboots an instance
// Supported via: POST /api/v1/instance-operations/restart
func (c *LambdaLabsClient) RebootInstance(ctx context.Context, instanceID v1.CloudProviderInstanceID) error {
request := openapi.RestartInstanceRequest{
InstanceIds: []string{string(instanceID)},
}
_, err := c.restartInstance(ctx, request)
if err != nil {
return fmt.Errorf("failed to reboot instance: %w", err)
}
return nil
}
// MergeInstanceForUpdate merges instance data for updates
func convertLambdaLabsInstanceToV1Instance(instance openapi.Instance) *v1.Instance {
var instanceIP string
if instance.Ip.IsSet() {
instanceIP = *instance.Ip.Get()
}
var instanceName string
if instance.Name.IsSet() {
instanceName = *instance.Name.Get()
}
var instanceHostname string
if instance.Hostname.IsSet() {
instanceHostname = *instance.Hostname.Get()
}
nameSplit := strings.Split(instanceName, "--")
var cloudCredRefID string
if len(nameSplit) > 0 {
cloudCredRefID = nameSplit[0]
}
var createTime time.Time
if len(nameSplit) > 1 {
createTimeStr := nameSplit[1]
createTime, _ = time.Parse(lambdaLabsTimeNameFormat, createTimeStr)
}
var instancePrivateIP string
if instance.PrivateIp.IsSet() {
instancePrivateIP = *instance.PrivateIp.Get()
}
inst := v1.Instance{
RefID: instance.SshKeyNames[0],
CloudCredRefID: cloudCredRefID,
CreatedAt: createTime,
CloudID: v1.CloudProviderInstanceID(instance.Id),
Name: instanceName,
PublicIP: instanceIP,
PublicDNS: instanceIP,
PrivateIP: instancePrivateIP,
Hostname: instanceHostname,
Status: v1.Status{
LifecycleStatus: convertLambdaLabsStatusToV1Status(instance.Status),
},
InstanceType: instance.InstanceType.Name,
VolumeType: "ssd",
DiskSize: units.GiB * units.Base2Bytes(instance.InstanceType.Specs.StorageGib),
DiskSizeBytes: v1.NewBytes(v1.BytesValue(instance.InstanceType.Specs.StorageGib), v1.Gibibyte),
FirewallRules: v1.FirewallRules{
IngressRules: []v1.FirewallRule{generateFirewallRouteFromPort(22), generateFirewallRouteFromPort(2222)}, // TODO pull from api
EgressRules: []v1.FirewallRule{generateFirewallRouteFromPort(22), generateFirewallRouteFromPort(2222)}, // TODO pull from api
},
SSHUser: "ubuntu",
SSHPort: 22,
Stoppable: false,
Rebootable: true,
Location: instance.Region.Name,
}
inst.InstanceTypeID = v1.MakeGenericInstanceTypeIDFromInstance(inst)
return &inst
}
func generateFirewallRouteFromPort(port int32) v1.FirewallRule {
return v1.FirewallRule{
FromPort: port,
ToPort: port,
IPRanges: []string{"0.0.0.0/0"},
}
}
func convertLambdaLabsStatusToV1Status(status string) v1.LifecycleStatus {
switch status {
case "booting":
return v1.LifecycleStatusPending
case "active":
return v1.LifecycleStatusRunning
case "terminating":
return v1.LifecycleStatusTerminating
case "terminated":
return v1.LifecycleStatusTerminated
case "error":
return v1.LifecycleStatusFailed
case "unhealthy":
return v1.LifecycleStatusRunning
default:
return v1.LifecycleStatusEmpty
}
}
func (c *LambdaLabsClient) MergeInstanceForUpdate(_ v1.Instance, newInst v1.Instance) v1.Instance {
return newInst
}
func (c *LambdaLabsClient) MergeInstanceTypeForUpdate(_ v1.InstanceType, newIt v1.InstanceType) v1.InstanceType {
return newIt
}
func (c *LambdaLabsClient) addSSHKey(ctx context.Context, request openapi.AddSSHKeyRequest) (*openapi.AddSSHKey200Response, error) {
result, err := collections.RetryWithDataAndAttemptCount(func() (*openapi.AddSSHKey200Response, error) {
res, resp, err := c.client.DefaultAPI.AddSSHKey(c.makeAuthContext(ctx)).AddSSHKeyRequest(request).Execute()
if resp != nil {
defer resp.Body.Close() //nolint:errcheck // ignore because using defer (for some reason HandleErrDefer)
}
if err != nil {
return &openapi.AddSSHKey200Response{}, handleAPIError(ctx, resp, err)
}
return res, nil
}, c.backoff)
if err != nil {
return nil, err
}
return result, nil
}
func (c *LambdaLabsClient) launchInstance(ctx context.Context, request openapi.LaunchInstanceRequest) (*openapi.LaunchInstance200Response, error) {
result, err := collections.RetryWithDataAndAttemptCount(func() (*openapi.LaunchInstance200Response, error) {
res, resp, err := c.client.DefaultAPI.LaunchInstance(c.makeAuthContext(ctx)).LaunchInstanceRequest(request).Execute()
if resp != nil {
defer resp.Body.Close() //nolint:errcheck // ignore because using defer (for some reason HandleErrDefer)
}
if err != nil {
return &openapi.LaunchInstance200Response{}, handleAPIError(ctx, resp, err)
}
return res, nil
}, c.backoff)
if err != nil {
return nil, err
}
return result, nil
}
func (c *LambdaLabsClient) getInstance(ctx context.Context, instanceID string) (*openapi.GetInstance200Response, error) {
result, err := collections.RetryWithDataAndAttemptCount(func() (*openapi.GetInstance200Response, error) {
res, resp, err := c.client.DefaultAPI.GetInstance(c.makeAuthContext(ctx), instanceID).Execute()
if resp != nil {
defer resp.Body.Close() //nolint:errcheck // ignore because using defer (for some reason HandleErrDefer)
}
if err != nil {
return &openapi.GetInstance200Response{}, handleAPIError(ctx, resp, err)
}
return res, nil
}, c.backoff)
if err != nil {
return nil, err
}
return result, nil
}
func (c *LambdaLabsClient) terminateInstance(ctx context.Context, request openapi.TerminateInstanceRequest) (*openapi.TerminateInstance200Response, error) {
result, err := collections.RetryWithDataAndAttemptCount(func() (*openapi.TerminateInstance200Response, error) {
res, resp, err := c.client.DefaultAPI.TerminateInstance(c.makeAuthContext(ctx)).TerminateInstanceRequest(request).Execute()
if resp != nil {
defer resp.Body.Close() //nolint:errcheck // ignore because using defer (for some reason HandleErrDefer)
}
if err != nil {
return &openapi.TerminateInstance200Response{}, handleAPIError(ctx, resp, err)
}
return res, nil
}, c.backoff)
if err != nil {
return nil, err
}
return result, nil
}
func (c *LambdaLabsClient) listInstances(ctx context.Context) (*openapi.ListInstances200Response, error) {
result, err := collections.RetryWithDataAndAttemptCount(func() (*openapi.ListInstances200Response, error) {
res, resp, err := c.client.DefaultAPI.ListInstances(c.makeAuthContext(ctx)).Execute()
if resp != nil {
defer resp.Body.Close() //nolint:errcheck // ignore because using defer (for some reason HandleErrDefer)
}
if err != nil {
return &openapi.ListInstances200Response{}, handleAPIError(ctx, resp, err)
}
return res, nil
}, c.backoff)
if err != nil {
return nil, err
}
return result, nil
}
func (c *LambdaLabsClient) restartInstance(ctx context.Context, request openapi.RestartInstanceRequest) (*openapi.RestartInstance200Response, error) {
result, err := collections.RetryWithDataAndAttemptCount(func() (*openapi.RestartInstance200Response, error) {
res, resp, err := c.client.DefaultAPI.RestartInstance(c.makeAuthContext(ctx)).RestartInstanceRequest(request).Execute()
if resp != nil {
defer resp.Body.Close() //nolint:errcheck // ignore because using defer (for some reason HandleErrDefer)
}
if err != nil {
return &openapi.RestartInstance200Response{}, handleAPIError(ctx, resp, err)
}
return res, nil
}, c.backoff)
if err != nil {
return nil, err
}
return result, nil
}