packer-cn/builder/hyperone/step_create_image.go

66 lines
1.6 KiB
Go
Raw Normal View History

2019-01-23 08:04:05 -05:00
package hyperone
import (
"context"
"fmt"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hyperonecom/h1-client-go"
)
2019-02-23 08:57:19 -05:00
type stepCreateImage struct {
imageID string
}
2019-01-23 08:04:05 -05:00
func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*openapi.APIClient)
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
vmID := state.Get("vm_id").(string)
ui.Say("Creating image...")
image, _, err := client.ImageApi.ImageCreate(ctx, openapi.ImageCreate{
Name: config.ImageName,
Vm: vmID,
Service: config.ImageService,
Description: config.ImageDescription,
Tag: config.ImageTags,
})
if err != nil {
err := fmt.Errorf("error creating image: %s", formatOpenAPIError(err))
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
2019-02-23 08:57:19 -05:00
s.imageID = image.Id
2019-01-23 08:04:05 -05:00
state.Put("image_id", image.Id)
state.Put("image_name", image.Name)
return multistep.ActionContinue
}
2019-02-23 08:57:19 -05:00
func (s *stepCreateImage) Cleanup(state multistep.StateBag) {
if s.imageID == "" {
return
}
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
client := state.Get("client").(*openapi.APIClient)
ui := state.Get("ui").(packer.Ui)
_, err := client.ImageApi.ImageDelete(context.TODO(), s.imageID)
if err != nil {
ui.Error(fmt.Sprintf("error deleting image '%s' - consider deleting it manually: %s",
s.imageID, formatOpenAPIError(err)))
}
}