diff --git a/.gitignore b/.gitignore index 3de3eea..5c6fc7a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ packer_cache/* builder-* dist/* vendor/* -packer-plugin-xenserver \ No newline at end of file +packer-plugin-xenserver +ignore/* \ No newline at end of file diff --git a/builder/xenserver/common/artifact.go b/builder/xenserver/common/artifact.go index 32b6531..a101ad7 100644 --- a/builder/xenserver/common/artifact.go +++ b/builder/xenserver/common/artifact.go @@ -8,7 +8,7 @@ import ( ) // This is the common builder ID to all of these artifacts. -const BuilderId = "packer.xenserver" +const BuilderId = "packer.xcp-ng" type LocalArtifact struct { dir string diff --git a/builder/xenserver/common/common_config.go b/builder/xenserver/common/common_config.go index 6907e18..ce6dcf0 100644 --- a/builder/xenserver/common/common_config.go +++ b/builder/xenserver/common/common_config.go @@ -76,7 +76,7 @@ func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig } if c.ToolsIsoName == "" { - c.ToolsIsoName = "xs-tools.iso" + c.ToolsIsoName = "" } if c.HTTPPortMin == 0 { @@ -271,17 +271,17 @@ func (config CommonConfig) GetISOSR(c *Connection) (xenapi.SRRef, error) { } else { // Use the provided name label to find the SR to use - srs, err := c.GetClient().SR.GetByNameLabel(c.session, config.SrName) + srs, err := c.GetClient().SR.GetByNameLabel(c.session, config.SrISOName) if err != nil { return srRef, err } - + switch { case len(srs) == 0: - return srRef, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrName) + return srRef, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrISOName) case len(srs) > 1: - return srRef, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrName) + return srRef, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrISOName) } return srs[0], nil diff --git a/builder/xenserver/common/step_find_or_upload_vdi.go b/builder/xenserver/common/step_find_or_upload_vdi.go new file mode 100644 index 0000000..37b8453 --- /dev/null +++ b/builder/xenserver/common/step_find_or_upload_vdi.go @@ -0,0 +1,44 @@ +package common + +import ( + "context" + "fmt" + + "github.com/hashicorp/packer-plugin-sdk/multistep" + "github.com/hashicorp/packer-plugin-sdk/packer" +) + +type StepFindOrUploadVdi struct { + StepUploadVdi +} + +func (self *StepFindOrUploadVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + ui := state.Get("ui").(packer.Ui) + c := state.Get("client").(*Connection) + vdiName := self.VdiNameFunc() + + ui.Say(fmt.Sprintf("Attemping to find VDI '%s'", vdiName)) + + vdis, err := c.client.VDI.GetByNameLabel(c.session, vdiName) + if err != nil { + ui.Error(fmt.Sprintf("Failed to find VDI '%s' by name label: %s", vdiName, err.Error())) + return multistep.ActionHalt + } + + if len(vdis) > 1 { + ui.Error(fmt.Sprintf("Found more than one VDI with name '%s'. Name must be unique", vdiName)) + return multistep.ActionHalt + } else if len(vdis) == 1 { + + vdi := vdis[0] + + vdiUuid, err := c.client.VDI.GetUUID(c.session, vdi) + if err != nil { + ui.Error(fmt.Sprintf("Unable to get UUID of VDI '%s': %s", vdiName, err.Error())) + return multistep.ActionHalt + } + state.Put(self.VdiUuidKey, vdiUuid) + return multistep.ActionContinue + } + return self.uploadVdi(ctx, state) +} diff --git a/builder/xenserver/common/step_upload_vdi.go b/builder/xenserver/common/step_upload_vdi.go index 67a00f3..006e919 100644 --- a/builder/xenserver/common/step_upload_vdi.go +++ b/builder/xenserver/common/step_upload_vdi.go @@ -18,7 +18,7 @@ type StepUploadVdi struct { VdiUuidKey string } -func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { +func (self *StepUploadVdi) uploadVdi(ctx context.Context, state multistep.StateBag) multistep.StepAction { config := state.Get("commonconfig").(CommonConfig) ui := state.Get("ui").(packer.Ui) c := state.Get("client").(*Connection) @@ -33,10 +33,8 @@ func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) mu ui.Say(fmt.Sprintf("Step: Upload VDI '%s'", vdiName)) // Create VDI for the image - srs, err := c.client.SR.GetAll(c.session) - ui.Say(fmt.Sprintf("Step: Found SRs '%v'", srs)) - sr, err := config.GetISOSR(c) + ui.Say(fmt.Sprintf("Step: Found SR for upload '%v'", sr)) if err != nil { ui.Error(fmt.Sprintf("Unable to get SR: %v", err)) @@ -96,6 +94,10 @@ func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) mu return multistep.ActionContinue } +func (self *StepUploadVdi) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + return self.uploadVdi(ctx, state) +} + func (self *StepUploadVdi) Cleanup(state multistep.StateBag) { config := state.Get("commonconfig").(CommonConfig) ui := state.Get("ui").(packer.Ui) diff --git a/builder/xenserver/iso/builder.go b/builder/xenserver/iso/builder.go index a9e37f1..e31b0d9 100644 --- a/builder/xenserver/iso/builder.go +++ b/builder/xenserver/iso/builder.go @@ -187,7 +187,6 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p Url: self.config.ISOUrls, }, } - steps := []multistep.Step{ &xscommon.StepPrepareOutputDir{ Force: self.config.PackerForce, @@ -212,20 +211,22 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p }, VdiUuidKey: "floppy_vdi_uuid", }, - &xscommon.StepUploadVdi{ - VdiNameFunc: func() string { - if len(self.config.ISOUrls) > 0 { - return path.Base(self.config.ISOUrls[0]) - } - return "" + &xscommon.StepFindOrUploadVdi{ + xscommon.StepUploadVdi{ + VdiNameFunc: func() string { + if len(self.config.ISOUrls) > 0 { + return path.Base(self.config.ISOUrls[0]) + } + return "" + }, + ImagePathFunc: func() string { + if isoPath, ok := state.GetOk("iso_path"); ok { + return isoPath.(string) + } + return "" + }, + VdiUuidKey: "iso_vdi_uuid", }, - ImagePathFunc: func() string { - if isoPath, ok := state.GetOk("iso_path"); ok { - return isoPath.(string) - } - return "" - }, - VdiUuidKey: "iso_vdi_uuid", }, &xscommon.StepFindVdi{ VdiName: self.config.ToolsIsoName, diff --git a/debian-complied.pkr.hcl b/debian-complied.pkr.hcl new file mode 100644 index 0000000..3a995fb --- /dev/null +++ b/debian-complied.pkr.hcl @@ -0,0 +1,84 @@ +variable "remote_host" { + type = string + description = "The ip or fqdn of your XCP-ng. This will be pulled from the env var 'PKR_VAR_XAPI_HOST'" + sensitive = true + default = "IP" +} + +variable "remote_password" { + type = string + description = "The password used to interact with your XCP-ng. This will be pulled from the env var 'PKR_VAR_XAPI_PASSWORD'" + sensitive = true + default = "passw0rd" +} + +variable "remote_username" { + type = string + description = "The username used to interact with your XCP-ng. This will be pulled from the env var 'PKR_VAR_XAPI_USERNAME'" + sensitive = true + default = "root" +} + +variable "sr_iso_name" { + type = string + default = "ISOSR" + description = "The ISO-SR to packer will use" +} + +variable "sr_name" { + type = string + default = "SR" + description = "The name of the SR to packer will use" +} + +locals { + timestamp = regex_replace(timestamp(), "[- TZ:]", "") +} + +source "xcp-ng-iso" "debian-11" { + iso_checksum = "ce1dcd1fa272976ddc387554202013e69ecf1b02b38fba4f8c35c8b12b8f521e" + iso_checksum_type = "sha256" + iso_url = "https://cdimage.debian.org/cdimage/unofficial/non-free/cd-including-firmware/archive/11.5.0+nonfree/amd64/iso-cd/firmware-11.5.0-amd64-netinst.iso" + + sr_iso_name = var.sr_iso_name + sr_name = var.sr_name + tools_iso_name = "" + + remote_host = var.remote_host + remote_password = var.remote_password + remote_username = var.remote_username + + boot_command = [ + "", + "/install.amd/vmlinuz ", + "initrd=/install.amd/initrd.gz ", + "auto=true ", + "url=http://IPpreseed/preseed.cfg ", + "hostname=YeahItsWorks ", + "domain=ataxya.lab ", + "interface=auto ", + "vga=788 noprompt quiet--- " + ] + + # Change this to match the ISO of ubuntu you are using in the iso_url variable + clone_template = "Debian Bullseye 11" + vm_name = "My-super-Golden-Image-${local.timestamp}" + vm_description = "Build started: ${local.timestamp}" + vcpus_max = 4 + vcpus_atstartup = 4 + vm_memory = 4096 + disk_size = 10000 + + ssh_username = "packer" + ssh_password = "packer" + ssh_wait_timeout = "60000s" + ssh_handshake_attempts = 10000 + + output_directory = "packer-debian-11-iso" + keep_vm = "always" + format = "xva" +} + +build { + sources = ["xcp-ng-iso.debian-11"] +} diff --git a/debian-preseed.cfg b/debian-preseed.cfg deleted file mode 100644 index 134f5fe..0000000 --- a/debian-preseed.cfg +++ /dev/null @@ -1,106 +0,0 @@ -# To see all available options execute this command once the install is done: -# sudo less /var/log/installer/cdebconf/questions.dat -# If you need information about an option use the command below (example for keymap): -# grep -A 4 "keyboard-configuration/xkb-keymap" /var/log/installer/cdebconf/templates.dat - -d-i debconf/priority select critical -# Use network mirror for package installation -# d-i apt-setup/use_mirror boolean true - -# Automatic installation -d-i auto-install/enable boolean true - -# "linux-server" is substituted by "linux-image-amd64" -# Possible options : "linux-image-amd64"(default) or "linux-image-rt-amd64" -d-i base-installer/kernel/override-image string linux-server - -# Configure hardware clock -d-i clock-setup/utc boolean true -d-i clock-setup/utc-auto boolean true - -# d-i console-setup/ask_detect boolean false - -# d-i debconf/frontend select noninteractive - -# Set OS locale -d-i debian-installer/language string en -d-i debian-installer/country string US -d-i debian-installer/locale string en_US.UTF-8 - -# d-i debian-installer/framebuffer boolean false - -# Reboot once the install is done -d-i finish-install/reboot_in_progress note - -# Bootloader options -d-i grub-installer/only_debian boolean true -d-i grub-installer/with_other_os boolean true -d-i grub-installer/bootdev string /dev/xvda - -# Set the keyboard layout -d-i keyboard-configuration/xkb-keymap select us - -# Mirror from which packages will be downloaded -d-i mirror/country string manual -d-i mirror/http/directory string /debian -d-i mirror/http/hostname string httpredir.debian.org - -# Configure http proxy if needed "http://[[user][:pass]@]host[:port]/" -d-i mirror/http/proxy string - -# Disk configuration -d-i partman-efi/non_efi_system boolean true -d-i partman-auto-lvm/guided_size string max -d-i partman-auto/choose_recipe select atomic -d-i partman-auto/method string lvm -d-i partman-lvm/confirm boolean true -d-i partman-lvm/confirm_nooverwrite boolean true -d-i partman-lvm/device_remove_lvm boolean true -d-i partman/choose_partition select finish -d-i partman/confirm boolean true -d-i partman/confirm_nooverwrite boolean true -d-i partman/confirm_write_new_label boolean true - -# User configuration -d-i passwd/root-login boolean true -d-i passwd/root-password-again password packer -d-i passwd/root-password password packer -d-i passwd/user-fullname string packer -d-i passwd/user-uid string 1000 -d-i passwd/user-password password packer -d-i passwd/user-password-again password packer -d-i passwd/username string packer - -# Extra packages to be installed -d-i pkgsel/include string sudo - -d-i pkgsel/install-language-support boolean false -d-i pkgsel/update-policy select none - -# Whether to upgrade packages after debootstrap -d-i pkgsel/upgrade select full-upgrade - -# Set timezone -d-i time/zone string Europe/Paris - -# Allow weak user password -d-i user-setup/allow-password-weak boolean true - -# Home folder encryption -d-i user-setup/encrypt-home boolean false - -# Do not scan additional CDs -apt-cdrom-setup apt-setup/cdrom/set-first boolean false - -# Use network mirror -apt-mirror-setup apt-setup/use_mirror boolean true - -# Disable polularity contest -popularity-contest popularity-contest/participate boolean false - -# Select base install -tasksel tasksel/first multiselect standard, ssh-server - -# Setup passwordless sudo for packer user -d-i preseed/late_command string \ - echo "packer ALL=(ALL:ALL) NOPASSWD:ALL" > /target/etc/sudoers.d/packer && chmod 0440 /target/etc/sudoers.d/packer \ No newline at end of file diff --git a/preseed.cfg b/preseed.cfg new file mode 100644 index 0000000..8e5f6e8 --- /dev/null +++ b/preseed.cfg @@ -0,0 +1,80 @@ +# Locale Setup +d-i debian-installer/language string en +d-i debian-installer/country string US +d-i debian-installer/locale string en_US.UTF-8 + +# Keyboard Setup +d-i keyboard-configuration/xkb-keymap select us + +# Clock Setup +d-i time/zone string Europe/Paris +# d-i time/zone string Canada/Eastern +# Configure hardware clock +d-i clock-setup/utc boolean true +d-i clock-setup/utc-auto boolean true +# set above to false if making a bootable USB to run on same system as Windows + +# Network Setup +# https://bugs.launchpad.net/ubuntu/+source/netcfg/+bug/713385 +#d-i netcfg/choose_interface select auto +# make sure you also add "interface=auto" to your boot command too +# https://bugs.launchpad.net/ubuntu/+source/netcfg/+bug/713385 + +# User Setup +d-i passwd/root-login boolean true +d-i passwd/root-password-again password packer +d-i passwd/root-password password packer +d-i passwd/user-fullname string packer +d-i passwd/user-uid string 1000 +d-i passwd/user-password password packer +d-i passwd/user-password-again password packer +d-i passwd/username string packer + +# Package Setup +d-i apt-setup/cdrom/set-first boolean false +d-i apt-setup/cdrom/set-next boolean false +d-i apt-setup/cdrom/set-failed boolean false + +d-i mirror/country string manual +d-i mirror/http/hostname string deb.debian.org +d-i mirror/http/directory string /debian +d-i mirror/http/proxy string +# d-i mirror/http/proxy string ${var.apt_cache_url} +d-i apt-setup/contrib boolean true +d-i apt-setup/non-free boolean true +tasksel tasksel/first multiselect ssh-server, standard +d-i pkgsel/include string sudo, unattended-upgrades, dpkg +popularity-contest popularity-contest/participate boolean false + +# Whether to upgrade packages after debootstrap +d-i pkgsel/upgrade select full-upgrade + +# Disk configuration +d-i partman-basicfilesystems/choose_label string gpt +d-i partman-basicfilesystems/default_label string gpt +d-i partman-partitioning/choose_label string gpt +d-i partman-partitioning/default_label string gpt +d-i partman/choose_label string gpt +d-i partman/default_label string gpt +partman-partitioning partman-partitioning/choose_label select gpt +d-i partman-auto/disk string /dev/xvda +d-i partman-auto/method string regular +d-i partman-lvm/device_remove_lvm boolean true +d-i partman-md/device_remove_md boolean true +d-i partman-lvm/confirm boolean true +d-i partman-lvm/confirm_nooverwrite boolean true +d-i partman-auto/choose_recipe select atomic +d-i partman-md/confirm boolean true +d-i partman-partitioning/confirm_write_new_label boolean true +d-i partman/choose_partition select finish +d-i partman/confirm boolean true +d-i partman/confirm_nooverwrite boolean true + +d-i grub-installer/only_debian boolean true +d-i grub-installer/with_other_os boolean true +d-i grub-installer/bootdev string /dev/xvda + +# install tools +d-i preseed/late_command string in-target wget https://github.com/xenserver/xe-guest-utilities/releases/download/v7.20.2/xe-guest-utilities_7.20.2-1_amd64.deb; in-target export RUNLEVEL=1; in-target dpkg -i xe-guest-utilities_7.20.2-1_amd64.deb +# Final Setup +d-i finish-install/reboot_in_progress note diff --git a/test-brut.pkr.hcl b/test-brut.pkr.hcl deleted file mode 100644 index 5690685..0000000 --- a/test-brut.pkr.hcl +++ /dev/null @@ -1,65 +0,0 @@ -packer { - required_plugins { - xenserver= { - version = ">= v0.3.2" - source = "github.com/ddelnano/xenserver" - } - } -} - -variable "sr_iso_name" { - type = string - default = "ISOs" - description = "The ISO-SR to packer will use" - -} - -variable "sr_name" { - type = string - default = "Local storageD" - description = "The name of the SR to packer will use" -} - -locals { - timestamp = regex_replace(timestamp(), "[- TZ:]", "") -} - - -source "xenserver-iso" "ubuntu-2004" { - iso_checksum = "5035be37a7e9abbdc09f0d257f3e33416c1a0fb322ba860d42d74aa75c3468d4" - iso_checksum_type = "sha256" - iso_url = "http://releases.ubuntu.com/20.04/ubuntu-20.04.5-live-server-amd64.iso" - - sr_iso_name = var.sr_iso_name - sr_name = var.sr_name - tools_iso_name = "guest-tools.iso" - - remote_host = var.remote_host - remote_password = var.remote_password - remote_username = var.remote_username - - # Change this to match the ISO of ubuntu you are using in the iso_url variable - clone_template = "Ubuntu Focal Fossa 20.04" - vm_name = "packer-ubuntu-2004-${local.timestamp}" - vm_description = "Build started: ${local.timestamp}" - vcpus_max = 4 - vm_memory = 4096 - disk_size = 20000 - - floppy_files = [ - "examples/http/ubuntu-2004/meta-data", - "examples/http/ubuntu-2004/user-data", - ] - - ssh_username = "testuser" - ssh_password = "ubuntu" - ssh_wait_timeout = "60000s" - ssh_handshake_attempts = 10000 - - output_directory = "packer-ubuntu-2004-iso" - keep_vm = "always" -} - -build { - sources = ["xenserver-iso.ubuntu-2004"] -} diff --git a/test-debian.pkr.hcl b/test-debian.pkr.hcl deleted file mode 100644 index 7b061ec..0000000 --- a/test-debian.pkr.hcl +++ /dev/null @@ -1,66 +0,0 @@ -packer { - required_plugins { - xenserver= { - version = ">= v0.3.2" - source = "github.com/ddelnano/xenserver" - } - } -} - -variable "sr_iso_name" { - type = string - default = "ISOs" - description = "The ISO-SR to packer will use" - -} - -variable "sr_name" { - type = string - default = "Local storageD" - description = "The name of the SR to packer will use" -} - -locals { - timestamp = regex_replace(timestamp(), "[- TZ:]", "") -} - - -source "xenserver-iso" "debian-11" { - iso_checksum = "55f6f49b32d3797621297a9481a6cc3e21b3142f57d8e1279412ff5a267868d8" - iso_checksum_type = "sha256" - iso_url = "https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-11.6.0-amd64-DVD-1.iso" - - sr_iso_name = var.sr_iso_name - sr_name = var.sr_name - tools_iso_name = "guest-tools.iso" - - remote_host = var.remote_host - remote_password = var.remote_password - remote_username = var.remote_username - - # Change this to match the ISO of ubuntu you are using in the iso_url variable - clone_template = "Debian Bullseye 11" - vm_name = "packer-debian-11-${local.timestamp}" - vm_description = "Build started: ${local.timestamp}" - vcpus_max = 4 - vcpus_atstartup = 4 - vm_memory = 4096 - disk_size = 10000 - - floppy_files = [ - "debian-preseed.cfg", - ] - - ssh_username = "cecile" - ssh_password = "cecile" - ssh_wait_timeout = "60000s" - ssh_handshake_attempts = 10000 - - output_directory = "packer-debian-11-iso" - keep_vm = "always" - format = "none" -} - -build { - sources = ["xenserver-iso.debian-11"] -} diff --git a/variables.pkrvars.hcl.example b/variables.pkrvars.hcl.example deleted file mode 100644 index 9a6225e..0000000 --- a/variables.pkrvars.hcl.example +++ /dev/null @@ -1,21 +0,0 @@ -variable "remote_host" { - type = string - description = "The ip or fqdn of your XenServer. This will be pulled from the env var 'PKR_VAR_XAPI_HOST'" - sensitive = true - default = "ip" -} - -variable "remote_password" { - type = string - description = "The password used to interact with your XenServer. This will be pulled from the env var 'PKR_VAR_XAPI_PASSWORD'" - sensitive = true - default = "password" -} - -variable "remote_username" { - type = string - description = "The username used to interact with your XenServer. This will be pulled from the env var 'PKR_VAR_XAPI_USERNAME'" - sensitive = true - default = "root" - -}