-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from shunfei/develop
v0.3.2
- Loading branch information
Showing
22 changed files
with
339 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/shunfei/cronsun" | ||
) | ||
|
||
var ( | ||
nodeCmd string | ||
nodeInclude string | ||
nodeExclude string | ||
|
||
spliter = "," | ||
) | ||
|
||
func init() { | ||
NodeCmd.Flags().StringVar(&nodeCmd, "cmd", "", "the command send to node") | ||
NodeCmd.Flags().StringVar(&nodeInclude, "include", "", "the node ids that needs to execute the command, split by ',', e.g: '--include=aa,bb,cc', empty means all nodes") | ||
NodeCmd.Flags().StringVar(&nodeExclude, "exclude", "", "the node ids that doesn't need to execute the command, split by ',', e.g: '--exclude=aa,bb,cc', empty means none") | ||
} | ||
|
||
var NodeCmd = &cobra.Command{ | ||
Use: "node", | ||
Short: "Send some commands to nodes", | ||
Long: `Send a command to nodes and execute it. | ||
Available Commands: | ||
rmold: remove old version(< 0.3.0) node info from mongodb and etcd | ||
sync: sync node info to mongodb | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ea := NewExitAction() | ||
ea.After = func() { | ||
fmt.Println() | ||
cmd.Help() | ||
} | ||
nc, err := cronsun.ToNodeCmd(nodeCmd) | ||
if err != nil { | ||
ea.Exit(err.Error() + ": " + nodeCmd) | ||
} | ||
|
||
var include, exclude []string | ||
if len(nodeInclude) > 0 { | ||
include = strings.Split(nodeInclude, spliter) | ||
} | ||
if len(nodeExclude) > 0 { | ||
exclude = strings.Split(nodeExclude, spliter) | ||
} | ||
|
||
err = cronsun.PutCsctl(&cronsun.CsctlCmd{ | ||
Cmd: nc, | ||
Include: include, | ||
Exclude: exclude, | ||
}) | ||
if err != nil { | ||
ea.ExitOnErr(err) | ||
} | ||
|
||
fmt.Printf("command[%s] send success\n", nodeCmd) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cronsun | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
|
||
client "github.com/coreos/etcd/clientv3" | ||
|
||
"github.com/shunfei/cronsun/conf" | ||
) | ||
|
||
const ( | ||
NodeCmdUnknown NodeCmd = iota | ||
NodeCmdRmOld | ||
NodeCmdSync | ||
NodeCmdMax | ||
) | ||
|
||
var ( | ||
InvalidNodeCmdErr = errors.New("invalid node command") | ||
|
||
NodeCmds = []string{ | ||
"unknown", | ||
"rmold", | ||
"sync", | ||
} | ||
) | ||
|
||
type NodeCmd int | ||
|
||
func (cmd NodeCmd) String() string { | ||
if NodeCmdMax <= cmd || cmd <= NodeCmdUnknown { | ||
return "unknown" | ||
} | ||
return NodeCmds[cmd] | ||
} | ||
|
||
func ToNodeCmd(cmd string) (NodeCmd, error) { | ||
for nc := NodeCmdUnknown + 1; nc < NodeCmdMax; nc++ { | ||
if cmd == NodeCmds[nc] { | ||
return nc, nil | ||
} | ||
} | ||
return NodeCmdUnknown, InvalidNodeCmdErr | ||
} | ||
|
||
type CsctlCmd struct { | ||
// the command send to node | ||
Cmd NodeCmd | ||
// the node ids that needs to execute the command, empty means all node | ||
Include []string | ||
// the node ids that doesn't need to execute the command, empty means none | ||
Exclude []string | ||
} | ||
|
||
// 执行 csctl 发送的命令 | ||
// 注册到 /cronsun/csctl/<cmd> | ||
func PutCsctl(cmd *CsctlCmd) error { | ||
if NodeCmdMax <= cmd.Cmd || cmd.Cmd <= NodeCmdUnknown { | ||
return InvalidNodeCmdErr | ||
} | ||
|
||
params, err := json.Marshal(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = DefalutClient.Put(conf.Config.Csctl+NodeCmds[cmd.Cmd], string(params)) | ||
return err | ||
} | ||
|
||
func WatchCsctl() client.WatchChan { | ||
return DefalutClient.Watch(conf.Config.Csctl, client.WithPrefix()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package node | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/shunfei/cronsun" | ||
"github.com/shunfei/cronsun/log" | ||
) | ||
|
||
func (n *Node) executCsctlCmd(key, value []byte) error { | ||
cmd := &cronsun.CsctlCmd{} | ||
err := json.Unmarshal(value, cmd) | ||
if err != nil { | ||
log.Warnf("invalid csctl command[%s] value[%s], err: %s", string(key), string(value), err.Error()) | ||
return err | ||
} | ||
|
||
if cronsun.NodeCmdMax <= cmd.Cmd || cmd.Cmd <= cronsun.NodeCmdUnknown { | ||
log.Warnf("invalid csctl command[%s] value[%s], err: %s", string(key), string(value)) | ||
return cronsun.InvalidNodeCmdErr | ||
} | ||
|
||
switch cmd.Cmd { | ||
case cronsun.NodeCmdRmOld: | ||
n.Node.RmOldInfo() | ||
case cronsun.NodeCmdSync: | ||
n.Node.SyncToMgo() | ||
} | ||
|
||
log.Infof("%s execute csctl command[%s] success", n.String(), cmd.Cmd.String()) | ||
return nil | ||
} |
Oops, something went wrong.