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