CP-18743: Make packer-build-xenserver support VIF configuration

Signed-off-by: kunm <kun.ma@citrix.com>
This commit is contained in:
kunm 2016-09-01 23:15:08 +08:00
parent 2183cb5869
commit d1de461cdb
4 changed files with 41 additions and 31 deletions

View File

@ -18,11 +18,11 @@ type CommonConfig struct {
Password string `mapstructure:"remote_password"`
HostIp string `mapstructure:"remote_host"`
VMName string `mapstructure:"vm_name"`
VMDescription string `mapstructure:"vm_description"`
SrName string `mapstructure:"sr_name"`
FloppyFiles []string `mapstructure:"floppy_files"`
NetworkName string `mapstructure:"network_name"`
VMName string `mapstructure:"vm_name"`
VMDescription string `mapstructure:"vm_description"`
SrName string `mapstructure:"sr_name"`
FloppyFiles []string `mapstructure:"floppy_files"`
NetworkNames map[string]string `mapstructure:"network_names"`
HostPortMin uint `mapstructure:"host_port_min"`
HostPortMax uint `mapstructure:"host_port_max"`

View File

@ -100,7 +100,6 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, retErr error
templates := map[string]*string{
"clone_template": &self.config.CloneTemplate,
"network_name": &self.config.NetworkName,
"iso_checksum": &self.config.ISOChecksum,
"iso_checksum_type": &self.config.ISOChecksumType,
"iso_url": &self.config.ISOUrl,

View File

@ -109,7 +109,7 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
var network *xsclient.Network
if config.NetworkName == "" {
if len(config.NetworkNames) == 0 {
// No network has be specified. Use the management interface
network = new(xsclient.Network)
network.Ref = ""
@ -141,35 +141,39 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi
return multistep.ActionHalt
}
} else {
// Look up the network by it's name label
networks, err := client.GetNetworkByNameLabel(config.NetworkName)
_, err = instance.ConnectNetwork(network, "0")
if err != nil {
ui.Error(fmt.Sprintf("Error occured getting Network by name-label: %s", err.Error()))
return multistep.ActionHalt
ui.Say(err.Error())
}
switch {
case len(networks) == 0:
ui.Error(fmt.Sprintf("Couldn't find a network with the specified name-label '%s'. Aborting.", config.NetworkName))
return multistep.ActionHalt
case len(networks) > 1:
ui.Error(fmt.Sprintf("Found more than one network with the name '%s'. The name must be unique. Aborting.", config.NetworkName))
return multistep.ActionHalt
} else {
// Look up each network by it's name label
for i, networkNameLabel := range config.NetworkNames {
networks, err := client.GetNetworkByNameLabel(networkNameLabel)
if err != nil {
ui.Error(fmt.Sprintf("Error occured getting Network by name-label: %s", err.Error()))
return multistep.ActionHalt
}
switch {
case len(networks) == 0:
ui.Error(fmt.Sprintf("Couldn't find a network with the specified name-label '%s'. Aborting.", networkNameLabel))
return multistep.ActionHalt
case len(networks) > 1:
ui.Error(fmt.Sprintf("Found more than one network with the name '%s'. The name must be unique. Aborting.", networkNameLabel))
return multistep.ActionHalt
}
//we need the VIF index string
vifIndexString := fmt.Sprintf("%d", i)
_, err = instance.ConnectNetwork(networks[0], vifIndexString)
if err != nil {
ui.Say(err.Error())
}
}
network = networks[0]
}
if err != nil {
ui.Say(err.Error())
}
_, err = instance.ConnectNetwork(network, "0")
if err != nil {
ui.Say(err.Error())
}
instanceId, err := instance.GetUuid()

View File

@ -15,6 +15,13 @@
"iso_checksum": "4ed6c56d365bd3ab12cd88b8a480f4a62e7c66d2",
"iso_checksum_type": "sha1",
"iso_url": "{{user `mirror`}}/6.6/isos/x86_64/CentOS-6.6-x86_64-minimal.iso",
"vm_other_config": {
"conversionvm":"true"
},
"network_names": [
"Host internal management network",
"Pool-wide network associated with eth0"
],
"output_directory": "packer-centos-6.6-x86_64-xenserver",
"shutdown_command": "/sbin/halt",
"ssh_username": "root",