forked from CodisLabs/codis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_group.go
130 lines (113 loc) · 3.02 KB
/
server_group.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
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package main
import (
"fmt"
"strconv"
"github.com/docopt/docopt-go"
"github.com/wandoulabs/codis/pkg/models"
"github.com/wandoulabs/codis/pkg/utils/log"
)
// codis redis instance manage tool
func cmdServer(argv []string) (err error) {
usage := `usage:
codis-config server list
codis-config server add <group_id> <redis_addr> <role>
codis-config server remove <group_id> <redis_addr>
codis-config server promote <group_id> <redis_addr>
codis-config server add-group <group_id>
codis-config server remove-group <group_id>
`
args, err := docopt.Parse(usage, argv, true, "", false)
if err != nil {
log.ErrorErrorf(err, "parse args failed")
return err
}
log.Debugf("parse args = {%+v}", args)
if args["list"].(bool) {
return runListServerGroup()
}
groupId, err := strconv.Atoi(args["<group_id>"].(string))
if err != nil {
log.ErrorErrorf(err, "parse <group_id> failed")
return err
}
if args["remove-group"].(bool) {
return runRemoveServerGroup(groupId)
}
if args["add-group"].(bool) {
return runAddServerGroup(groupId)
}
serverAddr := args["<redis_addr>"].(string)
if args["add"].(bool) {
role := args["<role>"].(string)
return runAddServerToGroup(groupId, serverAddr, role)
}
if args["remove"].(bool) {
return runRemoveServerFromGroup(groupId, serverAddr)
}
if args["promote"].(bool) {
return runPromoteServerToMaster(groupId, serverAddr)
}
return nil
}
func runAddServerGroup(groupId int) error {
serverGroup := models.NewServerGroup(globalEnv.ProductName(), groupId)
var v interface{}
err := callApi(METHOD_PUT, "/api/server_groups", serverGroup, &v)
if err != nil {
return err
}
fmt.Println(jsonify(v))
return nil
}
func runPromoteServerToMaster(groupId int, addr string) error {
s := models.Server{
Addr: addr,
GroupId: groupId,
}
var v interface{}
err := callApi(METHOD_POST, fmt.Sprintf("/api/server_group/%d/promote", groupId), s, &v)
if err != nil {
return err
}
fmt.Println(jsonify(v))
return nil
}
func runAddServerToGroup(groupId int, addr string, role string) error {
server := models.NewServer(role, addr)
var v interface{}
err := callApi(METHOD_PUT, fmt.Sprintf("/api/server_group/%d/addServer", groupId), server, &v)
if err != nil {
return err
}
fmt.Println(jsonify(v))
return nil
}
func runListServerGroup() error {
var v interface{}
err := callApi(METHOD_GET, "/api/server_groups", nil, &v)
if err != nil {
return err
}
fmt.Println(jsonify(v))
return nil
}
func runRemoveServerGroup(groupId int) error {
var v interface{}
err := callApi(METHOD_DELETE, fmt.Sprintf("/api/server_group/%d", groupId), nil, &v)
if err != nil {
return err
}
fmt.Println(jsonify(v))
return nil
}
func runRemoveServerFromGroup(groupId int, addr string) error {
var v interface{}
err := callApi(METHOD_PUT, fmt.Sprintf("/api/server_group/%d/removeServer", groupId), models.Server{Addr: addr}, &v)
if err != nil {
return err
}
fmt.Println(jsonify(v))
return nil
}