This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
dashboard.go
179 lines (148 loc) · 3.61 KB
/
dashboard.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package dashboard
import (
"net/url"
"strings"
"github.com/sirupsen/logrus"
"github.com/pkg/browser"
"github.com/rancher/rio/cli/pkg/clicontext"
"github.com/rancher/rio/cli/pkg/progress"
v3 "github.com/rancher/rio/pkg/apis/management.cattle.io/v3"
"github.com/rancher/rio/pkg/config"
"github.com/rancher/rio/pkg/randomtoken"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Dashboard struct {
ResetAdmin bool `desc:"Reset admin password"`
}
func (d *Dashboard) Run(ctx *clicontext.CLIContext) error {
if err := enableDashboard(ctx); err != nil {
return err
}
dashboardURL, err := waitDashboard(ctx)
if err != nil {
return err
}
token, err := setup(ctx, d.ResetAdmin)
if err != nil {
return err
}
u, err := url.Parse(dashboardURL)
if err != nil {
return err
}
if token != "" {
q := u.Query()
q.Set("setup", token)
u.RawQuery = q.Encode()
}
u.Path = "/dashboard/"
q := u.String()
logrus.Infof("Opening browser to %s", q)
return browser.OpenURL(u.String())
}
func reset(ctx *clicontext.CLIContext) error {
err := ctx.Mgmt.Users().Delete("admin", nil)
if err != nil && !errors.IsNotFound(err) {
return err
}
err = ctx.Mgmt.Settings().Delete("first-login", nil)
if err != nil && !errors.IsNotFound(err) {
return err
}
p := progress.NewWriter()
for {
_ = ctx.Mgmt.Users().Delete("admin", nil)
_, err := ctx.Mgmt.Users().Get("admin", metav1.GetOptions{})
if errors.IsNotFound(err) {
return nil
}
p.Display("Resetting admin", 1)
}
}
func setup(ctx *clicontext.CLIContext, resetAdmin bool) (string, error) {
if resetAdmin {
if err := reset(ctx); err != nil {
return "", err
}
}
adm, err := ctx.Mgmt.Users().Get("admin", metav1.GetOptions{})
if err == nil {
if strings.HasPrefix(adm.Password, "$") {
return "", nil
}
return adm.Password, nil
}
err = ctx.Mgmt.Settings().Delete("first-login", nil)
if err != nil && !errors.IsNotFound(err) {
return "", err
}
ctx.Mgmt.Settings().Create(&v3.Setting{
ObjectMeta: metav1.ObjectMeta{
Name: "first-login",
},
Default: "true",
Value: "true",
Customized: true,
})
token, err := randomtoken.Generate()
if err != nil {
return "", err
}
_, err = ctx.Mgmt.Users().Create(&v3.User{
ObjectMeta: metav1.ObjectMeta{
Name: "admin",
},
Username: "admin",
Password: token,
MustChangePassword: true,
})
return token, err
}
func waitDashboard(ctx *clicontext.CLIContext) (string, error) {
w := progress.NewWriter()
first := true
for {
if !first {
w.Display("Waiting for dashboard service to be ready", 1)
}
first = false
svc, err := ctx.Rio.Services(ctx.SystemNamespace).Get("dashboard", metav1.GetOptions{})
if errors.IsNotFound(err) {
continue
} else if err != nil {
return "", err
}
if !svc.Status.DeploymentReady ||
len(svc.Status.AppEndpoints) == 0 ||
!strings.HasPrefix(svc.Status.AppEndpoints[0], "https://") {
continue
}
return svc.Status.AppEndpoints[0], nil
}
}
func enableDashboard(ctx *clicontext.CLIContext) error {
cm, err := ctx.Core.ConfigMaps(ctx.SystemNamespace).Get(config.ConfigName, metav1.GetOptions{})
if err != nil {
return err
}
cfg, err := config.FromConfigMap(cm)
if err != nil {
return err
}
f := cfg.Features["dashboard"]
if f.Enabled != nil && *f.Enabled {
return nil
}
f.Enabled = &[]bool{true}[0]
if cfg.Features == nil {
cfg.Features = map[string]config.FeatureConfig{}
}
cfg.Features["dashboard"] = f
cm, err = config.SetConfig(cm, cfg)
if err != nil {
return err
}
_, err = ctx.Core.ConfigMaps(ctx.SystemNamespace).Update(cm)
return err
}