2019-01-23 08:04:05 -05:00
|
|
|
package hyperone
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-02-06 06:27:57 -05:00
|
|
|
"strings"
|
2019-01-23 08:04:05 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2019-03-22 09:53:28 -04:00
|
|
|
openapi "github.com/hyperonecom/h1-client-go"
|
2019-01-23 08:04:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateVM struct {
|
|
|
|
vmID string
|
|
|
|
}
|
|
|
|
|
2019-01-28 06:30:15 -05:00
|
|
|
const (
|
|
|
|
chrootDiskName = "packer-chroot-disk"
|
|
|
|
)
|
|
|
|
|
2019-01-23 08:04:05 -05:00
|
|
|
func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*openapi.APIClient)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-01-23 08:04:05 -05:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
sshKey := state.Get("ssh_public_key").(string)
|
|
|
|
|
|
|
|
ui.Say("Creating VM...")
|
|
|
|
|
|
|
|
netAdapter := pickNetAdapter(config)
|
|
|
|
|
|
|
|
var sshKeys = []string{sshKey}
|
|
|
|
sshKeys = append(sshKeys, config.SSHKeys...)
|
|
|
|
|
2019-01-28 06:30:15 -05:00
|
|
|
disks := []openapi.VmCreateDisk{
|
|
|
|
{
|
|
|
|
Service: config.DiskType,
|
|
|
|
Size: config.DiskSize,
|
2019-01-23 08:04:05 -05:00
|
|
|
},
|
2019-01-28 06:30:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.ChrootDisk {
|
|
|
|
disks = append(disks, openapi.VmCreateDisk{
|
|
|
|
Service: config.ChrootDiskType,
|
|
|
|
Size: config.ChrootDiskSize,
|
|
|
|
Name: chrootDiskName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
options := openapi.VmCreate{
|
|
|
|
Name: config.VmName,
|
|
|
|
Image: config.SourceImage,
|
2019-02-01 12:57:43 -05:00
|
|
|
Service: config.VmType,
|
2019-01-28 06:30:15 -05:00
|
|
|
SshKeys: sshKeys,
|
|
|
|
Disk: disks,
|
2019-01-23 08:04:05 -05:00
|
|
|
Netadp: []openapi.VmCreateNetadp{netAdapter},
|
|
|
|
UserMetadata: config.UserData,
|
|
|
|
Tag: config.VmTags,
|
2020-06-28 21:17:38 -04:00
|
|
|
Username: config.Comm.SSHUsername,
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
vm, _, err := client.VmApi.VmCreate(ctx, options)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error creating VM: %s", formatOpenAPIError(err))
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.vmID = vm.Id
|
|
|
|
state.Put("vm_id", vm.Id)
|
2019-12-13 14:57:01 -05:00
|
|
|
// instance_id is the generic term used so that users can have access to the
|
|
|
|
// instance id inside of the provisioners, used in step_provision.
|
|
|
|
state.Put("instance_id", vm.Id)
|
2019-01-23 08:04:05 -05:00
|
|
|
|
|
|
|
hdds, _, err := client.VmApi.VmListHdd(ctx, vm.Id)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error listing hdd: %s", formatOpenAPIError(err))
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hdd := range hdds {
|
2019-01-28 06:30:15 -05:00
|
|
|
if hdd.Disk.Name == chrootDiskName {
|
|
|
|
state.Put("chroot_disk_id", hdd.Disk.Id)
|
2019-02-06 06:27:57 -05:00
|
|
|
controllerNumber := strings.ToLower(strings.Trim(hdd.ControllerNumber, "{}"))
|
|
|
|
state.Put("chroot_controller_number", controllerNumber)
|
|
|
|
state.Put("chroot_controller_location", int(hdd.ControllerLocation))
|
2019-02-02 18:24:06 -05:00
|
|
|
break
|
2019-01-28 06:30:15 -05:00
|
|
|
}
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
netadp, _, err := client.VmApi.VmListNetadp(ctx, vm.Id)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error listing netadp: %s", formatOpenAPIError(err))
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(netadp) < 1 {
|
|
|
|
err := fmt.Errorf("no network adapters found")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
publicIP, err := associatePublicIP(ctx, config, client, netadp[0])
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("error associating IP: %s", formatOpenAPIError(err))
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("public_ip", publicIP)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func pickNetAdapter(config *Config) openapi.VmCreateNetadp {
|
|
|
|
if config.Network == "" {
|
|
|
|
if config.PublicIP != "" {
|
|
|
|
return openapi.VmCreateNetadp{
|
2019-02-02 08:03:44 -05:00
|
|
|
Service: config.PublicNetAdpService,
|
2019-01-23 08:04:05 -05:00
|
|
|
Ip: []string{config.PublicIP},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var privateIPs []string
|
|
|
|
|
|
|
|
if config.PrivateIP == "" {
|
|
|
|
privateIPs = nil
|
|
|
|
} else {
|
|
|
|
privateIPs = []string{config.PrivateIP}
|
|
|
|
}
|
|
|
|
|
|
|
|
return openapi.VmCreateNetadp{
|
|
|
|
Service: "private",
|
|
|
|
Network: config.Network,
|
|
|
|
Ip: privateIPs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return openapi.VmCreateNetadp{
|
2019-02-02 08:03:44 -05:00
|
|
|
Service: config.PublicNetAdpService,
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func associatePublicIP(ctx context.Context, config *Config, client *openapi.APIClient, netadp openapi.Netadp) (string, error) {
|
|
|
|
if config.Network == "" || config.PublicIP == "" {
|
|
|
|
// Public IP belongs to attached net adapter
|
|
|
|
return netadp.Ip[0].Address, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var privateIP string
|
|
|
|
if config.PrivateIP == "" {
|
|
|
|
privateIP = netadp.Ip[0].Id
|
|
|
|
} else {
|
|
|
|
privateIP = config.PrivateIP
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, _, err := client.IpApi.IpActionAssociate(ctx, config.PublicIP, openapi.IpActionAssociate{Ip: privateIP})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip.Address, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.vmID == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-01-23 08:04:05 -05:00
|
|
|
|
2019-02-02 18:24:06 -05:00
|
|
|
ui.Say(fmt.Sprintf("Deleting VM %s...", s.vmID))
|
|
|
|
err := deleteVMWithDisks(s.vmID, state)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteVMWithDisks(vmID string, state multistep.StateBag) error {
|
|
|
|
client := state.Get("client").(*openapi.APIClient)
|
|
|
|
hdds, _, err := client.VmApi.VmListHdd(context.TODO(), vmID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error listing hdd: %s", formatOpenAPIError(err))
|
|
|
|
}
|
2019-01-23 08:04:05 -05:00
|
|
|
|
|
|
|
deleteOptions := openapi.VmDelete{}
|
2019-02-02 18:24:06 -05:00
|
|
|
for _, hdd := range hdds {
|
|
|
|
deleteOptions.RemoveDisks = append(deleteOptions.RemoveDisks, hdd.Disk.Id)
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
|
|
|
|
2019-02-02 18:24:06 -05:00
|
|
|
_, err = client.VmApi.VmDelete(context.TODO(), vmID, deleteOptions)
|
2019-01-23 08:04:05 -05:00
|
|
|
if err != nil {
|
2019-02-02 18:24:06 -05:00
|
|
|
return fmt.Errorf("Error deleting server '%s' - please delete it manually: %s", vmID, formatOpenAPIError(err))
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|
2019-02-02 18:24:06 -05:00
|
|
|
|
|
|
|
return nil
|
2019-01-23 08:04:05 -05:00
|
|
|
}
|