github.com/rothwerx/packer@v0.9.0/builder/amazon/instance/step_upload_x509_cert.go (about)

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