-
Notifications
You must be signed in to change notification settings - Fork 23
/
customer_attributes.go
124 lines (106 loc) · 2.98 KB
/
customer_attributes.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
119
120
121
122
123
124
package print
import (
"encoding/json"
"fmt"
"strings"
"text/tabwriter"
"text/template"
"github.com/replicatedhq/replicated/pkg/types"
)
var customerAttrsTmplSrc = `ID: {{ .Cust.ID }}
NAME: {{ .Cust.Name }}
EMAIL: {{ .Cust.Email }}
LICENSE_ID: {{ .Cust.InstallationID }}
EXPIRES: {{if .Cust.Expires }}{{ .Cust.Expires }}{{else}}Never{{end}}
{{if .Install}}
{{with .Login}}
LOGIN:
{{ . }}
{{end}}
INSTALL PREFLIGHT:
curl https://krew.sh/preflight | bash
{{with .Preflight}}
PREFLIGHT:
{{ . }}
{{end}}
{{with .Install}}
INSTALL:
{{ . }}
{{end}}
{{end}}
`
var customerAttrsTmpl = template.Must(template.New("CustomerAttributes").Parse(customerAttrsTmplSrc))
func CustomerAttrs(outputFormat string,
w *tabwriter.Writer,
appType string,
appSlug string,
ch *types.KotsChannel,
registryHostname string,
cust *types.Customer,
) error {
if outputFormat == "text" || outputFormat == "table" {
attrs := struct {
Login string
Preflight string
Install string
Cust *types.Customer
}{
Cust: cust,
}
if appType == "kots" {
attrs.Login = loginCommand(registryHostname, cust.Email, cust.InstallationID)
attrs.Preflight = preflightCommand(registryHostname, appSlug, ch)
attrs.Install = installCommand(registryHostname, appSlug, ch)
}
if err := customerAttrsTmpl.Execute(w, attrs); err != nil {
return err
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(cust, "", " ")
if _, err := fmt.Fprintln(w, string(cAsByte)); err != nil {
return err
}
}
return w.Flush()
}
func loginCommand(host, email, password string) string {
if email == "" {
return "Customer email required to login"
}
return fmt.Sprintf(` helm registry login %s --username %s --password %s`, host, email, password)
}
func preflightCommand(host, appSlug string, ch *types.KotsChannel) string {
preflightReleases := []types.ChartRelease{}
for _, chart := range ch.ChartReleases {
if chart.HasPreflightSpec {
preflightReleases = append(preflightReleases, chart)
}
}
if len(preflightReleases) == 0 {
return "No preflight checks found"
}
cmds := []string{}
for _, chart := range preflightReleases {
if ch.ChannelSlug == "stable" {
cmds = append(cmds, fmt.Sprintf(` helm template %s oci://%s/%s/%s | kubectl preflight -`,
chart.Name, host, appSlug, chart.Name))
} else {
cmds = append(cmds, fmt.Sprintf(` helm template %s oci://%s/%s/%s/%s | kubectl preflight -`,
chart.Name, host, appSlug, ch.ChannelSlug, chart.Name))
}
}
return strings.Join(cmds, " &&\n")
}
func installCommand(host, appSlug string, ch *types.KotsChannel) string {
cmds := []string{}
for _, chart := range ch.ChartReleases {
if ch.ChannelSlug == "stable" {
cmds = append(cmds, fmt.Sprintf(` helm install %s oci://%s/%s/%s`,
chart.Name, host, appSlug, chart.Name))
} else {
cmds = append(cmds, fmt.Sprintf(` helm install %s oci://%s/%s/%s/%s`,
chart.Name, host, appSlug, ch.ChannelSlug, chart.Name))
}
}
return strings.Join(cmds, " &&\n")
}