-
Notifications
You must be signed in to change notification settings - Fork 567
/
cmds.go
278 lines (248 loc) · 10.4 KB
/
cmds.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package cmds
import (
"context"
"fmt"
"github.com/pachyderm/pachyderm/v2/src/enterprise"
"github.com/pachyderm/pachyderm/v2/src/internal/cmdutil"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/grpcutil"
"github.com/pachyderm/pachyderm/v2/src/internal/pachctl"
"github.com/pachyderm/pachyderm/v2/src/license"
"github.com/spf13/cobra"
)
// ActivateCmd returns a cobra.Command to activate the license service,
// register the current pachd and activate enterprise features.
// This always runs against the current enterprise context, and can
// be used to activate a single-node pachd deployment or the enterprise
// server in a multi-node deployment.
func ActivateCmd(ctx context.Context, pachctlCfg *pachctl.Config) *cobra.Command {
var onlyActivate bool
activate := &cobra.Command{
Use: "{{alias}}",
Short: "Activate the license server with an activation code",
Long: "This command activates Enterprise Server with an activation code.",
Example: "\t- {{alias}}\n" +
"\t- {{alias}} --no-register\n",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
key, err := cmdutil.ReadPassword("Enterprise key: ")
if err != nil {
return errors.Wrapf(err, "could not read enterprise key")
}
c, err := pachctlCfg.NewOnUserMachine(ctx, true)
if err != nil {
return errors.Wrapf(err, "could not connect")
}
defer c.Close()
// Activate the license server
req := &license.ActivateRequest{
ActivationCode: key,
}
if _, err := c.License.Activate(c.Ctx(), req); err != nil {
return errors.EnsureStack(err)
}
if onlyActivate {
return nil
}
clusterInfo, ok := c.ClusterInfo()
if !ok {
return errors.New("internal: no cluster info in pach client?")
}
// Register the localhost as a cluster
resp, err := c.License.AddCluster(c.Ctx(),
&license.AddClusterRequest{
Id: "localhost",
Address: "grpc://localhost:1653",
UserAddress: "grpc://localhost:1653",
ClusterDeploymentId: clusterInfo.DeploymentId,
EnterpriseServer: true,
})
if err != nil {
return errors.Wrapf(err, "could not register pachd with the license service")
}
// activate the Enterprise service
_, err = c.Enterprise.Activate(c.Ctx(),
&enterprise.ActivateRequest{
Id: "localhost",
Secret: resp.Secret,
LicenseServer: "grpc://localhost:1653",
})
if err != nil {
return errors.Wrapf(err, "could not activate the enterprise service")
}
return nil
}),
}
activate.PersistentFlags().BoolVar(&onlyActivate, "no-register", false, "Activate auth on the active enterprise context")
return cmdutil.CreateAlias(activate, "license activate")
}
// AddClusterCmd returns a cobra.Command to register a cluster with the license server
func AddClusterCmd(ctx context.Context, pachctlCfg *pachctl.Config) *cobra.Command {
var id, address, secret string
addCluster := &cobra.Command{
Short: "Register a new cluster with the license server.",
Long: "This command registers a new cluster with Enterprise Server.",
Example: "\t- {{alias}} --id=my-cluster --address=grpc://my-cluster:1653 --secret=secret\n",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
c, err := pachctlCfg.NewOnUserMachine(ctx, true)
if err != nil {
return errors.Wrapf(err, "could not connect")
}
defer c.Close()
resp, err := c.License.AddCluster(c.Ctx(), &license.AddClusterRequest{
Id: id,
Address: address,
Secret: secret,
})
if err != nil {
return grpcutil.ScrubGRPC(err)
}
fmt.Printf("Shared secret: %v\n", resp.Secret)
return nil
}),
}
addCluster.PersistentFlags().StringVar(&id, "id", "", `Set the ID for the cluster to register.`)
addCluster.PersistentFlags().StringVar(&address, "address", "", `Set the host and port where the cluster can be reached.`)
addCluster.PersistentFlags().StringVar(&secret, "secret", "", `Set the shared secret to use to authenticate this cluster.`)
return cmdutil.CreateAlias(addCluster, "license add-cluster")
}
// UpdateClusterCmd returns a cobra.Command to register a cluster with the license server
func UpdateClusterCmd(ctx context.Context, pachctlCfg *pachctl.Config) *cobra.Command {
var id, address, userAddress, clusterDeploymentId string
updateCluster := &cobra.Command{
Short: "Update an existing cluster registered with the license server.",
Long: "This command updates an existing cluster registered with Enterprise Server.",
Example: "\t- {{alias}} --id=my-cluster --address=grpc://my-cluster:1653 \n" +
"\t- {{alias}} --id=my-cluster --user-address=grpc://my-cluster:1653\n" +
"\t- {{alias}} --id=my-cluster --cluster-deployment-id=1234\n",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
c, err := pachctlCfg.NewOnUserMachine(ctx, true)
if err != nil {
return errors.Wrapf(err, "could not connect")
}
defer c.Close()
_, err = c.License.UpdateCluster(c.Ctx(), &license.UpdateClusterRequest{
Id: id,
Address: address,
UserAddress: userAddress,
ClusterDeploymentId: clusterDeploymentId,
})
return grpcutil.ScrubGRPC(err)
}),
}
updateCluster.PersistentFlags().StringVar(&id, "id", "", `Set the ID for the cluster to update.`)
updateCluster.PersistentFlags().StringVar(&address, "address", "", `Set the host and port where the cluster can be reached by the enterprise server.`)
updateCluster.PersistentFlags().StringVar(&userAddress, "user-address", "", `Set the host and port where the cluster can be reached by a user.`)
updateCluster.PersistentFlags().StringVar(&clusterDeploymentId, "cluster-deployment-id", "", `Set the deployment ID of the updated cluster.`)
return cmdutil.CreateAlias(updateCluster, "license update-cluster")
}
// DeleteClusterCmd returns a cobra.Command to delete a cluster from the license server
func DeleteClusterCmd(ctx context.Context, pachctlCfg *pachctl.Config) *cobra.Command {
var id string
deleteCluster := &cobra.Command{
Short: "Delete a cluster registered with the license server.",
Long: "This command deletes a cluster registered with Enterprise Server.",
Example: "\t- {{alias}} --id=my-cluster\n",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
c, err := pachctlCfg.NewOnUserMachine(ctx, true)
if err != nil {
return errors.Wrapf(err, "could not connect")
}
defer c.Close()
_, err = c.License.DeleteCluster(c.Ctx(), &license.DeleteClusterRequest{
Id: id,
})
return grpcutil.ScrubGRPC(err)
}),
}
deleteCluster.PersistentFlags().StringVar(&id, "id", "", `Set the ID for the cluster to delete.`)
return cmdutil.CreateAlias(deleteCluster, "license delete-cluster")
}
// ListClustersCmd returns a cobra.Command to list clusters registered with the license server
func ListClustersCmd(ctx context.Context, pachctlCfg *pachctl.Config) *cobra.Command {
listClusters := &cobra.Command{
Short: "List clusters registered with the license server.",
Long: "This command lists clusters registered with Enterprise Server.",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
c, err := pachctlCfg.NewOnUserMachine(ctx, true)
if err != nil {
return errors.Wrapf(err, "could not connect")
}
defer c.Close()
resp, err := c.License.ListClusters(c.Ctx(), &license.ListClustersRequest{})
if err != nil {
return grpcutil.ScrubGRPC(err)
}
for _, cluster := range resp.Clusters {
fmt.Printf("id: %v\naddress: %v\nversion: %v\nauth_enabled: %v\nlast_heartbeat: %v\n---\n", cluster.Id, cluster.Address, cluster.Version, cluster.AuthEnabled, cluster.LastHeartbeat)
}
return nil
}),
}
return cmdutil.CreateAlias(listClusters, "license list-clusters")
}
// DeleteAllCmd returns a cobra.Command to disable enterprise features and
// clear the configuration of the license service.
func DeleteAllCmd(ctx context.Context, pachctlCfg *pachctl.Config) *cobra.Command {
activate := &cobra.Command{
Use: "{{alias}}",
Short: "Delete all data from the license server",
Long: "This command deletes all data from Enterprise Server.",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
c, err := pachctlCfg.NewOnUserMachine(ctx, true)
if err != nil {
return errors.Wrapf(err, "could not connect")
}
defer c.Close()
if _, err := c.License.DeleteAll(c.Ctx(), &license.DeleteAllRequest{}); err != nil {
return errors.EnsureStack(err)
}
fmt.Printf("All data deleted from license server.")
return nil
}),
}
return cmdutil.CreateAlias(activate, "license delete-all")
}
// GetStateCmd returns a cobra.Command to get the state of the license service.
func GetStateCmd(ctx context.Context, pachctlCfg *pachctl.Config) *cobra.Command {
getState := &cobra.Command{
Short: "Get the configuration of the license service.",
Long: "This command returns the configuration of the Enterprise Server.",
Run: cmdutil.Run(func(args []string) error {
c, err := pachctlCfg.NewOnUserMachine(ctx, true)
if err != nil {
return errors.Wrapf(err, "could not connect")
}
defer c.Close()
resp, err := c.License.GetActivationCode(c.Ctx(), &license.GetActivationCodeRequest{})
if err != nil {
return errors.EnsureStack(err)
}
if resp.State == enterprise.State_NONE {
fmt.Println("No Pachyderm Enterprise license is configured")
return nil
}
ts := resp.GetInfo().GetExpires().AsTime()
fmt.Printf("Pachyderm Enterprise token state: %s\nExpiration: %s\nLicense: %s\n",
resp.State.String(), ts.String(), resp.ActivationCode)
return nil
}),
}
return cmdutil.CreateAlias(getState, "license get-state")
}
// Cmds returns pachctl commands related to Pachyderm Enterprise
func Cmds(mainCtx context.Context, pachctlCfg *pachctl.Config) []*cobra.Command {
var commands []*cobra.Command
enterprise := &cobra.Command{
Short: "License commmands manage the Enterprise License service",
Long: "License commands manage the Enterprise License service",
}
commands = append(commands, cmdutil.CreateAlias(enterprise, "license"))
commands = append(commands, ActivateCmd(mainCtx, pachctlCfg))
commands = append(commands, AddClusterCmd(mainCtx, pachctlCfg))
commands = append(commands, UpdateClusterCmd(mainCtx, pachctlCfg))
commands = append(commands, DeleteClusterCmd(mainCtx, pachctlCfg))
commands = append(commands, ListClustersCmd(mainCtx, pachctlCfg))
commands = append(commands, DeleteAllCmd(mainCtx, pachctlCfg))
commands = append(commands, GetStateCmd(mainCtx, pachctlCfg))
return commands
}