Skip to content

Commit f8de164

Browse files
SeanHaiWine93
authored andcommitted
Feature(monitor): now we support deploy monitor.
Signed-off-by: wanghai01 <[email protected]>
1 parent 2cf7c66 commit f8de164

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2831
-808
lines changed

cli/cli/cli.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type CurveAdm struct {
6969
clusterName string // current cluster name
7070
clusterTopologyData string // cluster topology
7171
clusterPoolData string // cluster pool
72+
monitor storage.Monitor
7273
}
7374

7475
/*
@@ -173,6 +174,13 @@ func (curveadm *CurveAdm) init() error {
173174
log.Field("ClusterName", cluster.Name))
174175
}
175176

177+
// (8) Get monitor configure
178+
monitor, err := s.GetMonitor(cluster.Id)
179+
if err != nil {
180+
log.Error("Get monitor failed", log.Field("Error", err))
181+
return errno.ERR_GET_MONITOR_FAILED.E(err)
182+
}
183+
176184
curveadm.logpath = logpath
177185
curveadm.config = config
178186
curveadm.in = os.Stdin
@@ -186,6 +194,7 @@ func (curveadm *CurveAdm) init() error {
186194
curveadm.clusterName = cluster.Name
187195
curveadm.clusterTopologyData = cluster.Topology
188196
curveadm.clusterPoolData = cluster.Pool
197+
curveadm.monitor = monitor
189198

190199
return nil
191200
}
@@ -267,6 +276,7 @@ func (curveadm *CurveAdm) ClusterUUId() string { return curveadm.c
267276
func (curveadm *CurveAdm) ClusterName() string { return curveadm.clusterName }
268277
func (curveadm *CurveAdm) ClusterTopologyData() string { return curveadm.clusterTopologyData }
269278
func (curveadm *CurveAdm) ClusterPoolData() string { return curveadm.clusterPoolData }
279+
func (curveadm *CurveAdm) Monitor() storage.Monitor { return curveadm.monitor }
270280

271281
func (curveadm *CurveAdm) GetHost(host string) (*hosts.HostConfig, error) {
272282
if len(curveadm.Hosts()) == 0 {

cli/command/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/opencurve/curveadm/cli/command/cluster"
3333
"github.com/opencurve/curveadm/cli/command/config"
3434
"github.com/opencurve/curveadm/cli/command/hosts"
35+
"github.com/opencurve/curveadm/cli/command/monitor"
3536
"github.com/opencurve/curveadm/cli/command/pfs"
3637
"github.com/opencurve/curveadm/cli/command/playground"
3738
"github.com/opencurve/curveadm/cli/command/target"
@@ -64,6 +65,7 @@ func addSubCommands(cmd *cobra.Command, curveadm *cli.CurveAdm) {
6465
playground.NewPlaygroundCommand(curveadm), // curveadm playground ...
6566
target.NewTargetCommand(curveadm), // curveadm target ...
6667
pfs.NewPFSCommand(curveadm), // curveadm pfs ...
68+
monitor.NewMonitorCommand(curveadm), // curveadm monitor ...
6769

6870
NewAuditCommand(curveadm), // curveadm audit
6971
NewCleanCommand(curveadm), // curveadm clean

cli/command/monitor/clean.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

cli/command/monitor/cmd.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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-17
20+
* Author: wanghai (SeanHai)
21+
*/
22+
23+
package monitor
24+
25+
import (
26+
"github.com/opencurve/curveadm/cli/cli"
27+
cliutil "github.com/opencurve/curveadm/internal/utils"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func NewMonitorCommand(curveadm *cli.CurveAdm) *cobra.Command {
32+
cmd := &cobra.Command{
33+
Use: "monitor",
34+
Short: "Manage monitor",
35+
Args: cliutil.NoArgs,
36+
RunE: cliutil.ShowHelp(curveadm.Err()),
37+
}
38+
39+
cmd.AddCommand(
40+
NewDeployCommand(curveadm),
41+
NewStartCommand(curveadm),
42+
NewStopCommand(curveadm),
43+
NewStatusCommand(curveadm),
44+
NewCleanCommand(curveadm),
45+
NewRestartCommand(curveadm),
46+
NewReloadCommand(curveadm),
47+
)
48+
return cmd
49+
}

0 commit comments

Comments
 (0)