From 7b9d6832cf9b1c4cc184ff1a9c75a6600125ef7a Mon Sep 17 00:00:00 2001 From: Dex Date: Mon, 3 Dec 2018 19:53:55 -0800 Subject: [PATCH] Add Customer and installation ID to template context for replicated.app What I Did ------------ Add Customer and installation ID to template context for replicated.app, This means the following now resolve for `ship init replicated.app/...`: ``` {{repl Installation "customer_id"}} {{repl Installation "installation_id"}} ``` How I Did it ------------ Use the ReleaseMeta for ShipCtx resolution instead of using viper.Get How to verify it ------------ Include the Installation template functions in an app. This example from the integration test: in: ``` - inline: contents: | #!/bin/bash echo "tested nothing" echo "customer {{repl Installation "customer_id" }}" echo "install {{repl Installation "installation_id" }}" dest: ./scripts/test.sh mode: 0777 ``` out: ``` echo "tested nothing" echo "customer 4AbspOwKN4lZUCn49u_G" echo "install z6jTxRuXHn_l6UlYQz3hWz6-" ``` Description for the Changelog ------------ Fix template context injection of release metadata for replicated.app -style applications in ship. :boat: --- .../init_app/basic/expected/.ship/release.yml | 2 ++ integration/init_app/basic/expected/.ship/state.json | 4 ++-- .../basic/expected/installer/scripts/test.sh | 2 ++ integration/init_app/basic/input/.ship/release.yml | 2 ++ integration/init_app/basic/metadata.yaml | 2 +- .../update/app_basic/expected/.ship/release.yml | 2 ++ .../update/app_basic/expected/.ship/state.json | 4 ++-- .../app_basic/expected/installer/scripts/test.sh | 3 +++ pkg/helm/repo_add.go | 4 +--- pkg/lifecycle/render/helm/template.go | 3 +-- pkg/lifecycle/render/helm/template_test.go | 3 +-- pkg/specs/upstream_test.go | 4 +--- pkg/templates/installation_context.go | 4 ++-- pkg/templates/installation_context_test.go | 12 ++++++++---- 14 files changed, 30 insertions(+), 21 deletions(-) diff --git a/integration/init_app/basic/expected/.ship/release.yml b/integration/init_app/basic/expected/.ship/release.yml index f2e54f8c2..5581ea0a4 100644 --- a/integration/init_app/basic/expected/.ship/release.yml +++ b/integration/init_app/basic/expected/.ship/release.yml @@ -12,6 +12,8 @@ assets: contents: | #!/bin/bash echo "tested nothing" + echo "customer {{repl Installation "customer_id" }}" + echo "install {{repl Installation "installation_id" }}" dest: ./scripts/test.sh mode: 0777 config: diff --git a/integration/init_app/basic/expected/.ship/state.json b/integration/init_app/basic/expected/.ship/state.json index 5c5a745f2..faa49feae 100644 --- a/integration/init_app/basic/expected/.ship/state.json +++ b/integration/init_app/basic/expected/.ship/state.json @@ -2,13 +2,13 @@ "v1": { "config": {}, "upstream": "__upstream__", - "contentSHA": "2260e2110304496fc8544b8bf51c95352cdf9300270fe7c7fa9f6e1b76855d1c", + "contentSHA": "6faeec976f37e880c5e70cf578412a57fbfda3b35bf8e6f73221fe5fe88f1058", "metadata": { "applicationType": "replicated.app", "customerID": "__customerID__", "installationID": "__installationID__", "releaseNotes": "integration tests", - "version": "1.0.0-SNAPSHOT" + "version": "1.0.2" }, "releaseName": "integration-replicated-app-basic" } diff --git a/integration/init_app/basic/expected/installer/scripts/test.sh b/integration/init_app/basic/expected/installer/scripts/test.sh index 7be660431..3f9ed2c91 100644 --- a/integration/init_app/basic/expected/installer/scripts/test.sh +++ b/integration/init_app/basic/expected/installer/scripts/test.sh @@ -1,2 +1,4 @@ #!/bin/bash echo "tested nothing" +echo "customer -Am-_6i5pw0u4AbspOwKN4lZUCn49u_G" +echo "install 3Z6uuPbVz6jTxRuXHn_l6UlYQz3hWz6-" diff --git a/integration/init_app/basic/input/.ship/release.yml b/integration/init_app/basic/input/.ship/release.yml index f2e54f8c2..5581ea0a4 100644 --- a/integration/init_app/basic/input/.ship/release.yml +++ b/integration/init_app/basic/input/.ship/release.yml @@ -12,6 +12,8 @@ assets: contents: | #!/bin/bash echo "tested nothing" + echo "customer {{repl Installation "customer_id" }}" + echo "install {{repl Installation "installation_id" }}" dest: ./scripts/test.sh mode: 0777 config: diff --git a/integration/init_app/basic/metadata.yaml b/integration/init_app/basic/metadata.yaml index d15ef206f..b8e0770bf 100644 --- a/integration/init_app/basic/metadata.yaml +++ b/integration/init_app/basic/metadata.yaml @@ -1,3 +1,3 @@ customer_id: "-Am-_6i5pw0u4AbspOwKN4lZUCn49u_G" installation_id: "3Z6uuPbVz6jTxRuXHn_l6UlYQz3hWz6-" -release_version: "1.0.0-SNAPSHOT" \ No newline at end of file +release_version: "1.0.2" \ No newline at end of file diff --git a/integration/update/app_basic/expected/.ship/release.yml b/integration/update/app_basic/expected/.ship/release.yml index f2e54f8c2..5581ea0a4 100644 --- a/integration/update/app_basic/expected/.ship/release.yml +++ b/integration/update/app_basic/expected/.ship/release.yml @@ -12,6 +12,8 @@ assets: contents: | #!/bin/bash echo "tested nothing" + echo "customer {{repl Installation "customer_id" }}" + echo "install {{repl Installation "installation_id" }}" dest: ./scripts/test.sh mode: 0777 config: diff --git a/integration/update/app_basic/expected/.ship/state.json b/integration/update/app_basic/expected/.ship/state.json index 52a1002c6..d4aa35908 100644 --- a/integration/update/app_basic/expected/.ship/state.json +++ b/integration/update/app_basic/expected/.ship/state.json @@ -2,13 +2,13 @@ "v1": { "config": {}, "upstream": "staging.replicated.app/some-cool-ci-tool?installation_id=3Z6uuPbVz6jTxRuXHn_l6UlYQz3hWz6-&customer_id=-Am-_6i5pw0u4AbspOwKN4lZUCn49u_G", - "contentSHA": "2260e2110304496fc8544b8bf51c95352cdf9300270fe7c7fa9f6e1b76855d1c", + "contentSHA": "6faeec976f37e880c5e70cf578412a57fbfda3b35bf8e6f73221fe5fe88f1058", "metadata": { "applicationType": "replicated.app", "customerID": "-Am-_6i5pw0u4AbspOwKN4lZUCn49u_G", "installationID": "3Z6uuPbVz6jTxRuXHn_l6UlYQz3hWz6-", "releaseNotes": "integration tests", - "version": "1.0.0-SNAPSHOT" + "version": "1.0.2" }, "releaseName": "integration-replicated-app-basic" } diff --git a/integration/update/app_basic/expected/installer/scripts/test.sh b/integration/update/app_basic/expected/installer/scripts/test.sh index 7be660431..f2ebd915d 100644 --- a/integration/update/app_basic/expected/installer/scripts/test.sh +++ b/integration/update/app_basic/expected/installer/scripts/test.sh @@ -1,2 +1,5 @@ #!/bin/bash echo "tested nothing" +echo "customer -Am-_6i5pw0u4AbspOwKN4lZUCn49u_G" +echo "install 3Z6uuPbVz6jTxRuXHn_l6UlYQz3hWz6-" + diff --git a/pkg/helm/repo_add.go b/pkg/helm/repo_add.go index 10bc749ec..f42031a69 100644 --- a/pkg/helm/repo_add.go +++ b/pkg/helm/repo_add.go @@ -26,11 +26,9 @@ import ( "bytes" "fmt" "io" - - "github.com/pkg/errors" - "syscall" + "github.com/pkg/errors" "golang.org/x/crypto/ssh/terminal" "k8s.io/helm/pkg/getter" "k8s.io/helm/pkg/helm/helmpath" diff --git a/pkg/lifecycle/render/helm/template.go b/pkg/lifecycle/render/helm/template.go index 3efdcc9b6..a1428e81e 100644 --- a/pkg/lifecycle/render/helm/template.go +++ b/pkg/lifecycle/render/helm/template.go @@ -10,8 +10,6 @@ import ( "regexp" "strings" - "k8s.io/helm/pkg/chartutil" - "github.com/emosbaugh/yaml" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" @@ -26,6 +24,7 @@ import ( "github.com/replicatedhq/ship/pkg/util" "github.com/spf13/afero" "github.com/spf13/viper" + "k8s.io/helm/pkg/chartutil" ) const ( diff --git a/pkg/lifecycle/render/helm/template_test.go b/pkg/lifecycle/render/helm/template_test.go index 1a5eeae41..428848af1 100644 --- a/pkg/lifecycle/render/helm/template_test.go +++ b/pkg/lifecycle/render/helm/template_test.go @@ -6,8 +6,6 @@ import ( "path/filepath" "testing" - "k8s.io/helm/pkg/chartutil" - "github.com/golang/mock/gomock" "github.com/replicatedhq/libyaml" "github.com/replicatedhq/ship/pkg/api" @@ -24,6 +22,7 @@ import ( "github.com/spf13/viper" "github.com/stretchr/testify/require" "gopkg.in/yaml.v2" + "k8s.io/helm/pkg/chartutil" ) func TestLocalTemplater(t *testing.T) { diff --git a/pkg/specs/upstream_test.go b/pkg/specs/upstream_test.go index 664bc24d4..ca125802a 100644 --- a/pkg/specs/upstream_test.go +++ b/pkg/specs/upstream_test.go @@ -8,11 +8,9 @@ import ( "net/url" "testing" + "github.com/google/go-github/github" "github.com/replicatedhq/ship/pkg/specs/githubclient" - "github.com/replicatedhq/ship/pkg/state" - - "github.com/google/go-github/github" "github.com/replicatedhq/ship/pkg/testing/logger" "github.com/stretchr/testify/require" ) diff --git a/pkg/templates/installation_context.go b/pkg/templates/installation_context.go index 42c354825..ed872b572 100644 --- a/pkg/templates/installation_context.go +++ b/pkg/templates/installation_context.go @@ -39,7 +39,7 @@ func (ctx *InstallationContext) FuncMap() template.FuncMap { case "state_file_path": return constants.StatePath case "customer_id": - return ctx.Viper.GetString("customer-id") + return ctx.Meta.CustomerID case "semver": return ctx.Meta.Semver case "channel_name": @@ -49,7 +49,7 @@ func (ctx *InstallationContext) FuncMap() template.FuncMap { case "release_id": return ctx.Meta.ReleaseID case "installation_id": - return ctx.Viper.GetString("installation-id") + return ctx.Meta.InstallationID case "release_notes": return ctx.Meta.ReleaseNotes } diff --git a/pkg/templates/installation_context_test.go b/pkg/templates/installation_context_test.go index c687b3686..02935aa0e 100644 --- a/pkg/templates/installation_context_test.go +++ b/pkg/templates/installation_context_test.go @@ -98,14 +98,18 @@ func testCases() []TestInstallation { Expected: "It's " + constants.StatePath, }, { - Name: "customer_id", - Meta: api.ReleaseMetadata{}, + Name: "customer_id", + Meta: api.ReleaseMetadata{ + CustomerID: "abc", + }, Tpl: `It's {{repl Installation "customer_id" }}`, Expected: `It's abc`, }, { - Name: "installation_id", - Meta: api.ReleaseMetadata{}, + Name: "installation_id", + Meta: api.ReleaseMetadata{ + InstallationID: "xyz", + }, Tpl: `It's {{repl Installation "installation_id" }}`, Expected: `It's xyz`, },