2016-02-16 05:06:22 -05:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-02-16 05:06:22 -05:00
|
|
|
"crypto/rsa"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2016-02-16 05:06:22 -05:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepGetPassword reads the password from a booted OpenStack server and sets
|
|
|
|
// it on the WinRM config.
|
|
|
|
type StepGetPassword struct {
|
2019-08-01 20:02:36 -04:00
|
|
|
Debug bool
|
|
|
|
Comm *communicator.Config
|
|
|
|
BuildName string
|
2016-02-16 05:06:22 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepGetPassword) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2016-02-16 05:06:22 -05:00
|
|
|
|
|
|
|
// Skip if we're not using winrm
|
|
|
|
if s.Comm.Type != "winrm" {
|
|
|
|
log.Printf("[INFO] Not using winrm communicator, skipping get password...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we already have a password, skip it
|
|
|
|
if s.Comm.WinRMPassword != "" {
|
|
|
|
ui.Say("Skipping waiting for password since WinRM password set...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need the v2 compute client
|
|
|
|
computeClient, err := config.computeV2Client()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing compute client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Waiting for password since WinRM password is not set...")
|
|
|
|
server := state.Get("server").(*servers.Server)
|
|
|
|
var password string
|
|
|
|
|
2018-08-27 09:58:00 -04:00
|
|
|
privateKey, err := ssh.ParseRawPrivateKey(s.Comm.SSHPrivateKey)
|
2016-02-16 05:06:22 -05:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error parsing private key: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
for ; password == "" && err == nil; password, err = servers.GetPassword(computeClient, server.ID).ExtractPassword(privateKey.(*rsa.PrivateKey)) {
|
|
|
|
|
|
|
|
// Check for an interrupt in between attempts.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Retrying to get a administrator password evry 5 seconds.")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Password retrieved!"))
|
|
|
|
s.Comm.WinRMPassword = password
|
|
|
|
|
|
|
|
// In debug-mode, we output the password
|
|
|
|
if s.Debug {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Password (since debug is enabled) \"%s\"", s.Comm.WinRMPassword))
|
|
|
|
}
|
|
|
|
|
2020-11-19 17:03:11 -05:00
|
|
|
packersdk.LogSecretFilter.Set(s.Comm.WinRMPassword)
|
2019-08-01 20:02:36 -04:00
|
|
|
|
2016-02-16 05:06:22 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-12-12 19:42:53 -05:00
|
|
|
func (s *StepGetPassword) Cleanup(multistep.StateBag) {}
|