Skip to content

Commit 69f9018

Browse files
committed
Add: CLI tool to improve cluster management
Signed-off-by: Mofei Zhang <mofei2816@gmail.com>
1 parent 2b83f31 commit 69f9018

17 files changed

+874
-175
lines changed

cli/cli-sample.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"masterAddr": [
3+
"master.chubao.io"
4+
]
5+
}

cli/cli.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2018 The Chubao Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
"io/ioutil"
21+
"os"
22+
"path"
23+
24+
"github.com/chubaofs/chubaofs/cli/cmd"
25+
"github.com/chubaofs/chubaofs/sdk/master"
26+
"github.com/spf13/cobra"
27+
)
28+
29+
var (
30+
CommitID string
31+
BranchName string
32+
BuildTime string
33+
)
34+
35+
var (
36+
defaultHomeDir, _ = os.UserHomeDir()
37+
defaultConfigName = ".cfs-cli.json"
38+
defaultConfigPath = path.Join(defaultHomeDir, defaultConfigName)
39+
defaultConfigData = []byte(`
40+
{
41+
"masterAddr": [
42+
"master.chubao.io"
43+
]
44+
}
45+
`)
46+
)
47+
48+
type config struct {
49+
MasterAddr []string `json:"masterAddr"`
50+
}
51+
52+
func runCLI() (err error) {
53+
var cfg *config
54+
if cfg, err = loadConfig(); err != nil {
55+
return
56+
}
57+
err = setupCommands(cfg).Execute()
58+
return
59+
}
60+
61+
func loadConfig() (*config, error) {
62+
var err error
63+
var configData []byte
64+
if configData, err = ioutil.ReadFile(defaultConfigPath); err != nil && !os.IsNotExist(err) {
65+
return nil, err
66+
}
67+
if os.IsNotExist(err) {
68+
if err = ioutil.WriteFile(defaultConfigPath, defaultConfigData, 0600); err != nil {
69+
return nil, err
70+
}
71+
configData = defaultConfigData
72+
}
73+
var config = &config{}
74+
if err = json.Unmarshal(configData, config); err != nil {
75+
return nil, err
76+
}
77+
return config, nil
78+
}
79+
80+
func setupCommands(cfg *config) *cobra.Command {
81+
fmt.Printf("Using master address: %v\n", cfg.MasterAddr)
82+
var mc = master.NewMasterClient(cfg.MasterAddr, false)
83+
return cmd.NewRootCmd(mc)
84+
}
85+
86+
func main() {
87+
var err error
88+
if err = runCLI(); err != nil {
89+
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
90+
os.Exit(1)
91+
}
92+
}

cli/cmd/cluster.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2018 The Chubao Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"strings"
21+
22+
"github.com/chubaofs/chubaofs/proto"
23+
"github.com/chubaofs/chubaofs/sdk/master"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
const (
28+
cmdClusterUse = "cluster [COMMAND]"
29+
cmdClusterShort = "Manage cluster components"
30+
)
31+
32+
func newClusterCmd(client *master.MasterClient) *cobra.Command {
33+
var cmd = &cobra.Command{
34+
Use: cmdClusterUse,
35+
Short: cmdClusterShort,
36+
}
37+
cmd.AddCommand(
38+
newClusterInfoCmd(client),
39+
)
40+
return cmd
41+
}
42+
43+
const (
44+
cmdClusterInfoUse = "info"
45+
cmdClusterInfoShort = "Show cluster summary information"
46+
)
47+
48+
func newClusterInfoCmd(client *master.MasterClient) *cobra.Command {
49+
var cmd = &cobra.Command{
50+
Use: cmdClusterInfoUse,
51+
Short: cmdClusterInfoShort,
52+
Run: func(cmd *cobra.Command, args []string) {
53+
var err error
54+
var cv *proto.ClusterView
55+
if cv, err = client.AdminAPI().GetCluster(); err != nil {
56+
errout("Get cluster info fail:\n%v\n", err)
57+
os.Exit(1)
58+
}
59+
stdout("\n[Summary]\n")
60+
stdout(formatClusterView(cv))
61+
stdout("\n")
62+
},
63+
}
64+
return cmd
65+
}
66+
67+
func formatClusterView(cv *proto.ClusterView) string {
68+
var sb = strings.Builder{}
69+
sb.WriteString(fmt.Sprintf(" Name : %v\n", cv.Name))
70+
sb.WriteString(fmt.Sprintf(" Auto allocate : %v\n", !cv.DisableAutoAlloc))
71+
sb.WriteString(fmt.Sprintf(" MetaNode count: %v\n", len(cv.MetaNodes)))
72+
sb.WriteString(fmt.Sprintf(" DataNode count: %v\n", len(cv.DataNodes)))
73+
sb.WriteString(fmt.Sprintf(" Volume count : %v\n", len(cv.VolStatInfo)))
74+
return sb.String()
75+
}

cli/cmd/root.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2018 The Chubao Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
"os"
20+
"path"
21+
22+
"github.com/chubaofs/chubaofs/sdk/master"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
const (
27+
cmdRootShort = "ChubaoFS Command Line Interface (CLI)"
28+
)
29+
30+
func NewRootCmd(client *master.MasterClient) *cobra.Command {
31+
var cmd = &cobra.Command{
32+
Use: path.Base(os.Args[0]),
33+
Short: cmdRootShort,
34+
Args: cobra.MinimumNArgs(0),
35+
}
36+
cmd.AddCommand(
37+
newClusterCmd(client),
38+
newVolCmd(client),
39+
newUserCmd(client),
40+
)
41+
return cmd
42+
}
43+
44+
func stdout(format string, a ...interface{}) {
45+
_, _ = fmt.Fprintf(os.Stdout, format, a...)
46+
}
47+
48+
func errout(format string, a ...interface{}) {
49+
_, _ = fmt.Fprintf(os.Stderr, format, a...)
50+
}

0 commit comments

Comments
 (0)