github.phpd.cn/hashicorp/packer@v1.3.2/builder/hcloud/step_create_server.go (about)

     1  package hcloud
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"io/ioutil"
     8  
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"github.com/hashicorp/packer/packer"
    11  	"github.com/hetznercloud/hcloud-go/hcloud"
    12  )
    13  
    14  type stepCreateServer struct {
    15  	serverId int
    16  }
    17  
    18  func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    19  	client := state.Get("hcloudClient").(*hcloud.Client)
    20  	ui := state.Get("ui").(packer.Ui)
    21  	c := state.Get("config").(*Config)
    22  	sshKeyId := state.Get("ssh_key_id").(int)
    23  
    24  	// Create the server based on configuration
    25  	ui.Say("Creating server...")
    26  
    27  	userData := c.UserData
    28  	if c.UserDataFile != "" {
    29  		contents, err := ioutil.ReadFile(c.UserDataFile)
    30  		if err != nil {
    31  			state.Put("error", fmt.Errorf("Problem reading user data file: %s", err))
    32  			return multistep.ActionHalt
    33  		}
    34  
    35  		userData = string(contents)
    36  	}
    37  
    38  	serverCreateResult, _, err := client.Server.Create(context.TODO(), hcloud.ServerCreateOpts{
    39  		Name:       c.ServerName,
    40  		ServerType: &hcloud.ServerType{Name: c.ServerType},
    41  		Image:      &hcloud.Image{Name: c.Image},
    42  		SSHKeys:    []*hcloud.SSHKey{{ID: sshKeyId}},
    43  		Location:   &hcloud.Location{Name: c.Location},
    44  		UserData:   userData,
    45  	})
    46  	if err != nil {
    47  		err := fmt.Errorf("Error creating server: %s", err)
    48  		state.Put("error", err)
    49  		ui.Error(err.Error())
    50  		return multistep.ActionHalt
    51  	}
    52  	state.Put("server_ip", serverCreateResult.Server.PublicNet.IPv4.IP.String())
    53  	// We use this in cleanup
    54  	s.serverId = serverCreateResult.Server.ID
    55  
    56  	// Store the server id for later
    57  	state.Put("server_id", serverCreateResult.Server.ID)
    58  
    59  	_, errCh := client.Action.WatchProgress(context.TODO(), serverCreateResult.Action)
    60  	for {
    61  		select {
    62  		case err1 := <-errCh:
    63  			if err1 == nil {
    64  				return multistep.ActionContinue
    65  			} else {
    66  				err := fmt.Errorf("Error creating server: %s", err)
    67  				state.Put("error", err)
    68  				ui.Error(err.Error())
    69  				return multistep.ActionHalt
    70  			}
    71  
    72  		}
    73  	}
    74  }
    75  
    76  func (s *stepCreateServer) Cleanup(state multistep.StateBag) {
    77  	// If the serverID isn't there, we probably never created it
    78  	if s.serverId == 0 {
    79  		return
    80  	}
    81  
    82  	client := state.Get("hcloudClient").(*hcloud.Client)
    83  	ui := state.Get("ui").(packer.Ui)
    84  
    85  	// Destroy the server we just created
    86  	ui.Say("Destroying server...")
    87  	_, err := client.Server.Delete(context.TODO(), &hcloud.Server{ID: s.serverId})
    88  	if err != nil {
    89  		ui.Error(fmt.Sprintf(
    90  			"Error destroying server. Please destroy it manually: %s", err))
    91  	}
    92  }