2016-01-11 06:22:41 -05:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-01-11 06:22:41 -05:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2016-01-11 06:22:41 -05:00
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateTemplate struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-01-11 06:22:41 -05:00
|
|
|
client := state.Get("client").(*cloudstack.CloudStackClient)
|
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2016-01-11 06:22:41 -05:00
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Creating template: %s", config.TemplateName))
|
|
|
|
|
|
|
|
// Retrieve the instance ID from the previously saved state.
|
|
|
|
instanceID, ok := state.Get("instance_id").(string)
|
|
|
|
if !ok || instanceID == "" {
|
2017-07-14 01:11:30 -04:00
|
|
|
err := fmt.Errorf("Could not retrieve instance_id from state!")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
2016-01-11 06:22:41 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new parameter struct.
|
|
|
|
p := client.Template.NewCreateTemplateParams(
|
|
|
|
config.TemplateDisplayText,
|
|
|
|
config.TemplateName,
|
|
|
|
config.TemplateOS,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Configure the template according to the supplied config.
|
|
|
|
p.SetIsfeatured(config.TemplateFeatured)
|
|
|
|
p.SetIspublic(config.TemplatePublic)
|
|
|
|
p.SetIsdynamicallyscalable(config.TemplateScalable)
|
|
|
|
p.SetPasswordenabled(config.TemplatePasswordEnabled)
|
|
|
|
p.SetRequireshvm(config.TemplateRequiresHVM)
|
|
|
|
|
|
|
|
if config.Project != "" {
|
|
|
|
p.SetProjectid(config.Project)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.TemplateTag != "" {
|
|
|
|
p.SetTemplatetag(config.TemplateTag)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message("Retrieving the ROOT volume ID...")
|
2018-07-16 05:38:14 -04:00
|
|
|
volumeID, err := getRootVolumeID(client, instanceID, config.Project)
|
2016-01-11 06:22:41 -05:00
|
|
|
if err != nil {
|
2017-07-06 17:31:13 -04:00
|
|
|
state.Put("error", err)
|
2017-07-14 01:11:30 -04:00
|
|
|
ui.Error(err.Error())
|
2016-01-11 06:22:41 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the volume ID from which to create the template.
|
|
|
|
p.SetVolumeid(volumeID)
|
|
|
|
|
|
|
|
ui.Message("Creating the new template...")
|
|
|
|
template, err := client.Template.CreateTemplate(p)
|
|
|
|
if err != nil {
|
2017-07-14 01:11:30 -04:00
|
|
|
err := fmt.Errorf("Error creating the new template %s: %s", config.TemplateName, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
2016-01-11 06:22:41 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is kind of nasty, but it appears to be needed to prevent corrupt templates.
|
|
|
|
// When CloudStack says the template creation is done and you then delete the source
|
|
|
|
// volume shortly after, it seems to corrupt the newly created template. Giving it an
|
|
|
|
// additional 30 seconds to really finish up, seem to prevent that from happening.
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
|
|
|
|
ui.Message("Template has been created!")
|
|
|
|
|
2017-07-06 17:31:13 -04:00
|
|
|
// Store the template.
|
2016-01-11 06:22:41 -05:00
|
|
|
state.Put("template", template)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup any resources that may have been created during the Run phase.
|
|
|
|
func (s *stepCreateTemplate) Cleanup(state multistep.StateBag) {
|
|
|
|
// Nothing to cleanup for this step.
|
|
|
|
}
|
|
|
|
|
2018-07-16 05:38:14 -04:00
|
|
|
func getRootVolumeID(client *cloudstack.CloudStackClient, instanceID, projectID string) (string, error) {
|
2016-01-11 06:22:41 -05:00
|
|
|
// Retrieve the virtual machine object.
|
|
|
|
p := client.Volume.NewListVolumesParams()
|
|
|
|
|
|
|
|
// Set the type and virtual machine ID
|
|
|
|
p.SetType("ROOT")
|
|
|
|
p.SetVirtualmachineid(instanceID)
|
2018-07-16 05:38:14 -04:00
|
|
|
if projectID != "" {
|
|
|
|
p.SetProjectid(projectID)
|
2018-07-13 11:56:27 -04:00
|
|
|
}
|
2016-01-11 06:22:41 -05:00
|
|
|
|
|
|
|
volumes, err := client.Volume.ListVolumes(p)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to retrieve ROOT volume: %s", err)
|
|
|
|
}
|
|
|
|
if volumes.Count != 1 {
|
|
|
|
return "", fmt.Errorf("Could not find ROOT disk of instance %s", instanceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return volumes.Volumes[0].Id, nil
|
|
|
|
}
|