-
Notifications
You must be signed in to change notification settings - Fork 12
/
cluster.go
156 lines (135 loc) · 4.27 KB
/
cluster.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
// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package equinixmetal
import (
"crypto/rand"
"fmt"
"os"
"path/filepath"
"github.com/flatcar-linux/mantle/platform"
"github.com/flatcar-linux/mantle/platform/api/equinixmetal"
"github.com/flatcar-linux/mantle/platform/conf"
"github.com/packethost/packngo"
)
type cluster struct {
*platform.BaseCluster
flight *flight
sshKeyID string
}
func (pc *cluster) NewMachine(userdata *conf.UserData) (platform.Machine, error) {
conf, err := pc.RenderUserData(userdata, map[string]string{
"$public_ipv4": "${COREOS_PACKET_IPV4_PUBLIC_0}",
"$private_ipv4": "${COREOS_PACKET_IPV4_PRIVATE_0}",
})
if err != nil {
return nil, err
}
vmname := pc.vmname()
// Stream the console somewhere temporary until we have a machine ID
consolePath := filepath.Join(pc.RuntimeConf().OutputDir, "console-"+vmname+".txt")
var cons *console
var pcons equinixmetal.Console // need a nil interface value if unused
var device *packngo.Device
// Do not shadow assignments to err (i.e., use a, err := something) in the for loop
// because the "continue" case needs to access the previous error to return it when the
// maximal number of retries is reached or to print it at the beginning of the loop.
for retry := 0; retry <= 2; retry++ {
if err != nil {
plog.Warningf("Retrying to provision a machine after error: %q", err)
if pc.sshKeyID != "" {
err = os.Remove(consolePath)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
}
}
if pc.sshKeyID != "" {
// We can only read the console if EquinixMetal has our SSH key
var f *os.File
f, err = os.OpenFile(consolePath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return nil, err
}
cons = &console{
pc: pc,
f: f,
done: make(chan interface{}),
}
pcons = cons
}
var id string
select {
case i := <-pc.flight.devicesPool:
id = i
default:
id = ""
}
// CreateOrUpdateDevice unconditionally closes console when done with it
device, err = pc.flight.api.CreateOrUpdateDevice(vmname, conf, pcons, id)
if err != nil {
continue // provisioning error
}
mach := &machine{
cluster: pc,
device: device,
console: cons,
}
mach.publicIP = pc.flight.api.GetDeviceAddress(device, 4, true)
mach.privateIP = pc.flight.api.GetDeviceAddress(device, 4, false)
if mach.publicIP == "" || mach.privateIP == "" {
pc.flight.api.DeleteDevice(mach.ID())
err = fmt.Errorf("couldn't find IP addresses for device")
continue // provisioning error
}
// Warning: the assumption is that within one test a machine doesn't get reused
// (if a test would create, destroy and create a machine explicitly)
// otherwise the console file names will clash
dir := filepath.Join(pc.RuntimeConf().OutputDir, mach.ID())
if err = os.Mkdir(dir, 0777); err != nil {
pc.flight.api.DeleteDevice(mach.ID())
return nil, err
}
if cons != nil {
if err = os.Rename(consolePath, filepath.Join(dir, "console.txt")); err != nil {
pc.flight.api.DeleteDevice(mach.ID())
return nil, err
}
}
confPath := filepath.Join(dir, "user-data")
if err = conf.WriteFile(confPath); err != nil {
pc.flight.api.DeleteDevice(mach.ID())
return nil, err
}
if mach.journal, err = platform.NewJournal(dir); err != nil {
pc.flight.api.DeleteDevice(mach.ID())
return nil, err
}
if err = platform.StartMachine(mach, mach.journal); err != nil {
pc.flight.api.DeleteDevice(mach.ID())
continue // provisioning error
}
pc.AddMach(mach)
return mach, nil
}
return nil, err
}
func (pc *cluster) vmname() string {
b := make([]byte, 5)
rand.Read(b)
return fmt.Sprintf("%s-%x", pc.Name()[0:13], b)
}
func (pc *cluster) Destroy() {
pc.BaseCluster.Destroy()
pc.flight.DelCluster(pc)
}