Skip to content

Commit

Permalink
use build name to ensure that winrm password and other shared state i…
Browse files Browse the repository at this point in the history
…s not overwritten if two builders need the password in the same packer run.
  • Loading branch information
SwampDragons committed Apr 16, 2018
1 parent 747d1eb commit 3afb243
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 42 deletions.
11 changes: 6 additions & 5 deletions builder/amazon/common/step_get_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
// StepGetPassword reads the password from a Windows server and sets it
// on the WinRM config.
type StepGetPassword struct {
Debug bool
Comm *communicator.Config
Timeout time.Duration
Debug bool
Comm *communicator.Config
Timeout time.Duration
BuildName string
}

func (s *StepGetPassword) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
Expand Down Expand Up @@ -94,13 +95,13 @@ WaitLoop:
"Password (since debug is enabled): %s", s.Comm.WinRMPassword))
}
// 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
}

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) {
Expand Down
7 changes: 4 additions & 3 deletions builder/amazon/ebs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
},
instanceStep,
&awscommon.StepGetPassword{
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout,
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout,
BuildName: b.config.PackerBuildName,
},
&communicator.StepConnect{
Config: &b.config.RunConfig.Comm,
Expand Down
7 changes: 4 additions & 3 deletions builder/amazon/ebssurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
},
instanceStep,
&awscommon.StepGetPassword{
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout,
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout,
BuildName: b.config.PackerBuildName,
},
&communicator.StepConnect{
Config: &b.config.RunConfig.Comm,
Expand Down
7 changes: 4 additions & 3 deletions builder/amazon/ebsvolume/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Ctx: b.config.ctx,
},
&awscommon.StepGetPassword{
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout,
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout,
BuildName: b.config.PackerBuildName,
},
&communicator.StepConnect{
Config: &b.config.RunConfig.Comm,
Expand Down
7 changes: 4 additions & 3 deletions builder/amazon/instance/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
},
instanceStep,
&awscommon.StepGetPassword{
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout,
Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm,
Timeout: b.config.WindowsPasswordTimeout,
BuildName: b.config.PackerBuildName,
},
&communicator.StepConnect{
Config: &b.config.RunConfig.Comm,
Expand Down
3 changes: 2 additions & 1 deletion builder/azure/arm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
NewStepGetIPAddress(azureClient, ui, endpointConnectType),
&StepSaveWinRMPassword{
Password: b.config.tmpAdminPassword,
Password: b.config.tmpAdminPassword,
BuildName: b.config.PackerBuildName,
},
&communicator.StepConnectWinRM{
Config: &b.config.Comm,
Expand Down
2 changes: 1 addition & 1 deletion builder/azure/arm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func setRuntimeValues(c *Config) {

c.tmpAdminPassword = tempName.AdminPassword
// 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
if c.TempComputeName == "" {
Expand Down
7 changes: 4 additions & 3 deletions builder/azure/arm/step_save_winrm_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (
)

type StepSaveWinRMPassword struct {
Password string
Password string
BuildName string
}

func (s *StepSaveWinRMPassword) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
// 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
}

func (s *StepSaveWinRMPassword) Cleanup(multistep.StateBag) {
commonhelper.RemoveSharedStateFile("winrm_password")
commonhelper.RemoveSharedStateFile("winrm_password", s.BuildName)
}
12 changes: 6 additions & 6 deletions common/step_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ func (s *StepHTTPServer) Run(_ context.Context, state multistep.StateBag) multis
}

func SetHTTPPort(port string) error {
return common.SetSharedState("port", port)
return common.SetSharedState("port", port, "")
}

func SetHTTPIP(ip string) error {
return common.SetSharedState("ip", ip)
return common.SetSharedState("ip", ip, "")
}

func GetHTTPAddr() string {
ip, err := common.RetrieveSharedState("ip")
ip, err := common.RetrieveSharedState("ip", "")
if err != nil {
return ""
}

port, err := common.RetrieveSharedState("port")
port, err := common.RetrieveSharedState("port", "")
if err != nil {
return ""
}
Expand All @@ -101,6 +101,6 @@ func (s *StepHTTPServer) Cleanup(multistep.StateBag) {
// Close the listener so that the HTTP server stops
s.l.Close()
}
common.RemoveSharedStateFile("port")
common.RemoveSharedStateFile("ip")
common.RemoveSharedStateFile("port", "")
common.RemoveSharedStateFile("ip", "")
}
16 changes: 8 additions & 8 deletions helper/common/shared_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (

// Used to set variables which we need to access later in the build, where
// 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")
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 {
return ioutil.WriteFile(sharedStateFilename(key), []byte(value), 0600)
func SetSharedState(key string, value string, buildName string) error {
return ioutil.WriteFile(sharedStateFilename(key, buildName), []byte(value), 0600)
}

func RetrieveSharedState(key string) (string, error) {
value, err := ioutil.ReadFile(sharedStateFilename(key))
func RetrieveSharedState(key string, buildName string) (string, error) {
value, err := ioutil.ReadFile(sharedStateFilename(key, buildName))
if err != nil {
return "", err
}
return string(value), nil
}

func RemoveSharedStateFile(key string) {
os.Remove(sharedStateFilename(key))
func RemoveSharedStateFile(key string, buildName string) {
os.Remove(sharedStateFilename(key, buildName))
}
12 changes: 6 additions & 6 deletions provisioner/powershell/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (p *Provisioner) createFlattenedEnvVars(elevated bool) (flattened string) {

// interpolate environment variables
p.config.ctx.Data = &EnvVarsTemplate{
WinRMPassword: getWinRMPassword(),
WinRMPassword: getWinRMPassword(p.config.PackerBuildName),
}
// Split vars into key/value components
for _, envVar := range p.config.Vars {
Expand Down Expand Up @@ -445,7 +445,7 @@ func (p *Provisioner) createCommandTextNonPrivileged() (command string, err erro
p.config.ctx.Data = &ExecuteCommandTemplate{
Path: p.config.RemotePath,
Vars: envVarPath,
WinRMPassword: getWinRMPassword(),
WinRMPassword: getWinRMPassword(p.config.PackerBuildName),
}
command, err = interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)

Expand All @@ -457,8 +457,8 @@ func (p *Provisioner) createCommandTextNonPrivileged() (command string, err erro
return command, nil
}

func getWinRMPassword() string {
winRMPass, _ := commonhelper.RetrieveSharedState("winrm_password")
func getWinRMPassword(buildName string) string {
winRMPass, _ := commonhelper.RetrieveSharedState("winrm_password", buildName)
return winRMPass
}

Expand All @@ -472,7 +472,7 @@ func (p *Provisioner) createCommandTextPrivileged() (command string, err error)
p.config.ctx.Data = &ExecuteCommandTemplate{
Path: p.config.RemotePath,
Vars: envVarPath,
WinRMPassword: getWinRMPassword(),
WinRMPassword: getWinRMPassword(p.config.PackerBuildName),
}
command, err = interpolate.Render(p.config.ElevatedExecuteCommand, &p.config.ctx)
if err != nil {
Expand Down Expand Up @@ -530,7 +530,7 @@ func (p *Provisioner) generateElevatedRunner(command string) (uploadedPath strin
}
// Replace ElevatedPassword for winrm users who used this feature
p.config.ctx.Data = &EnvVarsTemplate{
WinRMPassword: getWinRMPassword(),
WinRMPassword: getWinRMPassword(p.config.PackerBuildName),
}

p.config.ElevatedPassword, _ = interpolate.Render(p.config.ElevatedPassword, &p.config.ctx)
Expand Down

0 comments on commit 3afb243

Please sign in to comment.