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

Fixes #16, export as template #17

Open
wants to merge 1 commit 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
10 changes: 5 additions & 5 deletions Godeps/Godeps.json

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

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ A brief explanation of what the config parameters mean:
* `vm_vcpus` - the number of vCPUs to assign during build
* `vm_disks` - a nested array of disk name: capacity pairs. Allows creating more than one virtual disk, and assigning each a name. If disk_size is also present, it takes priority and this setting is completely ignored. Using arrays enforces drive creation order, which can be very important for matching up to device names in Kickstart scripts, for example.
* `nfs_mount` - Used for VHD artifacts, the NFS mount for the sr_name
* `convert_to_template` - Convert the VM to a Template before final export. Boolean.

Once you've updated the config file with your own parameters, you can use packer to build this VM with the following command:

Expand Down Expand Up @@ -144,6 +145,7 @@ A brief explanation of what the config parameters mean:
* `vm_name` - the name that should be given to the created VM.
* `source_vm` - the name of the VM to clone and operate on
* `nfs_mount` - Used for VHD artifacts, the NFS mount for the sr_name
* `convert_to_template` - Convert the VM to a Template before final export. Boolean.

Once you've updated the config file with your own parameters, you can use packer to build this VM with the following command:

Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ IFS=$OLDIFS

# Copy our OS/Arch to the bin/ directory
echo "==> Copying binaries for this platform..."
DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
DEV_PLATFORM="./pkg/${XC_OS}_${XC_ARCH}"
for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
cp -v ${F} bin/
cp -v ${F} ${MAIN_GOPATH}/bin/
Expand Down
9 changes: 5 additions & 4 deletions builder/xenserver/common/common_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ 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"`
KeepVM string `mapstructure:"keep_vm"`
IPGetter string `mapstructure:"ip_getter"`
ConvertToTemplate bool `mapstructure:"convert_to_template"`
}

func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error {
Expand Down
63 changes: 53 additions & 10 deletions builder/xenserver/common/step_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (self *StepExport) Run(state multistep.StateBag) multistep.StepAction {
suffix := ".vhd"
extrauri := "&format=vhd"

exportFiles := make([]string, 0, 1)
exportFiles := make([]string, 0, 1)

instance, err := client.GetVMByUuid(instance_uuid)
if err != nil {
Expand All @@ -107,6 +107,22 @@ func (self *StepExport) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionContinue

case "vhd":
// Before exporting, convert it to a template (if requested)
// If a template, we assume all VIFs should be removed at export time,
// to make it more generic and reusable
if config.ConvertToTemplate {
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
}
vifs, _ := instance.GetVIFs()
for _, vif := range vifs {
ui.Say("Destroying VM network interfaces for template export")
vif.Destroy()
}
}

disks, err := instance.GetDisks()
if err != nil {
Expand Down Expand Up @@ -145,14 +161,28 @@ func (self *StepExport) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt
}

exportFiles = append(exportFiles , export_filename)
exportFiles = append(exportFiles, export_filename)

dst.Close()
}

case "xva":
// export the VM

// Before exporting, convert it to a template (if requested)
// If a template, we assume all VIFs should be removed at export time,
// to make it more generic and reusable
if config.ConvertToTemplate {
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
}
vifs, _ := instance.GetVIFs()
for _, vif := range vifs {
ui.Say("Destroying VM network interfaces for template export")
vif.Destroy()
}
}
export_url := fmt.Sprintf("https://%s/export?uuid=%s&session_id=%s",
client.Host,
instance_uuid,
Expand All @@ -168,15 +198,29 @@ func (self *StepExport) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt
}

exportFiles = append(exportFiles , export_filename)
exportFiles = append(exportFiles, export_filename)

case "vdi_raw":
suffix = ".raw"
extrauri = ""
fallthrough
case "vdi_vhd":
// export the disks

// Before exporting, convert it to a template (if requested)
// If a template, we assume all VIFs should be removed at export time,
// to make it more generic and reusable
if config.ConvertToTemplate {
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
}
vifs, _ := instance.GetVIFs()
for _, vif := range vifs {
ui.Say("Destroying VM network interfaces for template export")
vif.Destroy()
}
}
disks, err := instance.GetDisks()
if err != nil {
ui.Error(fmt.Sprintf("Could not get VM disks: %s", err.Error()))
Expand Down Expand Up @@ -233,7 +277,6 @@ func (self *StepExport) Run(state multistep.StateBag) multistep.StepAction {

}


disk_export_filename := fmt.Sprintf("%s/%s%s", config.OutputDir, disk_uuid, suffix)

ui.Say("Getting VDI " + disk_export_url)
Expand All @@ -249,7 +292,7 @@ func (self *StepExport) Run(state multistep.StateBag) multistep.StepAction {
}

default:
panic(fmt.Sprintf("Unknown export format '%s'", self.OutputFormat ))
panic(fmt.Sprintf("Unknown export format '%s'", self.OutputFormat))
}

state.Put("export_files", exportFiles)
Expand Down
3 changes: 2 additions & 1 deletion examples/centos-6.6.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"vm_description": "Build time: {{isotime}}",
"vm_disks": [
["rootvol", "40000"]
]
],
"convert_to_template": false
}
],

Expand Down
7 changes: 5 additions & 2 deletions examples/centos-7.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"<tab><end> ks=http://10.204.137.80/ks/centos7-ks.cfg<enter><wait>"
],
"boot_wait": "10s",
"disk_size": 40960,
"vm_disks": [
["rootvol", "40000"]
],
"http_directory": "/var/www/html/packer",
"iso_url": "http://mirrors.seas.harvard.edu/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1503-01.iso",
"iso_name": "CentOS-7-x86_64-Minimal-1503-01.iso",
Expand All @@ -26,7 +28,8 @@
"vm_name": "packer-centos-7-x86_64-acs-minimal-template",
"vm_memory": "8192",
"vm_vcpus": 1,
"nfs_mount": "10.204.136.41:/vol/exports/packer"
"nfs_mount": "10.204.136.41:/vol/exports/packer",
"convert_to_template": false
}
],

Expand Down