forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
77 lines (62 loc) · 2.22 KB
/
reader.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
package stemcell
import (
"github.com/pivotal-golang/yaml"
"path/filepath"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
biproperty "github.com/cloudfoundry/bosh-utils/property"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type manifest struct {
Name string
Version string
OS string `yaml:"operating_system"`
SHA1 string
CloudProperties map[interface{}]interface{} `yaml:"cloud_properties"`
}
// Reader reads a stemcell tarball and returns a stemcell object containing
// parsed information (e.g. version, name)
type Reader interface {
Read(stemcellTarballPath string, extractedPath string) (ExtractedStemcell, error)
}
type reader struct {
compressor boshcmd.Compressor
fs boshsys.FileSystem
}
func NewReader(compressor boshcmd.Compressor, fs boshsys.FileSystem) Reader {
return reader{compressor: compressor, fs: fs}
}
func (s reader) Read(stemcellTarballPath string, extractedPath string) (ExtractedStemcell, error) {
err := s.compressor.DecompressFileToDir(stemcellTarballPath, extractedPath, boshcmd.CompressorOptions{})
if err != nil {
return nil, bosherr.WrapErrorf(err, "Extracting stemcell from '%s' to '%s'", stemcellTarballPath, extractedPath)
}
var rawManifest manifest
manifestPath := filepath.Join(extractedPath, "stemcell.MF")
manifestContents, err := s.fs.ReadFile(manifestPath)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Reading stemcell manifest '%s'", manifestPath)
}
err = yaml.Unmarshal(manifestContents, &rawManifest)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Parsing stemcell manifest: %s", manifestContents)
}
manifest := Manifest{
Name: rawManifest.Name,
Version: rawManifest.Version,
OS: rawManifest.OS,
SHA1: rawManifest.SHA1,
}
cloudProperties, err := biproperty.BuildMap(rawManifest.CloudProperties)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Parsing stemcell cloud_properties: %#v", rawManifest.CloudProperties)
}
manifest.CloudProperties = cloudProperties
manifest.ImagePath = filepath.Join(extractedPath, "image")
stemcell := NewExtractedStemcell(
manifest,
extractedPath,
s.fs,
)
return stemcell, nil
}