-
Notifications
You must be signed in to change notification settings - Fork 111
/
unpack.go
260 lines (224 loc) · 6.69 KB
/
unpack.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
package airgap
import (
"archive/tar"
"bufio"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/chef/automate/lib/io/fileutils"
"github.com/pkg/errors"
)
// InstallBundleFileTypeFlag signifies which type of file an item in the install
// bundle is
type InstallBundleFileTypeFlag byte
const (
// Hartifact signifies that the file is a .hart
Hartifact InstallBundleFileTypeFlag = iota
// OriginKey signifies the file is one of the public origin keys
OriginKey
// HabBin signifies the file is the hab executable
HabBin
// Manifest signifies the file is the manifest.json
Manifest
)
// InstallBundleItem represents an item in the install bundle
type InstallBundleItem struct {
Typeflag InstallBundleFileTypeFlag
Name string
FileInfo os.FileInfo
}
// ReadVisitFunc is the func called for each item in the install bundle. The
// receiver may choose to ignore it or read from the provided reader
type ReadVisitFunc func(InstallBundleItem, io.Reader) error
// Read reads an install bundle. It calls the provided ReadVisitFunc for each
// item in the install bundle.
func Read(installBundlePath string, f ReadVisitFunc) error {
installBundleFile, err := os.Open(installBundlePath)
if err != nil {
return errors.Wrapf(err, "Failed to open install bundle file %s", installBundlePath)
}
defer installBundleFile.Close() // nolint: errcheck
bufReader := bufio.NewReader(installBundleFile)
// Read the header:
// AIB-1\n\n
s, err := bufReader.ReadString('\n')
if err != nil {
return errors.Wrap(err, "Could not read artifact file")
}
if s != "AIB-1\n" {
return errors.New("Malformed install bundle file")
}
s, err = bufReader.ReadString('\n')
if err != nil {
return err
}
if s != "\n" {
return errors.New("Malformed install bundle file")
}
reader := tar.NewReader(bufReader)
for {
header, err := reader.Next()
if err != nil {
if err == io.EOF {
break
}
return errors.Wrap(err, "Could not read archive")
}
installBundleItem := InstallBundleItem{}
if header.Typeflag != tar.TypeReg {
continue
}
if header.Name == "manifest.json" {
installBundleItem.Typeflag = Manifest
} else if header.Name == "bin/hab" {
installBundleItem.Typeflag = HabBin
} else if strings.HasPrefix(header.Name, "hab/cache/artifacts/") {
installBundleItem.Typeflag = Hartifact
} else if strings.HasPrefix(header.Name, "hab/cache/keys/") {
installBundleItem.Typeflag = OriginKey
} else {
continue
}
installBundleItem.Name = filepath.Base(header.Name)
installBundleItem.FileInfo = header.FileInfo()
err = f(installBundleItem, reader)
if err != nil {
return err
}
}
return nil
}
// UnpackMetadata is the result of unpacking an install bundle. It contains the paths
// to the files that were unpacked
type UnpackMetadata struct {
HartifactPaths []string
OriginKeyPaths []string
ManifestPath string
HabBinPath string
}
type unpackOpts struct {
root string
hartsOnly bool
}
// UnpackOpt are functional options for Unpack
type UnpackOpt func(*unpackOpts)
// WithUnpackRoot sets the root directory for unpacking. By default,
// it is /
func WithUnpackRoot(root string) UnpackOpt {
return func(opts *unpackOpts) {
opts.root = root
}
}
// WithUnpackHartsOnly only unpacks the hartifacts and their keys.
func WithUnpackHartsOnly(hartsOnly bool) UnpackOpt {
return func(opts *unpackOpts) {
opts.hartsOnly = hartsOnly
}
}
// Unpack puts the files in the given install bundle at the right place on disk
func Unpack(installBundlePath string, opts ...UnpackOpt) (UnpackMetadata, error) {
conf := unpackOpts{
root: "/",
}
for _, o := range opts {
o(&conf)
}
metadata := UnpackMetadata{
HartifactPaths: []string{},
OriginKeyPaths: []string{},
}
hartsDir := path.Join(conf.root, "hab/cache/artifacts")
keysDir := path.Join(conf.root, "hab/cache/keys")
manifestDir := path.Join(conf.root, "hab/svc/deployment-service/data/airgap")
tmpDir := path.Join(conf.root, "hab/tmp")
requiredDirectories := []string{
hartsDir,
keysDir,
}
if !conf.hartsOnly {
requiredDirectories = append(requiredDirectories,
manifestDir,
tmpDir,
)
}
for _, d := range requiredDirectories {
err := os.MkdirAll(d, 0755)
if err != nil {
return metadata, errors.Wrapf(err, "Could not create directory %s", d)
}
}
err := Read(installBundlePath, func(item InstallBundleItem, reader io.Reader) error {
if conf.hartsOnly && !(item.Typeflag == Hartifact || item.Typeflag == OriginKey) {
return nil
}
switch item.Typeflag {
case Hartifact:
hartPath := path.Join(hartsDir, item.Name)
metadata.HartifactPaths = append(metadata.HartifactPaths, hartPath)
err := fileutils.AtomicWrite(hartPath, reader)
if err != nil {
return errors.Wrapf(err, "Could not write hartifact %s", item.Name)
}
case OriginKey:
keyPath := path.Join(keysDir, item.Name)
metadata.OriginKeyPaths = append(metadata.OriginKeyPaths, keyPath)
err := fileutils.AtomicWrite(keyPath, reader)
if err != nil {
return errors.Wrapf(err, "Could not write origin key %s", item.Name)
}
case HabBin:
// Unpack to a directory other than /hab/bin to avoid binlinking
// issues in the studio.
metadata.HabBinPath = path.Join(tmpDir, "hab")
err := fileutils.AtomicWrite(metadata.HabBinPath, reader, fileutils.WithAtomicWriteFileMode(0755))
if err != nil {
return errors.Wrapf(err, "Could not write hab binary")
}
case Manifest:
metadata.ManifestPath = path.Join(manifestDir, "manifest.json")
err := fileutils.AtomicWrite(metadata.ManifestPath, reader)
if err != nil {
return errors.Wrap(err, "Could not write manifest")
}
}
return nil
})
return metadata, err
}
func GetMetadata(installBundlePath string) (UnpackMetadata, []byte, error) {
metadata := UnpackMetadata{
HartifactPaths: []string{},
OriginKeyPaths: []string{},
}
var (
hartsDir = "/hab/cache/artifacts"
keysDir = "/hab/cache/keys"
manifestDir = "/hab/svc/deployment-service/data/airgap"
tmpDir = "/hab/tmp"
)
manifestBytes := []byte{}
err := Read(installBundlePath, func(item InstallBundleItem, reader io.Reader) error {
switch item.Typeflag {
case Hartifact:
hartPath := path.Join(hartsDir, item.Name)
metadata.HartifactPaths = append(metadata.HartifactPaths, hartPath)
case OriginKey:
keyPath := path.Join(keysDir, item.Name)
metadata.OriginKeyPaths = append(metadata.OriginKeyPaths, keyPath)
case HabBin:
metadata.HabBinPath = path.Join(tmpDir, "hab")
case Manifest:
var err error
metadata.ManifestPath = path.Join(manifestDir, "manifest.json")
manifestBytes, err = ioutil.ReadAll(reader)
if err != nil {
return errors.Wrap(err, "Could not read manifest")
}
}
return nil
})
return metadata, manifestBytes, err
}