-
Notifications
You must be signed in to change notification settings - Fork 74
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
[Issue 71] add skip_convert_to_template option #283
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,21 +14,23 @@ import ( | |
packersdk "github.com/hashicorp/packer-plugin-sdk/packer" | ||
) | ||
|
||
// stepFinalizeTemplateConfig does any required modifications to the configuration _after_ | ||
// stepFinalizeConfig does any required modifications to the configuration _after_ | ||
// the VM has been converted into a template, such as updating name and description, or | ||
// unmounting the installation ISO. | ||
type stepFinalizeTemplateConfig struct{} | ||
type stepFinalizeConfig struct{} | ||
|
||
type templateFinalizer interface { | ||
type finalizer interface { | ||
GetVmConfig(*proxmox.VmRef) (map[string]interface{}, error) | ||
SetVmConfig(*proxmox.VmRef, map[string]interface{}) (interface{}, error) | ||
StartVm(*proxmox.VmRef) (string, error) | ||
ShutdownVm(*proxmox.VmRef) (string, error) | ||
} | ||
|
||
var _ templateFinalizer = &proxmox.Client{} | ||
var _ finalizer = &proxmox.Client{} | ||
|
||
func (s *stepFinalizeTemplateConfig) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { | ||
func (s *stepFinalizeConfig) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { | ||
ui := state.Get("ui").(packersdk.Ui) | ||
client := state.Get("proxmoxClient").(templateFinalizer) | ||
client := state.Get("proxmoxClient").(finalizer) | ||
c := state.Get("config").(*Config) | ||
vmRef := state.Get("vmRef").(*proxmox.VmRef) | ||
|
||
|
@@ -45,7 +47,7 @@ func (s *stepFinalizeTemplateConfig) Run(ctx context.Context, state multistep.St | |
|
||
vmParams, err := client.GetVmConfig(vmRef) | ||
if err != nil { | ||
err := fmt.Errorf("error fetching template config: %s", err) | ||
err := fmt.Errorf("error fetching config: %s", err) | ||
state.Put("error", err) | ||
ui.Error(err.Error()) | ||
return multistep.ActionHalt | ||
|
@@ -139,6 +141,17 @@ func (s *stepFinalizeTemplateConfig) Run(ctx context.Context, state multistep.St | |
changes["delete"] = strings.Join(deleteItems, ",") | ||
|
||
if len(changes) > 0 { | ||
// Adding a Cloud-Init drive or removing CD-ROM devices won't take effect without a power off and on of the QEMU VM | ||
if c.SkipConvertToTemplate { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why is the shutdown only done when there are changes? I would've thought this should be coupled with the call to |
||
ui.Say("Hardware changes pending for VM, stopping VM") | ||
_, err := client.ShutdownVm(vmRef) | ||
if err != nil { | ||
err := fmt.Errorf("Error stopping VM: %s", err) | ||
state.Put("error", err) | ||
ui.Error(err.Error()) | ||
return multistep.ActionHalt | ||
} | ||
} | ||
_, err := client.SetVmConfig(vmRef, changes) | ||
if err != nil { | ||
err := fmt.Errorf("Error updating template: %s", err) | ||
|
@@ -148,7 +161,19 @@ func (s *stepFinalizeTemplateConfig) Run(ctx context.Context, state multistep.St | |
} | ||
} | ||
|
||
// When build artifact is to be a VM, return a running VM | ||
if c.SkipConvertToTemplate { | ||
ui.Say("Resuming VM") | ||
_, err := client.StartVm(vmRef) | ||
if err != nil { | ||
err := fmt.Errorf("Error starting VM: %s", err) | ||
state.Put("error", err) | ||
ui.Error(err.Error()) | ||
return multistep.ActionHalt | ||
} | ||
} | ||
|
||
return multistep.ActionContinue | ||
} | ||
|
||
func (s *stepFinalizeTemplateConfig) Cleanup(state multistep.StateBag) {} | ||
func (s *stepFinalizeConfig) Cleanup(state multistep.StateBag) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to add
StartVm
to that contract? It seems that the function is not invoked in the step, and AFAICT this is not used elsewhere, so I wonder if that might be a leftover from a WIP state of the PR?Feel free to correct me if I'm wrong though, I might've missed something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was left over from a WIP version of the PR where the returned artifact was a running VM. I noted in #72 (comment) a desire to have a running VM returned, so I've rerolled with those changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah understood, I missed the resume VM part.
I wonder though, the current workflow appears to work (can't test it as I don't have a proxmox machine on hand), but I would think we can keep the VM running throughout the process if we're not exporting to a template, don't we?
In this case we can change a bit the
Cleanup
method of thestep_start_vm
so it doesn't kill and delete the VM, unless it cannot?Also to be sure, since the cleanup for the VM will run after
step_finalize_config
, will the VM still exist? I'd assume this will be stopped and removed, won't it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A stop and start of the VM is required if there are VM hardware changes for the VM's Cloud-Init storage and CD-ROM devices (step
step_finalize_config
).These changes aren't picked up by the VM unless it's completely stopped then started again, they can't be hot-swapped or mounted on an ACPI reboot. This is a limitation on the Proxmox backend.
I've reworked the logic to only stop the VM artifact if there are changes to be done in
step_finalize_config
(if len(changes) > 0) that way if Cloud-Init isn't being configured or CD-ROM devices are being kept, the VM won't restart.