/
mapper.go
143 lines (118 loc) · 4.3 KB
/
mapper.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
package util
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
// GenericFileAssignment will take a path and parse check it for specific
// matches for file paths, and then find the given paths.
// - Absolute paths are inputs starting with / and remain unmodified.
// - Relative paths are inputs starting with ./ and append the source or pwd.
// - Empty paths are unmodified.
// Results are checked against os.Stat and the results are returned with an error or nil.
func GenericFileAssignment(input, path string, check bool) (string, error) {
// The playbook will be located on the host if the remote flag is enabled.
if input != "" {
if strings.HasPrefix(input, "/") {
input = fmt.Sprintf("%v", input)
}
if strings.HasPrefix(input, "./") {
input = strings.Replace(input, "./", "", 1)
}
pwd, _ := os.Getwd()
if path == "" {
input = fmt.Sprintf("%v/%v", pwd, input)
} else {
input = fmt.Sprintf("%v", input)
}
if check {
_, err := os.Stat(input)
os.IsNotExist(err)
if err != nil {
return input, errors.New("Specified file " + input + " does not exist")
}
}
}
return input, nil
}
// GenericPlaybookAssignment will return the path of the only available playbook file under
// the roles tests directory, to be used when the default playbook path is invalid.
func GenericPlaybookAssignment(input, path string) (string, error) {
var files []string
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
files = append(files, path)
return nil
})
if err != nil {
return input, errors.New("no playbook.yml file was found, please provide path to playbook.")
}
for _, file := range files {
if strings.Contains(file, ".yml") {
if strings.HasSuffix(file, "playbook.yml") {
return strings.Replace(file, path+string(os.PathSeparator), "", -1), nil
}
}
}
return input, nil
}
// MapPlaybook will adjust the playbook path for the appropriate
// path based on the configuration. ie remote or not, and
// guesswork based upon input. For example, paths starting with
// /, ./ or otherwise.
func MapPlaybook(config *AnsibleConfig) {
playbook, err := GenericFileAssignment(config.PlaybookFile, config.HostPath, true)
if err != nil {
playbook, err = GenericPlaybookAssignment(config.PlaybookFile, config.HostPath)
}
config.PlaybookFile = playbook
if err != nil {
log.Fatalf("Specified playbook file %v does not exist.", config.PlaybookFile)
}
if !config.Remote && config.PlaybookFile != "" {
pwd, _ := os.Getwd()
config.PlaybookFile = strings.Replace(config.PlaybookFile, pwd, config.RemotePath, -1)
}
if err == nil {
if config.Remote && config.RemotePath == "" {
pwd, _ := os.Getwd()
config.RemotePath = pwd
} else if !config.Remote && config.RemotePath == "" {
config.RemotePath = "/etc/ansible/roles/role_under_test"
}
}
}
// MapInventory will adjust the inventory path for the appropriate
// path based on the configuration. ie remote or not, and
// guesswork based upon input. For example, paths starting with
// /, ./ or otherwise.
func MapInventory(CID string, config *AnsibleConfig) {
inventory, err := GenericFileAssignment(config.Inventory, config.HostPath, false)
config.Inventory = inventory
if err != nil {
log.Fatalf("Specified inventory file %v does not exist.", config.Inventory)
}
if !config.Remote && config.Inventory != "" {
pwd, _ := os.Getwd()
config.Inventory = strings.Replace(config.Inventory, pwd, config.RemotePath, -1)
config.Inventory = fmt.Sprintf("%v/%v", config.RemotePath, config.Inventory)
}
}
// MapRequirements will adjust the requirements path for the appropriate
// path based on the configuration. ie remote or not, and
// guesswork based upon input. For example, paths starting with
// /, ./ or otherwise.
func MapRequirements(config *AnsibleConfig) {
requirements, err := GenericFileAssignment(config.RequirementsFile, config.HostPath, true)
config.RequirementsFile = requirements
if err != nil {
log.Fatalf("Specified requirements file %v does not exist.", config.RequirementsFile)
}
if !config.Remote && config.RequirementsFile != "" {
pwd, _ := os.Getwd()
config.RequirementsFile = strings.Replace(config.RequirementsFile, pwd, config.RemotePath, -1)
config.RequirementsFile = fmt.Sprintf("%v/%v", config.RemotePath, config.RequirementsFile)
}
}