-
Notifications
You must be signed in to change notification settings - Fork 39
/
import.go
330 lines (311 loc) · 8.45 KB
/
import.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (c) 2013-2017, Jeremy Bingham (<[email protected]>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/ctdk/goiardi/client"
"github.com/ctdk/goiardi/cookbook"
"github.com/ctdk/goiardi/databag"
"github.com/ctdk/goiardi/environment"
"github.com/ctdk/goiardi/filestore"
"github.com/ctdk/goiardi/loginfo"
"github.com/ctdk/goiardi/node"
"github.com/ctdk/goiardi/report"
"github.com/ctdk/goiardi/role"
"github.com/ctdk/goiardi/sandbox"
"github.com/ctdk/goiardi/shovey"
"github.com/ctdk/goiardi/user"
"github.com/tideland/golib/logger"
)
func importAll(fileName string) error {
fp, err := os.Open(fileName)
if err != nil {
return err
}
exportedData := &ExportData{}
dec := json.NewDecoder(fp)
dec.UseNumber()
if err := dec.Decode(&exportedData); err != nil {
return err
}
// What versions of the exported data are supported?
// At the moment it's only 1.0.
if exportedData.MajorVersion == 1 && (exportedData.MinorVersion == 0 || exportedData.MinorVersion == 1) {
logger.Infof("Importing data, version %d.%d created on %s", exportedData.MajorVersion, exportedData.MinorVersion, exportedData.CreatedTime)
// load clients
logger.Infof("Loading clients")
for _, v := range exportedData.Data["client"] {
c, err := client.NewFromJSON(v.(map[string]interface{}))
if err != nil {
return err
}
pkerr := c.SetPublicKey(v.(map[string]interface{})["public_key"])
if pkerr != nil {
return pkerr
}
gerr := c.Save()
if gerr != nil {
return gerr
}
}
// load users
logger.Infof("Loading users")
for _, v := range exportedData.Data["user"] {
pwhash, _ := v.(map[string]interface{})["password"].(string)
v.(map[string]interface{})["password"] = ""
u, err := user.NewFromJSON(v.(map[string]interface{}))
if err != nil {
return err
}
u.SetPasswdHash(pwhash)
pkerr := u.SetPublicKey(v.(map[string]interface{})["public_key"])
if pkerr != nil {
return pkerr
}
gerr := u.Save()
if gerr != nil {
return gerr
}
}
// load filestore
logger.Infof("Loading filestore")
for _, v := range exportedData.Data["filestore"] {
fileData, err := base64.StdEncoding.DecodeString(v.(map[string]interface{})["Data"].(string))
if err != nil {
return err
}
fdBuf := bytes.NewBuffer(fileData)
fdRc := ioutil.NopCloser(fdBuf)
fs, err := filestore.New(v.(map[string]interface{})["Chksum"].(string), fdRc, int64(fdBuf.Len()))
if err != nil {
return err
}
if err = fs.Save(); err != nil {
return err
}
}
// load cookbooks
logger.Infof("Loading cookbooks")
for _, v := range exportedData.Data["cookbook"] {
cb, err := cookbook.New(v.(map[string]interface{})["Name"].(string))
if err != nil {
return err
}
gerr := cb.Save()
if gerr != nil {
return gerr
}
for ver, cbvData := range v.(map[string]interface{})["Versions"].(map[string]interface{}) {
cbvData, cerr := checkAttrs(cbvData.(map[string]interface{}))
if cerr != nil {
return cerr
}
_, cbverr := cb.NewVersion(ver, cbvData)
if cbverr != nil {
return cbverr
}
}
}
// load data bags
logger.Infof("Loading data bags")
for _, v := range exportedData.Data["data_bag"] {
dbag, err := databag.New(v.(map[string]interface{})["Name"].(string))
if err != nil {
return err
}
gerr := dbag.Save()
if gerr != nil {
return gerr
}
for _, dbagData := range v.(map[string]interface{})["DataBagItems"].(map[string]interface{}) {
_, dbierr := dbag.NewDBItem(dbagData.(map[string]interface{})["raw_data"].(map[string]interface{}))
if dbierr != nil {
return dbierr
}
}
gerr = dbag.Save()
if gerr != nil {
return gerr
}
}
// load environments
logger.Infof("Loading environments")
for _, v := range exportedData.Data["environment"] {
envData, cerr := checkAttrs(v.(map[string]interface{}))
if cerr != nil {
return nil
}
if envData["name"].(string) != "_default" {
e, err := environment.NewFromJSON(envData)
if err != nil {
return err
}
gerr := e.Save()
if gerr != nil {
return gerr
}
}
}
// load nodes
logger.Infof("Loading nodes")
for _, v := range exportedData.Data["node"] {
nodeData, cerr := checkAttrs(v.(map[string]interface{}))
if cerr != nil {
return nil
}
n, err := node.NewFromJSON(nodeData)
if err != nil {
return err
}
gerr := n.Save()
if gerr != nil {
return gerr
}
}
// load roles
logger.Infof("Loading roles")
for _, v := range exportedData.Data["role"] {
roleData, cerr := checkAttrs(v.(map[string]interface{}))
if cerr != nil {
return nil
}
r, err := role.NewFromJSON(roleData)
if err != nil {
return err
}
gerr := r.Save()
if gerr != nil {
return gerr
}
}
// load sandboxes
logger.Infof("Loading sandboxes")
for _, v := range exportedData.Data["sandbox"] {
sbid, _ := v.(map[string]interface{})["Id"].(string)
sbts, _ := v.(map[string]interface{})["CreationTime"].(string)
sbcomplete, _ := v.(map[string]interface{})["Completed"].(bool)
sbck, _ := v.(map[string]interface{})["Checksums"].([]interface{})
sbTime, err := time.Parse(time.RFC3339, sbts)
if err != nil {
return err
}
sbChecksums := make([]string, len(sbck))
for i, c := range sbck {
sbChecksums[i] = c.(string)
}
sbox := &sandbox.Sandbox{ID: sbid, CreationTime: sbTime, Completed: sbcomplete, Checksums: sbChecksums}
if err = sbox.Save(); err != nil {
return err
}
}
// load loginfos
logger.Infof("Loading loginfo")
for _, v := range exportedData.Data["loginfo"] {
if err := loginfo.Import(v.(map[string]interface{})); err != nil {
return err
}
}
// load reports
logger.Infof("Loading reports")
for _, o := range exportedData.Data["report"] {
// handle data exported from a bugged report export
var nodeName string
v := o.(map[string]interface{})
if n, ok := v["node_name"]; ok {
nodeName = n.(string)
} else if n, ok := v["nodeName"]; ok {
nodeName = n.(string)
}
v["action"] = "start"
if st, ok := v["start_time"].(string); ok {
t, err := time.Parse(time.RFC3339, st)
if err != nil {
return err
}
v["start_time"] = t.Format(report.ReportTimeFormat)
}
if et, ok := v["end_time"].(string); ok {
t, err := time.Parse(time.RFC3339, et)
if err != nil {
return err
}
v["end_time"] = t.Format(report.ReportTimeFormat)
}
r, err := report.NewFromJSON(nodeName, v)
if err != nil {
return err
}
gerr := r.Save()
if gerr != nil {
return gerr
}
v["action"] = "end"
if err := r.UpdateFromJSON(v); err != nil {
return err
}
gerr = r.Save()
if gerr != nil {
return gerr
}
}
if exportedData.MinorVersion == 1 {
// import shovey jobs, run, and streams, and node
// statuses
logger.Infof("Loading node statuses...")
for _, v := range exportedData.Data["node_status"] {
ns := v.(map[string]interface{})
err := node.ImportStatus(ns)
if err != nil {
return err
}
}
logger.Infof("Loading shoveys...")
for _, v := range exportedData.Data["shovey"] {
s := v.(map[string]interface{})
err := shovey.ImportShovey(s)
if err != nil {
return err
}
}
logger.Infof("Loading shovey runs...")
for _, v := range exportedData.Data["shovey_run"] {
s := v.(map[string]interface{})
err := shovey.ImportShoveyRun(s)
if err != nil {
return err
}
}
logger.Infof("Loading shovey run streams...")
for _, v := range exportedData.Data["shovey_run_stream"] {
s := v.(map[string]interface{})
err := shovey.ImportShoveyRunStream(s)
if err != nil {
return err
}
}
}
} else {
err := fmt.Errorf("goiardi export data version %d.%d is not supported by this version of goiardi", exportedData.MajorVersion, exportedData.MinorVersion)
return err
}
return nil
}