-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta-init.go
More file actions
230 lines (199 loc) · 5.4 KB
/
meta-init.go
File metadata and controls
230 lines (199 loc) · 5.4 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"path/filepath"
"flag"
"fmt"
"os"
"os/exec"
"net/http"
"io"
"encoding/json"
"io/ioutil"
"runtime"
"reflect"
"strings"
"sort"
)
var (
displayVersion = flag.Bool("version", false, "display version")
metaFile = flag.String("meta-file", "", "Metadata file path")
metaDirectory = flag.String("meta-directory", "", "Metadata directory path")
)
const VERSION = "0.1.0"
type Hash map[string]interface{}
func Version() string {
return fmt.Sprintf("meta-init v%s (built w/%s)", VERSION, runtime.Version())
}
func init() {
}
func main() {
var metadataContent []byte
var errChk error
var metaface interface{}
flag.Parse()
if *displayVersion {
fmt.Printf("%s", Version())
return
}
if *metaFile != "" {
content, errChk := ioutil.ReadFile(*metaFile)
if errChk != nil {
fmt.Printf("ERROR: Failed to read metadata file (%s)\n", *metaFile)
fmt.Printf(" *** %s\n", errChk.Error())
return
}
metadataContent = append(metadataContent, content...)
} else if *metaDirectory != "" {
items, errChk := ioutil.ReadDir(*metaDirectory)
if errChk != nil {
fmt.Printf("ERROR: Failed to read data directory (%s)\n", *metaDirectory)
fmt.Printf(" *** %s\n", errChk.Error())
return
}
for _, item := range items {
filePath := *metaDirectory + "/" + item.Name()
content, errChk := ioutil.ReadFile(filePath)
if errChk != nil {
fmt.Printf("ERROR: Failed to read metadata file (%s)\n", filePath)
fmt.Printf(" *** %s\n", errChk.Error())
return
}
metadataContent = append(metadataContent, content...)
}
} else {
fmt.Printf("ERROR: Metadata file or directory of parts is required!\n")
return
}
errChk = json.Unmarshal(metadataContent, &metaface)
if errChk != nil {
fmt.Printf("ERROR: Failed to load JSON metadata!\n")
fmt.Printf(" *** %s\n", errChk.Error())
}
topLevel := Hash(metaface.(map[string]interface{}))
meta := unpackHash("AWS::CloudFormation::Init", topLevel)
config := unpackHash("config", meta)
if config != nil {
fmt.Printf("Processing config section... \n")
_ = processConfig(config)
fmt.Printf(" ----> [config section] DONE\n")
}
commands := unpackHash("commands", config)
if commands != nil {
fmt.Printf("Processing commands section... \n")
_ = processCommands(commands)
fmt.Printf(" ----> [commands section] DONE\n")
}
}
func processConfig(config Hash) bool {
files := unpackHash("files", config)
if files != nil {
_ = processFiles(files)
}
return true
}
// still needs chmod and chown
func processFiles(files Hash) bool {
var errChk error
for fileName := range files {
dir := filepath.Dir(fileName)
errChk = os.MkdirAll(dir, 0755)
if errChk != nil {
return false
}
fileInfo := unpackHash(fileName, files)
if fileInfo["content"] != nil {
content := reflect.ValueOf(fileInfo["content"])
kind := content.Kind().String()
if kind == "string" {
errChk = ioutil.WriteFile(fileName, []byte(content.String()), 0600)
} else if kind == "map" {
mard, _ := json.Marshal(fileInfo["content"])
errChk = ioutil.WriteFile(fileName, mard, 0600)
} else {
fmt.Printf("WARN: Unknown value type. Cannot process - %s\n", kind)
}
if errChk != nil {
return false
}
} else if fileInfo["source"] != nil {
out, _ := os.Create(fileName)
defer out.Close()
resp, _ := http.Get(strings.Replace(fileInfo["source"].(string), "\"", "", -1))
defer resp.Body.Close()
io.Copy(out, resp.Body)
}
}
return true
}
func processCommands(commands Hash) bool {
process := true
commandIdentifiers := make([]string, len(commands))
i := 0
for k, _ := range commands {
commandIdentifiers[i] = k
i++
}
sort.Strings(commandIdentifiers)
for _, commandIdentifier := range commandIdentifiers {
process = true
commandInfo := unpackHash(commandIdentifier, commands)
test := commandInfo["test"]
if test != nil {
process = applyCommand(reflect.ValueOf(test))
}
if process {
if !applyCommand(reflect.ValueOf(commands[commandIdentifier])) {
fmt.Printf("ERROR: Command failed [%s]\n", commandIdentifier)
return false
}
} else {
fmt.Printf("WARN: Command skipped due to test result [%s]\n", commandIdentifier)
}
}
return true
}
func applyCommand(thing reflect.Value) bool {
var errChk error
kind := thing.Kind().String()
if kind == "string" {
cmd := exec.Command("sh", "-c", thing.String())
errChk = cmd.Run()
if errChk != nil {
fmt.Printf("WARN: Command unsuccessful: %s\n", thing.String())
fmt.Printf("WARN: Command returned failure: %s\n", errChk.Error())
return false
}
} else if kind == "map" {
hash := Hash(thing.Interface().(map[string]interface{}))
command := hash["command"].(string)
cmd := exec.Command("sh", "-c", command)
env_map := unpackHash("env", hash)
if env_map != nil {
env := []string{}
for env_name := range env_map {
new_val := env_name + "=" + env_map[env_name].(string)
env = append(env, new_val)
}
cmd.Env = env
}
if hash["cwd"] != nil {
cmd.Dir = hash["cwd"].(string)
}
errChk = cmd.Run()
if errChk != nil {
fmt.Printf("WARN: Command unsuccessful: %s\n", command)
fmt.Printf("WARN: Command returned failure: %s\n", errChk.Error())
return false
}
} else {
fmt.Printf("WARN: Unknown command type. Cannot process - %s\n", kind)
return false
}
return true
}
func unpackHash(key string, hash Hash) Hash {
if hash[key] != nil {
return Hash(hash[key].(map[string]interface{}))
}
return nil
}