2018-10-17 06:15:47 -04:00
|
|
|
package hcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-08-05 04:10:59 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-10-17 06:15:47 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2018-10-17 06:15:47 -04:00
|
|
|
"github.com/hetznercloud/hcloud-go/hcloud"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateServer struct {
|
|
|
|
serverId int
|
|
|
|
}
|
|
|
|
|
2018-11-20 13:57:44 -05:00
|
|
|
func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-10-17 06:15:47 -04:00
|
|
|
client := state.Get("hcloudClient").(*hcloud.Client)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-10-17 06:15:47 -04:00
|
|
|
c := state.Get("config").(*Config)
|
|
|
|
sshKeyId := state.Get("ssh_key_id").(int)
|
|
|
|
|
|
|
|
// Create the server based on configuration
|
|
|
|
ui.Say("Creating server...")
|
|
|
|
|
|
|
|
userData := c.UserData
|
|
|
|
if c.UserDataFile != "" {
|
|
|
|
contents, err := ioutil.ReadFile(c.UserDataFile)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Problem reading user data file: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-03-06 02:05:08 -05:00
|
|
|
|
2018-10-17 06:15:47 -04:00
|
|
|
userData = string(contents)
|
|
|
|
}
|
|
|
|
|
2018-11-20 13:57:44 -05:00
|
|
|
sshKeys := []*hcloud.SSHKey{{ID: sshKeyId}}
|
|
|
|
for _, k := range c.SSHKeys {
|
|
|
|
sshKey, _, err := client.SSHKey.Get(ctx, k)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", fmt.Errorf("Error fetching SSH key: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-12-14 16:30:26 -05:00
|
|
|
if sshKey == nil {
|
|
|
|
state.Put("error", fmt.Errorf("Could not find key: %s", k))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-11-20 13:57:44 -05:00
|
|
|
sshKeys = append(sshKeys, sshKey)
|
|
|
|
}
|
|
|
|
|
2019-08-05 04:10:59 -04:00
|
|
|
var image *hcloud.Image
|
|
|
|
if c.Image != "" {
|
|
|
|
image = &hcloud.Image{Name: c.Image}
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
image, err = getImageWithSelectors(ctx, client, c)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
ui.Message(fmt.Sprintf("Using image %s with ID %d", image.Description, image.ID))
|
|
|
|
}
|
|
|
|
|
2019-03-07 05:41:39 -05:00
|
|
|
serverCreateResult, _, err := client.Server.Create(ctx, hcloud.ServerCreateOpts{
|
2018-10-17 06:15:47 -04:00
|
|
|
Name: c.ServerName,
|
|
|
|
ServerType: &hcloud.ServerType{Name: c.ServerType},
|
2019-08-05 04:10:59 -04:00
|
|
|
Image: image,
|
2018-11-20 13:57:44 -05:00
|
|
|
SSHKeys: sshKeys,
|
2018-10-17 06:15:47 -04:00
|
|
|
Location: &hcloud.Location{Name: c.Location},
|
|
|
|
UserData: userData,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-10-18 02:03:04 -04:00
|
|
|
err := fmt.Errorf("Error creating server: %s", err)
|
2018-10-17 06:15:47 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
state.Put("server_ip", serverCreateResult.Server.PublicNet.IPv4.IP.String())
|
|
|
|
// We use this in cleanup
|
|
|
|
s.serverId = serverCreateResult.Server.ID
|
|
|
|
|
|
|
|
// Store the server id for later
|
|
|
|
state.Put("server_id", serverCreateResult.Server.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", serverCreateResult.Server.ID)
|
2018-10-17 06:15:47 -04:00
|
|
|
|
2019-03-07 05:41:39 -05:00
|
|
|
if err := waitForAction(ctx, client, serverCreateResult.Action); err != nil {
|
2018-11-24 11:56:09 -05:00
|
|
|
err := fmt.Errorf("Error creating server: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
for _, nextAction := range serverCreateResult.NextActions {
|
2019-03-07 05:41:39 -05:00
|
|
|
if err := waitForAction(ctx, client, nextAction); err != nil {
|
2018-11-24 11:56:09 -05:00
|
|
|
err := fmt.Errorf("Error creating server: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2018-10-17 06:15:47 -04:00
|
|
|
}
|
|
|
|
}
|
2018-11-23 14:35:32 -05:00
|
|
|
|
|
|
|
if c.RescueMode != "" {
|
2019-03-07 01:24:22 -05:00
|
|
|
ui.Say("Enabling Rescue Mode...")
|
2019-03-07 05:41:39 -05:00
|
|
|
rootPassword, err := setRescue(ctx, client, serverCreateResult.Server, c.RescueMode, sshKeys)
|
2019-03-06 01:57:51 -05:00
|
|
|
if err != nil {
|
2018-11-23 14:35:32 -05:00
|
|
|
err := fmt.Errorf("Error enabling rescue mode: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2019-03-07 01:24:22 -05:00
|
|
|
}
|
|
|
|
ui.Say("Reboot server...")
|
2019-03-07 05:41:39 -05:00
|
|
|
action, _, err := client.Server.Reset(ctx, serverCreateResult.Server)
|
2019-03-07 01:24:22 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error rebooting server: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-03-07 05:41:39 -05:00
|
|
|
if err := waitForAction(ctx, client, action); err != nil {
|
2019-03-07 01:24:22 -05:00
|
|
|
err := fmt.Errorf("Error rebooting server: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
2019-03-06 01:57:51 -05:00
|
|
|
if c.RescueMode == "freebsd64" {
|
|
|
|
// We will set this only on freebsd
|
|
|
|
ui.Say("Using Root Password instead of SSH Keys...")
|
|
|
|
c.Comm.SSHPassword = rootPassword
|
|
|
|
}
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
2018-10-17 06:15:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
|
|
|
|
// If the serverID isn't there, we probably never created it
|
|
|
|
if s.serverId == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state.Get("hcloudClient").(*hcloud.Client)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-10-17 06:15:47 -04:00
|
|
|
|
|
|
|
// Destroy the server we just created
|
|
|
|
ui.Say("Destroying server...")
|
|
|
|
_, err := client.Server.Delete(context.TODO(), &hcloud.Server{ID: s.serverId})
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error destroying server. Please destroy it manually: %s", err))
|
|
|
|
}
|
|
|
|
}
|
2018-11-23 14:35:32 -05:00
|
|
|
|
2019-03-06 01:57:51 -05:00
|
|
|
func setRescue(ctx context.Context, client *hcloud.Client, server *hcloud.Server, rescue string, sshKeys []*hcloud.SSHKey) (string, error) {
|
2018-11-23 14:35:32 -05:00
|
|
|
rescueChanged := false
|
|
|
|
if server.RescueEnabled {
|
|
|
|
rescueChanged = true
|
|
|
|
action, _, err := client.Server.DisableRescue(ctx, server)
|
|
|
|
if err != nil {
|
2019-03-06 01:57:51 -05:00
|
|
|
return "", err
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
2018-11-26 03:10:17 -05:00
|
|
|
if err := waitForAction(ctx, client, action); err != nil {
|
2019-03-06 01:57:51 -05:00
|
|
|
return "", err
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if rescue != "" {
|
|
|
|
rescueChanged = true
|
2019-03-06 01:57:51 -05:00
|
|
|
if rescue == "freebsd64" {
|
|
|
|
sshKeys = nil // freebsd64 doesn't allow ssh keys so we will remove them here
|
|
|
|
}
|
2018-11-23 14:35:32 -05:00
|
|
|
res, _, err := client.Server.EnableRescue(ctx, server, hcloud.ServerEnableRescueOpts{
|
|
|
|
Type: hcloud.ServerRescueType(rescue),
|
|
|
|
SSHKeys: sshKeys,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-03-06 01:57:51 -05:00
|
|
|
return "", err
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
2018-11-26 03:10:17 -05:00
|
|
|
if err := waitForAction(ctx, client, res.Action); err != nil {
|
2019-03-06 01:57:51 -05:00
|
|
|
return "", err
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
2019-03-06 01:57:51 -05:00
|
|
|
return res.RootPassword, nil
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
|
|
|
if rescueChanged {
|
2018-11-26 03:49:45 -05:00
|
|
|
action, _, err := client.Server.Reset(ctx, server)
|
2018-11-23 14:35:32 -05:00
|
|
|
if err != nil {
|
2019-03-06 01:57:51 -05:00
|
|
|
return "", err
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
2018-11-26 03:10:17 -05:00
|
|
|
if err := waitForAction(ctx, client, action); err != nil {
|
2019-03-06 01:57:51 -05:00
|
|
|
return "", err
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 01:57:51 -05:00
|
|
|
return "", nil
|
2018-11-23 14:35:32 -05:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:10:17 -05:00
|
|
|
func waitForAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action) error {
|
2018-11-23 14:35:32 -05:00
|
|
|
_, errCh := client.Action.WatchProgress(ctx, action)
|
|
|
|
if err := <-errCh; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-05 04:10:59 -04:00
|
|
|
|
|
|
|
func getImageWithSelectors(ctx context.Context, client *hcloud.Client, c *Config) (*hcloud.Image, error) {
|
|
|
|
var allImages []*hcloud.Image
|
|
|
|
|
|
|
|
var selector = strings.Join(c.ImageFilter.WithSelector, ",")
|
|
|
|
opts := hcloud.ImageListOpts{
|
|
|
|
ListOpts: hcloud.ListOpts{LabelSelector: selector},
|
|
|
|
Status: []hcloud.ImageStatus{hcloud.ImageStatusAvailable},
|
|
|
|
}
|
|
|
|
|
|
|
|
allImages, err := client.Image.AllWithOpts(ctx, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(allImages) == 0 {
|
|
|
|
return nil, fmt.Errorf("no image found for selector %q", selector)
|
|
|
|
}
|
|
|
|
if len(allImages) > 1 {
|
|
|
|
if !c.ImageFilter.MostRecent {
|
|
|
|
return nil, fmt.Errorf("more than one image found for selector %q", selector)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(allImages, func(i, j int) bool {
|
|
|
|
return allImages[i].Created.After(allImages[j].Created)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return allImages[0], nil
|
|
|
|
}
|