diff --git a/.drone.yml b/.drone.yml index b911ce3..2d0e82c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -9,21 +9,22 @@ steps: commands: - | yamllint -d "{extends: relaxed, rules: {line-length: disable}}" scripts + - | + packer init -upgrade \ + ./packer - | packer validate \ - -var-file=packer/variables.vsphere.json \ -var vm_name=$DRONE_BUILD_NUMBER-${DRONE_COMMIT_SHA:0:10} \ -var vsphere_password=$${VSPHERE_PASSWORD} \ -var winrm_password=$${WINRM_PASSWORD} \ - packer/adds.json + ./packer - | packer build \ - -on-error=cleanup \ - -var-file=packer/variables.vsphere.json \ + -on-error=cleanup -timestamp-ui \ -var vm_name=$DRONE_BUILD_NUMBER-${DRONE_COMMIT_SHA:0:10} \ -var vsphere_password=$${VSPHERE_PASSWORD} \ -var winrm_password=$${WINRM_PASSWORD} \ - packer/adds.json + ./packer/adds environment: VSPHERE_PASSWORD: from_secret: vsphere_password diff --git a/packer/adds.pkr.hcl b/packer/adds.pkr.hcl new file mode 100644 index 0000000..f7e6be8 --- /dev/null +++ b/packer/adds.pkr.hcl @@ -0,0 +1,90 @@ +packer { + required_plugins { + windows-update = { + version = "0.12.0" + source = "github.com/rgl/windows-update" + } + } +} + +source "vsphere-clone" "adds" { + vcenter_server = var.vcenter_server + username = var.vsphere_username + password = var.vsphere_password + insecure_connection = "true" + + vm_name = "adds-${var.vm_name}" + datacenter = var.vsphere_datacenter + host = var.vsphere_host + folder = var.vsphere_folder + datastore = var.vsphere_datastore + + template = "Windows-Server-2019-LTSC" + + boot_order = "disk,cdrom" + boot_command = [""] + boot_wait = "2m30s" + + communicator = "winrm" + winrm_password = var.winrm_password + winrm_timeout = "10m" + winrm_username = "administrator" + + RAM = 8192 + CPUs = 2 + + floppy_files = [ + "packer/preseed/ADDS/Sysprep_Unattend.xml" + ] + + shutdown_command = "C:\\Windows\\System32\\Sysprep\\sysprep.exe /generalize /oobe /unattend:A:\\Sysprep_Unattend.xml" + shutdown_timeout = "1h" + + export { + images = false + } +} + +build { + sources = ["source.vsphere-clone.adds"] + + provisioner "powershell" { + inline = [ + "New-Item -Path 'C:\\Payload\\Scripts' -ItemType 'Directory' -Force:$True -Confirm:$False" + ] + } + + provisioner "file" { + destination = "C:\\Payload\\" + source = "scripts/ADDS/payload/" + } + + provisioner "powershell" { + scripts = [ + "scripts/ADDS/Install-Prerequisites.ps1", + "scripts/ADDS/Register-ScheduledTask.ps1" + ] + } + + post-processor "shell-local" { + inline = [ + "pwsh -command \"& scripts/Update-OvfConfiguration.ps1 \\", + " -OVFFile './output-adds/adds-${var.vm_name}.ovf' \\", + " -Parameter @{'appliance.name'='ADDS';'appliance.version'='${var.vm_name}'}\"", + "pwsh -file scripts/Update-Manifest.ps1 \\", + " -ManifestFileName './output-adds/adds-${var.vm_name}.mf'", + "ovftool --acceptAllEulas --allowExtraConfig --overwrite \\", + " './output-adds/adds-${var.vm_name}.ovf' \\", + " /output/ADDS-appliance.ova" + ] + } + post-processor "shell-local" { + inline = [ + "pwsh -file scripts/Remove-Resources.ps1 \\", + " -VMName 'adds-${var.vm_name}' \\", + " -VSphereFQDN '${var.vcenter_server}' \\", + " -VSphereUsername '${var.vsphere_username}' \\", + " -VSpherePassword '${var.vsphere_password}'" + ] + } +} \ No newline at end of file diff --git a/packer/variables.pkr.hcl b/packer/variables.pkr.hcl new file mode 100644 index 0000000..f7fc838 --- /dev/null +++ b/packer/variables.pkr.hcl @@ -0,0 +1,14 @@ +variable "vcenter_server" {} +variable "vsphere_username" {} +variable "vsphere_password" {} + +variable "vsphere_host" {} +variable "vsphere_datacenter" {} + +variable "vsphere_templatefolder" {} +variable "vsphere_folder" {} +variable "vsphere_datastore" {} +variable "vsphere_network" {} + +variable "vm_name" {} +variable "winrm_password" {} \ No newline at end of file diff --git a/packer/variables.vsphere.json b/packer/variables.vsphere.json deleted file mode 100644 index 606a295..0000000 --- a/packer/variables.vsphere.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "vcenter_server": "bv11-vc.bessems.lan", - "vsphere_username": "administrator@vsphere.local", - "vsphere_datacenter": "DeSchakel", - "vsphere_host": "bv11-esx.bessems.lan", - "vsphere_hostip": "192.168.11.200", - "vsphere_datastore": "Datastore02.SSD", - "vsphere_folder": "/Packer", - "vsphere_templatefolder": "/Templates", - "vsphere_network": "LAN" -} \ No newline at end of file diff --git a/packer/vsphere.auto.pkrvars.hcl b/packer/vsphere.auto.pkrvars.hcl new file mode 100644 index 0000000..44b10ca --- /dev/null +++ b/packer/vsphere.auto.pkrvars.hcl @@ -0,0 +1,9 @@ +vcenter_server = "bv11-vc.bessems.lan" +vsphere_username = "administrator@vsphere.local" +vsphere_datacenter = "DeSchakel" +vsphere_host = "bv11-esx.bessems.lan" +vsphere_hostip = "192.168.11.200" +vsphere_datastore = "Datastore01.SSD" +vsphere_folder = "/Packer" +vsphere_templatefolder = "/Templates" +vsphere_network = "LAN" \ No newline at end of file