Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for static IP #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions builder/xenserver/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,18 @@ func (self *VM) SetStaticMemoryRange(min, max uint) (err error) {
return
}

func (self *VM) ConnectVdi(vdi *VDI, vdiType VDIType) (err error) {
func (self *VM) ConnectVdi(vdi *VDI, vdiType VDIType, userdevice string) (err error) {

// 1. Create a VBD

vbd_rec := make(xmlrpc.Struct)
vbd_rec["VM"] = self.Ref
vbd_rec["VDI"] = vdi.Ref
vbd_rec["userdevice"] = "autodetect"
if (userdevice == nil) {
vbd_rec["userdevice"] = "autodetect"
} else {
vbd_rec["userdevice"] = userdevice
}
vbd_rec["empty"] = false
vbd_rec["other_config"] = make(xmlrpc.Struct)
vbd_rec["qos_algorithm_type"] = ""
Expand Down
8 changes: 6 additions & 2 deletions builder/xenserver/common/common_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type CommonConfig struct {
SSHPassword string `mapstructure:"ssh_password"`
SSHPort uint `mapstructure:"ssh_port"`
SSHUser string `mapstructure:"ssh_username"`
SSHHost string `mapstructure:"ssh_host"`
SSHConfig `mapstructure:",squash"`

RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"`
Expand All @@ -54,6 +55,9 @@ type CommonConfig struct {
Format string `mapstructure:"format"`
KeepVM string `mapstructure:"keep_vm"`
IPGetter string `mapstructure:"ip_getter"`

DiskBSize uint `mapstructure:"diskb_size"`
DiskCSize uint `mapstructure:"diskc_size"`
}

func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error {
Expand Down Expand Up @@ -196,9 +200,9 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig
}

switch c.IPGetter {
case "auto", "tools", "http":
case "auto", "tools", "http", "static":
default:
errs = append(errs, errors.New("ip_getter must be one of 'auto', 'tools', 'http'"))
errs = append(errs, errors.New("ip_getter must be one of 'auto', 'tools', 'http', 'static'"))
}

return errs
Expand Down
1 change: 1 addition & 0 deletions builder/xenserver/common/step_attach_vdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (self *StepAttachVdi) Run(state multistep.StateBag) multistep.StepAction {
}

err = instance.ConnectVdi(self.vdi, self.VdiType, "")
ui.Error(fmt.Sprintf("VDI %s", self.vdi))
if err != nil {
ui.Error(fmt.Sprintf("Error attaching VDI '%s': '%s'", vdiUuid, err.Error()))
return multistep.ActionHalt
Expand Down
6 changes: 6 additions & 0 deletions builder/xenserver/common/step_wait_for_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func (self *StepWaitForIP) Run(state multistep.StateBag) multistep.StepAction {

}

if config.IPGetter == "static" {
ip = config.SSHHost
ui.Message(fmt.Sprintf("Static IP is defined as '%s'", ip))
return true,nil
}

return false, nil
},
}.Wait(state)
Expand Down
2 changes: 2 additions & 0 deletions builder/xenserver/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
VdiUuidKey: "tools_vdi_uuid",
VdiType: xsclient.CD,
},
//Add the extra disks here?
new(stepAddDisks),
new(xscommon.StepStartVmPaused),
new(xscommon.StepGetVNCPort),
&xscommon.StepForwardPortOverSSH{
Expand Down
108 changes: 108 additions & 0 deletions builder/xenserver/iso/step_add_disks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package iso

import (
"fmt"

"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
xsclient "github.com/xenserver/go-xenserver-client"
)

type stepAddDisks struct {
instance *xsclient.VM
vdiB *xsclient.VDI
vdiC *xsclient.VDI
}


func (self *stepAddDisks) Run(state multistep.StateBag) multistep.StepAction {

client := state.Get("client").(xsclient.XenAPIClient)
config := state.Get("config").(config)
ui := state.Get("ui").(packer.Ui)

ui.Say("Step: Add disks")

sr, err := config.GetSR(client)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
return multistep.ActionHalt
}

uuid := state.Get("instance_uuid").(string)
instance, err := client.GetVMByUuid(uuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt
}

// Add additional disks - this is a terrible hack until my golang gets better
if (config.DiskBSize !=0) {
ui.Say(fmt.Sprintf("Creating second disk with size %d", config.DiskBSize))

vdiB, err := sr.CreateVdi("Packer-disk-b", int64(config.DiskBSize*1024*1024))
if err != nil {
ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error()))
}

self.vdiB = vdiB

err = instance.ConnectVdi(vdiB, xsclient.Disk, "")
if err != nil {
ui.Error(fmt.Sprintf("Unable to connect packer disk VDI: %s", err.Error()))
return multistep.ActionHalt
}


}

if (config.DiskCSize !=0) {
ui.Say(fmt.Sprintf("Creating third disk with size %d", config.DiskCSize))

vdiC, err := sr.CreateVdi("Packer-disk-c", int64(config.DiskCSize*1024*1024))
if err != nil {
ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error()))
}

self.vdiC = vdiC

err = instance.ConnectVdi(vdiC, xsclient.Disk, "")
if err != nil {
ui.Error(fmt.Sprintf("Unable to connect packer disk VDI: %s", err.Error()))
return multistep.ActionHalt
}

}

return multistep.ActionContinue
}

func (self *stepAddDisks) Cleanup(state multistep.StateBag) {
config := state.Get("config").(config)
if config.ShouldKeepVM(state) {
return
}

ui := state.Get("ui").(packer.Ui)

ui.Say("Destroying additional disks")


//TODO: learn golang and make this better
if self.vdiB != nil {
ui.Say("Destroying second VDI")
err := self.vdiB.Destroy()
if err != nil {
ui.Error(err.Error())
}
}

if self.vdiC != nil {
ui.Say("Destroying third VDI")
err := self.vdiB.Destroy()
if err != nil {
ui.Error(err.Error())
}
}

}
3 changes: 3 additions & 0 deletions builder/xenserver/iso/step_create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
type stepCreateInstance struct {
instance *xsclient.VM
vdi *xsclient.VDI
vdiB *xsclient.VDI
vdiC *xsclient.VDI
}

func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
Expand Down Expand Up @@ -192,4 +194,5 @@ func (self *stepCreateInstance) Cleanup(state multistep.StateBag) {
ui.Error(err.Error())
}
}

}
4 changes: 4 additions & 0 deletions docs/builders/xenserver-iso.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ each category, the available options are alphabetized and described.
If it doesn't shut down in this time, it is an error. By default, the timeout
is "5m", or five minutes.

* `ssh_host` - The IP address to use for the virtual machine. This is useful for
building in an environment where static IP assignment must be used. The
`ip_getter` option must also be set to `static`.

* `ssh_host_port_min` and `ssh_host_port_max` (integer) - The minimum and
maximum port to use for the SSH port on the host machine which is forwarded
to the SSH port on the guest machine. Because Packer often runs in parallel,
Expand Down