-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
146 lines (122 loc) · 3.73 KB
/
packet.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"strings"
"github.com/google/uuid"
consul "github.com/hashicorp/consul/api"
vault "github.com/hashicorp/vault/api"
pb "github.com/opencopilot/core/core"
"github.com/opencopilot/core/instance"
packet "github.com/packethost/packngo"
)
// GetPacketProjectFromAuthPayload returns the Packet project of a project level API key
func GetPacketProjectFromAuthPayload(auth string) (string, error) {
packetClient := packet.NewClientWithAuth("", auth, nil)
var project map[string]interface{}
_, err := packetClient.DoRequest("GET", "/project", "", &project)
if err != nil {
return "", err
}
projectID, ok := project["id"]
if !ok {
return "", errors.New("problem verifying project from auth")
}
return projectID.(string), nil
}
// GetPacketInstance gets an instance by ID
func GetPacketInstance(consulClient *consul.Client, instanceID string) (*instance.Instance, error) {
i, err := instance.NewInstance(consulClient, instanceID)
if err != nil {
return nil, err
}
return i, nil
}
// CreatePacketInstance creates the necessary data structures in Consul for a new instance, and provisions a device on Packet
func CreatePacketInstance(consulClient *consul.Client, vaultClient *vault.Client, in *pb.CreateInstanceRequest) (*instance.Instance, error) {
id := uuid.New()
packetClient := packet.NewClientWithAuth("", in.Auth.Payload, nil)
projID, err := GetPacketProjectFromAuthPayload(in.Auth.Payload)
if err != nil {
return nil, err
}
instance, err := instance.CreateInstance(consulClient, vaultClient, instance.CreateInstanceRequest{
ID: id.String(),
Owner: projID,
Device: "", // can't set this yet because we don't know what the device ID is until it's provisioned
Provider: "PACKET",
})
if err != nil {
return nil, err
}
token, err := instance.GenerateConsulToken(consulClient)
if err != nil {
return nil, err
}
logical := vaultClient.Logical()
_, err = logical.Write("secret/bootstrap/"+id.String(), map[string]interface{}{
"consul_token": token,
})
if err != nil {
return nil, err
}
customData := map[string]interface{}{
"COPILOT": map[string]interface{}{
"INSTANCE_ID": id.String(),
"CORE_ADDR": PublicAddress,
"PACKET_AUTH": in.Auth.Payload,
},
}
customDataJSON, err := json.Marshal(customData)
if err != nil {
return nil, err
}
userDataString, err := ioutil.ReadFile("./assets/packet.userdata.sh")
if err != nil {
return nil, err
}
createReq := packet.DeviceCreateRequest{
Hostname: "opencopilot-" + strings.Split(id.String(), "-")[0],
ProjectID: projID,
Facility: in.Region,
Plan: in.Type,
OS: "ubuntu_16_04",
BillingCycle: "hourly",
CustomData: string(customDataJSON),
UserData: string(userDataString),
}
device, _, err := packetClient.Devices.Create(&createReq)
if err != nil {
instance.DestroyInstance(consulClient, vaultClient)
return nil, err
}
_, err = instance.SetInstanceFields(consulClient, map[string]string{
"device": device.ID,
})
if err != nil {
return nil, err
}
return instance, nil
}
// DestroyPacketInstance destroys a packet instance
func DestroyPacketInstance(consulClient *consul.Client, vaultClient *vault.Client, in *pb.DestroyInstanceRequest) error {
packetClient := packet.NewClientWithAuth("", in.Auth.Payload, nil)
instance, err := instance.NewInstance(consulClient, in.InstanceId)
if err != nil {
return err
}
device, _, err := packetClient.Devices.Get(instance.Device)
if err != nil {
return err
}
if device.State != "active" {
return errors.New("Device is still provisioning")
}
err = instance.DestroyInstance(consulClient, vaultClient)
if err != nil {
return err
}
packetClient.Devices.Delete(instance.Device)
return nil
}