If vhd or vhdx extension is specified for ISOUrls, we want to use an existing hard drive which means that we don't need to specify hard drive size

Filepath.ext includes the dot
This commit is contained in:
Taliesin Sisson 2017-05-29 02:36:01 +01:00 committed by Vijaya Bhaskar Reddy Kondreddi
parent 3d0ac529e0
commit 8232759397
4 changed files with 96 additions and 11 deletions

View File

@ -40,7 +40,7 @@ func (s *StepCloneVM) Run(state multistep.StateBag) multistep.StepAction {
harddrivePath := "" harddrivePath := ""
if harddrivePathRaw, ok := state.GetOk("iso_path"); ok { if harddrivePathRaw, ok := state.GetOk("iso_path"); ok {
extension := strings.ToLower(filepath.Ext(harddrivePathRaw.(string))) extension := strings.ToLower(filepath.Ext(harddrivePathRaw.(string)))
if extension == "vhd" || extension == "vhdx" { if extension == ".vhd" || extension == ".vhdx" {
harddrivePath = harddrivePathRaw.(string) harddrivePath = harddrivePathRaw.(string)
} else { } else {
log.Println("No existing virtual harddrive, not attaching.") log.Println("No existing virtual harddrive, not attaching.")

View File

@ -39,7 +39,7 @@ func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
harddrivePath := "" harddrivePath := ""
if harddrivePathRaw, ok := state.GetOk("iso_path"); ok { if harddrivePathRaw, ok := state.GetOk("iso_path"); ok {
extension := strings.ToLower(filepath.Ext(harddrivePathRaw.(string))) extension := strings.ToLower(filepath.Ext(harddrivePathRaw.(string)))
if extension == "vhd" || extension == "vhdx" { if extension == ".vhd" || extension == ".vhdx" {
harddrivePath = harddrivePathRaw.(string) harddrivePath = harddrivePathRaw.(string)
} else { } else {
log.Println("No existing virtual harddrive, not attaching.") log.Println("No existing virtual harddrive, not attaching.")

View File

@ -117,22 +117,15 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx) isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
warnings = append(warnings, isoWarnings...) warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...) errs = packer.MultiErrorAppend(errs, isoErrs...)
if len(b.config.ISOConfig.ISOUrls) > 0 {
extension := strings.ToLower(filepath.Ext(b.config.ISOConfig.ISOUrls[0]))
if extension == "vhd" || extension == "vhdx" {
b.config.ISOConfig.TargetExtension = extension
}
}
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...) errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...)
if b.config.ISOConfig.TargetExtension != "vhd" && b.config.ISOConfig.TargetExtension != "vhdx" { if len(b.config.ISOConfig.ISOUrls) < 1 || (strings.ToLower(filepath.Ext(b.config.ISOConfig.ISOUrls[0])) != ".vhd" && strings.ToLower(filepath.Ext(b.config.ISOConfig.ISOUrls[0])) != ".vhdx") {
//We only create a new hard drive if an existing one to copy from does not exist //We only create a new hard drive if an existing one to copy from does not exist
err = b.checkDiskSize() err = b.checkDiskSize()
if err != nil { if err != nil {

View File

@ -300,6 +300,98 @@ func TestBuilderPrepare_ISOUrl(t *testing.T) {
} }
} }
func TestBuilderPrepare_SizeNotRequiredWhenUsingExistingHarddrive(t *testing.T) {
var b Builder
config := testConfig()
delete(config, "iso_url")
delete(config, "iso_urls")
delete(config, "disk_size")
config["disk_size"] = 1
// Test just iso_urls set but with vhdx
delete(config, "iso_url")
config["iso_urls"] = []string{
"http://www.packer.io/hdd.vhdx",
"http://www.hashicorp.com/dvd.iso",
}
b = Builder{}
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Errorf("should not have error: %s", err)
}
expected := []string{
"http://www.packer.io/hdd.vhdx",
"http://www.hashicorp.com/dvd.iso",
}
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
t.Fatalf("bad: %#v", b.config.ISOUrls)
}
// Test just iso_urls set but with vhd
delete(config, "iso_url")
config["iso_urls"] = []string{
"http://www.packer.io/hdd.vhd",
"http://www.hashicorp.com/dvd.iso",
}
b = Builder{}
warns, err = b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Errorf("should not have error: %s", err)
}
expected = []string{
"http://www.packer.io/hdd.vhd",
"http://www.hashicorp.com/dvd.iso",
}
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
t.Fatalf("bad: %#v", b.config.ISOUrls)
}
}
func TestBuilderPrepare_SizeIsRequiredWhenNotUsingExistingHarddrive(t *testing.T) {
var b Builder
config := testConfig()
delete(config, "iso_url")
delete(config, "iso_urls")
delete(config, "disk_size")
config["disk_size"] = 1
// Test just iso_urls set but with vhdx
delete(config, "iso_url")
config["iso_urls"] = []string{
"http://www.packer.io/os.iso",
"http://www.hashicorp.com/dvd.iso",
}
b = Builder{}
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Errorf("should have error")
}
expected := []string{
"http://www.packer.io/os.iso",
"http://www.hashicorp.com/dvd.iso",
}
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
t.Fatalf("bad: %#v", b.config.ISOUrls)
}
}
func TestBuilderPrepare_FloppyFiles(t *testing.T) { func TestBuilderPrepare_FloppyFiles(t *testing.T) {
var b Builder var b Builder
config := testConfig() config := testConfig()