forked from eciancio/OpenNebulaUp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_machines.go
215 lines (193 loc) · 5.13 KB
/
read_machines.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"gopkg.in/yaml.v2"
)
type ioData interface {
ReadDir(dirname string) ([]string, error)
WriteString(line string) error
SetDataLocation(location string) error
Create() error
}
type fileIO struct {
file string
}
func (data fileIO) Create() error {
_, err := os.Create(data.file)
if err != nil {
fmt.Printf("Can't create file\n")
panic(err)
}
return err
}
func (data fileIO) WriteString(line string) error {
f, err := os.OpenFile(data.file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
if err != nil {
fmt.Printf("Couldn't Open File: %s for writing\n", data.file)
}
_, err = f.WriteString(line)
if err != nil {
fmt.Println(err)
}
return err
}
func (data *fileIO) SetDataLocation(location string) error {
data.file = location
return nil
}
func (data fileIO) ReadDir(dirname string) ([]string, error) {
files, err := ioutil.ReadDir(dirname)
var fileNames []string
if err != nil {
return fileNames, err
}
for _, file := range files {
fileNames = append(fileNames, file.Name())
}
return fileNames, nil
}
func GetReposMachines(repo string) map[string]Machine {
data := GetMachinesYaml(repo)
return ReadYamlData(data)
}
func GetReposTags(repo string) map[string]Tags {
data := GetCsMock(repo)
return ReadCsMockData(data)
}
func GetMachinesYaml(repo string) []byte {
path := GetMachinesYamlPath(repo)
data, err := ioutil.ReadFile(path)
if err != nil {
return []byte("")
}
return data
}
func GetCsMock(repo string) []byte {
path := GetCsmockPath(repo)
data, err := ioutil.ReadFile(path)
if err != nil {
return []byte("")
}
return data
}
func ReadYamlData(data []byte) map[string]Machine {
machines := make(map[string]Machine)
err := yaml.Unmarshal(data, &machines)
if err != nil {
fmt.Printf("Error unmarshalling machins.yml yaml in %s\n", string(data))
panic(err)
}
return machines
}
func ReadCsMockData(data []byte) map[string]Tags {
tags := make(map[string]Tags)
err := yaml.Unmarshal(data, &tags)
if err != nil {
fmt.Printf("Error unmarshalling csmock yaml in %s\n", string(data))
panic(err)
}
return tags
}
func GetMachinesYamlPath(repo string) string {
if repo == "salt-states" {
return "../salt-states/vagrant-env/machines.yml"
}
base_machines := "../%s/machines.yml"
return fmt.Sprintf(base_machines, repo)
}
func GetCsmockPath(repo string) string {
if repo == "salt-states" {
return ""
}
base_machines := "../%s/csmock.yml"
return fmt.Sprintf(base_machines, repo)
}
func FindAllSaltDirectories(data ioData) []string {
files, err := data.ReadDir("../")
if err != nil {
log.Fatal(err)
}
var dirNames []string
for _, file := range files {
if strings.HasPrefix(file, "salt-") && file != "salt-states" {
dirNames = append(dirNames, file)
}
}
return dirNames
}
func GetAllMachines(data ioData) map[string]OpenNebulaMachine {
machs := make(map[string]OpenNebulaMachine)
repos := FindAllSaltDirectories(data)
var machines map[string]Machine
var tags map[string]Tags
for _, repo := range repos {
if repo == "salt-pillar" {
continue // no machines in pillar
}
machines = GetReposMachines(repo)
tags = GetReposTags(repo)
for name, machine := range machines {
tag := GetTagLookup(tags, name) // for partial matching of tags
operatingSystem := GetOperatingSystem(machines, name)
machs[name] = machine.GetNewOpenNebulaMachine(name, tag, operatingSystem)
}
}
base_machines := GetReposMachines("salt-states")
for name, machine := range base_machines {
var empty_tag = Tags{}
operatingSystem := GetOperatingSystem(base_machines, name)
machs[name] = machine.GetNewOpenNebulaMachine(name, empty_tag, operatingSystem)
}
return machs
}
func GetTagLookup(tags map[string]Tags, name string) Tags {
var machine_tag = Tags{}
if tag, ok := tags[name]; ok {
for _, tag_data := range tag.Tags {
machine_tag.Tags = append(machine_tag.Tags, tag_data)
}
}
for tag_title, tag_object := range tags {
if strings.HasSuffix(tag_title, "-") && strings.HasPrefix(name, tag_title) {
for _, tag_data := range tag_object.Tags {
machine_tag.Tags = append(machine_tag.Tags, tag_data)
}
}
}
return machine_tag
}
func (mach *Machine) GetNewOpenNebulaMachine(name string, tag_data Tags, operatingSystem string) OpenNebulaMachine {
var roles []string
for _, data := range tag_data.Tags {
if strings.HasPrefix(data["name"], "role-") {
roles = append(roles, strings.TrimLeft(data["name"], "role-"))
}
}
var team_env string
var MySyncedFolders []SyncedFolder
if mach.TeamEnv == "" {
team_env = "qa"
} else {
team_env = mach.TeamEnv
}
for _, folder := range mach.SyncedFolder {
SyncedFolder := SyncedFolder{folder["source"], folder["destination"]}
MySyncedFolders = append(MySyncedFolders, SyncedFolder)
}
return OpenNebulaMachine{name, operatingSystem, roles, "", 0, false, mach.Hostname, mach.Mem, mach.VCPU, MySyncedFolders, team_env, tag_data}
}
func GetOperatingSystem(machines map[string]Machine, name string) string {
inherits := machines[name].Inherit
if inherits == "" {
return name
}
if _, ok := machines[inherits]; !ok {
return inherits
}
return GetOperatingSystem(machines, inherits)
}