github.phpd.cn/hashicorp/packer@v1.3.2/builder/googlecompute/step_wait_startup_script.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/hashicorp/packer/common"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  type StepWaitStartupScript int
    14  
    15  // Run reads the instance metadata and looks for the log entry
    16  // indicating the startup script finished.
    17  func (s *StepWaitStartupScript) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    18  	config := state.Get("config").(*Config)
    19  	driver := state.Get("driver").(Driver)
    20  	ui := state.Get("ui").(packer.Ui)
    21  	instanceName := state.Get("instance_name").(string)
    22  
    23  	ui.Say("Waiting for any running startup script to finish...")
    24  
    25  	// Keep checking the serial port output to see if the startup script is done.
    26  	err := common.Retry(10, 60, 0, func(_ uint) (bool, error) {
    27  		status, err := driver.GetInstanceMetadata(config.Zone,
    28  			instanceName, StartupScriptStatusKey)
    29  
    30  		if err != nil {
    31  			err := fmt.Errorf("Error getting startup script status: %s", err)
    32  			return false, err
    33  		}
    34  
    35  		if status == StartupScriptStatusError {
    36  			err = errors.New("Startup script error.")
    37  			return false, err
    38  		}
    39  
    40  		done := status == StartupScriptStatusDone
    41  		if !done {
    42  			ui.Say("Startup script not finished yet. Waiting...")
    43  		}
    44  
    45  		return done, nil
    46  	})
    47  
    48  	if err != nil {
    49  		err := fmt.Errorf("Error waiting for startup script to finish: %s", err)
    50  		state.Put("error", err)
    51  		ui.Error(err.Error())
    52  		return multistep.ActionHalt
    53  	}
    54  	ui.Say("Startup script, if any, has finished running.")
    55  	return multistep.ActionContinue
    56  }
    57  
    58  // Cleanup.
    59  func (s *StepWaitStartupScript) Cleanup(state multistep.StateBag) {}