2013-07-16 02:43:01 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2019-09-24 12:44:19 -04:00
|
|
|
"fmt"
|
2013-07-16 02:43:01 -04:00
|
|
|
"log"
|
2019-12-13 16:03:09 -05:00
|
|
|
"os"
|
2013-08-31 02:28:31 -04:00
|
|
|
"time"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2019-10-04 14:36:57 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-07-16 02:43:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepProvision runs the provisioners.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// communicator packer.Communicator
|
|
|
|
// hook packer.Hook
|
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2019-10-04 14:36:57 -04:00
|
|
|
|
|
|
|
func PopulateProvisionHookData(state multistep.StateBag) map[string]interface{} {
|
2019-12-16 14:22:21 -05:00
|
|
|
hookData := make(map[string]interface{})
|
|
|
|
|
2019-12-14 06:44:09 -05:00
|
|
|
// Load Builder hook data from state, if it has been set.
|
2019-12-16 14:22:21 -05:00
|
|
|
hd, ok := state.GetOk("generated_data")
|
|
|
|
if ok {
|
|
|
|
hookData = hd.(map[string]interface{})
|
|
|
|
}
|
2019-12-14 06:44:09 -05:00
|
|
|
|
2019-12-13 16:03:09 -05:00
|
|
|
// instance_id is placed in state by the builders.
|
|
|
|
// Not yet implemented in Chroot, lxc/lxd, Azure, Qemu.
|
|
|
|
// Implemented in most others including digitalOcean (droplet id),
|
|
|
|
// docker (container_id), and clouds which use "server" internally instead
|
|
|
|
// of instance.
|
|
|
|
id, ok := state.GetOk("instance_id")
|
|
|
|
if ok {
|
|
|
|
hookData["ID"] = id
|
|
|
|
} else {
|
|
|
|
// Warn user that the id isn't implemented
|
|
|
|
hookData["ID"] = "ERR_ID_NOT_IMPLEMENTED_BY_BUILDER"
|
|
|
|
}
|
2020-01-16 06:04:03 -05:00
|
|
|
|
2019-12-16 14:22:21 -05:00
|
|
|
hookData["PackerRunUUID"] = os.Getenv("PACKER_RUN_UUID")
|
2020-01-27 10:49:31 -05:00
|
|
|
hookData["PackerHTTPAddr"] = GetHTTPAddr()
|
2019-12-13 16:03:09 -05:00
|
|
|
|
2019-10-04 14:36:57 -04:00
|
|
|
// Read communicator data into hook data
|
2019-12-13 13:02:13 -05:00
|
|
|
comm, ok := state.GetOk("communicator_config")
|
2019-10-04 14:36:57 -04:00
|
|
|
if !ok {
|
2020-01-20 10:29:38 -05:00
|
|
|
log.Printf("Unable to load communicator config from state to populate provisionHookData")
|
2019-10-04 14:36:57 -04:00
|
|
|
return hookData
|
|
|
|
}
|
2019-12-13 13:02:13 -05:00
|
|
|
commConf := comm.(*communicator.Config)
|
2019-10-04 14:36:57 -04:00
|
|
|
|
|
|
|
// Loop over all field values and retrieve them from the ssh config
|
2019-12-13 13:02:13 -05:00
|
|
|
hookData["Host"] = commConf.Host()
|
|
|
|
hookData["Port"] = commConf.Port()
|
|
|
|
hookData["User"] = commConf.User()
|
|
|
|
hookData["Password"] = commConf.Password()
|
2019-12-13 14:57:01 -05:00
|
|
|
hookData["ConnType"] = commConf.Type
|
2020-03-09 12:25:05 -04:00
|
|
|
hookData["SSHPublicKey"] = string(commConf.SSHPublicKey)
|
|
|
|
hookData["SSHPrivateKey"] = string(commConf.SSHPrivateKey)
|
2019-12-13 14:57:01 -05:00
|
|
|
|
2019-12-13 13:02:13 -05:00
|
|
|
// Backwards compatibility; in practice, WinRMPassword is fulfilled by
|
|
|
|
// Password.
|
|
|
|
hookData["WinRMPassword"] = commConf.WinRMPassword
|
|
|
|
|
2019-07-11 12:37:59 -04:00
|
|
|
return hookData
|
|
|
|
}
|
|
|
|
|
2019-12-11 18:43:11 -05:00
|
|
|
type StepProvision struct {
|
|
|
|
Comm packer.Communicator
|
|
|
|
}
|
|
|
|
|
2019-09-24 12:44:19 -04:00
|
|
|
func (s *StepProvision) runWithHook(ctx context.Context, state multistep.StateBag, hooktype string) multistep.StepAction {
|
|
|
|
// hooktype will be either packer.HookProvision or packer.HookCleanupProvision
|
2013-11-12 11:24:16 -05:00
|
|
|
comm := s.Comm
|
|
|
|
if comm == nil {
|
2015-06-15 13:26:46 -04:00
|
|
|
raw, ok := state.Get("communicator").(packer.Communicator)
|
|
|
|
if ok {
|
|
|
|
comm = raw.(packer.Communicator)
|
|
|
|
}
|
2013-11-12 11:24:16 -05:00
|
|
|
}
|
2019-07-11 12:37:59 -04:00
|
|
|
|
2013-08-31 15:17:59 -04:00
|
|
|
hook := state.Get("hook").(packer.Hook)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-16 02:43:01 -04:00
|
|
|
|
2019-07-11 12:37:59 -04:00
|
|
|
hookData := PopulateProvisionHookData(state)
|
|
|
|
|
2020-01-30 05:27:58 -05:00
|
|
|
// Update state generated_data with complete hookData
|
|
|
|
// to make them accessible by post-processors
|
|
|
|
state.Put("generated_data", hookData)
|
|
|
|
|
2013-08-31 02:28:31 -04:00
|
|
|
// Run the provisioner in a goroutine so we can continually check
|
|
|
|
// for cancellations...
|
2019-09-24 12:44:19 -04:00
|
|
|
if hooktype == packer.HookProvision {
|
|
|
|
log.Println("Running the provision hook")
|
|
|
|
} else if hooktype == packer.HookCleanupProvision {
|
|
|
|
ui.Say("Provisioning step had errors: Running the cleanup provisioner, if present...")
|
|
|
|
}
|
2013-08-31 02:28:31 -04:00
|
|
|
errCh := make(chan error, 1)
|
|
|
|
go func() {
|
2019-10-04 14:36:57 -04:00
|
|
|
errCh <- hook.Run(ctx, hooktype, ui, comm, hookData)
|
2013-08-31 02:28:31 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
2019-09-24 12:44:19 -04:00
|
|
|
if hooktype == packer.HookProvision {
|
|
|
|
// We don't overwrite the error if it's a cleanup
|
|
|
|
// provisioner being run.
|
|
|
|
state.Put("error", err)
|
|
|
|
} else if hooktype == packer.HookCleanupProvision {
|
|
|
|
origErr := state.Get("error").(error)
|
|
|
|
state.Put("error", fmt.Errorf("Cleanup failed: %s. "+
|
|
|
|
"Original Provisioning error: %s", err, origErr))
|
|
|
|
}
|
2013-08-31 02:28:31 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-16 02:43:01 -04:00
|
|
|
|
2013-08-31 02:28:31 -04:00
|
|
|
return multistep.ActionContinue
|
2019-03-27 07:29:09 -04:00
|
|
|
case <-ctx.Done():
|
|
|
|
log.Printf("Cancelling provisioning due to context cancellation: %s", ctx.Err())
|
|
|
|
return multistep.ActionHalt
|
2013-08-31 02:28:31 -04:00
|
|
|
case <-time.After(1 * time.Second):
|
2013-08-31 15:17:59 -04:00
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
2013-08-31 02:39:29 -04:00
|
|
|
log.Println("Cancelling provisioning due to interrupt...")
|
2013-08-31 02:28:31 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 02:43:01 -04:00
|
|
|
}
|
|
|
|
|
2019-09-24 12:44:19 -04:00
|
|
|
func (s *StepProvision) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
return s.runWithHook(ctx, state, packer.HookProvision)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepProvision) Cleanup(state multistep.StateBag) {
|
2019-09-25 16:43:29 -04:00
|
|
|
// We have a "final" provisioner that gets defined by "error-cleanup-provisioner"
|
2019-09-24 12:44:19 -04:00
|
|
|
// which we only call if there's an error during the provision run and
|
2019-09-25 16:43:29 -04:00
|
|
|
// the "error-cleanup-provisioner" is defined.
|
2019-09-24 12:44:19 -04:00
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
s.runWithHook(context.Background(), state, packer.HookCleanupProvision)
|
|
|
|
}
|
|
|
|
}
|