|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + v1 "github.com/brevdev/cloud/v1" |
| 11 | + sfcnodes "github.com/sfcompute/nodes-go" |
| 12 | + "github.com/sfcompute/nodes-go/packages/param" |
| 13 | +) |
| 14 | + |
| 15 | +// define function to convert string to b64 |
| 16 | +func toBase64(s string) string { |
| 17 | + return base64.StdEncoding.EncodeToString([]byte(s)) |
| 18 | +} |
| 19 | + |
| 20 | +// define function to add ssh key to cloud init |
| 21 | +func sshKeyCloudInit(sshKey string) string { |
| 22 | + return toBase64(fmt.Sprintf("#cloud-config\nssh_authorized_keys:\n - %s", sshKey)) |
| 23 | +} |
| 24 | + |
| 25 | +func mapSFCStatus(s string) v1.LifecycleStatus { |
| 26 | + switch strings.ToLower(s) { |
| 27 | + case "pending", "nodefailure", "unspecified", "awaitingcapacity", "unknown", "failed": |
| 28 | + return v1.LifecycleStatusPending |
| 29 | + case "running": |
| 30 | + return v1.LifecycleStatusRunning |
| 31 | + // case "stopping": |
| 32 | + //return v1.LifecycleStatusStopping |
| 33 | + case "stopped": |
| 34 | + return v1.LifecycleStatusStopped |
| 35 | + case "terminating", "released": |
| 36 | + return v1.LifecycleStatusTerminating |
| 37 | + case "destroyed", "deleted": |
| 38 | + return v1.LifecycleStatusTerminated |
| 39 | + default: |
| 40 | + return v1.LifecycleStatusPending |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (c *SFCClient) CreateInstance(ctx context.Context, attrs v1.CreateInstanceAttrs) (*v1.Instance, error) { |
| 45 | + resp, err := c.client.Nodes.New(ctx, sfcnodes.NodeNewParams{ |
| 46 | + CreateNodesRequest: sfcnodes.CreateNodesRequestParam{ |
| 47 | + DesiredCount: 1, |
| 48 | + MaxPricePerNodeHour: 1000, |
| 49 | + Zone: attrs.Location, |
| 50 | + ImageID: param.Opt[string]{Value: attrs.ImageID}, //this needs to point to a valid image |
| 51 | + CloudInitUserData: param.Opt[string]{Value: sshKeyCloudInit(attrs.PublicKey)}, // encode ssh key to b64-wrapped cloud-init script |
| 52 | + }, |
| 53 | + }) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + if len(resp.Data) == 0 { |
| 59 | + return nil, fmt.Errorf("no nodes returned") |
| 60 | + } |
| 61 | + node := resp.Data[0] |
| 62 | + |
| 63 | + inst := &v1.Instance{ |
| 64 | + Name: attrs.Name, |
| 65 | + RefID: attrs.RefID, |
| 66 | + CloudCredRefID: c.refID, |
| 67 | + CloudID: v1.CloudProviderInstanceID(node.ID), // SFC ID |
| 68 | + ImageID: attrs.ImageID, |
| 69 | + InstanceType: attrs.InstanceType, |
| 70 | + Location: attrs.Location, |
| 71 | + CreatedAt: time.Now(), |
| 72 | + Status: v1.Status{LifecycleStatus: mapSFCStatus(fmt.Sprint(node.Status))}, // map SDK status to our lifecycle |
| 73 | + InstanceTypeID: v1.InstanceTypeID(node.GPUType), |
| 74 | + SSHPort: 2222, // we use 2222/tcp for all of our SSH ports |
| 75 | + } |
| 76 | + |
| 77 | + return inst, nil |
| 78 | +} |
| 79 | + |
| 80 | +func (c *SFCClient) GetInstance(ctx context.Context, id v1.CloudProviderInstanceID) (*v1.Instance, error) { |
| 81 | + node, err := c.client.Nodes.Get(ctx, string(id)) |
| 82 | + if err != nil { |
| 83 | + panic(err.Error()) |
| 84 | + } |
| 85 | + var vmID string |
| 86 | + if len(node.VMs.Data) > 0 { |
| 87 | + vmID = node.VMs.Data[0].ID |
| 88 | + fmt.Println(vmID) |
| 89 | + } |
| 90 | + |
| 91 | + ssh, err := c.client.VMs.SSH(ctx, sfcnodes.VMSSHParams{VMID: vmID}) |
| 92 | + if err != nil { |
| 93 | + panic(err.Error()) |
| 94 | + } |
| 95 | + |
| 96 | + inst := &v1.Instance{ |
| 97 | + Name: node.Name, |
| 98 | + RefID: c.refID, |
| 99 | + CloudCredRefID: c.refID, |
| 100 | + CloudID: v1.CloudProviderInstanceID(node.ID), // SFC ID |
| 101 | + PublicIP: ssh.SSHHostname, |
| 102 | + CreatedAt: time.Unix(node.CreatedAt, 0), |
| 103 | + Status: v1.Status{LifecycleStatus: mapSFCStatus(fmt.Sprint(node.Status))}, // map SDK status to our lifecycle |
| 104 | + InstanceTypeID: v1.InstanceTypeID(node.GPUType), |
| 105 | + } |
| 106 | + return inst, nil |
| 107 | +} |
| 108 | + |
| 109 | +func (c *SFCClient) ListInstances(ctx context.Context, args v1.ListInstancesArgs) ([]v1.Instance, error) { |
| 110 | + resp, err := c.client.Nodes.List(ctx, sfcnodes.NodeListParams{}) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + var instances []v1.Instance |
| 116 | + for _, node := range resp.Data { |
| 117 | + inst, err := c.GetInstance(ctx, v1.CloudProviderInstanceID(node.ID)) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + if inst != nil { |
| 122 | + instances = append(instances, *inst) |
| 123 | + } |
| 124 | + } |
| 125 | + return instances, nil |
| 126 | +} |
| 127 | + |
| 128 | +func (c *SFCClient) TerminateInstance(ctx context.Context, id v1.CloudProviderInstanceID) error { |
| 129 | + // release the node first |
| 130 | + _, errRelease := c.client.Nodes.Release(ctx, string(id)) |
| 131 | + if errRelease != nil { |
| 132 | + panic(errRelease.Error()) |
| 133 | + } |
| 134 | + // then delete the node |
| 135 | + errDelete := c.client.Nodes.Delete(ctx, string(id)) |
| 136 | + if errDelete != nil { |
| 137 | + panic(errDelete.Error()) |
| 138 | + } |
| 139 | + return nil |
| 140 | +} |
| 141 | + |
| 142 | +// Optional if supported: |
| 143 | +func (c *SFCClient) RebootInstance(ctx context.Context, id v1.CloudProviderInstanceID) error { |
| 144 | + return fmt.Errorf("not implemented") |
| 145 | +} |
| 146 | +func (c *SFCClient) StopInstance(ctx context.Context, id v1.CloudProviderInstanceID) error { |
| 147 | + return fmt.Errorf("not implemented") |
| 148 | +} |
| 149 | +func (c *SFCClient) StartInstance(ctx context.Context, id v1.CloudProviderInstanceID) error { |
| 150 | + return fmt.Errorf("not implemented") |
| 151 | +} |
| 152 | + |
| 153 | +// Merge strategies (pass-through is acceptable baseline). |
| 154 | +func (c *SFCClient) MergeInstanceForUpdate(_ v1.Instance, newInst v1.Instance) v1.Instance { |
| 155 | + return newInst |
| 156 | +} |
| 157 | +func (c *SFCClient) MergeInstanceTypeForUpdate(_ v1.InstanceType, newIt v1.InstanceType) v1.InstanceType { |
| 158 | + return newIt |
| 159 | +} |
0 commit comments