use build name to ensure that winrm password and other shared state is not overwritten if two builders need the password in the same packer run.

This commit is contained in:
Megan Marsh 2018-04-16 11:51:04 -07:00
parent 747d1eba27
commit 3afb243f11
11 changed files with 49 additions and 42 deletions

View File

@ -21,9 +21,10 @@ import (
// StepGetPassword reads the password from a Windows server and sets it // StepGetPassword reads the password from a Windows server and sets it
// on the WinRM config. // on the WinRM config.
type StepGetPassword struct { type StepGetPassword struct {
Debug bool Debug bool
Comm *communicator.Config Comm *communicator.Config
Timeout time.Duration Timeout time.Duration
BuildName string
} }
func (s *StepGetPassword) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepGetPassword) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
@ -94,13 +95,13 @@ WaitLoop:
"Password (since debug is enabled): %s", s.Comm.WinRMPassword)) "Password (since debug is enabled): %s", s.Comm.WinRMPassword))
} }
// store so that we can access this later during provisioning // store so that we can access this later during provisioning
commonhelper.SetSharedState("winrm_password", s.Comm.WinRMPassword) commonhelper.SetSharedState("winrm_password", s.Comm.WinRMPassword, s.BuildName)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *StepGetPassword) Cleanup(multistep.StateBag) { func (s *StepGetPassword) Cleanup(multistep.StateBag) {
commonhelper.RemoveSharedStateFile("winrm_password") commonhelper.RemoveSharedStateFile("winrm_password", s.BuildName)
} }
func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-chan struct{}) (string, error) { func (s *StepGetPassword) waitForPassword(state multistep.StateBag, cancel <-chan struct{}) (string, error) {

View File

@ -193,9 +193,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
instanceStep, instanceStep,
&awscommon.StepGetPassword{ &awscommon.StepGetPassword{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm, Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout, Timeout: b.config.WindowsPasswordTimeout,
BuildName: b.config.PackerBuildName,
}, },
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.RunConfig.Comm, Config: &b.config.RunConfig.Comm,

View File

@ -207,9 +207,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
instanceStep, instanceStep,
&awscommon.StepGetPassword{ &awscommon.StepGetPassword{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm, Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout, Timeout: b.config.WindowsPasswordTimeout,
BuildName: b.config.PackerBuildName,
}, },
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.RunConfig.Comm, Config: &b.config.RunConfig.Comm,

View File

@ -186,9 +186,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Ctx: b.config.ctx, Ctx: b.config.ctx,
}, },
&awscommon.StepGetPassword{ &awscommon.StepGetPassword{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm, Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout, Timeout: b.config.WindowsPasswordTimeout,
BuildName: b.config.PackerBuildName,
}, },
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.RunConfig.Comm, Config: &b.config.RunConfig.Comm,

View File

@ -269,9 +269,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
instanceStep, instanceStep,
&awscommon.StepGetPassword{ &awscommon.StepGetPassword{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm, Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout, Timeout: b.config.WindowsPasswordTimeout,
BuildName: b.config.PackerBuildName,
}, },
&communicator.StepConnect{ &communicator.StepConnect{
Config: &b.config.RunConfig.Comm, Config: &b.config.RunConfig.Comm,

View File

@ -177,7 +177,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
NewStepDeployTemplate(azureClient, ui, b.config, deploymentName, GetVirtualMachineDeployment), NewStepDeployTemplate(azureClient, ui, b.config, deploymentName, GetVirtualMachineDeployment),
NewStepGetIPAddress(azureClient, ui, endpointConnectType), NewStepGetIPAddress(azureClient, ui, endpointConnectType),
&StepSaveWinRMPassword{ &StepSaveWinRMPassword{
Password: b.config.tmpAdminPassword, Password: b.config.tmpAdminPassword,
BuildName: b.config.PackerBuildName,
}, },
&communicator.StepConnectWinRM{ &communicator.StepConnectWinRM{
Config: &b.config.Comm, Config: &b.config.Comm,

View File

@ -359,7 +359,7 @@ func setRuntimeValues(c *Config) {
c.tmpAdminPassword = tempName.AdminPassword c.tmpAdminPassword = tempName.AdminPassword
// store so that we can access this later during provisioning // store so that we can access this later during provisioning
commonhelper.SetSharedState("winrm_password", c.tmpAdminPassword) commonhelper.SetSharedState("winrm_password", c.tmpAdminPassword, c.PackerConfig.PackerBuildName)
c.tmpCertificatePassword = tempName.CertificatePassword c.tmpCertificatePassword = tempName.CertificatePassword
if c.TempComputeName == "" { if c.TempComputeName == "" {

View File

@ -8,15 +8,16 @@ import (
) )
type StepSaveWinRMPassword struct { type StepSaveWinRMPassword struct {
Password string Password string
BuildName string
} }
func (s *StepSaveWinRMPassword) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepSaveWinRMPassword) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
// store so that we can access this later during provisioning // store so that we can access this later during provisioning
commonhelper.SetSharedState("winrm_password", s.Password) commonhelper.SetSharedState("winrm_password", s.Password, s.BuildName)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *StepSaveWinRMPassword) Cleanup(multistep.StateBag) { func (s *StepSaveWinRMPassword) Cleanup(multistep.StateBag) {
commonhelper.RemoveSharedStateFile("winrm_password") commonhelper.RemoveSharedStateFile("winrm_password", s.BuildName)
} }

View File

@ -76,20 +76,20 @@ func (s *StepHTTPServer) Run(_ context.Context, state multistep.StateBag) multis
} }
func SetHTTPPort(port string) error { func SetHTTPPort(port string) error {
return common.SetSharedState("port", port) return common.SetSharedState("port", port, "")
} }
func SetHTTPIP(ip string) error { func SetHTTPIP(ip string) error {
return common.SetSharedState("ip", ip) return common.SetSharedState("ip", ip, "")
} }
func GetHTTPAddr() string { func GetHTTPAddr() string {
ip, err := common.RetrieveSharedState("ip") ip, err := common.RetrieveSharedState("ip", "")
if err != nil { if err != nil {
return "" return ""
} }
port, err := common.RetrieveSharedState("port") port, err := common.RetrieveSharedState("port", "")
if err != nil { if err != nil {
return "" return ""
} }
@ -101,6 +101,6 @@ func (s *StepHTTPServer) Cleanup(multistep.StateBag) {
// Close the listener so that the HTTP server stops // Close the listener so that the HTTP server stops
s.l.Close() s.l.Close()
} }
common.RemoveSharedStateFile("port") common.RemoveSharedStateFile("port", "")
common.RemoveSharedStateFile("ip") common.RemoveSharedStateFile("ip", "")
} }

View File

@ -9,23 +9,23 @@ import (
// Used to set variables which we need to access later in the build, where // Used to set variables which we need to access later in the build, where
// state bag and config information won't work // state bag and config information won't work
func sharedStateFilename(suffix string) string { func sharedStateFilename(suffix string, buildName string) string {
uuid := os.Getenv("PACKER_RUN_UUID") uuid := os.Getenv("PACKER_RUN_UUID")
return filepath.Join(os.TempDir(), fmt.Sprintf("packer-%s-%s", uuid, suffix)) return filepath.Join(os.TempDir(), fmt.Sprintf("packer-%s-%s-%s", uuid, suffix, buildName))
} }
func SetSharedState(key string, value string) error { func SetSharedState(key string, value string, buildName string) error {
return ioutil.WriteFile(sharedStateFilename(key), []byte(value), 0600) return ioutil.WriteFile(sharedStateFilename(key, buildName), []byte(value), 0600)
} }
func RetrieveSharedState(key string) (string, error) { func RetrieveSharedState(key string, buildName string) (string, error) {
value, err := ioutil.ReadFile(sharedStateFilename(key)) value, err := ioutil.ReadFile(sharedStateFilename(key, buildName))
if err != nil { if err != nil {
return "", err return "", err
} }
return string(value), nil return string(value), nil
} }
func RemoveSharedStateFile(key string) { func RemoveSharedStateFile(key string, buildName string) {
os.Remove(sharedStateFilename(key)) os.Remove(sharedStateFilename(key, buildName))
} }

View File

@ -377,7 +377,7 @@ func (p *Provisioner) createFlattenedEnvVars(elevated bool) (flattened string) {
// interpolate environment variables // interpolate environment variables
p.config.ctx.Data = &EnvVarsTemplate{ p.config.ctx.Data = &EnvVarsTemplate{
WinRMPassword: getWinRMPassword(), WinRMPassword: getWinRMPassword(p.config.PackerBuildName),
} }
// Split vars into key/value components // Split vars into key/value components
for _, envVar := range p.config.Vars { for _, envVar := range p.config.Vars {
@ -445,7 +445,7 @@ func (p *Provisioner) createCommandTextNonPrivileged() (command string, err erro
p.config.ctx.Data = &ExecuteCommandTemplate{ p.config.ctx.Data = &ExecuteCommandTemplate{
Path: p.config.RemotePath, Path: p.config.RemotePath,
Vars: envVarPath, Vars: envVarPath,
WinRMPassword: getWinRMPassword(), WinRMPassword: getWinRMPassword(p.config.PackerBuildName),
} }
command, err = interpolate.Render(p.config.ExecuteCommand, &p.config.ctx) command, err = interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
@ -457,8 +457,8 @@ func (p *Provisioner) createCommandTextNonPrivileged() (command string, err erro
return command, nil return command, nil
} }
func getWinRMPassword() string { func getWinRMPassword(buildName string) string {
winRMPass, _ := commonhelper.RetrieveSharedState("winrm_password") winRMPass, _ := commonhelper.RetrieveSharedState("winrm_password", buildName)
return winRMPass return winRMPass
} }
@ -472,7 +472,7 @@ func (p *Provisioner) createCommandTextPrivileged() (command string, err error)
p.config.ctx.Data = &ExecuteCommandTemplate{ p.config.ctx.Data = &ExecuteCommandTemplate{
Path: p.config.RemotePath, Path: p.config.RemotePath,
Vars: envVarPath, Vars: envVarPath,
WinRMPassword: getWinRMPassword(), WinRMPassword: getWinRMPassword(p.config.PackerBuildName),
} }
command, err = interpolate.Render(p.config.ElevatedExecuteCommand, &p.config.ctx) command, err = interpolate.Render(p.config.ElevatedExecuteCommand, &p.config.ctx)
if err != nil { if err != nil {
@ -530,7 +530,7 @@ func (p *Provisioner) generateElevatedRunner(command string) (uploadedPath strin
} }
// Replace ElevatedPassword for winrm users who used this feature // Replace ElevatedPassword for winrm users who used this feature
p.config.ctx.Data = &EnvVarsTemplate{ p.config.ctx.Data = &EnvVarsTemplate{
WinRMPassword: getWinRMPassword(), WinRMPassword: getWinRMPassword(p.config.PackerBuildName),
} }
p.config.ElevatedPassword, _ = interpolate.Render(p.config.ElevatedPassword, &p.config.ctx) p.config.ElevatedPassword, _ = interpolate.Render(p.config.ElevatedPassword, &p.config.ctx)