|
| 1 | +/* |
| 2 | +* Copyright (c) 2023 NetEase Inc. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | +* Project: Curveadm |
| 19 | +* Created Date: 2023-04-27 |
| 20 | +* Author: wanghai (SeanHai) |
| 21 | + */ |
| 22 | + |
| 23 | +package monitor |
| 24 | + |
| 25 | +import ( |
| 26 | + "github.com/opencurve/curveadm/cli/cli" |
| 27 | + comm "github.com/opencurve/curveadm/internal/common" |
| 28 | + "github.com/opencurve/curveadm/internal/configure" |
| 29 | + "github.com/opencurve/curveadm/internal/errno" |
| 30 | + "github.com/opencurve/curveadm/internal/playbook" |
| 31 | + tui "github.com/opencurve/curveadm/internal/tui/common" |
| 32 | + cliutil "github.com/opencurve/curveadm/internal/utils" |
| 33 | + "github.com/spf13/cobra" |
| 34 | +) |
| 35 | + |
| 36 | +const ( |
| 37 | + CLEAN_EXAMPLE = `Examples: |
| 38 | + $ curveadm monitor clean # Clean everything for monitor |
| 39 | + $ curveadm monitor clean --only='data' # Clean data for monitor |
| 40 | + $ curveadm monitor clean --role=grafana --only=container # Clean container for grafana service` |
| 41 | +) |
| 42 | + |
| 43 | +var ( |
| 44 | + CLEAN_PLAYBOOK_STEPS = []int{ |
| 45 | + playbook.CLEAN_MONITOR, |
| 46 | + } |
| 47 | + |
| 48 | + CLEAN_ITEMS = []string{ |
| 49 | + comm.CLEAN_ITEM_DATA, |
| 50 | + comm.CLEAN_ITEM_CONTAINER, |
| 51 | + } |
| 52 | +) |
| 53 | + |
| 54 | +type cleanOptions struct { |
| 55 | + id string |
| 56 | + role string |
| 57 | + host string |
| 58 | + only []string |
| 59 | +} |
| 60 | + |
| 61 | +func NewCleanCommand(curveadm *cli.CurveAdm) *cobra.Command { |
| 62 | + var options cleanOptions |
| 63 | + |
| 64 | + cmd := &cobra.Command{ |
| 65 | + Use: "clean [OPTIONS]", |
| 66 | + Short: "Clean monitor's environment", |
| 67 | + Args: cliutil.NoArgs, |
| 68 | + Example: CLEAN_EXAMPLE, |
| 69 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 70 | + return runClean(curveadm, options) |
| 71 | + }, |
| 72 | + DisableFlagsInUseLine: true, |
| 73 | + } |
| 74 | + |
| 75 | + flags := cmd.Flags() |
| 76 | + flags.StringVar(&options.id, "id", "*", "Specify monitor service id") |
| 77 | + flags.StringVar(&options.role, "role", "*", "Specify monitor service role") |
| 78 | + flags.StringVar(&options.host, "host", "*", "Specify monitor service host") |
| 79 | + flags.StringSliceVarP(&options.only, "only", "o", CLEAN_ITEMS, "Specify clean item") |
| 80 | + return cmd |
| 81 | +} |
| 82 | + |
| 83 | +func genCleanPlaybook(curveadm *cli.CurveAdm, |
| 84 | + mcs []*configure.MonitorConfig, |
| 85 | + options cleanOptions) (*playbook.Playbook, error) { |
| 86 | + mcs = configure.FilterMonitorConfig(curveadm, mcs, configure.FilterMonitorOption{ |
| 87 | + Id: options.id, |
| 88 | + Role: options.role, |
| 89 | + Host: options.host, |
| 90 | + }) |
| 91 | + if len(mcs) == 0 { |
| 92 | + return nil, errno.ERR_NO_SERVICES_MATCHED |
| 93 | + } |
| 94 | + steps := CLEAN_PLAYBOOK_STEPS |
| 95 | + pb := playbook.NewPlaybook(curveadm) |
| 96 | + for _, step := range steps { |
| 97 | + pb.AddStep(&playbook.PlaybookStep{ |
| 98 | + Type: step, |
| 99 | + Configs: mcs, |
| 100 | + Options: map[string]interface{}{ |
| 101 | + comm.KEY_CLEAN_ITEMS: options.only, |
| 102 | + }, |
| 103 | + }) |
| 104 | + } |
| 105 | + return pb, nil |
| 106 | +} |
| 107 | + |
| 108 | +func runClean(curveadm *cli.CurveAdm, options cleanOptions) error { |
| 109 | + // 1) parse monitor config |
| 110 | + mcs, err := parseMonitorConfig(curveadm) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + // 2) generate clean playbook |
| 116 | + pb, err := genCleanPlaybook(curveadm, mcs, options) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + // 3) confirm by user |
| 122 | + if pass := tui.ConfirmYes(tui.PromptCleanService(options.role, options.host, options.only)); !pass { |
| 123 | + curveadm.WriteOut(tui.PromptCancelOpetation("clean monitor service")) |
| 124 | + return errno.ERR_CANCEL_OPERATION |
| 125 | + } |
| 126 | + |
| 127 | + // 4) run playground |
| 128 | + err = pb.Run() |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + return nil |
| 133 | +} |
0 commit comments