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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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-05 10:20:42 -05:00
|
|
|
state.Put("chroot_disk_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
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
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
|
|
|
}
|