-
Notifications
You must be signed in to change notification settings - Fork 566
/
cmd.go
463 lines (428 loc) · 14.1 KB
/
cmd.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package cmd
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
golog "log"
"os"
"path"
"strings"
"syscall"
"text/tabwriter"
"time"
etcd "github.com/coreos/etcd/clientv3"
"github.com/facebookgo/pidfile"
"github.com/fatih/color"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/types"
"github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/version"
"github.com/pachyderm/pachyderm/src/client/version/versionpb"
admincmds "github.com/pachyderm/pachyderm/src/server/admin/cmds"
authcmds "github.com/pachyderm/pachyderm/src/server/auth/cmds"
debugcmds "github.com/pachyderm/pachyderm/src/server/debug/cmds"
enterprisecmds "github.com/pachyderm/pachyderm/src/server/enterprise/cmds"
pfscmds "github.com/pachyderm/pachyderm/src/server/pfs/cmds"
"github.com/pachyderm/pachyderm/src/server/pkg/cmdutil"
deploycmds "github.com/pachyderm/pachyderm/src/server/pkg/deploy/cmds"
"github.com/pachyderm/pachyderm/src/server/pkg/metrics"
ppscmds "github.com/pachyderm/pachyderm/src/server/pps/cmds"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
)
const (
bashCompletionPath = "/etc/bash_completion.d/pachctl"
bashCompletionFunc = `
__pachctl_get_object() {
if [[ ${#nouns[@]} -ne $1 ]]; then
return
fi
local pachctl_output out
if pachctl_output=$(eval pachctl $2 2>/dev/null); then
out=($(echo "${pachctl_output}" | awk -v c=$3 'NR > 1 {print $c}'))
COMPREPLY+=($(compgen -W "${out[*]}" -- "$cur"))
fi
}
__pachctl_get_repo() {
__pachctl_get_object $1 "list-repo" 1
}
__pachctl_get_commit() {
__pachctl_get_object $1 "list-commit $2" 2
}
__pachctl_get_path() {
__pachctl_get_object $1 "glob-file $2 $3 \"${words[${#words[@]}-1]}**\"" 1
}
__pachctl_get_branch() {
__pachctl_get_object $1 "list-branch $2" 1
}
__pachctl_get_job() {
__pachctl_get_object $1 "list-job" 1
}
__pachctl_get_pipeline() {
__pachctl_get_object $1 "list-pipeline" 1
}
__pachctl_get_datum() {
__pachctl_get_object $1 "list-datum $2" 1
}
__custom_func() {
case ${last_command} in
pachctl_update-repo | pachctl_inspect-repo | pachctl_delete-repo | pachctl_list-commit | pachctl_list-branch)
__pachctl_get_repo 0
;;
pachctl_start-commit | pachctl_subscribe-commit | pachctl_delete-branch)
__pachctl_get_repo 0
__pachctl_get_branch 1 ${nouns[0]}
;;
pachctl_finish-commit | pachctl_inspect-commit | pachctl_delete-commit | pachctl_glob-file)
__pachctl_get_repo 0
__pachctl_get_commit 1 ${nouns[0]}
;;
pachctl_set-branch)
__pachctl_get_repo 0
__pachctl_get_commit 1 ${nouns[0]}
__pachctl_get_branch 1 ${nouns[0]}
;;
pachctl_put-file)
__pachctl_get_repo 0
__pachctl_get_branch 1 ${nouns[0]}
__pachctl_get_path 2 ${nouns[0]} ${nouns[1]}
;;
pachctl_copy-file | pachctl_diff-file)
__pachctl_get_repo 0
__pachctl_get_commit 1 ${nouns[0]}
__pachctl_get_path 2 ${nouns[0]} ${nouns[1]}
__pachctl_get_repo 3
__pachctl_get_commit 4 ${nouns[3]}
__pachctl_get_path 5 ${nouns[3]} ${nouns[4]}
;;
pachctl_get-file | pachctl_inspect-file | pachctl_list-file | pachctl_delete-file)
__pachctl_get_repo 0
__pachctl_get_commit 1 ${nouns[0]}
__pachctl_get_path 2 ${nouns[0]} ${nouns[1]}
;;
pachctl_inspect-job | pachctl_delete-job | pachctl_stop-job | pachctl_list-datum)
__pachctl_get_job 0
;;
pachctl_inspect-datum)
__pachctl_get_job 0
__pachctl_get_datum 1 ${nouns[0]}
;;
pachctl_inspect-pipeline | pachctl_delete-pipeline | pachctl_start-pipeline | pachctl_stop-pipeline | pachctl_run-pipeline)
__pachctl_get_pipeline 0
;;
*)
;;
esac
}`
)
// PachctlCmd creates a cobra.Command which can deploy pachyderm clusters and
// interact with them (it implements the pachctl binary).
func PachctlCmd() (*cobra.Command, error) {
var verbose bool
var noMetrics bool
raw := false
rawFlag := func(cmd *cobra.Command) {
cmd.Flags().BoolVar(&raw, "raw", false, "disable pretty printing, print raw json")
}
marshaller := &jsonpb.Marshaler{Indent: " "}
rootCmd := &cobra.Command{
Use: os.Args[0],
Long: `Access the Pachyderm API.
Environment variables:
ADDRESS=<host>:<port>, the pachd server to connect to (e.g. 127.0.0.1:30650).
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if !verbose {
// Silence any grpc logs
l := log.New()
l.Level = log.FatalLevel
grpclog.SetLogger(l)
} else {
// etcd overrides grpc's logs--there's no way to enable one without
// enabling both
etcd.SetLogger(golog.New(os.Stderr, "[etcd/grpc] ", golog.LstdFlags|golog.Lshortfile))
}
},
BashCompletionFunction: bashCompletionFunc,
}
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Output verbose logs")
rootCmd.PersistentFlags().BoolVarP(&noMetrics, "no-metrics", "", false, "Don't report user metrics for this command")
pfsCmds := pfscmds.Cmds(&noMetrics)
for _, cmd := range pfsCmds {
rootCmd.AddCommand(cmd)
}
ppsCmds, err := ppscmds.Cmds(&noMetrics)
if err != nil {
return nil, err
}
for _, cmd := range ppsCmds {
rootCmd.AddCommand(cmd)
}
deployCmds := deploycmds.Cmds(&noMetrics)
for _, cmd := range deployCmds {
rootCmd.AddCommand(cmd)
}
authCmds := authcmds.Cmds()
for _, cmd := range authCmds {
rootCmd.AddCommand(cmd)
}
enterpriseCmds := enterprisecmds.Cmds()
for _, cmd := range enterpriseCmds {
rootCmd.AddCommand(cmd)
}
adminCmds := admincmds.Cmds(&noMetrics)
for _, cmd := range adminCmds {
rootCmd.AddCommand(cmd)
}
debugCmds := debugcmds.Cmds(&noMetrics)
for _, cmd := range debugCmds {
rootCmd.AddCommand(cmd)
}
var clientOnly bool
versionCmd := &cobra.Command{
Use: "version",
Short: "Return version information.",
Long: "Return version information.",
Run: cmdutil.RunFixedArgs(0, func(args []string) (retErr error) {
if clientOnly {
if raw {
if err := marshaller.Marshal(os.Stdout, version.Version); err != nil {
return err
}
} else {
fmt.Println(version.PrettyPrintVersion(version.Version))
}
return nil
}
if !noMetrics {
start := time.Now()
startMetricsWait := metrics.StartReportAndFlushUserAction("Version", start)
defer startMetricsWait()
defer func() {
finishMetricsWait := metrics.FinishReportAndFlushUserAction("Version", retErr, start)
finishMetricsWait()
}()
}
writer := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
if raw {
if err := marshaller.Marshal(os.Stdout, version.Version); err != nil {
return err
}
} else {
printVersionHeader(writer)
printVersion(writer, "pachctl", version.Version)
if err := writer.Flush(); err != nil {
return err
}
}
pachClient, err := client.NewOnUserMachine(false, "user")
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
version, err := pachClient.GetVersion(ctx, &types.Empty{})
if err != nil {
buf := bytes.NewBufferString("")
errWriter := tabwriter.NewWriter(buf, 20, 1, 3, ' ', 0)
fmt.Fprintf(errWriter, "pachd\t(version unknown) : error connecting to pachd server at address (%v): %v\n\nplease make sure pachd is up (`kubectl get all`) and portforwarding is enabled\n", pachClient.GetAddress(), grpc.ErrorDesc(err))
errWriter.Flush()
return errors.New(buf.String())
}
if raw {
if err := marshaller.Marshal(os.Stdout, version); err != nil {
return err
}
} else {
printVersion(writer, "pachd", version)
if err := writer.Flush(); err != nil {
return err
}
}
return nil
}),
}
versionCmd.Flags().BoolVar(&clientOnly, "client-only", false, "If set, "+
"only print pachctl's version, but don't make any RPCs to pachd. Useful "+
"if pachd is unavailable")
rawFlag(versionCmd)
deleteAll := &cobra.Command{
Use: "delete-all",
Short: "Delete everything.",
Long: `Delete all repos, commits, files, pipelines and jobs.
This resets the cluster to its initial state.`,
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
client, err := client.NewOnUserMachine(!noMetrics, "user")
if err != nil {
return err
}
red := color.New(color.FgRed).SprintFunc()
var repos, pipelines []string
repoInfos, err := client.ListRepo()
if err != nil {
return err
}
for _, ri := range repoInfos {
repos = append(repos, red(ri.Repo.Name))
}
pipelineInfos, err := client.ListPipeline()
if err != nil {
return err
}
for _, pi := range pipelineInfos {
pipelines = append(pipelines, red(pi.Pipeline.Name))
}
fmt.Printf("Are you sure you want to delete all ACLs, repos, commits, files, pipelines and jobs?\nyN\n")
if len(repos) > 0 {
fmt.Printf("Repos to delete: %s\n", strings.Join(repos, ", "))
}
if len(pipelines) > 0 {
fmt.Printf("Pipelines to delete: %s\n", strings.Join(pipelines, ", "))
}
r := bufio.NewReader(os.Stdin)
bytes, err := r.ReadBytes('\n')
if err != nil {
return err
}
if bytes[0] == 'y' || bytes[0] == 'Y' {
return client.DeleteAll()
}
return nil
}),
}
var port int
var samlPort int
var uiPort int
var uiWebsocketPort int
var kubeCtlFlags string
var namespace string
portForward := &cobra.Command{
Use: "port-forward",
Short: "Forward a port on the local machine to pachd. This command blocks.",
Long: "Forward a port on the local machine to pachd. This command blocks.",
Run: cmdutil.RunFixedArgs(0, func(args []string) error {
pidfile.SetPidfilePath(path.Join(os.Getenv("HOME"), ".pachyderm/port-forward.pid"))
pid, err := pidfile.Read()
if err != nil && !os.IsNotExist(err) {
return err
}
if pid != 0 {
if err := syscall.Kill(-pid, syscall.SIGKILL); err != nil {
if !strings.Contains(err.Error(), "no such process") {
return err
}
}
}
if err := pidfile.Write(); err != nil {
return err
}
kubeCtlFlags += fmt.Sprintf(" --namespace=%s", namespace)
var eg errgroup.Group
eg.Go(func() error {
fmt.Println("Forwarding the pachd (Pachyderm daemon) port...")
stdin := strings.NewReader(fmt.Sprintf(`
pod=$(kubectl %s get pod -l app=pachd --output='jsonpath={.items[0].metadata.name}')
kubectl %s port-forward "$pod" %d:650
`, kubeCtlFlags, kubeCtlFlags, port))
if err := cmdutil.RunIO(cmdutil.IO{
Stdin: stdin,
Stderr: os.Stderr,
}, "sh"); err != nil {
fmt.Println("Could not forward Pachd port")
return fmt.Errorf("Could not forward Pachd port")
}
return nil
})
eg.Go(func() error {
fmt.Println("Forwarding the SAML ACS port...")
stdin := strings.NewReader(fmt.Sprintf(`
pod=$(kubectl %s get pod -l suite=pachyderm,app=pachd --output='jsonpath={.items[0].metadata.name}')
kubectl %s port-forward "$pod" %d:654
`, kubeCtlFlags, kubeCtlFlags, samlPort))
if err := cmdutil.RunIO(cmdutil.IO{
Stdin: stdin,
Stderr: os.Stderr,
}, "sh"); err != nil {
return fmt.Errorf("Could not forward Pachyderm SAML ACS port")
}
return nil
})
eg.Go(func() error {
stdin := strings.NewReader(fmt.Sprintf(`
pod=$(kubectl %s get pod -l app=dash --output='jsonpath={.items[0].metadata.name}')
kubectl %s port-forward "$pod" %d:8080
`, kubeCtlFlags, kubeCtlFlags, uiPort))
if err := cmdutil.RunIO(cmdutil.IO{
Stdin: stdin,
}, "sh"); err != nil {
fmt.Fprintf(os.Stderr, "Is the dashboard deployed? If not, deploy with \"pachctl deploy local --dashboard-only\"")
return fmt.Errorf("Could not forward dash UI port")
}
return nil
})
eg.Go(func() error {
fmt.Printf("Forwarding the dash (Pachyderm dashboard) UI port to http://localhost:%v ...\n", uiPort)
stdin := strings.NewReader(fmt.Sprintf(`
pod=$(kubectl %v get pod -l app=dash --output='jsonpath={.items[0].metadata.name}')
kubectl %v port-forward "$pod" %d:8081
`, kubeCtlFlags, kubeCtlFlags, uiWebsocketPort))
if err := cmdutil.RunIO(cmdutil.IO{
Stdin: stdin,
}, "sh"); err != nil {
return fmt.Errorf("Could not forward dash websocket port")
}
return nil
})
fmt.Println("CTRL-C to exit")
fmt.Println("NOTE: kubernetes port-forward often outputs benign error messages, these should be ignored unless they seem to be impacting your ability to connect over the forwarded port.")
return eg.Wait()
}),
}
portForward.Flags().IntVarP(&port, "port", "p", 30650, "The local port to bind pachd to.")
portForward.Flags().IntVar(&samlPort, "saml-port", 30654, "The local port to bind pachd's SAML ACS to.")
portForward.Flags().IntVarP(&uiPort, "ui-port", "u", 30080, "The local port to bind Pachyderm's dash service to.")
portForward.Flags().IntVarP(&uiWebsocketPort, "proxy-port", "x", 30081, "The local port to bind Pachyderm's dash proxy service to.")
portForward.Flags().StringVarP(&kubeCtlFlags, "kubectlflags", "k", "", "Any kubectl flags to proxy, e.g. --kubectlflags='--kubeconfig /some/path/kubeconfig'")
portForward.Flags().StringVar(&namespace, "namespace", "default", "Kubernetes namespace Pachyderm is deployed in.")
completion := &cobra.Command{
Use: "completion",
Short: "Install bash completion code.",
Long: "Install bash completion code.",
Run: cmdutil.RunFixedArgs(0, func(args []string) (retErr error) {
bashCompletionFile, err := os.Create(bashCompletionPath)
if err != nil {
if os.IsPermission(err) {
fmt.Fprintf(os.Stderr, "Permission error installing completions, rerun this command with sudo:\n$ sudo env \"PATH=$PATH\" pachctl completion\n")
}
return err
}
defer func() {
if err := bashCompletionFile.Close(); err != nil && retErr == nil {
retErr = err
}
}()
if err := rootCmd.GenBashCompletion(bashCompletionFile); err != nil {
return err
}
fmt.Printf("Bash completions installed in %s, you must restart bash to enable completions.\n", bashCompletionPath)
return nil
}),
}
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(deleteAll)
rootCmd.AddCommand(portForward)
rootCmd.AddCommand(completion)
return rootCmd, nil
}
func printVersionHeader(w io.Writer) {
fmt.Fprintf(w, "COMPONENT\tVERSION\t\n")
}
func printVersion(w io.Writer, component string, v *versionpb.Version) {
fmt.Fprintf(w, "%s\t%s\t\n", component, version.PrettyPrintVersion(v))
}