2016-01-11 06:22:41 -05:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-01-11 06:22:41 -05:00
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateTemplate struct{}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepCreateTemplate) Run(_ 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)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
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...")
|
|
|
|
volumeID, err := getRootVolumeID(client, instanceID)
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRootVolumeID(client *cloudstack.CloudStackClient, instanceID string) (string, error) {
|
|
|
|
// Retrieve the virtual machine object.
|
|
|
|
p := client.Volume.NewListVolumesParams()
|
|
|
|
|
|
|
|
// Set the type and virtual machine ID
|
|
|
|
p.SetType("ROOT")
|
|
|
|
p.SetVirtualmachineid(instanceID)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|