From 8a0aa68a4ee11d82ec33d42298bc05995e6c5292 Mon Sep 17 00:00:00 2001 From: Dave Woodward Date: Sat, 31 Oct 2020 20:26:33 -0500 Subject: [PATCH 1/3] Add calls to winrepo.update_git_repos and pkg.refresh_db if the operating system is Windows. --- go.mod | 1 + go.sum | 3 +++ provisioner/salt-masterless/provisioner.go | 26 ++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/go.mod b/go.mod index c53e9c9d8..6d4674aa8 100644 --- a/go.mod +++ b/go.mod @@ -93,6 +93,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-testing-interface v1.0.3 // indirect github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed + github.com/mitchellh/gox v1.0.1 // indirect github.com/mitchellh/iochan v1.0.0 github.com/mitchellh/mapstructure v1.2.3 github.com/mitchellh/panicwrap v1.0.0 diff --git a/go.sum b/go.sum index 8a908afec..65e7fb9c7 100644 --- a/go.sum +++ b/go.sum @@ -376,6 +376,7 @@ github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1 github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.1.0 h1:bPIoEKD27tNdebFGGxxYwcL4nepeY4j1QP23PFRGzg0= github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E= @@ -510,6 +511,8 @@ github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZX github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4= github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= +github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI= +github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4= github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY= github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= diff --git a/provisioner/salt-masterless/provisioner.go b/provisioner/salt-masterless/provisioner.go index d343e3ef5..032cba88c 100644 --- a/provisioner/salt-masterless/provisioner.go +++ b/provisioner/salt-masterless/provisioner.go @@ -398,6 +398,32 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C } } + if p.config.GuestOSType == provisioner.WindowsOSType { + { + ui.Message("Running salt-call --local winrepo.update_git_repos") + cmd := &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("%s --local winrepo.update_git_repos", filepath.Join(p.config.SaltBinDir, "salt-call")))} + if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) + } + + return fmt.Errorf("Error executing salt-call --local winrepo.update_git_repos: %s", err) + } + } + + { + ui.Message("Running salt-call --local pkg.refresh_db") + cmd := &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("%s --local pkg.refresh_db", filepath.Join(p.config.SaltBinDir, "salt-call")))} + if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) + } + + return fmt.Errorf("Error executing salt-call --local pkg.refresh_db: %s", err) + } + } + } + ui.Message(fmt.Sprintf("Running: salt-call --local %s", p.config.CmdArgs)) cmd := &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("%s --local %s", filepath.Join(p.config.SaltBinDir, "salt-call"), p.config.CmdArgs))} if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { From 4152d527df5e1bcd5db5fe6987e8be81ccdcca35 Mon Sep 17 00:00:00 2001 From: Dave Woodward Date: Mon, 2 Nov 2020 21:27:11 -0600 Subject: [PATCH 2/3] Include download and installation of Git to satisfy the winrepo.update_git_repos prerequisite. Also removed unneeded p.sudo since we know we are working with Windows. --- provisioner/salt-masterless/provisioner.go | 40 ++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/provisioner/salt-masterless/provisioner.go b/provisioner/salt-masterless/provisioner.go index 032cba88c..33a748b61 100644 --- a/provisioner/salt-masterless/provisioner.go +++ b/provisioner/salt-masterless/provisioner.go @@ -399,9 +399,45 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C } if p.config.GuestOSType == provisioner.WindowsOSType { + { + ui.Message("Downloading Git for Windows") + cmd := &packer.RemoteCmd{Command: fmt.Sprintf("powershell [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.28.0.windows.1/Git-2.28.0-64-bit.exe -OutFile $env:TEMP/Git.exe")} + if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) + } + + return fmt.Errorf("Unable to Download Git for Windows: %s", err) + } + } + + { + ui.Message("Installing Git for Windows") + cmd := &packer.RemoteCmd{Command: fmt.Sprintf("powershell Start-Process -FilePath $env:TEMP/Git.exe /SILENT -Wait")} + if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) + } + + return fmt.Errorf("Unable to Install Git for Windows: %s", err) + } + } + + { + ui.Message("Cleaning Up After Git for Windows") + cmd := &packer.RemoteCmd{Command: fmt.Sprintf("powershell Remove-Item $env:TEMP/Git.exe")} + if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) + } + + return fmt.Errorf("Unable to Clean-up After Git for Windows: %s", err) + } + } + { ui.Message("Running salt-call --local winrepo.update_git_repos") - cmd := &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("%s --local winrepo.update_git_repos", filepath.Join(p.config.SaltBinDir, "salt-call")))} + cmd := &packer.RemoteCmd{Command: fmt.Sprintf("%s --local winrepo.update_git_repos", filepath.Join(p.config.SaltBinDir, "salt-call"))} if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { if err == nil { err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) @@ -413,7 +449,7 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C { ui.Message("Running salt-call --local pkg.refresh_db") - cmd := &packer.RemoteCmd{Command: p.sudo(fmt.Sprintf("%s --local pkg.refresh_db", filepath.Join(p.config.SaltBinDir, "salt-call")))} + cmd := &packer.RemoteCmd{Command: fmt.Sprintf("%s --local pkg.refresh_db", filepath.Join(p.config.SaltBinDir, "salt-call"))} if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { if err == nil { err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) From 257439786c753d739c856c0e5b798cfed74c7141 Mon Sep 17 00:00:00 2001 From: Dave Woodward Date: Tue, 3 Nov 2020 10:39:51 -0600 Subject: [PATCH 3/3] Remove brackets intended to scope usage of cmd variable and leverage unique variable names instead. --- provisioner/salt-masterless/provisioner.go | 80 ++++++++++------------ 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/provisioner/salt-masterless/provisioner.go b/provisioner/salt-masterless/provisioner.go index 33a748b61..e23b48c0d 100644 --- a/provisioner/salt-masterless/provisioner.go +++ b/provisioner/salt-masterless/provisioner.go @@ -399,64 +399,54 @@ func (p *Provisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.C } if p.config.GuestOSType == provisioner.WindowsOSType { - { - ui.Message("Downloading Git for Windows") - cmd := &packer.RemoteCmd{Command: fmt.Sprintf("powershell [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.28.0.windows.1/Git-2.28.0-64-bit.exe -OutFile $env:TEMP/Git.exe")} - if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { - if err == nil { - err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) - } - - return fmt.Errorf("Unable to Download Git for Windows: %s", err) + ui.Message("Downloading Git for Windows") + cmd1 := &packer.RemoteCmd{Command: fmt.Sprintf("powershell [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.28.0.windows.1/Git-2.28.0-64-bit.exe -OutFile $env:TEMP/Git.exe")} + if err = cmd1.RunWithUi(ctx, comm, ui); (err != nil || cmd1.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd1.ExitStatus()) } - } - { - ui.Message("Installing Git for Windows") - cmd := &packer.RemoteCmd{Command: fmt.Sprintf("powershell Start-Process -FilePath $env:TEMP/Git.exe /SILENT -Wait")} - if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { - if err == nil { - err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) - } + return fmt.Errorf("Unable to Download Git for Windows: %s", err) + } - return fmt.Errorf("Unable to Install Git for Windows: %s", err) + ui.Message("Installing Git for Windows") + cmd2 := &packer.RemoteCmd{Command: fmt.Sprintf("powershell Start-Process -FilePath $env:TEMP/Git.exe /SILENT -Wait")} + if err = cmd2.RunWithUi(ctx, comm, ui); (err != nil || cmd2.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd2.ExitStatus()) } - } - { - ui.Message("Cleaning Up After Git for Windows") - cmd := &packer.RemoteCmd{Command: fmt.Sprintf("powershell Remove-Item $env:TEMP/Git.exe")} - if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { - if err == nil { - err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) - } + return fmt.Errorf("Unable to Install Git for Windows: %s", err) + } - return fmt.Errorf("Unable to Clean-up After Git for Windows: %s", err) + ui.Message("Cleaning Up After Git for Windows") + cmd3 := &packer.RemoteCmd{Command: fmt.Sprintf("powershell Remove-Item $env:TEMP/Git.exe")} + if err = cmd3.RunWithUi(ctx, comm, ui); (err != nil || cmd3.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd3.ExitStatus()) } - } - { - ui.Message("Running salt-call --local winrepo.update_git_repos") - cmd := &packer.RemoteCmd{Command: fmt.Sprintf("%s --local winrepo.update_git_repos", filepath.Join(p.config.SaltBinDir, "salt-call"))} - if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { - if err == nil { - err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) - } + return fmt.Errorf("Unable to Clean-up After Git for Windows: %s", err) + } - return fmt.Errorf("Error executing salt-call --local winrepo.update_git_repos: %s", err) + ui.Message("Running salt-call --local winrepo.update_git_repos") + cmd4 := &packer.RemoteCmd{Command: fmt.Sprintf("%s --local winrepo.update_git_repos", filepath.Join(p.config.SaltBinDir, "salt-call"))} + if err = cmd4.RunWithUi(ctx, comm, ui); (err != nil || cmd4.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd4.ExitStatus()) } - } - { - ui.Message("Running salt-call --local pkg.refresh_db") - cmd := &packer.RemoteCmd{Command: fmt.Sprintf("%s --local pkg.refresh_db", filepath.Join(p.config.SaltBinDir, "salt-call"))} - if err = cmd.RunWithUi(ctx, comm, ui); (err != nil || cmd.ExitStatus() != 0) && !p.config.NoExitOnFailure { - if err == nil { - err = fmt.Errorf("Bad exit status: %d", cmd.ExitStatus()) - } + return fmt.Errorf("Error executing salt-call --local winrepo.update_git_repos: %s", err) + } - return fmt.Errorf("Error executing salt-call --local pkg.refresh_db: %s", err) + ui.Message("Running salt-call --local pkg.refresh_db") + cmd5 := &packer.RemoteCmd{Command: fmt.Sprintf("%s --local pkg.refresh_db", filepath.Join(p.config.SaltBinDir, "salt-call"))} + if err = cmd5.RunWithUi(ctx, comm, ui); (err != nil || cmd5.ExitStatus() != 0) && !p.config.NoExitOnFailure { + if err == nil { + err = fmt.Errorf("Bad exit status: %d", cmd5.ExitStatus()) } + + return fmt.Errorf("Error executing salt-call --local pkg.refresh_db: %s", err) } }