/
ignition.go
337 lines (276 loc) · 6.66 KB
/
ignition.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
331
332
333
334
335
336
337
package gasoline
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/cavaliergopher/cpio"
ignutil "github.com/coreos/ignition/v2/config/util"
igntypes "github.com/coreos/ignition/v2/config/v3_2/types"
"github.com/vincent-petithory/dataurl"
)
const (
ignitionImage string = "ignition.img"
ignitionImagePath string = "images/ignition.img"
ignitionFile string = "config.ign"
sourceIgnitionFile string = "source-config.ign"
updatedIgnitionFile string = "config.ign"
binaryDestFolder string = "/usr/local/bin"
)
// imgUnpackIgnition extracts the config.ign file contained within the
// compressed cpio archive and saves it into the temp folder with a new name
func ImgUnpackIgnition(tmpPath string) error {
ignitionImgPath := filepath.Join(tmpPath, isoTempSubFolder, ignitionImagePath)
log.Println("Unpacking ignition image...")
f, err := os.Open(ignitionImgPath)
if err != nil {
return err
}
defer f.Close()
g, err := gzip.NewReader(f)
if err != nil {
return err
}
r := cpio.NewReader(g)
if err != nil {
return err
}
for {
hdr, err := r.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if hdr.Name != ignitionFile {
return fmt.Errorf("invalid ignition file found: %s", hdr.Name)
}
dest, err := os.Create(filepath.Join(tmpPath, sourceIgnitionFile))
if err != nil {
return err
}
defer dest.Close()
_, err = io.Copy(dest, r)
if err != nil {
return err
}
}
return nil
}
// Adds a binary file to the source ignition file, ad save it
// using a default name
func AddFileToIgnitionConfig(tmpPath string, addFile string) error {
sourceIgnition := filepath.Join(tmpPath, sourceIgnitionFile)
destIgnition := filepath.Join(tmpPath, updatedIgnitionFile)
log.Printf("Adding %s to %s\n", addFile, destIgnition)
f, err := os.ReadFile(sourceIgnition)
if err != nil {
return err
}
var config igntypes.Config
err = json.Unmarshal(f, &config)
if err != nil {
return err
}
// Grab the new file to be added to ignition
newFileData, err := os.ReadFile(addFile)
if err != nil {
return err
}
config.Storage.Files = append(config.Storage.Files, igntypes.File{
Node: igntypes.Node{
Group: igntypes.NodeGroup{},
Overwrite: ignutil.BoolToPtr(true),
Path: filepath.Join(binaryDestFolder, filepath.Base(addFile)),
User: igntypes.NodeUser{
Name: ignutil.StrToPtr("root"),
},
},
FileEmbedded1: igntypes.FileEmbedded1{
Contents: igntypes.Resource{
Source: ignutil.StrToPtr(dataurl.EncodeBytes(newFileData)),
},
Mode: ignutil.IntToPtr(0644),
},
})
newIgnition, err := json.Marshal(config)
if err != nil {
return err
}
err = os.WriteFile(destIgnition, newIgnition, 0777)
if err != nil {
return err
}
return nil
}
// Creats a new ignition.img containing an updated ignition file
func ImgRepackIgnition(tmpPath string) error {
configFilePath := filepath.Join(tmpPath, updatedIgnitionFile)
newIgnitionImgPath := filepath.Join(tmpPath, ignitionImage)
log.Println("Repacking new ignition.img with the updated config.ign file")
f, err := os.Create(newIgnitionImgPath)
if err != nil {
return err
}
defer f.Close()
gw := gzip.NewWriter(f)
defer gw.Close()
w := cpio.NewWriter(gw)
defer w.Close()
// add config file to the archive
err = store(w, configFilePath)
if err != nil {
return err
}
return nil
}
// Overwrites the old ignition image with the new one
func OverwriteOldIgnitionImage(tmpPath string) error {
// Copy the updated ignition archive in the temp folder
srcFile := filepath.Join(tmpPath, ignitionImage)
dstFile := filepath.Join(tmpPath, isoTempSubFolder, ignitionImagePath)
srcStat, err := os.Stat(srcFile)
if err != nil {
return err
}
dstState, err := os.Stat(dstFile)
if err != nil {
return err
}
log.Printf("New ignition.img size: %dK (old was %dK)", srcStat.Size()/1024, dstState.Size()/1024)
err = os.Rename(srcFile, dstFile)
if err != nil {
return err
}
return nil
}
func ImgIgnitionAppend(tmpPath string, addFilePath string) error {
// This temporary folder will contain all the files to be
// added in the new ignition.img archive
tmpIgnitionPath := filepath.Join(tmpPath, "tmp-ignition")
err := os.Mkdir(tmpIgnitionPath, 0755)
if err != nil {
return err
}
// Copy the additional file in the temp folder
input, err := ioutil.ReadFile(addFilePath)
if err != nil {
return err
}
dstFile := filepath.Join(tmpIgnitionPath, filepath.Base(addFilePath))
err = ioutil.WriteFile(dstFile, input, 0644)
if err != nil {
return err
}
// Extract the old ignition.img in the tempo folder
err = extractCpioFiles(tmpPath, tmpIgnitionPath)
if err != nil {
return err
}
// Create the new archive
err = createCpioArchive(tmpPath, tmpIgnitionPath)
if err != nil {
return err
}
return nil
}
// Extracts the content of the ignition.img in the temp folder
func extractCpioFiles(tmpPath string, dstPath string) error {
oldIgnitionImgPath := filepath.Join(tmpPath, isoTempSubFolder, ignitionImagePath)
// Open existing ignition.img
f, err := os.Open(oldIgnitionImgPath)
if err != nil {
return err
}
defer f.Close()
g, err := gzip.NewReader(f)
if err != nil {
return err
}
defer g.Close()
cpioReader := cpio.NewReader(g)
if err != nil {
return err
}
for {
hdr, err := cpioReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
dstFile, err := os.Create(filepath.Join(dstPath, hdr.Name))
if err != nil {
return err
}
defer dstFile.Close()
if _, err := io.Copy(dstFile, cpioReader); err != nil {
return err
}
}
return nil
}
func createCpioArchive(tmpPath string, srcPath string) error {
var buf bytes.Buffer
cpioWriter := cpio.NewWriter(&buf)
files, err := ioutil.ReadDir(srcPath)
if err != nil {
return nil
}
for _, f := range files {
err := store(cpioWriter, filepath.Join(srcPath, f.Name()))
if err != nil {
return err
}
}
err = cpioWriter.Close()
if err != nil {
return err
}
out, err := os.Create(filepath.Join(tmpPath, ignitionImage))
if err != nil {
return err
}
defer out.Close()
bufWriter := bufio.NewWriter(out)
defer bufWriter.Flush()
gz := gzip.NewWriter(bufWriter)
defer gz.Close()
_, err = io.Copy(gz, &buf)
if err != nil {
return err
}
return nil
}
func store(w *cpio.Writer, fn string) error {
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
hdr, err := cpio.FileInfoHeader(fi, "")
if err != nil {
return err
}
if err := w.WriteHeader(hdr); err != nil {
return err
}
if !fi.IsDir() {
if _, err := io.Copy(w, f); err != nil {
return err
}
}
return err
}