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

Added patch to build.sh to fix missing go-vnc, updated README #50

Closed
wants to merge 17 commits into from
Closed
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
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ You can check out packer [here](https://packer.io).
## Dependencies
* Packer >= 0.7.2 (https://packer.io)
* XenServer > 6.2 (http://xenserver.org)
* Golang (tested with 1.2.1)

* Golang >= 1.6; < 2.0

## Install Go

Expand All @@ -20,21 +19,15 @@ Follow these instructions and install golang on your system:

## Install Packer

Clone the Packer repository:

```shell
git clone https://github.com/mitchellh/packer.git
```

Then follow the [instructions to build and install a development version of Packer](https://github.com/mitchellh/packer#developing-packer).
Follow the [instructions to build and install a development version of Packer](https://github.com/mitchellh/packer/blob/master/CONTRIBUTING.md#setting-up-go-to-work-on-packer).

## Compile the plugin

Once you have installed Packer, you must compile this plugin and install the
resulting binary.

```shell
cd $GOROOT
cd $GOPATH
mkdir -p src/github.com/xenserver/
cd src/github.com/xenserver
git clone https://github.com/xenserver/packer-builder-xenserver.git
Expand Down Expand Up @@ -102,4 +95,4 @@ packer build centos-6.6.json
# Documentation

For complete documentation on configuration commands, see either [the
xenserver-iso docs](https://github.com/rdobson/packer-builder-xenserver/blob/master/docs/builders/xenserver-iso.html.markdown) or [the xenserver-xva docs](https://github.com/rdobson/packer-builder-xenserver/blob/master/docs/builders/xenserver-xva.html.markdown).
xenserver-iso docs](https://github.com/xenserver/packer-builder-xenserver/blob/master/docs/builders/xenserver-iso.html.markdown) or [the xenserver-xva docs](https://github.com/xenserver/packer-builder-xenserver/blob/master/docs/builders/xenserver-xva.html.markdown).
3 changes: 3 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ rm -rf pkg/*
rm -rf $GOPATH/pkg/*
mkdir -p bin/

# Fix for build failing due to missing go-vnc
go get github.com/mitchellh/go-vnc

gox \
-os="${XC_OS}" \
-arch="${XC_ARCH}" \
Expand Down
14 changes: 8 additions & 6 deletions builder/xenserver/common/common_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ type CommonConfig struct {
RawSSHWaitTimeout string `mapstructure:"ssh_wait_timeout"`
SSHWaitTimeout time.Duration

OutputDir string `mapstructure:"output_directory"`
Format string `mapstructure:"format"`
KeepVM string `mapstructure:"keep_vm"`
IPGetter string `mapstructure:"ip_getter"`
OutputDir string `mapstructure:"output_directory"`
Format string `mapstructure:"format"`
DiskDrives uint `mapstructure:"disk_drives"`
KeepTemplateVIFs bool `mapstructure:"keep_template_vifs"`
KeepVM string `mapstructure:"keep_vm"`
IPGetter string `mapstructure:"ip_getter"`
}

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

switch c.Format {
case "xva", "xva_compressed", "vdi_raw", "vdi_vhd", "none":
case "xva", "xva_template", "vdi_raw", "vdi_vhd", "none":
default:
errs = append(errs, errors.New("format must be one of 'xva', 'vdi_raw', 'vdi_vhd', 'none'"))
errs = append(errs, errors.New("format must be one of 'xva', 'xva_template', 'vdi_raw', 'vdi_vhd', 'none'"))
}

switch c.KeepVM {
Expand Down
103 changes: 103 additions & 0 deletions builder/xenserver/common/step_configure_disk_drives.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package common

import (
"fmt"
"strings"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"github.com/nilshell/xmlrpc"
xsclient "github.com/xenserver/go-xenserver-client"
)

type StepConfigureDiskDrives struct {}

func (self *StepConfigureDiskDrives) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
config := state.Get("commonconfig").(CommonConfig)
client := state.Get("client").(xsclient.XenAPIClient)
ui.Say("Step: Configure disk drives")

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
}

vbds, err := instance.GetVBDs()
if err != nil {
ui.Error(fmt.Sprintf("Error getting VBDs: %s", err.Error()))
return multistep.ActionHalt
}

var current_number_of_disk_drives uint = 0
for _, vbd := range vbds {
vbd_rec, err := vbd.GetRecord()
if err != nil {
ui.Error(fmt.Sprintf("Error getting VBD record: %s", err.Error()))
return multistep.ActionHalt
}
if vbd_rec["type"].(string) == "CD" {
if current_number_of_disk_drives < config.DiskDrives {
ui.Say("Ejecting disk drive")
err = vbd.Eject()
if err != nil && !strings.Contains(err.Error(), "VBD_IS_EMPTY") {
ui.Error(fmt.Sprintf("Error ejecting VBD: %s", err.Error()))
return multistep.ActionHalt
}
current_number_of_disk_drives++
} else {
ui.Say("Destroying excess disk drive")
_ = vbd.Eject()
_ = vbd.Unplug()
err = vbd.Destroy()
if err != nil {
ui.Error(fmt.Sprintf("Error destroying VBD: %s", err.Error()))
return multistep.ActionHalt
}
}
}
}

if current_number_of_disk_drives < config.DiskDrives {
vbd_rec := make(xmlrpc.Struct)
vbd_rec["VM"] = instance.Ref
vbd_rec["VDI"] = "OpaqueRef:NULL"
vbd_rec["userdevice"] = "autodetect"
vbd_rec["empty"] = true
vbd_rec["other_config"] = make(xmlrpc.Struct)
vbd_rec["qos_algorithm_type"] = ""
vbd_rec["qos_algorithm_params"] = make(xmlrpc.Struct)
vbd_rec["mode"] = "RO"
vbd_rec["bootable"] = true
vbd_rec["unpluggable"] = false
vbd_rec["type"] = "CD"
for current_number_of_disk_drives < config.DiskDrives {
ui.Say("Creating disk drive")

result := xsclient.APIResult{}
err := client.APICall(&result, "VBD.create", vbd_rec)

if err != nil {
ui.Error("Error creating disk drive. Retrying...")
continue
}

vbd_ref := result.Value.(string)

result = xsclient.APIResult{}
err = client.APICall(&result, "VBD.get_uuid", vbd_ref)

if err != nil {
ui.Error("Error verifying disk drive. Retrying...")
continue
}

current_number_of_disk_drives++
}
}

return multistep.ActionContinue
}

func (self *StepConfigureDiskDrives) Cleanup(state multistep.StateBag) {}
1 change: 1 addition & 0 deletions builder/xenserver/common/step_detach_vdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type StepDetachVdi struct {

func (self *StepDetachVdi) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)

client := state.Get("client").(xsclient.XenAPIClient)

var vdiUuid string
Expand Down
36 changes: 26 additions & 10 deletions builder/xenserver/common/step_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,38 @@ func (StepExport) Run(state multistep.StateBag) multistep.StepAction {

ui.Say("Step: export artifact")

compress_option_xe := "compress=false"
compress_option_url := ""

switch config.Format {
case "none":
ui.Say("Skipping export")
return multistep.ActionContinue

case "xva_compressed":
compress_option_xe = "compress=true"
compress_option_url = "use_compression=true&"
fallthrough
case "xva_template":
ui.Say("Converting VM to template before export")
err = instance.SetIsATemplate(true)
if err != nil {
ui.Error(fmt.Sprintf("Error converting VM to a template prior to export: %s", err.Error()))
return multistep.ActionHalt
}
if !config.KeepTemplateVIFs {
ui.Say("Destroying VM network interfaces for template export")
vifs, err := instance.GetVIFs()
if err != nil {
ui.Error(fmt.Sprintf("Error getting VIFs: %s", err.Error()))
return multistep.ActionHalt
}
for _, vif := range vifs {
err = vif.Destroy()
if err != nil {
ui.Error(fmt.Sprintf("Error destroying VIF: %s", err.Error()))
return multistep.ActionHalt
}
}
}
case "xva":
// export the VM

export_filename := fmt.Sprintf("%s/%s.xva", config.OutputDir, config.VMName)
ui.Say("Exporting to: " + export_filename)

use_xe := os.Getenv("USE_XE") == "1"
if xe, e := exec.LookPath("xe"); e == nil && use_xe {
Expand All @@ -122,17 +138,16 @@ func (StepExport) Run(state multistep.StateBag) multistep.StepAction {
"-pw", client.Password,
"vm-export",
"vm="+instance_uuid,
compress_option_xe,
"compress=true",
"filename="+export_filename,
)

ui.Say(fmt.Sprintf("Getting XVA %+v %+v", cmd.Path, cmd.Args))

err = cmd.Run()
} else {
export_url := fmt.Sprintf("https://%s/export?%suuid=%s&session_id=%s",
export_url := fmt.Sprintf("https://%s/export?use_compression=true&uuid=%s&session_id=%s",
client.Host,
compress_option_url,
instance_uuid,
client.Session.(string),
)
Expand Down Expand Up @@ -210,6 +225,7 @@ func (StepExport) Run(state multistep.StateBag) multistep.StepAction {
}

disk_export_filename := fmt.Sprintf("%s/%s%s", config.OutputDir, disk_uuid, suffix)
ui.Say("Exporting to: " + disk_export_filename)

ui.Say("Getting VDI " + disk_export_url)
err = downloadFile(disk_export_url, disk_export_filename, ui)
Expand Down
1 change: 1 addition & 0 deletions builder/xenserver/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
&xscommon.StepDetachVdi{
VdiUuidKey: "floppy_vdi_uuid",
},
new(xscommon.StepConfigureDiskDrives),
new(xscommon.StepExport),
}

Expand Down
1 change: 1 addition & 0 deletions builder/xenserver/xva/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa
&xscommon.StepDetachVdi{
VdiUuidKey: "tools_vdi_uuid",
},
new(xscommon.StepConfigureDiskDrives),
new(xscommon.StepExport),
}

Expand Down
8 changes: 7 additions & 1 deletion builder/xenserver/xva/step_import_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ func (self *stepImportInstance) Run(state multistep.StateBag) multistep.StepActi
}
state.Put("instance_uuid", instanceId)

instance.SetDescription(config.VMDescription)
err = instance.SetNameLabel(config.VMName)
if err != nil {
ui.Error(fmt.Sprintf("Error setting VM name: %s", err.Error()))
return multistep.ActionHalt
}

err = instance.SetDescription(config.VMDescription)
if err != nil {
ui.Error(fmt.Sprintf("Error setting VM description: %s", err.Error()))
return multistep.ActionHalt
Expand Down
9 changes: 8 additions & 1 deletion docs/builders/xenserver-iso.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ each category, the available options are alphabetized and described.
run `xe template-list`. Setting the correct value hints to XenServer how to
optimize the virtual hardware to work best with that operating system.

* `disk_drives` (integer) - How many DVD drives to keep on the VM when exporting.
Default is 0.

* `disk_size` (integer) - The size, in megabytes, of the hard disk to create
for the VM. By default, this is 40000 (about 40 GB).

Expand All @@ -113,7 +116,7 @@ each category, the available options are alphabetized and described.
characters (\*, ?, and []) are allowed. Directory names are also allowed,
which will add all the files found in the directory to the floppy.

* `format` (string) - Either "xva", "vdi_raw" or "none", this specifies the
* `format` (string) - Either "xva", "xva_template", "vdi_raw" or "none", this specifies the
output format of the exported virtual machine. This defaults to "xva". Set to
"vdi_raw" to export just the raw disk image. Set to "none" to export nothing;
this is only useful with "keep_vm" set to "always" or "on_success".
Expand Down Expand Up @@ -144,6 +147,10 @@ each category, the available options are alphabetized and described.
must point to the same file (same checksum). By default this is empty
and `iso_url` is used. Only one of `iso_url` or `iso_urls` can be specified.

* `keep_template_vifs` (boolean) - Whether you want to keep VIFs on the VM prior to
exporting the XVA template. Removing them may make the template more generic and
reusable. Default is "false".

* `keep_vm` (string) - Determine when to keep the VM and when to clean it up. This
can be "always", "never" or "on_success". By default this is "never", and Packer
always deletes the VM regardless of whether the process succeeded and an artifact
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.