Skip to content

Commit

Permalink
Refactor http server config into common
Browse files Browse the repository at this point in the history
  • Loading branch information
markpeek committed Nov 1, 2015
1 parent 31dd989 commit 7f149e5
Show file tree
Hide file tree
Showing 21 changed files with 105 additions and 536 deletions.
24 changes: 7 additions & 17 deletions builder/parallels/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Builder struct {

type Config struct {
common.PackerConfig `mapstructure:",squash"`
common.HTTPConfig `mapstructure:",squash"`
common.ISOConfig `mapstructure:",squash"`
parallelscommon.FloppyConfig `mapstructure:",squash"`
parallelscommon.OutputConfig `mapstructure:",squash"`
Expand All @@ -39,9 +40,6 @@ type Config struct {
GuestOSType string `mapstructure:"guest_os_type"`
HardDriveInterface string `mapstructure:"hard_drive_interface"`
HostInterfaces []string `mapstructure:"host_interfaces"`
HTTPDir string `mapstructure:"http_directory"`
HTTPPortMin uint `mapstructure:"http_port_min"`
HTTPPortMax uint `mapstructure:"http_port_max"`
SkipCompaction bool `mapstructure:"skip_compaction"`
VMName string `mapstructure:"vm_name"`

Expand Down Expand Up @@ -77,6 +75,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...)

errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(
errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
Expand Down Expand Up @@ -110,14 +109,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
"Run it to see all available values: `prlctl create x -d list` ")
}

if b.config.HTTPPortMin == 0 {
b.config.HTTPPortMin = 8000
}

if b.config.HTTPPortMax == 0 {
b.config.HTTPPortMax = 9000
}

if len(b.config.HostInterfaces) == 0 {
b.config.HostInterfaces = []string{"en0", "en1", "en2", "en3", "en4", "en5", "en6", "en7",
"en8", "en9", "ppp0", "ppp1", "ppp2"}
Expand All @@ -132,11 +123,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs, errors.New("hard_drive_interface can only be ide, sata, or scsi"))
}

if b.config.HTTPPortMin > b.config.HTTPPortMax {
errs = packer.MultiErrorAppend(
errs, errors.New("http_port_min must be less than http_port_max"))
}

// Warnings
if b.config.ShutdownCommand == "" {
warnings = append(warnings,
Expand Down Expand Up @@ -185,7 +171,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&common.StepCreateFloppy{
Files: b.config.FloppyFiles,
},
new(stepHTTPServer),
&common.StepHTTPServer{
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax,
},
new(stepCreateVM),
new(stepCreateDisk),
new(stepSetBootOrder),
Expand Down
39 changes: 0 additions & 39 deletions builder/parallels/iso/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,45 +138,6 @@ func TestBuilderPrepare_HardDriveInterface(t *testing.T) {
}
}

func TestBuilderPrepare_HTTPPort(t *testing.T) {
var b Builder
config := testConfig()

// Bad
config["http_port_min"] = 1000
config["http_port_max"] = 500
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatal("should have error")
}

// Bad
config["http_port_min"] = -500
b = Builder{}
warns, err = b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatal("should have error")
}

// Good
config["http_port_min"] = 500
config["http_port_max"] = 1000
b = Builder{}
warns, err = b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}
}

func TestBuilderPrepare_InvalidKey(t *testing.T) {
var b Builder
config := testConfig()
Expand Down
76 changes: 0 additions & 76 deletions builder/parallels/iso/step_http_server.go

This file was deleted.

24 changes: 7 additions & 17 deletions builder/qemu/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Builder struct {

type Config struct {
common.PackerConfig `mapstructure:",squash"`
common.HTTPConfig `mapstructure:",squash"`
common.ISOConfig `mapstructure:",squash"`
Comm communicator.Config `mapstructure:",squash"`

Expand All @@ -94,9 +95,6 @@ type Config struct {
Format string `mapstructure:"format"`
Headless bool `mapstructure:"headless"`
DiskImage bool `mapstructure:"disk_image"`
HTTPDir string `mapstructure:"http_directory"`
HTTPPortMin uint `mapstructure:"http_port_min"`
HTTPPortMax uint `mapstructure:"http_port_max"`
MachineType string `mapstructure:"machine_type"`
NetDevice string `mapstructure:"net_device"`
OutputDir string `mapstructure:"output_directory"`
Expand Down Expand Up @@ -160,14 +158,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
}
}

if b.config.HTTPPortMin == 0 {
b.config.HTTPPortMin = 8000
}

if b.config.HTTPPortMax == 0 {
b.config.HTTPPortMax = 9000
}

if b.config.MachineType == "" {
b.config.MachineType = "pc"
}
Expand Down Expand Up @@ -235,6 +225,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
warnings = append(warnings, isoWarnings...)
errs = packer.MultiErrorAppend(errs, isoErrs...)

errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
if es := b.config.Comm.Prepare(&b.config.ctx); len(es) > 0 {
errs = packer.MultiErrorAppend(errs, es...)
}
Expand Down Expand Up @@ -274,11 +265,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs, errors.New("unrecognized disk cache type"))
}

if b.config.HTTPPortMin > b.config.HTTPPortMax {
errs = packer.MultiErrorAppend(
errs, errors.New("http_port_min must be less than http_port_max"))
}

if !b.config.PackerForce {
if _, err := os.Stat(b.config.OutputDir); err == nil {
errs = packer.MultiErrorAppend(
Expand Down Expand Up @@ -357,7 +343,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
new(stepCreateDisk),
new(stepCopyDisk),
new(stepResizeDisk),
new(stepHTTPServer),
&common.StepHTTPServer{
HTTPDir: b.config.HTTPDir,
HTTPPortMin: b.config.HTTPPortMin,
HTTPPortMax: b.config.HTTPPortMax,
},
new(stepForwardSSH),
new(stepConfigureVNC),
steprun,
Expand Down
39 changes: 0 additions & 39 deletions builder/qemu/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,45 +206,6 @@ func TestBuilderPrepare_DiskSize(t *testing.T) {
}
}

func TestBuilderPrepare_HTTPPort(t *testing.T) {
var b Builder
config := testConfig()

// Bad
config["http_port_min"] = 1000
config["http_port_max"] = 500
warns, err := b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatal("should have error")
}

// Bad
config["http_port_min"] = -500
b = Builder{}
warns, err = b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err == nil {
t.Fatal("should have error")
}

// Good
config["http_port_min"] = 500
config["http_port_max"] = 1000
b = Builder{}
warns, err = b.Prepare(config)
if len(warns) > 0 {
t.Fatalf("bad: %#v", warns)
}
if err != nil {
t.Fatalf("should not have error: %s", err)
}
}

func TestBuilderPrepare_Format(t *testing.T) {
var b Builder
config := testConfig()
Expand Down
76 changes: 0 additions & 76 deletions builder/qemu/step_http_server.go

This file was deleted.

Loading

0 comments on commit 7f149e5

Please sign in to comment.