github.com/rothwerx/packer@v0.9.0/post-processor/shell-local/post-processor.go (about)

     1  package shell_local
     2  
     3  import (
     4  	"bufio"
     5  	"errors"
     6  	"fmt"
     7  	"github.com/mitchellh/packer/common"
     8  	"github.com/mitchellh/packer/helper/config"
     9  	"github.com/mitchellh/packer/packer"
    10  	"github.com/mitchellh/packer/template/interpolate"
    11  	"io/ioutil"
    12  	"os"
    13  	"strings"
    14  )
    15  
    16  type Config struct {
    17  	common.PackerConfig `mapstructure:",squash"`
    18  
    19  	// An inline script to execute. Multiple strings are all executed
    20  	// in the context of a single shell.
    21  	Inline []string
    22  
    23  	// The shebang value used when running inline scripts.
    24  	InlineShebang string `mapstructure:"inline_shebang"`
    25  
    26  	// The local path of the shell script to upload and execute.
    27  	Script string
    28  
    29  	// An array of multiple scripts to run.
    30  	Scripts []string
    31  
    32  	// An array of environment variables that will be injected before
    33  	// your command(s) are executed.
    34  	Vars []string `mapstructure:"environment_vars"`
    35  
    36  	// The command used to execute the script. The '{{ .Path }}' variable
    37  	// should be used to specify where the script goes, {{ .Vars }}
    38  	// can be used to inject the environment_vars into the environment.
    39  	ExecuteCommand string `mapstructure:"execute_command"`
    40  
    41  	ctx interpolate.Context
    42  }
    43  
    44  type PostProcessor struct {
    45  	config Config
    46  }
    47  
    48  type ExecuteCommandTemplate struct {
    49  	Vars string
    50  	Path string
    51  }
    52  
    53  func (p *PostProcessor) Configure(raws ...interface{}) error {
    54  	err := config.Decode(&p.config, &config.DecodeOpts{
    55  		Interpolate:        true,
    56  		InterpolateContext: &p.config.ctx,
    57  		InterpolateFilter: &interpolate.RenderFilter{
    58  			Exclude: []string{
    59  				"execute_command",
    60  			},
    61  		},
    62  	}, raws...)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	if p.config.ExecuteCommand == "" {
    68  		p.config.ExecuteCommand = "chmod +x {{.Path}}; {{.Vars}} {{.Path}}"
    69  	}
    70  
    71  	if p.config.Inline != nil && len(p.config.Inline) == 0 {
    72  		p.config.Inline = nil
    73  	}
    74  
    75  	if p.config.InlineShebang == "" {
    76  		p.config.InlineShebang = "/bin/sh -e"
    77  	}
    78  
    79  	if p.config.Scripts == nil {
    80  		p.config.Scripts = make([]string, 0)
    81  	}
    82  
    83  	if p.config.Vars == nil {
    84  		p.config.Vars = make([]string, 0)
    85  	}
    86  
    87  	var errs *packer.MultiError
    88  	if p.config.Script != "" && len(p.config.Scripts) > 0 {
    89  		errs = packer.MultiErrorAppend(errs,
    90  			errors.New("Only one of script or scripts can be specified."))
    91  	}
    92  
    93  	if p.config.Script != "" {
    94  		p.config.Scripts = []string{p.config.Script}
    95  	}
    96  
    97  	if len(p.config.Scripts) == 0 && p.config.Inline == nil {
    98  		errs = packer.MultiErrorAppend(errs,
    99  			errors.New("Either a script file or inline script must be specified."))
   100  	} else if len(p.config.Scripts) > 0 && p.config.Inline != nil {
   101  		errs = packer.MultiErrorAppend(errs,
   102  			errors.New("Only a script file or an inline script can be specified, not both."))
   103  	}
   104  
   105  	for _, path := range p.config.Scripts {
   106  		if _, err := os.Stat(path); err != nil {
   107  			errs = packer.MultiErrorAppend(errs,
   108  				fmt.Errorf("Bad script '%s': %s", path, err))
   109  		}
   110  	}
   111  
   112  	// Do a check for bad environment variables, such as '=foo', 'foobar'
   113  	for idx, kv := range p.config.Vars {
   114  		vs := strings.SplitN(kv, "=", 2)
   115  		if len(vs) != 2 || vs[0] == "" {
   116  			errs = packer.MultiErrorAppend(errs,
   117  				fmt.Errorf("Environment variable not in format 'key=value': %s", kv))
   118  		} else {
   119  			// Replace single quotes so they parse
   120  			vs[1] = strings.Replace(vs[1], "'", `'"'"'`, -1)
   121  
   122  			// Single quote env var values
   123  			p.config.Vars[idx] = fmt.Sprintf("%s='%s'", vs[0], vs[1])
   124  		}
   125  	}
   126  
   127  	if errs != nil && len(errs.Errors) > 0 {
   128  		return errs
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
   135  
   136  	scripts := make([]string, len(p.config.Scripts))
   137  	copy(scripts, p.config.Scripts)
   138  
   139  	// If we have an inline script, then turn that into a temporary
   140  	// shell script and use that.
   141  	if p.config.Inline != nil {
   142  		tf, err := ioutil.TempFile("", "packer-shell")
   143  		if err != nil {
   144  			return nil, false, fmt.Errorf("Error preparing shell script: %s", err)
   145  		}
   146  		defer os.Remove(tf.Name())
   147  
   148  		// Set the path to the temporary file
   149  		scripts = append(scripts, tf.Name())
   150  
   151  		// Write our contents to it
   152  		writer := bufio.NewWriter(tf)
   153  		writer.WriteString(fmt.Sprintf("#!%s\n", p.config.InlineShebang))
   154  		for _, command := range p.config.Inline {
   155  			if _, err := writer.WriteString(command + "\n"); err != nil {
   156  				return nil, false, fmt.Errorf("Error preparing shell script: %s", err)
   157  			}
   158  		}
   159  
   160  		if err := writer.Flush(); err != nil {
   161  			return nil, false, fmt.Errorf("Error preparing shell script: %s", err)
   162  		}
   163  
   164  		tf.Close()
   165  	}
   166  
   167  	// Build our variables up by adding in the build name and builder type
   168  	envVars := make([]string, len(p.config.Vars)+2)
   169  	envVars[0] = fmt.Sprintf("PACKER_BUILD_NAME='%s'", p.config.PackerBuildName)
   170  	envVars[1] = fmt.Sprintf("PACKER_BUILDER_TYPE='%s'", p.config.PackerBuilderType)
   171  	copy(envVars[2:], p.config.Vars)
   172  
   173  	for _, file := range artifact.Files() {
   174  		for _, path := range scripts {
   175  			// Flatten the environment variables
   176  			flattendVars := strings.Join(envVars, " ")
   177  
   178  			path := strings.Join([]string{path, file}, " ")
   179  			p.config.ctx.Data = &ExecuteCommandTemplate{
   180  				Vars: flattendVars,
   181  				Path: path,
   182  			}
   183  
   184  			command, err := interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
   185  			if err != nil {
   186  				return nil, false, fmt.Errorf("Error processing command: %s", err)
   187  			}
   188  
   189  			ui.Say(fmt.Sprintf("Post processing with local shell script: %s", command))
   190  
   191  			comm := &Communicator{}
   192  
   193  			cmd := &packer.RemoteCmd{Command: command}
   194  
   195  			ui.Say(fmt.Sprintf(
   196  				"Executing local script: %s",
   197  				path))
   198  			if err := cmd.StartWithUi(comm, ui); err != nil {
   199  				return nil, false, fmt.Errorf(
   200  					"Error executing script: %s\n\n"+
   201  						"Please see output above for more information.",
   202  					path)
   203  			}
   204  			if cmd.ExitStatus != 0 {
   205  				return nil, false, fmt.Errorf(
   206  					"Erroneous exit code %d while executing script: %s\n\n"+
   207  						"Please see output above for more information.",
   208  					cmd.ExitStatus,
   209  					path)
   210  			}
   211  		}
   212  	}
   213  
   214  	return artifact, true, nil
   215  }