-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·191 lines (167 loc) · 4.96 KB
/
main.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
package main
import (
"flag"
"log"
"os"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/pagination"
"github.com/gophercloud/utils/openstack/clientconfig"
"github.com/txn2/txeh"
ini "gopkg.in/ini.v1"
)
type Inventory struct {
tag string
Element []Element
}
type Element struct {
id string
Name string
Ip string
}
var fullInventory []Inventory
func main() {
mainGroup := flag.String("main-group", "staging:children", "Specify the main group file contained in the inventory file. Use always <name>:children form, otherwise it will break the code!")
path := flag.String("output", "./hosts", "Specify output path for inventory.ini file")
filename := flag.String("filename", "inventory.ini", "Specify inventory output filename")
domainName := flag.String("domain", ".it-mil1.ecs.compute.internal", "Specify domain name for the hosts")
reboot := flag.Bool("reboot", false, "Tell the app to reboot groups instances.")
flag.Parse()
args := os.Args
server_opts := servers.ListOpts{}
if _, err := os.Stat(*path); err != nil {
if err := os.Mkdir(*path, os.ModePerm); err != nil {
log.Fatalf("Erorr: %v\n", err)
}
}
os.Remove(*path + "/" + *filename)
os.Create(*path + "/" + *filename)
provider, err := initProvider()
if err != nil {
log.Fatalf("Error initializing Openstack provider: %v\n", err)
return
}
client, err := initCompute(provider)
if err != nil {
log.Fatalf("Error initializing Compute client: %v\n", err)
return
}
for tagIdx := 1; tagIdx < len(args); tagIdx++ {
inventory, err := retriveServers(client, server_opts, args[tagIdx], *domainName, *reboot)
if err != nil {
log.Panicf("Could not retrive servers: %v\n", err)
return
}
fullInventory = append(fullInventory, inventory)
}
err = generateInventoryFile(fullInventory, *path+"/"+*filename, *domainName, *mainGroup)
if err != nil {
log.Panicf("Error writing INI file: %v\n", err)
return
}
}
func authenticate() (client *gophercloud.ProviderClient, err error) {
opts := &clientconfig.ClientOpts{
Cloud: "ocloud",
}
client, err = clientconfig.AuthenticatedClient(opts)
if err != nil {
log.Fatalf("Error authenticating user: %v\n", err)
return nil, err
}
return client, nil
}
func initProvider() (provider *gophercloud.ProviderClient, err error) {
provider, err = authenticate()
if err != nil {
log.Fatalf("Error during authentication: %v", err)
}
if err != nil {
return nil, err
}
return provider, nil
}
func initCompute(provider *gophercloud.ProviderClient) (client *gophercloud.ServiceClient, err error) {
client, err = openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
Region: "it-mil1",
Type: "compute",
})
if err != nil {
return nil, err
}
return client, nil
}
func retriveServers(client *gophercloud.ServiceClient, server_opts servers.ListOpts, tag string, domain string, reboot bool) (inventory Inventory, err error) {
pager := servers.List(client, server_opts)
pager.EachPage(func(p pagination.Page) (bool, error) {
serverlist, err := servers.ExtractServers(p)
for _, v := range serverlist {
for _, v2 := range v.Metadata {
if v2 == tag {
var element Element
element.id = v.ID
element.Name = v.Name
element.Ip, err = retriveNetworAddress(client, element.id)
inventory.Element = append(inventory.Element, element)
inventory.tag = tag
if reboot {
servers.Reboot(client, element.id, servers.RebootOpts{Type: servers.OSReboot})
log.Printf("Rebooted server %v\n", element.Name)
}
editHostsFile(element.Ip, element.Name+domain)
}
}
}
return false, err
})
return inventory, nil
}
func retriveNetworAddress(client *gophercloud.ServiceClient, id string) (ip string, err error) {
pager := servers.ListAddresses(client, id)
err = pager.EachPage(func(p pagination.Page) (bool, error) {
addressList, err := servers.ExtractAddresses(p)
for _, addresses := range addressList {
for idx := 1; idx < len(addresses); idx += 2 {
// Get only IPv4
if addresses[idx].Version == 4 {
ip = addresses[idx].Address
}
}
}
return false, err
})
if err != nil {
return "", err
}
return ip, nil
}
func generateInventoryFile(hosts []Inventory, path string, domain string, mainGroup string) (err error) {
output, err := ini.LoadSources(ini.LoadOptions{
AllowBooleanKeys: true,
}, path)
if err != nil {
return err
}
for _, host := range hosts {
output.Section(mainGroup).NewBooleanKey(host.tag)
for _, element := range host.Element {
output.Section(host.tag).NewBooleanKey(element.Name + domain)
}
}
output.SaveTo(path)
if err != nil {
return err
}
return nil
}
func editHostsFile(ip string, hostname string) {
hosts, err := txeh.NewHostsDefault()
if err != nil {
log.Fatalf("Error: %v\n", err)
}
hosts.AddHost(ip, hostname)
if err := hosts.Save(); err != nil {
log.Fatalf("Error saving hosts file: %v\n", err)
}
}