forked from cloudfoundry/bosh-cli
/
stemcell.go
130 lines (107 loc) · 3.1 KB
/
stemcell.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
package stemcell
import (
"fmt"
boshfu "github.com/cloudfoundry/bosh-utils/fileutil"
biproperty "github.com/cloudfoundry/bosh-utils/property"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"path/filepath"
yaml "gopkg.in/yaml.v2"
)
type ExtractedStemcell interface {
Manifest() Manifest
Cleanup() error
OsAndVersion() string
SetName(string)
SetVersion(string)
SetFormat([]string)
SetCloudProperties(biproperty.Map)
GetExtractedPath() string
Pack(string) error
EmptyImage() error
fmt.Stringer
}
type extractedStemcell struct {
manifest Manifest
extractedPath string
compressor boshfu.Compressor
fs boshsys.FileSystem
}
type Manifest struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
OS string `yaml:"operating_system"`
SHA1 string `yaml:"sha1"`
BoshProtocol string `yaml:"bosh_protocol"`
StemcellFormats []string `yaml:"stemcell_formats,omitempty"`
ApiVersion int `yaml:"api_version,omitempty"`
CloudProperties biproperty.Map `yaml:"cloud_properties"`
}
func NewExtractedStemcell(
manifest Manifest,
extractedPath string,
compressor boshfu.Compressor,
fs boshsys.FileSystem,
) ExtractedStemcell {
return &extractedStemcell{
manifest: manifest,
extractedPath: extractedPath,
compressor: compressor,
fs: fs,
}
}
func (s *extractedStemcell) Manifest() Manifest { return s.manifest }
func (s *extractedStemcell) Cleanup() error {
return s.fs.RemoveAll(s.extractedPath)
}
func (s *extractedStemcell) String() string {
return fmt.Sprintf("ExtractedStemcell{name=%s version=%s}", s.manifest.Name, s.manifest.Version)
}
func (s *extractedStemcell) OsAndVersion() string {
return fmt.Sprintf("%s/%s", s.manifest.OS, s.manifest.Version)
}
func (s *extractedStemcell) SetName(newName string) {
s.manifest.Name = newName
}
func (s *extractedStemcell) SetVersion(newVersion string) {
s.manifest.Version = newVersion
}
func (s *extractedStemcell) SetFormat(newFormats []string) {
s.manifest.StemcellFormats = newFormats
}
func (s *extractedStemcell) SetCloudProperties(newCloudProperties biproperty.Map) {
for key, value := range newCloudProperties {
s.manifest.CloudProperties[key] = value
}
}
func (s *extractedStemcell) Pack(destinationPath string) error {
defer s.Cleanup()
err := s.save()
if err != nil {
return err
}
intermediateStemcellPath, err := s.compressor.CompressFilesInDir(s.extractedPath)
if err != nil {
return err
}
return boshfu.NewFileMover(s.fs).Move(intermediateStemcellPath, destinationPath)
}
func (s *extractedStemcell) EmptyImage() error {
imagePath := filepath.Join(s.extractedPath, "image")
err := s.fs.WriteFile(imagePath, []byte{})
if err != nil {
return err
}
return nil
}
func (s *extractedStemcell) GetExtractedPath() string {
return s.extractedPath
}
func (s *extractedStemcell) save() error {
stemcellMfPath := filepath.Join(s.extractedPath, "stemcell.MF")
contents, _ := yaml.Marshal(s.manifest)
err := s.fs.WriteFile(stemcellMfPath, contents)
if err != nil {
return err
}
return nil
}