Ensure comments are easily readable in a standard terminal
This commit is contained in:
parent
e0bcba4913
commit
485d565e32
|
@ -1,5 +1,5 @@
|
||||||
// This package implements a provisioner for Packer that executes
|
// This package implements a provisioner for Packer that executes powershell
|
||||||
// powershell scripts within the remote machine.
|
// scripts within the remote machine.
|
||||||
package powershell
|
package powershell
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -39,8 +39,8 @@ type Config struct {
|
||||||
// converted from Windows to Unix-style.
|
// converted from Windows to Unix-style.
|
||||||
Binary bool
|
Binary bool
|
||||||
|
|
||||||
// An inline script to execute. Multiple strings are all executed
|
// An inline script to execute. Multiple strings are all executed in the
|
||||||
// in the context of a single shell.
|
// context of a single shell.
|
||||||
Inline []string
|
Inline []string
|
||||||
|
|
||||||
// The local path of the powershell script to upload and execute.
|
// The local path of the powershell script to upload and execute.
|
||||||
|
@ -49,32 +49,33 @@ type Config struct {
|
||||||
// An array of multiple scripts to run.
|
// An array of multiple scripts to run.
|
||||||
Scripts []string
|
Scripts []string
|
||||||
|
|
||||||
// An array of environment variables that will be injected before
|
// An array of environment variables that will be injected before your
|
||||||
// your command(s) are executed.
|
// command(s) are executed.
|
||||||
Vars []string `mapstructure:"environment_vars"`
|
Vars []string `mapstructure:"environment_vars"`
|
||||||
|
|
||||||
// The remote path where the local powershell script will be uploaded to.
|
// The remote path where the local powershell script will be uploaded to.
|
||||||
// This should be set to a writable file that is in a pre-existing directory.
|
// This should be set to a writable file that is in a pre-existing
|
||||||
|
// directory.
|
||||||
RemotePath string `mapstructure:"remote_path"`
|
RemotePath string `mapstructure:"remote_path"`
|
||||||
|
|
||||||
// The remote path where the file containing the environment variables
|
// The remote path where the file containing the environment variables
|
||||||
// will be uploaded to. This should be set to a writable file that is
|
// will be uploaded to. This should be set to a writable file that is in a
|
||||||
// in a pre-existing directory.
|
// pre-existing directory.
|
||||||
RemoteEnvVarPath string `mapstructure:"remote_env_var_path"`
|
RemoteEnvVarPath string `mapstructure:"remote_env_var_path"`
|
||||||
|
|
||||||
// The command used to execute the script. The '{{ .Path }}' variable
|
// The command used to execute the script. The '{{ .Path }}' variable
|
||||||
// should be used to specify where the script goes, {{ .Vars }}
|
// should be used to specify where the script goes, {{ .Vars }} can be
|
||||||
// can be used to inject the environment_vars into the environment.
|
// used to inject the environment_vars into the environment.
|
||||||
ExecuteCommand string `mapstructure:"execute_command"`
|
ExecuteCommand string `mapstructure:"execute_command"`
|
||||||
|
|
||||||
// The command used to execute the elevated script. The '{{ .Path }}' variable
|
// The command used to execute the elevated script. The '{{ .Path }}'
|
||||||
// should be used to specify where the script goes, {{ .Vars }}
|
// variable should be used to specify where the script goes, {{ .Vars }}
|
||||||
// can be used to inject the environment_vars into the environment.
|
// can be used to inject the environment_vars into the environment.
|
||||||
ElevatedExecuteCommand string `mapstructure:"elevated_execute_command"`
|
ElevatedExecuteCommand string `mapstructure:"elevated_execute_command"`
|
||||||
|
|
||||||
// The timeout for retrying to start the process. Until this timeout
|
// The timeout for retrying to start the process. Until this timeout is
|
||||||
// is reached, if the provisioner can't start a process, it retries.
|
// reached, if the provisioner can't start a process, it retries. This
|
||||||
// This can be set high to allow for reboots.
|
// can be set high to allow for reboots.
|
||||||
StartRetryTimeout time.Duration `mapstructure:"start_retry_timeout"`
|
StartRetryTimeout time.Duration `mapstructure:"start_retry_timeout"`
|
||||||
|
|
||||||
// This is used in the template generation to format environment variables
|
// This is used in the template generation to format environment variables
|
||||||
|
@ -85,15 +86,16 @@ type Config struct {
|
||||||
// inside the `ElevatedExecuteCommand` template.
|
// inside the `ElevatedExecuteCommand` template.
|
||||||
ElevatedEnvVarFormat string `mapstructure:"elevated_env_var_format"`
|
ElevatedEnvVarFormat string `mapstructure:"elevated_env_var_format"`
|
||||||
|
|
||||||
// Instructs the communicator to run the remote script as a
|
// Instructs the communicator to run the remote script as a Windows
|
||||||
// Windows scheduled task, effectively elevating the remote
|
// scheduled task, effectively elevating the remote user by impersonating
|
||||||
// user by impersonating a logged-in user
|
// a logged-in user
|
||||||
ElevatedUser string `mapstructure:"elevated_user"`
|
ElevatedUser string `mapstructure:"elevated_user"`
|
||||||
ElevatedPassword string `mapstructure:"elevated_password"`
|
ElevatedPassword string `mapstructure:"elevated_password"`
|
||||||
|
|
||||||
// Valid Exit Codes - 0 is not always the only valid error code!
|
// Valid Exit Codes - 0 is not always the only valid error code! See
|
||||||
// See http://www.symantec.com/connect/articles/windows-system-error-codes-exit-codes-description for examples
|
// http://www.symantec.com/connect/articles/windows-system-error-codes-exit-codes-description
|
||||||
// such as 3010 - "The requested operation is successful. Changes will not be effective until the system is rebooted."
|
// for examples such as 3010 - "The requested operation is successful.
|
||||||
|
// Changes will not be effective until the system is rebooted."
|
||||||
ValidExitCodes []int `mapstructure:"valid_exit_codes"`
|
ValidExitCodes []int `mapstructure:"valid_exit_codes"`
|
||||||
|
|
||||||
ctx interpolate.Context
|
ctx interpolate.Context
|
||||||
|
@ -115,7 +117,8 @@ type EnvVarsTemplate struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
//Create passthrough for winrm password so we can fill it in once we know it
|
// Create passthrough for winrm password so we can fill it in once we know
|
||||||
|
// it
|
||||||
p.config.ctx.Data = &EnvVarsTemplate{
|
p.config.ctx.Data = &EnvVarsTemplate{
|
||||||
WinRMPassword: `{{.WinRMPassword}}`,
|
WinRMPassword: `{{.WinRMPassword}}`,
|
||||||
}
|
}
|
||||||
|
@ -232,9 +235,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes the inline scripts, concatenates them
|
// Takes the inline scripts, concatenates them into a temporary file and
|
||||||
// into a temporary file and returns a string containing the location
|
// returns a string containing the location of said file.
|
||||||
// of said file.
|
|
||||||
func extractScript(p *Provisioner) (string, error) {
|
func extractScript(p *Provisioner) (string, error) {
|
||||||
temp, err := ioutil.TempFile(os.TempDir(), "packer-powershell-provisioner")
|
temp, err := ioutil.TempFile(os.TempDir(), "packer-powershell-provisioner")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -288,11 +290,10 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
||||||
return fmt.Errorf("Error processing command: %s", err)
|
return fmt.Errorf("Error processing command: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload the file and run the command. Do this in the context of
|
// Upload the file and run the command. Do this in the context of a
|
||||||
// a single retryable function so that we don't end up with
|
// single retryable function so that we don't end up with the case
|
||||||
// the case that the upload succeeded, a restart is initiated,
|
// that the upload succeeded, a restart is initiated, and then the
|
||||||
// and then the command is executed but the file doesn't exist
|
// command is executed but the file doesn't exist any longer.
|
||||||
// any longer.
|
|
||||||
var cmd *packer.RemoteCmd
|
var cmd *packer.RemoteCmd
|
||||||
err = p.retryable(func() error {
|
err = p.retryable(func() error {
|
||||||
if _, err := f.Seek(0, 0); err != nil {
|
if _, err := f.Seek(0, 0); err != nil {
|
||||||
|
@ -330,13 +331,13 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provisioner) Cancel() {
|
func (p *Provisioner) Cancel() {
|
||||||
// Just hard quit. It isn't a big deal if what we're doing keeps
|
// Just hard quit. It isn't a big deal if what we're doing keeps running
|
||||||
// running on the other side.
|
// on the other side.
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// retryable will retry the given function over and over until a
|
// retryable will retry the given function over and over until a non-error is
|
||||||
// non-error is returned.
|
// returned.
|
||||||
func (p *Provisioner) retryable(f func() error) error {
|
func (p *Provisioner) retryable(f func() error) error {
|
||||||
startTimeout := time.After(p.config.StartRetryTimeout)
|
startTimeout := time.After(p.config.StartRetryTimeout)
|
||||||
for {
|
for {
|
||||||
|
@ -349,9 +350,8 @@ func (p *Provisioner) retryable(f func() error) error {
|
||||||
err = fmt.Errorf("Retryable error: %s", err)
|
err = fmt.Errorf("Retryable error: %s", err)
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
|
|
||||||
// Check if we timed out, otherwise we retry. It is safe to
|
// Check if we timed out, otherwise we retry. It is safe to retry
|
||||||
// retry since the only error case above is if the command
|
// since the only error case above is if the command failed to START.
|
||||||
// failed to START.
|
|
||||||
select {
|
select {
|
||||||
case <-startTimeout:
|
case <-startTimeout:
|
||||||
return err
|
return err
|
||||||
|
@ -361,12 +361,15 @@ func (p *Provisioner) retryable(f func() error) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Environment variables required within the remote environment are uploaded within a PS script and
|
// Environment variables required within the remote environment are uploaded
|
||||||
// then enabled by 'dot sourcing' the script immediately prior to execution of the main command
|
// within a PS script and then enabled by 'dot sourcing' the script
|
||||||
|
// immediately prior to execution of the main command
|
||||||
func (p *Provisioner) prepareEnvVars(elevated bool) (err error) {
|
func (p *Provisioner) prepareEnvVars(elevated bool) (err error) {
|
||||||
// Collate all required env vars into a plain string with required formatting applied
|
// Collate all required env vars into a plain string with required
|
||||||
|
// formatting applied
|
||||||
flattenedEnvVars := p.createFlattenedEnvVars(elevated)
|
flattenedEnvVars := p.createFlattenedEnvVars(elevated)
|
||||||
// Create a powershell script on the target build fs containing the flattened env vars
|
// Create a powershell script on the target build fs containing the
|
||||||
|
// flattened env vars
|
||||||
err = p.uploadEnvVars(flattenedEnvVars)
|
err = p.uploadEnvVars(flattenedEnvVars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -427,9 +430,9 @@ func (p *Provisioner) createFlattenedEnvVars(elevated bool) (flattened string) {
|
||||||
|
|
||||||
func (p *Provisioner) uploadEnvVars(flattenedEnvVars string) (err error) {
|
func (p *Provisioner) uploadEnvVars(flattenedEnvVars string) (err error) {
|
||||||
// Upload all env vars to a powershell script on the target build file
|
// Upload all env vars to a powershell script on the target build file
|
||||||
// system. Do this in the context of a single retryable function so
|
// system. Do this in the context of a single retryable function so that
|
||||||
// that we gracefully handle any errors created by transient conditions
|
// we gracefully handle any errors created by transient conditions such as
|
||||||
// such as a system restart
|
// a system restart
|
||||||
envVarReader := strings.NewReader(flattenedEnvVars)
|
envVarReader := strings.NewReader(flattenedEnvVars)
|
||||||
log.Printf("Uploading env vars to %s", p.config.RemoteEnvVarPath)
|
log.Printf("Uploading env vars to %s", p.config.RemoteEnvVarPath)
|
||||||
err = p.retryable(func() error {
|
err = p.retryable(func() error {
|
||||||
|
@ -454,7 +457,8 @@ func (p *Provisioner) createCommandText() (command string, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provisioner) createCommandTextNonPrivileged() (command string, err error) {
|
func (p *Provisioner) createCommandTextNonPrivileged() (command string, err error) {
|
||||||
// Prepare everything needed to enable the required env vars within the remote environment
|
// Prepare everything needed to enable the required env vars within the
|
||||||
|
// remote environment
|
||||||
err = p.prepareEnvVars(false)
|
err = p.prepareEnvVars(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -481,7 +485,8 @@ func getWinRMPassword(buildName string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provisioner) createCommandTextPrivileged() (command string, err error) {
|
func (p *Provisioner) createCommandTextPrivileged() (command string, err error) {
|
||||||
// Prepare everything needed to enable the required env vars within the remote environment
|
// Prepare everything needed to enable the required env vars within the
|
||||||
|
// remote environment
|
||||||
err = p.prepareEnvVars(true)
|
err = p.prepareEnvVars(true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -497,8 +502,9 @@ func (p *Provisioner) createCommandTextPrivileged() (command string, err error)
|
||||||
return "", fmt.Errorf("Error processing command: %s", err)
|
return "", fmt.Errorf("Error processing command: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OK so we need an elevated shell runner to wrap our command, this is going to have its own path
|
// OK so we need an elevated shell runner to wrap our command, this is
|
||||||
// generate the script and update the command runner in the process
|
// going to have its own path generate the script and update the command
|
||||||
|
// runner in the process
|
||||||
path, err := p.generateElevatedRunner(command)
|
path, err := p.generateElevatedRunner(command)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Error generating elevated runner: %s", err)
|
return "", fmt.Errorf("Error generating elevated runner: %s", err)
|
||||||
|
@ -515,23 +521,23 @@ func (p *Provisioner) generateElevatedRunner(command string) (uploadedPath strin
|
||||||
|
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
|
|
||||||
// Output from the elevated command cannot be returned directly to
|
// Output from the elevated command cannot be returned directly to the
|
||||||
// the Packer console. In order to be able to view output from elevated
|
// Packer console. In order to be able to view output from elevated
|
||||||
// commands and scripts an indirect approach is used by which the
|
// commands and scripts an indirect approach is used by which the commands
|
||||||
// commands output is first redirected to file. The output file is then
|
// output is first redirected to file. The output file is then 'watched'
|
||||||
// 'watched' by Packer while the elevated command is running and any
|
// by Packer while the elevated command is running and any content
|
||||||
// content appearing in the file is written out to the console.
|
// appearing in the file is written out to the console. Below the portion
|
||||||
// Below the portion of command required to redirect output from the
|
// of command required to redirect output from the command to file is
|
||||||
// command to file is built and appended to the existing command string
|
// built and appended to the existing command string
|
||||||
taskName := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
taskName := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
||||||
// Only use %ENVVAR% format for environment variables when setting
|
// Only use %ENVVAR% format for environment variables when setting the log
|
||||||
// the log file path; Do NOT use $env:ENVVAR format as it won't be
|
// file path; Do NOT use $env:ENVVAR format as it won't be expanded
|
||||||
// expanded correctly in the elevatedTemplate
|
// correctly in the elevatedTemplate
|
||||||
logFile := `%SYSTEMROOT%/Temp/` + taskName + ".out"
|
logFile := `%SYSTEMROOT%/Temp/` + taskName + ".out"
|
||||||
command += fmt.Sprintf(" > %s 2>&1", logFile)
|
command += fmt.Sprintf(" > %s 2>&1", logFile)
|
||||||
|
|
||||||
// elevatedTemplate wraps the command in a single quoted XML text
|
// elevatedTemplate wraps the command in a single quoted XML text string
|
||||||
// string so we need to escape characters considered 'special' in XML.
|
// so we need to escape characters considered 'special' in XML.
|
||||||
err = xml.EscapeText(&buffer, []byte(command))
|
err = xml.EscapeText(&buffer, []byte(command))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Error escaping characters special to XML in command %s: %s", command, err)
|
return "", fmt.Errorf("Error escaping characters special to XML in command %s: %s", command, err)
|
||||||
|
|
Loading…
Reference in New Issue