packer-cn/builder/hcloud/step_create_server.go

173 lines
4.7 KiB
Go
Raw Normal View History

2018-10-17 06:15:47 -04:00
package hcloud
import (
"context"
"fmt"
"io/ioutil"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"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)
ui := state.Get("ui").(packer.Ui)
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 01:57:51 -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)
}
2018-10-17 06:15:47 -04:00
serverCreateResult, _, err := client.Server.Create(context.TODO(), hcloud.ServerCreateOpts{
Name: c.ServerName,
ServerType: &hcloud.ServerType{Name: c.ServerType},
Image: &hcloud.Image{Name: c.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)
2018-11-26 03:10:17 -05:00
if err := waitForAction(context.TODO(), client, serverCreateResult.Action); err != nil {
err := fmt.Errorf("Error creating server: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
for _, nextAction := range serverCreateResult.NextActions {
2018-11-26 03:10:17 -05:00
if err := waitForAction(context.TODO(), client, nextAction); err != nil {
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
}
}
if c.RescueMode != "" {
2019-03-06 01:57:51 -05:00
rootPassword, err := setRescue(context.TODO(), client, serverCreateResult.Server, c.RescueMode, sshKeys)
if err != nil {
err := fmt.Errorf("Error enabling rescue mode: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
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
}
}
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)
ui := state.Get("ui").(packer.Ui)
// 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))
}
}
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) {
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-26 03:10:17 -05:00
if err := waitForAction(ctx, client, action); err != nil {
2019-03-06 01:57:51 -05:00
return "", err
}
}
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
}
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-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
}
2019-03-06 01:57:51 -05:00
return res.RootPassword, nil
}
if rescueChanged {
2018-11-26 03:49:45 -05:00
action, _, err := client.Server.Reset(ctx, server)
if err != nil {
2019-03-06 01:57:51 -05:00
return "", err
}
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
}
}
2019-03-06 01:57:51 -05:00
return "", nil
}
2018-11-26 03:10:17 -05:00
func waitForAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action) error {
_, errCh := client.Action.WatchProgress(ctx, action)
if err := <-errCh; err != nil {
return err
}
return nil
}