github.phpd.cn/hashicorp/packer@v1.3.2/builder/oracle/oci/step_get_default_credentials.go (about)

     1  package oci
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  
     8  	commonhelper "github.com/hashicorp/packer/helper/common"
     9  	"github.com/hashicorp/packer/helper/communicator"
    10  	"github.com/hashicorp/packer/helper/multistep"
    11  	"github.com/hashicorp/packer/packer"
    12  )
    13  
    14  type stepGetDefaultCredentials struct {
    15  	Debug     bool
    16  	Comm      *communicator.Config
    17  	BuildName string
    18  }
    19  
    20  func (s *stepGetDefaultCredentials) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
    21  	var (
    22  		driver = state.Get("driver").(*driverOCI)
    23  		ui     = state.Get("ui").(packer.Ui)
    24  		id     = state.Get("instance_id").(string)
    25  	)
    26  
    27  	// Skip if we're not using winrm
    28  	if s.Comm.Type != "winrm" {
    29  		log.Printf("[INFO] Not using winrm communicator, skipping get password...")
    30  		return multistep.ActionContinue
    31  	}
    32  
    33  	// If we already have a password, skip it
    34  	if s.Comm.WinRMPassword != "" {
    35  		ui.Say("Skipping waiting for password since WinRM password set...")
    36  		return multistep.ActionContinue
    37  	}
    38  
    39  	username, password, err := driver.GetInstanceInitialCredentials(ctx, id)
    40  	if err != nil {
    41  		err = fmt.Errorf("Error getting instance's credentials: %s", err)
    42  		ui.Error(err.Error())
    43  		state.Put("error", err)
    44  		return multistep.ActionHalt
    45  	}
    46  	s.Comm.WinRMPassword = password
    47  	s.Comm.WinRMUser = username
    48  
    49  	if s.Debug {
    50  		ui.Message(fmt.Sprintf(
    51  			"[DEBUG] (OCI default credentials): Credentials (since debug is enabled): %s", password))
    52  	}
    53  
    54  	// store so that we can access this later during provisioning
    55  	commonhelper.SetSharedState("winrm_password", s.Comm.WinRMPassword, s.BuildName)
    56  	packer.LogSecretFilter.Set(s.Comm.WinRMPassword)
    57  	return multistep.ActionContinue
    58  }
    59  
    60  func (s *stepGetDefaultCredentials) Cleanup(state multistep.StateBag) {
    61  	// no cleanup
    62  }