github.com/replicatedhq/ship@v0.55.0/pkg/templates/installation_context.go (about) 1 package templates 2 3 import ( 4 "text/template" 5 6 "github.com/go-kit/kit/log" 7 "github.com/go-kit/kit/log/level" 8 "github.com/replicatedhq/ship/pkg/api" 9 "github.com/replicatedhq/ship/pkg/constants" 10 "github.com/replicatedhq/ship/pkg/util" 11 12 "github.com/spf13/viper" 13 ) 14 15 type InstallationContext struct { 16 Meta api.ReleaseMetadata 17 Viper *viper.Viper 18 Logger log.Logger 19 } 20 21 func (ctx *InstallationContext) entitlementValue(name string) string { 22 if ctx.Meta.Entitlements.Values == nil { 23 level.Debug(ctx.Logger).Log("event", "EntitlementValue.empty") 24 return "" 25 } 26 27 for _, value := range ctx.Meta.Entitlements.Values { 28 if value.Key == name { 29 return value.Value 30 } 31 } 32 33 level.Debug(ctx.Logger).Log("event", "EntitlementValue.notFound", "key", name, "values.count", len(ctx.Meta.Entitlements.Values)) 34 return "" 35 } 36 37 func (ctx *InstallationContext) shipCustomerRelease() string { 38 restrictedMeta := ctx.Meta 39 restrictedMeta.ConfigSpec = "" 40 restrictedMeta.CollectSpec = "" 41 restrictedMeta.AnalyzeSpec = "" 42 restrictedMeta.GithubContents = nil 43 restrictedMeta.Images = nil 44 45 data, err := util.MarshalIndent(2, restrictedMeta) 46 if err != nil { 47 level.Error(ctx.Logger).Log("msg", "unable to marshal release meta", "err", err) 48 return "" 49 } 50 return string(data) 51 } 52 53 func (ctx *InstallationContext) shipCustomerReleaseFull() string { 54 data, err := util.MarshalIndent(2, ctx.Meta) 55 if err != nil { 56 level.Error(ctx.Logger).Log("msg", "unable to marshal release meta", "err", err) 57 return "" 58 } 59 return string(data) 60 } 61 62 func (ctx *InstallationContext) configSpec() string { 63 return ctx.Meta.ConfigSpec 64 } 65 66 func (ctx *InstallationContext) collectSpec() string { 67 return ctx.Meta.CollectSpec 68 } 69 70 func (ctx *InstallationContext) analyzeSpec() string { 71 return ctx.Meta.AnalyzeSpec 72 } 73 74 func (ctx *InstallationContext) FuncMap() template.FuncMap { 75 return template.FuncMap{ 76 "ShipCustomerRelease": ctx.shipCustomerRelease, 77 "ShipCustomerReleaseFull": ctx.shipCustomerReleaseFull, 78 "EntitlementValue": ctx.entitlementValue, 79 "LicenseFieldValue": ctx.entitlementValue, 80 "ConfigSpec": ctx.configSpec, 81 "CollectSpec": ctx.collectSpec, 82 "AnalyzeSpec": ctx.analyzeSpec, 83 "Installation": func(name string) string { 84 switch name { 85 case "state_file_path": 86 return constants.StatePath 87 case "customer_id": 88 return ctx.Meta.CustomerID 89 case "semver": 90 return ctx.Meta.Semver 91 case "channel_name": 92 return ctx.Meta.ChannelName 93 case "channel_id": 94 return ctx.Meta.ChannelID 95 case "release_id": 96 return ctx.Meta.ReleaseID 97 case "installation_id": 98 if ctx.Meta.InstallationID != "" { 99 // don't warn here, installation_id warnings should happen higher up, closer to the CLI/UX part of the stack 100 return ctx.Meta.InstallationID 101 } 102 level.Warn(ctx.Logger).Log("warning", "template function installation_id is deprecated, please switch to license_id") 103 return ctx.Meta.LicenseID 104 case "release_notes": 105 return ctx.Meta.ReleaseNotes 106 case "app_slug": 107 return ctx.Meta.AppSlug 108 case "license_id": 109 if ctx.Meta.LicenseID != "" { 110 return ctx.Meta.LicenseID 111 } 112 level.Warn(ctx.Logger).Log("warning", "license_id not set, falling back to deprecated installation_id") 113 return ctx.Meta.InstallationID 114 } 115 return "" 116 }, 117 } 118 }