-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
97 lines (85 loc) · 2.49 KB
/
files.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
package utils
import (
_ "embed"
"fmt"
"os"
"path/filepath"
)
var rootDir string = "./overcloud-deploy"
const (
TRIPLEO_ANSIBLE_INVENTORY_YAML string = "tripleo-ansible-inventory.yaml"
OVERCLOUD_EXPORT string = "overcloud-export.yaml"
OVERCLOUD_PASSWORDS string = "overcloud-passwords.yaml"
GLOBAL_VARS string = "config-download/overcloud/global_vars.yaml"
TRIPLEO_OVERCLOUD_NETWORK_DATA string = "tripleo-overcloud-network-data.yaml"
BAREMETAL_DEPLOY string = "tripleo-overcloud-baremetal-deployment.yaml"
DEPLOY_STEPS_ONE string = "config-download/overcloud/external_deploy_steps_tasks_step1.yaml"
// Add more file names as needed
)
func GetFullPath(fileName string) string {
return rootDir + "/" + fileName
}
func SetRootDir(dir string) {
rootDir = dir
}
func SearchFile(targetFileName string) (string, error) {
var foundFilePath string
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}
if !info.IsDir() && info.Name() == targetFileName {
foundFilePath = path
return filepath.SkipDir
}
return nil
})
if err != nil {
return "", err
}
if foundFilePath == "" {
return "", fmt.Errorf("file not found: %s starting at %s", targetFileName, rootDir)
}
return foundFilePath, nil
}
func WalkDir(dirname string) {
err := filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return err
}
fmt.Printf("dir: %v: name: %s\n", info.IsDir(), path)
return nil
})
if err != nil {
fmt.Println(err)
}
}
func WriteByteData(buf []byte, dir string, fileName string) error {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
if err = os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("unable to mkdir: %s", dir)
}
}
dFile, err := os.Create(dir + "/" + fileName)
if err != nil {
fmt.Printf("unable to create/open file: %s", fileName)
}
defer dFile.Close()
fmt.Printf("Writing %s...\n", fileName)
if _, err = dFile.Write(buf); err != nil {
return fmt.Errorf("unable to write instructions to: %s", dir+"/"+fileName)
}
return nil
}
func GetTripleoOvercloudNetworkData() ([]map[string]interface{}, error) {
// Assume for now a 1:1 mapping between the number of 17.1 OSP controllers and
// the number of OCP master nodes
networks, err := YamlToList(GetFullPath(TRIPLEO_OVERCLOUD_NETWORK_DATA))
if err != nil {
return nil, err
}
return networks, nil
}