-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanage_hostnames.go
195 lines (170 loc) · 4.16 KB
/
manage_hostnames.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"sort"
"strings"
"sync"
"gopkg.in/yaml.v2"
)
func (mach OpenNebulaMachine) ReadHostname(env Env) string {
hostname := mach.Hostname
if hostname != "" {
return hostname
}
hostname = mach.GetRepresentative(env)
if hostname != "" {
return hostname
}
return ""
}
func (mach OpenNebulaMachine) GetHostname(env Env) string {
hostname := mach.ReadHostname(env)
if hostname != "" {
return hostname
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("Representative Hostname not found for VM\nIf no specific hostname is needed for top.sls hit Enter\nElse type in specific hostname here: ")
hostname, _ = reader.ReadString('\n')
return strings.TrimSuffix(hostname, "\n")
}
func (mach OpenNebulaMachine) GetRepresentative(env Env) string {
index := GetRolesIndex(mach.Roles)
if hostname, ok := env.hostnameMapping[index]; ok {
return hostname
}
return ""
}
func (mach OpenNebulaMachine) CommentEtcHosts() error {
dest := fmt.Sprintf("root@%s", mach.IP)
cmd := exec.Command("ssh", "-oBatchMode=yes", dest, "echo \"# Opennebula Hosts\" >> /etc/hosts")
err := cmd.Run()
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func (mach OpenNebulaMachine) GetEtcHosts(env Env, wg *sync.WaitGroup) error {
defer wg.Done()
dest := fmt.Sprintf("root@%s", mach.IP)
cmd := exec.Command("ssh", "-oBatchMode=yes", dest, "cat /etc/hosts")
output, err := cmd.Output()
if err != nil {
fmt.Println(err)
return err
}
hosts_file := string(output)
src := fmt.Sprintf("%s/%s.hosts", env.config.ConfigPath, mach.Name)
f, err := os.Create(src)
defer f.Close()
if err != nil {
fmt.Println("Error created new host file")
panic(err)
}
hosts_lines := strings.Split(hosts_file, "\n")
for _, line := range hosts_lines {
f.WriteString(line + "\n")
if line == "# Opennebula Hosts" {
break
}
}
for _, machine := range env.machs {
if machine.Status && machine.Name != mach.Name {
line := fmt.Sprintf("%s %s\n", machine.IP, machine.Hostname)
f.WriteString(line)
}
}
scp_dest := fmt.Sprintf("root@%s:/etc/hosts", mach.IP)
cmd2 := exec.Command("scp", src, scp_dest)
err2 := cmd2.Run()
if err2 != nil {
fmt.Println("Error in scp of host file")
panic(err2)
}
os.Remove(src)
return nil
}
func UpdateHostFiles(env Env) error {
var wg sync.WaitGroup
for _, mach := range env.machs {
if mach.Status {
wg.Add(1)
go mach.GetEtcHosts(env, &wg)
}
}
wg.Wait()
return nil
}
type String struct {
Values []string
}
type Roles struct {
Roles []string `yaml:"roles"`
}
func ReadHostnameData(data []byte) map[string]Roles {
roles := make(map[string]Roles)
err := yaml.Unmarshal(data, &roles)
if err != nil {
fmt.Printf("Error unmarshalling machines.yml yaml in %s\n", string(data))
panic(err)
}
return roles
}
func GetRolesIndex(roles []string) string {
sort.Strings(roles)
index := strings.Join(roles, ",")
return index
}
func GetHostnameFile(config *Config) {
path := config.ConfigPath + "/hostnameMapping.yml"
if _, err := os.Stat(path); err == nil {
return
}
fileUrl := "http://global.mt.lldns.net/llnwsae/llnwsae-tester/tests/hostnameMapping.yml"
DownloadFile(path, fileUrl)
}
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
func ReadHostnameFile(config *Config) []byte {
GetHostnameFile(config)
path := config.ConfigPath + "/hostnameMapping.yml"
data, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("Error unmarshalling hostnameMapping.yml yaml in %s\n", string(data))
panic(err)
}
return data
}
func GetHostnameMapping(config *Config) map[string]string {
data := ReadHostnameFile(config)
return GetHostnameMap(data)
}
func GetHostnameMap(data []byte) map[string]string {
RolesToHostnames := make(map[string]string)
roles := ReadHostnameData(data)
for name, role := range roles {
RolesToHostnames[GetRolesIndex(role.Roles)] = name
}
return RolesToHostnames
}