This repository has been archived by the owner on Jun 28, 2019. It is now read-only.
/
hosts.go
173 lines (148 loc) · 3.77 KB
/
hosts.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
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"text/tabwriter"
"github.com/cockroachdb/roachprod/cloud"
"github.com/cockroachdb/roachprod/config"
"github.com/cockroachdb/roachprod/install"
"github.com/pkg/errors"
)
const (
sshKeyPathPrefix = "${HOME}/.ssh/roachprod"
)
func initHostDir() error {
hd := os.ExpandEnv(config.DefaultHostDir)
return os.MkdirAll(hd, 0755)
}
func syncHosts(cloud *cloud.Cloud) error {
hd := os.ExpandEnv(config.DefaultHostDir)
// Write all host files. We're the only process doing this due to the file
// lock acquired by syncAll, but other processes may be reading the host
// files concurrently so we need to write the files atomically by writing to
// a temporary file and renaming.
for _, c := range cloud.Clusters {
filename := path.Join(hd, c.Name)
tmpFile := filename + ".tmp"
err := func() error {
file, err := os.Create(tmpFile)
if err != nil {
return errors.Wrapf(err, "problem creating file %s", filename)
}
defer file.Close()
// Align columns left and separate with at least two spaces.
tw := tabwriter.NewWriter(file, 0, 8, 2, ' ', 0)
tw.Write([]byte("# user@host\tlocality\tvpcId\n"))
for _, vm := range c.VMs {
tw.Write([]byte(fmt.Sprintf(
"%s@%s\t%s\t%s\n", vm.RemoteUser, vm.PublicIP, vm.Locality(), vm.VPC)))
}
if err := tw.Flush(); err != nil {
return errors.Wrapf(err, "problem writing file %s", filename)
}
return nil
}()
if err != nil {
return err
}
if err := os.Rename(tmpFile, filename); err != nil {
return err
}
}
return gcHostsFiles(cloud)
}
func gcHostsFiles(cloud *cloud.Cloud) error {
hd := os.ExpandEnv(config.DefaultHostDir)
files, err := ioutil.ReadDir(hd)
if err != nil {
return err
}
for _, file := range files {
if !file.Mode().IsRegular() {
continue
}
if _, ok := cloud.Clusters[file.Name()]; ok {
continue
}
filename := filepath.Join(hd, file.Name())
if err = os.Remove(filename); err != nil {
log.Printf("failed to remove file %s", filename)
}
}
return nil
}
func newInvalidHostsLineErr(line string) error {
return fmt.Errorf("invalid hosts line, expected <username>@<host> [locality] [vpcId], got %q", line)
}
func loadClusters() error {
hd := os.ExpandEnv(config.DefaultHostDir)
files, err := ioutil.ReadDir(hd)
if err != nil {
return err
}
for _, file := range files {
if !file.Mode().IsRegular() {
continue
}
if strings.HasSuffix(file.Name(), ".tmp") {
continue
}
filename := filepath.Join(hd, file.Name())
contents, err := ioutil.ReadFile(filename)
if err != nil {
return errors.Wrapf(err, "could not read %s", filename)
}
lines := strings.Split(string(contents), "\n")
c := &install.SyncedCluster{
Name: file.Name(),
}
for _, l := range lines {
// We'll consume the fields as we go along
fields := strings.Fields(l)
if len(fields) == 0 {
continue
} else if len(fields[0]) > 0 && fields[0][0] == '#' {
// Comment line.
continue
} else if len(fields) > 3 {
return newInvalidHostsLineErr(l)
}
parts := strings.Split(fields[0], "@")
fields = fields[1:]
var n, u string
if len(parts) == 1 {
u = config.OSUser.Username
n = parts[0]
} else if len(parts) == 2 {
u = parts[0]
n = parts[1]
} else {
return newInvalidHostsLineErr(l)
}
var locality string
if len(fields) > 0 {
locality = fields[0]
fields = fields[1:]
}
var vpc string
if len(fields) > 0 {
vpc = fields[0]
fields = fields[1:]
}
if len(fields) > 0 {
return newInvalidHostsLineErr(l)
}
c.VMs = append(c.VMs, n)
c.Users = append(c.Users, u)
c.Localities = append(c.Localities, locality)
c.VPCs = append(c.VPCs, vpc)
}
install.Clusters[file.Name()] = c
}
return nil
}