2013-07-21 00:00:12 -04:00
|
|
|
package instance
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-07-21 00:00:12 -04:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -05:00
|
|
|
"os"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-07-21 00:00:12 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepUploadX509Cert struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepUploadX509Cert) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 16:03:13 -04:00
|
|
|
comm := state.Get("communicator").(packer.Communicator)
|
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-07-21 00:00:12 -04:00
|
|
|
|
|
|
|
x509RemoteCertPath := config.X509UploadPath + "/cert.pem"
|
|
|
|
x509RemoteKeyPath := config.X509UploadPath + "/key.pem"
|
|
|
|
|
|
|
|
ui.Say("Uploading X509 Certificate...")
|
|
|
|
if err := s.uploadSingle(comm, x509RemoteCertPath, config.X509CertPath); err != nil {
|
2013-08-31 16:03:13 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error uploading X509 cert: %s", err))
|
|
|
|
ui.Error(state.Get("error").(error).Error())
|
2013-07-21 00:00:12 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.uploadSingle(comm, x509RemoteKeyPath, config.X509KeyPath); err != nil {
|
2013-08-31 16:03:13 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error uploading X509 cert: %s", err))
|
|
|
|
ui.Error(state.Get("error").(error).Error())
|
2013-07-21 00:00:12 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 16:03:13 -04:00
|
|
|
state.Put("x509RemoteCertPath", x509RemoteCertPath)
|
|
|
|
state.Put("x509RemoteKeyPath", x509RemoteKeyPath)
|
2013-07-24 16:41:49 -04:00
|
|
|
|
2013-07-21 00:03:29 -04:00
|
|
|
return multistep.ActionContinue
|
2013-07-21 00:00:12 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 16:03:13 -04:00
|
|
|
func (s *StepUploadX509Cert) Cleanup(multistep.StateBag) {}
|
2013-07-21 00:00:12 -04:00
|
|
|
|
|
|
|
func (s *StepUploadX509Cert) uploadSingle(comm packer.Communicator, dst, src string) error {
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-05-10 00:03:35 -04:00
|
|
|
return comm.Upload(dst, f, nil)
|
2013-07-21 00:00:12 -04:00
|
|
|
}
|