From 6bf790a9754f4e8688c612fc3680cbe2cd47d7d0 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Fri, 25 Sep 2015 11:52:21 -0700 Subject: [PATCH] post-processor/atlas: support sending compile ids Requires https://github.com/hashicorp/atlas-go/pull/44 --- post-processor/atlas/post-processor.go | 20 ++++++++++++++++++-- post-processor/atlas/post-processor_test.go | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/post-processor/atlas/post-processor.go b/post-processor/atlas/post-processor.go index 59335086c..7b8a4d98b 100644 --- a/post-processor/atlas/post-processor.go +++ b/post-processor/atlas/post-processor.go @@ -15,7 +15,10 @@ import ( "github.com/mitchellh/packer/template/interpolate" ) -const BuildEnvKey = "ATLAS_BUILD_ID" +const ( + BuildEnvKey = "ATLAS_BUILD_ID" + CompileEnvKey = "ATLAS_COMPILE_ID" +) // Artifacts can return a string for this state key and the post-processor // will use automatically use this as the type. The user's value overrides @@ -43,7 +46,8 @@ type Config struct { ctx interpolate.Context user, name string - buildId int + buildId int + compileId int } type PostProcessor struct { @@ -96,6 +100,17 @@ func (p *PostProcessor) Configure(raws ...interface{}) error { p.config.buildId = int(raw) } + // If we have a compile ID, save it + if v := os.Getenv(CompileEnvKey); v != "" { + raw, err := strconv.ParseInt(v, 0, 0) + if err != nil { + return fmt.Errorf( + "Error parsing compile ID: %s", err) + } + + p.config.compileId = int(raw) + } + // Build the client p.client = atlas.DefaultClient() if p.config.ServerAddr != "" { @@ -150,6 +165,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac ID: artifact.Id(), Metadata: p.metadata(artifact), BuildID: p.config.buildId, + CompileID: p.config.compileId, } if fs := artifact.Files(); len(fs) > 0 { diff --git a/post-processor/atlas/post-processor_test.go b/post-processor/atlas/post-processor_test.go index 2f8c63c47..add222bb6 100644 --- a/post-processor/atlas/post-processor_test.go +++ b/post-processor/atlas/post-processor_test.go @@ -40,6 +40,20 @@ func TestPostProcessorConfigure_buildId(t *testing.T) { } } +func TestPostProcessorConfigure_compileId(t *testing.T) { + defer os.Setenv(CompileEnvKey, os.Getenv(CompileEnvKey)) + os.Setenv(CompileEnvKey, "5") + + var p PostProcessor + if err := p.Configure(validDefaults()); err != nil { + t.Fatalf("err: %s", err) + } + + if p.config.compileId != 5 { + t.Fatalf("bad: %#v", p.config.compileId) + } +} + func TestPostProcessorMetadata(t *testing.T) { var p PostProcessor if err := p.Configure(validDefaults()); err != nil {