This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
/
amazonec2.go
162 lines (149 loc) · 3.66 KB
/
amazonec2.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
package amazonec2
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/docker/machine/drivers/amazonec2"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/drivers/rpc"
"github.com/gofn/gofn/iaas"
"github.com/gofrs/uuid"
)
// Provider definition, represents a concrete implementation of an iaas
type Provider struct {
iaas.Provider
}
type driverConfig struct {
DriverName string `json:"DriverName"`
Driver struct {
InstanceID string `json:"InstanceID"`
MachineName string `json:"MachineName"`
IPAddress string `json:"IPAddress"`
AMI string `json:"AMI"`
SSHKeyID int `json:"SSHKeyID"`
} `json:"Driver"`
}
func getConfig(machineDir, hostName string) (config *driverConfig, err error) {
configPath := fmt.Sprintf("%s/%s/config.json", machineDir, hostName)
raw, err := ioutil.ReadFile(configPath)
if err != nil {
return
}
err = json.Unmarshal(raw, &config)
if err != nil {
return
}
return
}
func setFlags(driver *amazonec2.Driver) (err error) {
flags := driver.GetCreateFlags()
driverOpts := rpcdriver.RPCFlags{
Values: make(map[string]interface{}),
}
// conversion of flags extracted from:
// https://github.com/docker/machine/blob/master/commands/create.go#L354-L361
for _, f := range flags {
driverOpts.Values[f.String()] = f.Default()
// Hardcoded logic for boolean... :(
if f.Default() == nil {
driverOpts.Values[f.String()] = false
}
}
// TODO: receive this configs to remove hard coded values
driverOpts.Values["swarm-host"] = "tcp://0.0.0.0:3376"
driverOpts.Values["swarm-image"] = "swarm:latest"
driverOpts.Values["swarm-strategy"] = "spread"
driverOpts.Values["swarm-discovery"] = ""
driverOpts.Values["swarm-master"] = false
err = driver.SetConfigFromFlags(&driverOpts)
if err != nil {
return
}
driver.SetSwarmConfigFromFlags(&driverOpts)
return
}
func New(accessKey, secretKey string, opts ...iaas.ProviderOpts) (p *Provider, err error) {
p = &Provider{}
for _, opt := range opts {
if err = opt(&p.Provider); err != nil {
p = nil
return
}
}
var uid uuid.UUID
uid, err = uuid.NewV4()
if err != nil {
p = nil
return
}
name := fmt.Sprintf("gofn-%s", uid.String())
if p.Name == "" {
p.Name = name
}
clientPath := "/tmp/" + p.Name
if p.ClientPath == "" {
p.ClientPath = clientPath
}
p.Client = libmachine.NewClient(p.ClientPath, p.ClientPath+"/certs")
driver := amazonec2.NewDriver(p.Name, p.ClientPath)
driver.AccessKey = accessKey
driver.SecretKey = secretKey
if p.ImageSlug != "" {
driver.AMI = p.ImageSlug
}
if p.Region != "" {
driver.Region = p.Region
}
if p.Size != "" {
driver.InstanceType = p.Size
}
if p.KeyID != 0 {
driver.SSHKeyID = p.KeyID
}
if p.DiskSize != 0 {
driver.RootSize = int64(p.DiskSize)
}
err = setFlags(driver)
if err != nil {
p = nil
return
}
data, err := json.Marshal(driver)
if err != nil {
p = nil
return
}
p.Host, err = p.Client.NewHost(driver.DriverName(), data)
if err != nil {
p = nil
return
}
return
}
// CreateMachine on digitalocean
func (p *Provider) CreateMachine() (machine *iaas.Machine, err error) {
err = p.Client.Create(p.Host)
if err != nil {
return
}
config, err := getConfig(p.Client.GetMachinesDir(), p.Name)
if err != nil {
return
}
machine = &iaas.Machine{
ID: config.Driver.InstanceID,
IP: config.Driver.IPAddress,
Image: config.Driver.AMI,
Kind: config.DriverName,
Name: p.Name,
SSHKeysID: []int{config.Driver.SSHKeyID},
CertsDir: p.ClientPath + "/certs",
}
return
}
// DeleteMachine Shutdown and Delete a droplet
func (p *Provider) DeleteMachine() (err error) {
err = p.Host.Driver.Remove()
defer p.Client.Close()
return
}