/
builder.go
256 lines (219 loc) · 6.17 KB
/
builder.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
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/kdomanski/iso9660/util"
)
const (
tmpFolderPrefix = "iso_package_tmp"
)
type Builder struct {
Source string
Kickstart string
Arch string
tmpFolder string
isoSourceLabel string
dstFolderPath string
finalIsoPath string
}
// CleanAll remove all files from the temporal folder
func (c *Builder) CleanAll() {
os.RemoveAll(c.tmpFolder)
}
// Run add the given Kickstart file to given iso
func (c *Builder) Run() (string, error) {
err := c.CheckConfig()
if err != nil {
return "", err
}
dirName, err := os.MkdirTemp("/tmp/", tmpFolderPrefix)
if err != nil {
return "", err
}
InfoLogger.Printf("Creating temp folder: %s", dirName)
c.tmpFolder = dirName
err = c.GetIso()
if err != nil {
return "", err
}
err = c.MountIso()
if err != nil {
return "", err
}
err = c.UpdateFiles()
if err != nil {
return "", err
}
path, err := c.CreateFinalIso()
if err != nil {
return "", err
}
return path, nil
}
func (c *Builder) Upload(creds *S3Config, targetOutput string) error {
sess, err := creds.GetAwsSession()
if err != nil {
return fmt.Errorf("cannot get aws session: %v", err)
}
if c.finalIsoPath == "" {
return errors.New("Final iso cannot be found")
}
err = creds.UploadFile(sess, targetOutput, c.finalIsoPath)
if err != nil {
return fmt.Errorf("cannot upload filename to S3: %v", err)
}
return err
}
// CheckConfig validates that current config struct is valid
func (c *Builder) CheckConfig() error {
if c.Source == "" {
return errors.New("Source file cannot be nil")
}
if c.Kickstart == "" {
return errors.New("kickstart file cannot be nil")
}
_, err := os.Stat(c.Kickstart)
if err != nil {
return fmt.Errorf("kickstart file cannot be found: %v", err)
}
// @todo dependencies programs here geniso, xorriso, blkid
return nil
}
// Mount the iso in the source_iso folder
func (c *Builder) MountIso() error {
cmd := execCommand("blkid -s LABEL -o value %s", c.getIsoFilepath())
if cmd.Failed() {
return fmt.Errorf("cannot get iso label: %v", cmd.GetErrorMessage())
}
c.isoSourceLabel = strings.Trim(cmd.GetStdout(), "\n")
sourceFolderPath := filepath.Join(c.tmpFolder, "source_iso")
err := os.Mkdir(sourceFolderPath, 0775)
if err != nil {
return fmt.Errorf("cannot create source iso folder: %v", err)
}
c.dstFolderPath = filepath.Join(c.tmpFolder, "dest_iso")
err = os.Mkdir(c.dstFolderPath, 0775)
if err != nil {
return fmt.Errorf("cannot create dest iso folder: %v", err)
}
f, err := os.Open(c.getIsoFilepath())
if err != nil {
return fmt.Errorf("cannot open source iso file: %v", err)
}
defer f.Close()
if err = util.ExtractImageToDirectory(f, sourceFolderPath); err != nil {
return fmt.Errorf("cannot mount source iso file: %v", err)
}
err = Dir(sourceFolderPath, c.dstFolderPath)
if err != nil {
return fmt.Errorf("cannot copy source iso files: %v", err)
}
return nil
}
// UpdateFiles copy the Kickstart file and update all isolinux and grub to
// parse it.
func (c *Builder) UpdateFiles() error {
err := File(c.Kickstart, filepath.Join(c.dstFolderPath, "init.ks"))
if err != nil {
return fmt.Errorf("cannot copy kickstart file, %v", err)
}
grubPath := filepath.Join(c.dstFolderPath, "EFI/BOOT/GRUB.CFG")
grub, err := AppendKickstartTogrub(grubPath, "init.ks")
if err != nil {
return fmt.Errorf("cannot append kickstart file to grub, %v", err)
}
err = os.WriteFile(grubPath, grub.Bytes(), 0600)
if err != nil {
// This error message is clear enough
return err
}
isolinuxPath := filepath.Join(c.dstFolderPath, "/ISOLINUX/ISOLINUX.CFG")
isolinux, err := AppendKickstartToIsoLinux(isolinuxPath, "init.ks")
if err != nil {
return fmt.Errorf("cannot append kickstart file to isolinux, %v", err)
}
err = os.WriteFile(isolinuxPath, isolinux.Bytes(), 0600)
// This error message is clear enough
return err
}
func (c *Builder) CreateFinalIso() (string, error) {
finalPath := filepath.Join(c.tmpFolder, "final.iso")
switch c.Arch {
case x86ArchValue:
err := c.genIso(finalPath, c.isoSourceLabel)
c.finalIsoPath = finalPath
return finalPath, err
case armArchValue:
err := c.XorrisogenIso(finalPath, c.isoSourceLabel)
c.finalIsoPath = finalPath
return finalPath, err
default:
return "", fmt.Errorf("Not valid arch to build the image: %s", c.Arch)
}
}
func (c *Builder) XorrisogenIso(output string, label string) error {
command := `xorriso \
-as mkisofs \
-V %s \
-r -J -joliet-long -cache-inodes -efi-boot-part \
--efi-boot-image \
-e IMAGES/EFIBOOT.IMG \
-no-emul-boot \
%s > %s`
cmd := execCommand(command, label, c.dstFolderPath, output)
if cmd.Failed() {
return fmt.Errorf("Failed to create the iso %s", cmd.GetErrorMessage())
}
return nil
}
func (c *Builder) genIso(output string, label string) error {
command := `cd %[1]s && \
genisoimage -U -r -v -T -J \
-joliet-long \
-V %[2]s \
-b ISOLINUX/ISOLINUX.BIN \
-c ISOLINUX/BOOT.CAT \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
-eltorito-alt-boot \
-e IMAGES/EFIBOOT.IMG \
-no-emul-boot \
-o %[3]s .
`
cmd := execCommand(fmt.Sprintf(command, c.dstFolderPath, label, output))
if cmd.Failed() {
return fmt.Errorf("Failed to create the iso %s", cmd.GetErrorMessage())
}
InfoLogger.Printf("Iso created correctly at %s", output)
return nil
}
// Return the path for the original iso.
func (c *Builder) getIsoFilepath() string {
return filepath.Join(c.tmpFolder, "original.iso")
}
// GetIso donwloads the image from the given url and store it on
// c.getIsoFilepath()
func (c *Builder) GetIso() error {
out, err := os.Create(c.getIsoFilepath())
if err != nil {
return fmt.Errorf("cannot create temporal original iso file: %v", err)
}
defer out.Close()
resp, err := http.Get(c.Source)
if err != nil {
return fmt.Errorf("cannot download the iso file: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Cannot get source ISO image from url '%s' status %d", c.Source, resp.StatusCode)
}
InfoLogger.Printf("Writing iso image to '%s'", c.getIsoFilepath())
_, err = io.Copy(out, resp.Body)
return err
}