From b29d0b43781d5b9d245e40a6425a38c645617e67 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 21 Nov 2016 11:04:16 +0300 Subject: [PATCH] post-processor/manifest: fixes interpolation of output * rename `filename` to `output` for consistent with other post-processors * interpolate output with variables Signed-off-by: Vasiliy Tolstov --- post-processor/manifest/post-processor.go | 29 ++++++++++++----------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/post-processor/manifest/post-processor.go b/post-processor/manifest/post-processor.go index 7b4507cf6..5a75bd34c 100644 --- a/post-processor/manifest/post-processor.go +++ b/post-processor/manifest/post-processor.go @@ -18,9 +18,9 @@ import ( type Config struct { common.PackerConfig `mapstructure:",squash"` - Filename string `mapstructure:"filename"` - StripPath bool `mapstructure:"strip_path"` - ctx interpolate.Context + OutputPath string `mapstructure:"output"` + StripPath bool `mapstructure:"strip_path"` + ctx interpolate.Context } type PostProcessor struct { @@ -44,8 +44,12 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { return err } - if p.config.Filename == "" { - p.config.Filename = "packer-manifest.json" + if p.config.OutputPath == "" { + p.config.OutputPath = "packer-manifest.json" + } + + if err = interpolate.Validate(p.config.OutputPath, &p.config.ctx); err != nil { + return fmt.Errorf("Error parsing target template: %s", err) } return nil @@ -85,7 +89,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, source packer.Artifact) (packe // Create a lock file with exclusive access. If this fails we will retry // after a delay. - lockFilename := p.config.Filename + ".lock" + lockFilename := p.config.OutputPath + ".lock" for i := 0; i < 3; i++ { // The file should not be locked for very long so we'll keep this short. time.Sleep((time.Duration(i) * 200 * time.Millisecond)) @@ -97,20 +101,17 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, source packer.Artifact) (packe } defer os.Remove(lockFilename) - // TODO fix error on first run: - // * Post-processor failed: open packer-manifest.json: no such file or directory - // // Read the current manifest file from disk contents := []byte{} - if contents, err = ioutil.ReadFile(p.config.Filename); err != nil && !os.IsNotExist(err) { - return source, true, fmt.Errorf("Unable to open %s for reading: %s", p.config.Filename, err) + if contents, err = ioutil.ReadFile(p.config.OutputPath); err != nil && !os.IsNotExist(err) { + return source, true, fmt.Errorf("Unable to open %s for reading: %s", p.config.OutputPath, err) } // Parse the manifest file JSON, if we have one manifestFile := &ManifestFile{} if len(contents) > 0 { if err = json.Unmarshal(contents, manifestFile); err != nil { - return source, true, fmt.Errorf("Unable to parse content from %s: %s", p.config.Filename, err) + return source, true, fmt.Errorf("Unable to parse content from %s: %s", p.config.OutputPath, err) } } @@ -126,8 +127,8 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, source packer.Artifact) (packe // Write JSON to disk if out, err := json.MarshalIndent(manifestFile, "", " "); err == nil { - if err = ioutil.WriteFile(p.config.Filename, out, 0664); err != nil { - return source, true, fmt.Errorf("Unable to write %s: %s", p.config.Filename, err) + if err = ioutil.WriteFile(p.config.OutputPath, out, 0664); err != nil { + return source, true, fmt.Errorf("Unable to write %s: %s", p.config.OutputPath, err) } } else { return source, true, fmt.Errorf("Unable to marshal JSON %s", err)