1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package cmd
17
18 import (
19 "os"
20
21 "github.com/spf13/cobra"
22
23 cm "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common"
24 "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/debug"
25 "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/recover"
26 "github.com/chaos-mesh/chaos-mesh/pkg/log"
27 )
28
29 var managerNamespace, managerSvc string
30
31
32 var rootCmd = &cobra.Command{
33 Use: "chaosctl [command] [options]",
34 Short: "Interacting with chaos mesh",
35 Long: `
36 Interacting with chaos mesh
37
38 # show debug info
39 chaosctl debug networkchaos
40
41 # show logs of all chaos-mesh components
42 chaosctl logs
43
44 # forcedly recover chaos from pods
45 chaosctl recover networkchaos pod1 -n test`,
46 }
47
48
49
50 func Execute() {
51 rootLogger, err := log.NewDefaultZapLogger()
52 if err != nil {
53 cm.PrettyPrint("failed to initialize logger: ", 0, cm.Red)
54 cm.PrettyPrint(err.Error(), 1, cm.Red)
55 os.Exit(1)
56 }
57
58 rootCmd.PersistentFlags().StringVarP(&managerNamespace, "manager-namespace", "N", "chaos-mesh", "the namespace chaos-controller-manager in")
59 rootCmd.PersistentFlags().StringVarP(&managerSvc, "manager-svc", "s", "chaos-mesh-controller-manager", "the service to chaos-controller-manager")
60 err = rootCmd.RegisterFlagCompletionFunc("manager-namespace", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
61
62 return nil, cobra.ShellCompDirectiveNoFileComp
63 })
64 if err != nil {
65 cm.PrettyPrint("failed to register completion function for flag 'manager-namespace': ", 0, cm.Red)
66 cm.PrettyPrint(err.Error(), 1, cm.Red)
67 os.Exit(1)
68 }
69 err = rootCmd.RegisterFlagCompletionFunc("manager-svc", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
70
71 return nil, cobra.ShellCompDirectiveNoFileComp
72 })
73 if err != nil {
74 cm.PrettyPrint("failed to register completion function for flag 'manager-svc': ", 0, cm.Red)
75 cm.PrettyPrint(err.Error(), 1, cm.Red)
76 os.Exit(1)
77 }
78
79 logsCmd, err := NewLogsCmd()
80 if err != nil {
81 cm.PrettyPrint("failed to initialize cmd: ", 0, cm.Red)
82 cm.PrettyPrint("log command: "+err.Error(), 1, cm.Red)
83 os.Exit(1)
84 }
85
86 rootCmd.AddCommand(logsCmd)
87
88 debugCommand, err := NewDebugCommand(rootLogger.WithName("cmd-debug"), map[string]debug.Debug{
89 networkChaos: debug.NetworkDebug,
90 ioChaos: debug.IODebug,
91 stressChaos: debug.StressDebug,
92 httpChaos: debug.HTTPDebug,
93 })
94 if err != nil {
95 cm.PrettyPrint("failed to initialize cmd: ", 0, cm.Red)
96 cm.PrettyPrint("debug command: "+err.Error(), 1, cm.Red)
97 os.Exit(1)
98 }
99
100 recoverCommand, err := NewRecoverCommand(rootLogger.WithName("cmd-recover"), map[string]recover.RecovererBuilder{
101 httpChaos: recover.HTTPRecoverer,
102 ioChaos: recover.IORecoverer,
103 stressChaos: recover.StressRecoverer,
104 networkChaos: recover.NetworkRecoverer,
105 })
106 if err != nil {
107 cm.PrettyPrint("failed to initialize cmd: ", 0, cm.Red)
108 cm.PrettyPrint("recover command: "+err.Error(), 1, cm.Red)
109 os.Exit(1)
110 }
111
112 physicalMachineCommand, err := NewPhysicalMachineCommand()
113 if err != nil {
114 cm.PrettyPrint("failed to initialize cmd: ", 0, cm.Red)
115 cm.PrettyPrint("physicalmachine command: "+err.Error(), 1, cm.Red)
116 os.Exit(1)
117 }
118
119 rootCmd.AddCommand(debugCommand)
120 rootCmd.AddCommand(recoverCommand)
121 rootCmd.AddCommand(completionCmd)
122 rootCmd.AddCommand(forwardCmd)
123 rootCmd.AddCommand(physicalMachineCommand)
124
125 if err := rootCmd.Execute(); err != nil {
126 cm.PrettyPrint("failed to execute cmd: ", 0, cm.Red)
127 cm.PrettyPrint(err.Error(), 1, cm.Red)
128 os.Exit(1)
129 }
130 }
131
132 func noCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
133 return nil, cobra.ShellCompDirectiveNoFileComp
134 }
135