[Feature] Add '-force' support for templates #161
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello! Hope everyone's having a pleasant start to the week :)
Change Summary
This PR adds logic to template handling based on the usage of Packer's
-forceflag.If the force flag is used, the builder will replace/delete any templates with the same name as the current builds "vm_name" parameter.
If the force flag is not used, a new template will be created every time the builder is run.
Why These Changes?
I'm slowly migrating to XCP-NG in my HomeLab, and part of that process is checking out the support for the various IaC tools I'm used to with vSphere (Packer, Terraform, Ansible, etc).
I use Packer with vSphere to create "rolling" templates for the various OS's I use. This reduces the amount of time it takes to setup a VM cloned from the template, since it's always got the latest updates and patches.
I found that this XCP-NG builder works a bit differently than I and other users of the vSphere ISO builder are used to when it comes to managing existing templates.
vSphere does not allow templates with duplicate names using Packer, in which case you are required to use the
-forceflag to permit the builder to remove the old template before creating the new one. This behavior allows one to create "rolling" or "golden image" templates as I and other's are used to (PR #75)Since XCP-NG does allow you to have multiple templates with the same name, I wanted my changes to allow for both workflows:
I believe implementing this logic by utilizing the
-forceflag is appropriate since it's simple (already implemented by packer upstream, don't have to add new variables/parameters, users are already familiar with the flag's purpose/logic). Additionally, this implementation adheres to Packer's own documentation regarding the purpose and usage of the -force flag:Callouts
This PR only implements logic regarding the -force flag for templates. The XVA image creation logic was not touched. This is technically an inconsistency with the force flag logic since the XVA images seem to use their own logic for handling artifact creation (
format,skip_set_template)I am not a programmer by trade (DevOps/Sysadmin here 👋🏻) and most of my amateur experience is in Python. I thought this was a great excuse to learn some Golang and finally make my first contribution to an open source project 😄
With that being said, comments, questions, and concerns are absolutely welcome!
Cheers!