forked from gen0cide/laforge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.go
78 lines (69 loc) · 1.72 KB
/
host.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
package competition
import (
"encoding/json"
"fmt"
"io/ioutil"
yaml "gopkg.in/yaml.v2"
)
var (
AMIMap map[string]*AMI
)
type AMI struct {
OS string `json:"os"`
Regions map[string]string `json:"regions"`
Username string `json:"username"`
}
func LoadAMIs() {
var amis []*AMI
amiMap := make(map[string]*AMI)
json.Unmarshal(MustAsset("amis.json"), &amis)
for _, a := range amis {
amiMap[a.OS] = a
}
AMIMap = amiMap
}
type Host struct {
Hostname string `yaml:"hostname"`
OS string `yaml:"os"`
AMI string `yaml:"ami"`
InstanceSize string `yaml:"instance_size"`
LastOctet int `yaml:"last_octet"`
InternalCNAMEs []string `yaml:"internal_cnames"`
ExternalCNAMEs []string `yaml:"external_cnames"`
TCPPorts []int `yaml:"public_tcp"`
UDPPorts []int `yaml:"public_udp"`
Scripts []string `yaml:"scripts"`
UserDataScript string `yaml:"userdata_script"`
UserGroups []string `yaml:"user_groups"`
Vars `yaml:"variables"`
Network `yaml:"-"`
}
func (h *Host) GetAMI() string {
if len(h.AMI) > 0 {
return h.AMI
}
if _, ok := AMIMap[h.OS]; ok {
return AMIMap[h.OS].Regions[h.Network.Environment.AWSConfig.Region]
}
LogFatal(fmt.Sprintf("OS is invalid for host! host=%s os=%s", h.Hostname, h.OS))
return ""
}
func LoadHostFromFile(file string) (*Host, error) {
host := Host{}
hostConfig, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(hostConfig, &host)
if err != nil {
return nil, err
}
return &host, nil
}
func (h *Host) ToYAML() string {
y, err := yaml.Marshal(h)
if err != nil {
LogFatal("Error converting to YAML: " + err.Error())
}
return string(y)
}