From 59dac451a5a0c62e6507579b5eed279283ecc7ca Mon Sep 17 00:00:00 2001 From: nouney Date: Thu, 15 Sep 2016 22:15:56 +0200 Subject: [PATCH 1/2] provisioner/file: add "generated" key to allow files created on-the-fly. --- provisioner/file/provisioner.go | 5 ++++- provisioner/file/provisioner_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/provisioner/file/provisioner.go b/provisioner/file/provisioner.go index 2b9a6b5fe..207885bfe 100644 --- a/provisioner/file/provisioner.go +++ b/provisioner/file/provisioner.go @@ -26,6 +26,9 @@ type Config struct { // Direction Direction string + // False if the sources have to exist. + Generated bool + ctx interpolate.Context } @@ -61,7 +64,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { if p.config.Direction == "upload" { for _, src := range p.config.Sources { - if _, err := os.Stat(src); err != nil { + if _, err := os.Stat(src); p.config.Generated == false && err != nil { errs = packer.MultiErrorAppend(errs, fmt.Errorf("Bad source '%s': %s", src, err)) } diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index 53dc315d4..5a2b67037 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -45,6 +45,12 @@ func TestProvisionerPrepare_InvalidSource(t *testing.T) { if err == nil { t.Fatalf("should require existing file") } + + config["generated"] = false + err = p.Prepare(config) + if err == nil { + t.Fatalf("should required existing file") + } } func TestProvisionerPrepare_ValidSource(t *testing.T) { @@ -58,11 +64,28 @@ func TestProvisionerPrepare_ValidSource(t *testing.T) { config := testConfig() config["source"] = tf.Name() - err = p.Prepare(config) if err != nil { t.Fatalf("should allow valid file: %s", err) } + + config["generated"] = false + err = p.Prepare(config) + if err != nil { + t.Fatalf("should allow valid file: %s", err) + } +} + +func TestProvisionerPrepare_GeneratedSource(t *testing.T) { + var p Provisioner + + config := testConfig() + config["source"] = "/this/should/not/exist" + config["generated"] = true + err := p.Prepare(config) + if err != nil { + t.Fatalf("should allow non-existing file: %s", err) + } } func TestProvisionerPrepare_EmptyDestination(t *testing.T) { From ed32b6e3c6da1b79eb54d959be22e6cdb06177a2 Mon Sep 17 00:00:00 2001 From: nouney Date: Thu, 15 Sep 2016 22:46:10 +0200 Subject: [PATCH 2/2] provisioner/file: document "generated" key. --- website/source/docs/provisioners/file.html.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/website/source/docs/provisioners/file.html.md b/website/source/docs/provisioners/file.html.md index bb66fc220..fb932b512 100644 --- a/website/source/docs/provisioners/file.html.md +++ b/website/source/docs/provisioners/file.html.md @@ -46,6 +46,9 @@ The available configuration options are listed below. All elements are required. "upload." If it is set to "download" then the file "source" in the machine will be downloaded locally to "destination" +- `generated` (boolean) - If true, check the file existence only before uploading. + This allows to upload files created on-the-fly. This defaults to false. + ## Directory Uploads The file provisioner is also able to upload a complete directory to the remote