-
Notifications
You must be signed in to change notification settings - Fork 42
/
utils.go
176 lines (140 loc) · 3.76 KB
/
utils.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
package providerutils
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"gopkg.in/ini.v1"
)
/*
CREATE OPTIONS
*/
const DefaultHostGroup = "default"
func InterfaceToString(arr []interface{}) ([]string, diag.Diagnostics) {
var diags diag.Diagnostics
result := []string{}
for _, val := range arr {
tmpVal, ok := val.(string)
if !ok {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Error: couldn't parse value to string!",
})
}
result = append(result, tmpVal)
}
return result, diags
}
// Create a "verbpse" switch
// example: verbosity = 2 --> verbose_switch = "-vv"
func CreateVerboseSwitch(verbosity int) string {
verbose := ""
if verbosity == 0 {
return verbose
}
verbose += "-"
verbose += strings.Repeat("v", verbosity)
return verbose
}
// Build inventory.ini (NOT YAML)
// -- building inventory.ini is easier
func BuildPlaybookInventory(
inventoryDest string,
hostname string,
port int,
hostgroups []interface{},
) (string, diag.Diagnostics) {
var diags diag.Diagnostics
// Check if inventory file is already present
// if not, create one
fileInfo, err := os.CreateTemp("", inventoryDest)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Fail to create inventory file: %v", err),
})
}
tempFileName := fileInfo.Name()
log.Printf("Inventory %s was created", fileInfo.Name())
// Then, read inventory and add desired settings to it
inventory, err := ini.Load(tempFileName)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Fail to read inventory: %v", err),
})
}
tempHostgroups := hostgroups
if len(tempHostgroups) == 0 {
tempHostgroups = append(tempHostgroups, DefaultHostGroup)
}
if len(tempHostgroups) > 0 { // if there is a list of groups specified for the desired host
for _, hostgroup := range tempHostgroups {
hostgroupStr, okay := hostgroup.(string)
if !okay {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Couldn't assert type: string",
})
}
if !inventory.HasSection(hostgroupStr) {
_, err := inventory.NewRawSection(hostgroupStr, "")
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Fail to create a hostgroup: %v", err),
})
}
}
if !inventory.Section(hostgroupStr).HasKey(hostname) {
body := hostname
if port != -1 {
body += " ansible_port=" + strconv.Itoa(port)
}
inventory.Section(hostgroupStr).SetBody(body)
}
}
}
err = inventory.SaveTo(tempFileName)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Fail to create inventory: %v", err),
})
}
return tempFileName, diags
}
func RemoveFile(filename string) diag.Diagnostics {
var diags diag.Diagnostics
err := os.Remove(filename)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Fail to remove file %s: %v", filename, err),
})
}
return diags
}
func GetAllInventories(inventoryPrefix string) ([]string, diag.Diagnostics) {
var diags diag.Diagnostics
tempDir := os.TempDir()
log.Printf("[TEMP DIR]: %s", tempDir)
files, err := os.ReadDir(tempDir)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Fail to read dir %s: %v", tempDir, err),
})
}
inventories := []string{}
for _, file := range files {
if strings.HasPrefix(file.Name(), inventoryPrefix) {
inventoryAbsPath := filepath.Join(tempDir, file.Name())
inventories = append(inventories, inventoryAbsPath)
}
}
return inventories, diags
}