github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/instance/step_upload_x509_cert.go (about)

     1  package instance
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type StepUploadX509Cert struct{}
    13  
    14  func (s *StepUploadX509Cert) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    15  	comm := state.Get("communicator").(packer.Communicator)
    16  	config := state.Get("config").(*Config)
    17  	ui := state.Get("ui").(packer.Ui)
    18  
    19  	x509RemoteCertPath := config.X509UploadPath + "/cert.pem"
    20  	x509RemoteKeyPath := config.X509UploadPath + "/key.pem"
    21  
    22  	ui.Say("Uploading X509 Certificate...")
    23  	if err := s.uploadSingle(comm, x509RemoteCertPath, config.X509CertPath); err != nil {
    24  		state.Put("error", fmt.Errorf("Error uploading X509 cert: %s", err))
    25  		ui.Error(state.Get("error").(error).Error())
    26  		return multistep.ActionHalt
    27  	}
    28  
    29  	if err := s.uploadSingle(comm, x509RemoteKeyPath, config.X509KeyPath); err != nil {
    30  		state.Put("error", fmt.Errorf("Error uploading X509 cert: %s", err))
    31  		ui.Error(state.Get("error").(error).Error())
    32  		return multistep.ActionHalt
    33  	}
    34  
    35  	state.Put("x509RemoteCertPath", x509RemoteCertPath)
    36  	state.Put("x509RemoteKeyPath", x509RemoteKeyPath)
    37  
    38  	return multistep.ActionContinue
    39  }
    40  
    41  func (s *StepUploadX509Cert) Cleanup(multistep.StateBag) {}
    42  
    43  func (s *StepUploadX509Cert) uploadSingle(comm packer.Communicator, dst, src string) error {
    44  	f, err := os.Open(src)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	defer f.Close()
    49  
    50  	return comm.Upload(dst, f, nil)
    51  }