Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 7b0e874

Browse files
Natalie ArellanoCharlie Vieth
authored andcommitted
Make OS version configurable
This commit adds an optional 'os' flag to set the os version to either 2012R2 or 2016. [#146394549] Signed-off-by: Charlie Vieth <cviethjr@pivotal.io>
1 parent 0e76951 commit 7b0e874

File tree

2 files changed

+93
-15
lines changed

2 files changed

+93
-15
lines changed

main.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ import (
2222
"github.com/pivotal-cf-experimental/pcf-make-stemcell/rdiff"
2323
)
2424

25+
const DefaultOSVersion = "2012R2"
26+
2527
var (
2628
Version string
2729
OutputDir string
2830
OvaFile string
2931
OvfDir string
3032
VHDFile string
3133
VMDKFile string
34+
OSVersion string
3235
DeltaFile string
3336
EnableDebug bool
3437
DebugColor bool
@@ -37,8 +40,9 @@ var (
3740
var Debugf = func(format string, a ...interface{}) {}
3841

3942
const UsageMessage = `
40-
Usage %[1]s: [OPTIONS...] [-VMDK FILENAME] [[-VHD FILENAME] [-DELTA FILENAME]]
41-
[-OUTPUT DIRNAME] [-VERSION version]
43+
Usage %[1]s: [OPTIONS...] [-VMDK FILENAME] [[-VHD FILENAME]
44+
%[2]s [-DELTA FILENAME]] [-OUTPUT DIRNAME]
45+
%[2]s [-VERSION STEMCELL_VERSION] [-OS OS_VERSION]
4246
4347
Creates a BOSH stemcell from a VHD and DELTA (patch) file.
4448
@@ -77,17 +81,24 @@ Flags:
7781

7882
func init() {
7983
flag.Usage = func() {
80-
fmt.Fprintf(os.Stderr, UsageMessage, filepath.Base(os.Args[0]))
84+
exe := filepath.Base(os.Args[0])
85+
pad := strings.Repeat(" ", len(exe))
86+
fmt.Fprintf(os.Stderr, UsageMessage, exe, pad)
8187
flag.PrintDefaults()
8288
}
8389

8490
flag.StringVar(&VHDFile, "vhd", "", "VHD file to patch")
8591
flag.StringVar(&VMDKFile, "vmdk", "", "VMDK file to create stemcell from")
8692

87-
flag.StringVar(&DeltaFile, "delta", "", "Patch file that will be applied to the VHD")
93+
flag.StringVar(&DeltaFile, "delta", "",
94+
"Patch file that will be applied to the VHD")
8895
flag.StringVar(&DeltaFile, "d", "", "Patch file (shorthand)")
8996

90-
flag.StringVar(&Version, "version", "", "Stemcell version in the form of [DIGITS].[DIGITS] (e.x. 123.01)")
97+
flag.StringVar(&OSVersion, "os", DefaultOSVersion,
98+
"OS version must be either 2012R2 or 2016")
99+
100+
flag.StringVar(&Version, "version", "",
101+
"Stemcell version in the form of [DIGITS].[DIGITS] (e.x. 123.01)")
91102
flag.StringVar(&Version, "v", "", "Stemcell version (shorthand)")
92103

93104
flag.StringVar(&OutputDir, "output", "",
@@ -169,12 +180,20 @@ func ValidateFlags() []error {
169180
add(fmt.Errorf("output argument (%s): is not a directory\n", OutputDir))
170181
}
171182

172-
Debugf("validating version string: %s", Version)
183+
Debugf("validating stemcell version string: %s", Version)
173184
if err := validateVersion(Version); err != nil {
174185
add(err)
175186
}
176187

177-
name := filepath.Join(OutputDir, StemcellFilename(Version))
188+
Debugf("validating OS version: %s", OSVersion)
189+
switch OSVersion {
190+
case "2012R2", "2016":
191+
// Ok
192+
default:
193+
add(fmt.Errorf("OS version must be either 2012R2 or 2016 have: %s", OSVersion))
194+
}
195+
196+
name := filepath.Join(OutputDir, StemcellFilename(Version, OSVersion))
178197
Debugf("validating that stemcell filename (%s) does not exist", name)
179198
if _, err := os.Stat(name); !os.IsNotExist(err) {
180199
add(fmt.Errorf("file (%s) already exists - refusing to overwrite", name))
@@ -196,8 +215,9 @@ func validateVersion(s string) error {
196215
return nil
197216
}
198217

199-
func StemcellFilename(version string) string {
200-
return fmt.Sprintf("bosh-stemcell-%s-vsphere-esxi-windows2012R2-go_agent.tgz", version)
218+
func StemcellFilename(version, os string) string {
219+
return fmt.Sprintf("bosh-stemcell-%s-vsphere-esxi-windows%s-go_agent.tgz",
220+
version, os)
201221
}
202222

203223
var ErrInterupt = errors.New("interupt")
@@ -346,7 +366,7 @@ func (c *Config) CreateStemcell() error {
346366
return err
347367
}
348368

349-
c.Stemcell = filepath.Join(tmpdir, StemcellFilename(Version))
369+
c.Stemcell = filepath.Join(tmpdir, StemcellFilename(Version, OSVersion))
350370
stemcell, err := os.OpenFile(c.Stemcell, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
351371
if err != nil {
352372
return err
@@ -389,10 +409,10 @@ func (c *Config) CreateStemcell() error {
389409

390410
func (c *Config) WriteManifest() error {
391411
const format = `---
392-
name: bosh-vsphere-esxi-windows2012R2-go_agent
393-
version: %s
394-
sha1: %s
395-
operating_system: windows2012R2
412+
name: bosh-vsphere-esxi-windows%[1]s-go_agent
413+
version: %[2]s
414+
sha1: %[3]s
415+
operating_system: windows%[1]s
396416
cloud_properties:
397417
infrastructure: vsphere
398418
hypervisor: esxi
@@ -416,7 +436,7 @@ cloud_properties:
416436
defer f.Close()
417437
Debugf("created temp stemcell.MF file: %s", c.Manifest)
418438

419-
if _, err := fmt.Fprintf(f, format, Version, c.Sha1sum); err != nil {
439+
if _, err := fmt.Fprintf(f, format, OSVersion, Version, c.Sha1sum); err != nil {
420440
os.Remove(c.Manifest)
421441
return fmt.Errorf("writing stemcell.MF (%s): %s", c.Manifest, err)
422442
}
@@ -554,6 +574,8 @@ func ParseFlags() error {
554574
OutputDir = wd
555575
}
556576

577+
OSVersion = strings.ToUpper(OSVersion)
578+
557579
return nil
558580
}
559581

main_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,62 @@ func readdirnames(dirname string) ([]string, error) {
5757
return names, nil
5858
}
5959

60+
func TestOsVersion(t *testing.T) {
61+
const archive = "testdata/patch-test.tar.gz"
62+
63+
// Reset Version and OSVersion on exit
64+
oldVersion := Version
65+
oldOSVersion := OSVersion
66+
defer func() {
67+
Version = oldVersion
68+
OSVersion = oldOSVersion
69+
}()
70+
Version = "9000"
71+
OSVersion = "2016"
72+
73+
var err error
74+
OutputDir, err = TempDir("test-")
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
defer os.RemoveAll(OutputDir)
79+
80+
dirname := extractGzipArchive(t, archive)
81+
defer os.RemoveAll(dirname)
82+
vmdkPath := filepath.Join(dirname, "expected.vmdk")
83+
84+
conf := Config{stop: make(chan struct{})}
85+
86+
realMain(&conf, vmdkPath, "", "")
87+
88+
// assertions
89+
stemcellFilename := filepath.Base(conf.Stemcell)
90+
stemcellDirname := extractGzipArchive(t, filepath.Join(OutputDir, stemcellFilename))
91+
manifestFilepath := filepath.Join(stemcellDirname, "stemcell.MF")
92+
93+
manifest, err := readFile(manifestFilepath)
94+
if err != nil {
95+
t.Fatal(err)
96+
}
97+
98+
expectedOs := fmt.Sprintf("operating_system: windows%s", OSVersion)
99+
if !strings.Contains(manifest, expectedOs) {
100+
t.Errorf("TestOSVerson: stemcell.MF expected os: %s\n%s\n",
101+
expectedOs, manifest)
102+
}
103+
104+
expectedName := fmt.Sprintf("name: bosh-vsphere-esxi-windows%s-go_agent", OSVersion)
105+
if !strings.Contains(manifest, expectedName) {
106+
t.Errorf("TestOSVerson: stemcell.MF expected stemcell filename: %s\n%s\n",
107+
expectedName, manifest)
108+
}
109+
110+
if !strings.Contains(stemcellFilename, OSVersion) {
111+
t.Errorf("TestOSVerson: expected filename: %s got: %s",
112+
OSVersion, stemcellFilename)
113+
}
114+
}
115+
60116
func TestExtractOVA_Valid(t *testing.T) {
61117
const Count = 9
62118
const NameFmt = "file-%d"

0 commit comments

Comments
 (0)