-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·327 lines (295 loc) · 8.95 KB
/
main.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
// Create an OPSI package without any external dependencies
// Exit codes:
// 1: error (unspecific)
// 2: bad usage
// 3: unusable "from" input directory
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
)
// Metadata holds a key value map
type Metadata map[string]string
func main() {
flag.Usage = usage
opsidir := flag.String("opsidir", "./OPSI", "OPSI directory used as input")
datadir := flag.String("datadir", "./CLIENT_DATA", "data directory used as input")
control := flag.String("control", "./OPSI/control", "OPSI control file")
into := flag.String("into", ".", "OPSI package destination directory")
keep := flag.Bool("keep", false, "keep OPSI interim workbench for debugging purposes")
flag.Parse()
cmdMeta, err := parseArgs()
if err != nil {
log.Fatalf("cannot parse commandline: %s\n", err)
}
// Check input directories
if !exists(*opsidir) {
fmt.Fprintf(os.Stderr, "missing expected directory %s\n", *opsidir)
os.Exit(3)
}
if !exists(*datadir) {
fmt.Fprintf(os.Stderr, "missing expected directory %s\n", *datadir)
os.Exit(3)
}
if !exists(*control) {
fmt.Fprintf(os.Stderr, "missing controlfile %s\n", *control)
os.Exit(3)
}
controlfile, err := ioutil.ReadFile(*control)
if err != nil {
log.Fatalf("error reading %s: %s\n", *control, err)
}
log.Printf("using control file %s, into %s\n", *control, *into)
// Resolve controlfile template parameters
rs := resolve(string(controlfile), cmdMeta)
workbench, err := tmpDir()
if err != nil {
log.Fatalf("cannot create workbench: %s\n", err)
}
defer func(path string, remove bool) {
if remove {
log.Printf("removing workbench %s\n", path)
removeDir(path)
} else {
log.Printf("keeping workbench %s\n", path)
}
}(workbench, !*keep)
if err := mkpkg(rs.Bytes(), *opsidir, *datadir, *into, workbench); err != nil {
log.Fatal(err)
}
}
func compress(intoFilename string, fromFilename string) (os.FileInfo, error) {
log.Printf("compressing %s from %s\n", intoFilename, fromFilename)
from, err := os.Open(fromFilename)
if err != nil {
return nil, fmt.Errorf("error opening %s: %s", fromFilename, err)
}
defer from.Close()
into, err := os.Create(intoFilename)
if err != nil {
return nil, fmt.Errorf("error creating %s: %s", intoFilename, err)
}
defer into.Close()
w := gzip.NewWriter(into)
n, err := io.Copy(w, from)
if err != nil {
return nil, fmt.Errorf("error writing to %s: %s", intoFilename, err)
}
if err := w.Close(); err != nil {
return nil, fmt.Errorf("error closing %s: %s", intoFilename, err)
}
fi, err := os.Stat(intoFilename)
if err != nil {
return nil, fmt.Errorf("cannot stat() %s: %s", intoFilename, err)
}
var factor float64
if fi.Size() == 0 {
factor = 1.0
} else {
factor = float64(n) / float64(fi.Size())
}
log.Printf("compress factor %.1f\n", factor)
return fi, nil
}
// exists returns whether the given file or directory exists or not
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
panic(err)
}
// return filename for final OPSI package, which is derived from package's metadata
func filename(m Metadata) string {
return fmt.Sprintf("%s_%s-%s.opsi",
get(m, "product_id"),
get(m, "product_version"),
get(m, "package_version"))
}
func get(m Metadata, key string) string {
value := m[key]
if len(value) == 0 {
log.Fatalf("missing required value for key %q in metadata", key)
}
return value
}
// no care has been taken to minimize interim file creation. Cowardly refusing
// to stack tar writer into gzip writer because documentation says:
// "Writes may be buffered and not flushed until Close."
// This is definitely not something we want, so just to be on the safe side,
// each and every tar and gzip step starts from and ends in the file system.
// TODO research io.Pipe() approach
func mkpkg(controlfile []byte, opsidir, datadir, into, tmpdir string) error {
// create OPSI.tar
opsiTarFilename := filepath.Join(tmpdir, "OPSI.tar")
log.Printf("creating %s\n", opsiTarFilename)
opsiTarFile, err := os.Create(opsiTarFilename)
if err != nil {
return fmt.Errorf("cannot create %s: %s", opsiTarFilename, err)
}
tw := tar.NewWriter(opsiTarFile)
if err := writeControlfile(tw, controlfile); err != nil {
return fmt.Errorf("error writing control file to %s: %s", opsiTarFilename, err)
}
if err := write(tw, opsidir, true); err != nil {
return fmt.Errorf("error writing control file to %s: %s", opsiTarFilename, err)
}
if err := tw.Close(); err != nil {
return fmt.Errorf("error closing %s: %s", opsiTarFilename, err)
}
// create OPSI.tar.gz
opsiTarGzFilename := filepath.Join(tmpdir, "OPSI.tar.gz")
_, err = compress(opsiTarGzFilename, opsiTarFilename)
if err != nil {
return fmt.Errorf("cannot compress %s into %s: %s", opsiTarFilename, opsiTarGzFilename, err)
}
// create CLIENT_DATA.tar
clientTarFilename := filepath.Join(tmpdir, "CLIENT_DATA.tar")
log.Printf("creating %s\n", clientTarFilename)
clientTarFile, err := os.Create(clientTarFilename)
if err != nil {
return fmt.Errorf("cannot create %s: %s", clientTarFilename, err)
}
ctw := tar.NewWriter(clientTarFile)
if err := write(ctw, datadir, false); err != nil {
return fmt.Errorf("cannot write %s: %s", clientTarFilename, err)
}
if err := ctw.Close(); err != nil {
return fmt.Errorf("error closing %s: %s", clientTarFilename, err)
}
// create CLIENT_DATA.tar.gz
clientTarGzFilename := filepath.Join(tmpdir, "CLIENT_DATA.tar.gz")
log.Printf("creating %s from %s\n", clientTarGzFilename, clientTarFilename)
_, err = compress(clientTarGzFilename, clientTarFilename)
if err != nil {
return fmt.Errorf("cannot compress %s into %s: %s", clientTarFilename, clientTarGzFilename, err)
}
// create final OPSI package
m, err := parse(bytes.NewReader(controlfile))
if err != nil {
return fmt.Errorf("error parsing controlfile: %s", err)
}
opsiPath := filepath.Join(into, filename(m))
opsiFile, err := os.Create(opsiPath)
if err != nil {
return fmt.Errorf("error creating %s: %s", opsiPath, err)
}
otw := tar.NewWriter(opsiFile)
if err := write(otw, opsiTarGzFilename, false); err != nil {
return fmt.Errorf("error writing %s to %s: %s", opsiTarGzFilename, opsiPath, err)
}
if err := write(otw, clientTarGzFilename, false); err != nil {
return fmt.Errorf("error writing %s to %s: %s", clientTarGzFilename, opsiPath, err)
}
if err := otw.Close(); err != nil {
return fmt.Errorf("error closing %s: %s", opsiPath, err)
}
log.Printf("created OPSI package %s\n", opsiPath)
return nil
}
// returns list of key value pairs processed so far in case of error
func parseArgs() (Metadata, error) {
m := make(Metadata)
for _, p := range flag.Args() {
parts := strings.Split(p, "=")
if len(parts) != 2 {
return m, fmt.Errorf("want key=value, got %q", p)
}
key := strings.ToLower(strings.TrimSpace(parts[0]))
value := strings.TrimSpace(parts[1])
m[key] = value
}
return m, nil
}
func removeDir(path string) {
if err := os.RemoveAll(path); err != nil {
log.Fatal(fmt.Errorf("cannot remove %s: %s", path, err))
}
}
func resolve(controlfile string, m Metadata) bytes.Buffer {
t := template.New("controlfile")
tmpl, err := t.Parse(controlfile)
if err != nil {
log.Fatalf("error in control file: %s\n", err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, m)
if err != nil {
log.Fatalf("executing control file template: %s\n", err)
}
return buf
}
func tmpDir() (string, error) {
return ioutil.TempDir("", "opsi-")
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: opsi-mkpkg [key1=value1]*\n")
flag.PrintDefaults()
os.Exit(2)
}
func write(tw *tar.Writer, dir string, ignoreControlfile bool) error {
return filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// OPSI uses flat structure only
if info.IsDir() {
return nil
}
if ignoreControlfile && info.Name() == "control" {
log.Printf("skipping control file %s\n", path)
return nil
}
header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}
if err := tw.WriteHeader(header); err != nil {
return err
}
file, err := os.Open(path)
if err != nil {
return err
}
_, err = io.Copy(tw, file)
if err := file.Close(); err != nil {
log.Printf("ignoring error closing file %s: %s\n", path, err)
}
return err
})
}
func writeControlfile(tw *tar.Writer, controlfile []byte) error {
l := len(controlfile)
log.Printf("writing %d bytes controlfile to tar\n", l)
// synthetic, i.e. no underlying file information
hdr := &tar.Header{
Name: "control",
Mode: 0600,
ModTime: time.Now(),
Size: int64(l),
}
if err := tw.WriteHeader(hdr); err != nil {
return fmt.Errorf("cannot append header for control file: %s", err)
}
n, err := tw.Write(controlfile)
if err != nil {
return fmt.Errorf("cannot write content of control file: %s", err)
}
log.Printf("wrote %d bytes\n", n)
return nil
}