导入任务,并增加ls命令,可以列出当前node

pull/113/head
xyb 6 years ago
parent 9bb382dfcf
commit 3a2813c1c7

@ -26,7 +26,7 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.PersistentFlags().StringVarP(&confFile, "conf", "c", "conf/files/base.json", "base.json file path.")
rootCmd.AddCommand(subcmd.BackupCmd, subcmd.RestoreCmd, subcmd.UpgradeCmd, subcmd.NodeCmd)
rootCmd.AddCommand(subcmd.BackupCmd, subcmd.RestoreCmd, subcmd.UpgradeCmd, subcmd.NodeCmd, subcmd.ImportCmd, subcmd.LsCmd)
}
func main() {

@ -0,0 +1,123 @@
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"strings"
"time"
"math/rand"
"github.com/shunfei/cronsun"
"github.com/spf13/cobra"
)
type cron struct {
timer string
cmd string
}
var (
importNodes string
)
func init() {
ImportCmd.Flags().StringVar(&importNodes, "nodes", "", `the node ids that needs to run these imported job,
split by ',', e.g: '--nodes=aa,bb,cc', empty means no node will run`)
}
var ImportCmd = &cobra.Command{
Use: "import",
Short: `it will load the job from the crontab, but you must to confirm you can execute 'crontab -l'`,
Run: func(cmd *cobra.Command, args []string) {
ea := NewExitAction()
var nodeInclude []string
if len(importNodes) > 0 {
nodeInclude = strings.Split(importNodes, spliter)
}
crons := loadCrons()
total := len(crons)
var successCount int
ea.After = func() {
fmt.Printf("total:%d,success:%d,failed:%d\n", total, successCount, total-successCount)
cmd.Help()
}
rand.Seed(time.Now().Unix())
for _, cron := range crons {
job := cronsun.Job{}
job.ID = cronsun.NextID()
job.Command = cron.cmd
jr := &cronsun.JobRule{
Timer: "* " + cron.timer,
}
jr.NodeIDs = nodeInclude
job.Name = fmt.Sprintf("crontab-%d", rand.Intn(1000))
job.Group = "crontab"
job.Rules = append(job.Rules, jr)
// 默认先暂停
job.Pause = true
if err := job.Check(); err != nil {
ea.Exit("job check error:%s", err.Error())
}
b, err := json.Marshal(job)
if err != nil {
ea.Exit("json marshal error:%s", err.Error())
}
_, err = cronsun.DefalutClient.Put(job.Key(), string(b))
if err != nil {
ea.Exit("etcd put error:%s", err.Error())
}
successCount++
fmt.Printf("crontab-%s %s has import to the cronsun, the job id is:%s\n", cron.timer, cron.cmd, job.ID)
}
fmt.Printf("import fininsh,success:%d\n", successCount)
},
}
func loadCrons() []cron {
var crons []cron
cmd := exec.Command("crontab", "-l")
var b bytes.Buffer
cmd.Stdout = &b
cmd.Stderr = &b
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
result := strings.Split(b.String(), "\n")
for _, item := range result {
item = strings.TrimSpace(item)
if item != "" && !strings.HasPrefix(item, "#") {
spec := strings.Split(item, " ")
timer := strings.Join(spec[:5], " ")
cmd := strings.Join(spec[5:], " ")
crons = append(crons, cron{timer, cmd})
}
}
return crons
}

@ -0,0 +1,57 @@
package cmd
import (
"fmt"
"github.com/shunfei/cronsun"
"github.com/spf13/cobra"
)
var all bool
func init() {
LsCmd.Flags().BoolVarP(&all, "all", "a", false, "list all nodes include not alive")
}
var LsCmd = &cobra.Command{
Use: "ls",
Short: "list the nodes",
Run: func(cmd *cobra.Command, args []string) {
ea := NewExitAction()
ea.After = func() {
fmt.Println()
cmd.Help()
}
nodes, err := cronsun.GetNodes()
if err != nil {
ea.Exit(err.Error())
}
fmt.Print("ID")
for i := 0; i < 5; i++ {
fmt.Print("\t")
}
fmt.Print("ip\t\t\t")
fmt.Print("pid\t\t")
fmt.Print("hostname\t")
fmt.Print("alived\t")
fmt.Println()
for _, item := range nodes {
if !all && !item.Alived {
continue
}
fmt.Print(item.ID + "\t")
fmt.Print(item.IP + "\t\t")
fmt.Print(item.PID + "\t\t")
fmt.Print(item.Hostname + "\t\t")
if item.Alived {
fmt.Print("Yes" + "\t\t")
} else {
fmt.Print("No" + "\t\t")
}
}
},
}

@ -308,15 +308,15 @@ func AssetNames() []string {
// _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() (*asset, error){
"build.js": buildJs,
"build.js": buildJs,
"build.js.map": buildJsMap,
"flags.png": flagsPng,
"icons.eot": iconsEot,
"icons.svg": iconsSvg,
"icons.ttf": iconsTtf,
"icons.woff": iconsWoff,
"icons.woff2": iconsWoff2,
"index.html": indexHtml,
"flags.png": flagsPng,
"icons.eot": iconsEot,
"icons.svg": iconsSvg,
"icons.ttf": iconsTtf,
"icons.woff": iconsWoff,
"icons.woff2": iconsWoff2,
"index.html": indexHtml,
}
// AssetDir returns the file names below a certain
@ -358,16 +358,17 @@ type bintree struct {
Func func() (*asset, error)
Children map[string]*bintree
}
var _bintree = &bintree{nil, map[string]*bintree{
"build.js": &bintree{buildJs, map[string]*bintree{}},
"build.js": &bintree{buildJs, map[string]*bintree{}},
"build.js.map": &bintree{buildJsMap, map[string]*bintree{}},
"flags.png": &bintree{flagsPng, map[string]*bintree{}},
"icons.eot": &bintree{iconsEot, map[string]*bintree{}},
"icons.svg": &bintree{iconsSvg, map[string]*bintree{}},
"icons.ttf": &bintree{iconsTtf, map[string]*bintree{}},
"icons.woff": &bintree{iconsWoff, map[string]*bintree{}},
"icons.woff2": &bintree{iconsWoff2, map[string]*bintree{}},
"index.html": &bintree{indexHtml, map[string]*bintree{}},
"flags.png": &bintree{flagsPng, map[string]*bintree{}},
"icons.eot": &bintree{iconsEot, map[string]*bintree{}},
"icons.svg": &bintree{iconsSvg, map[string]*bintree{}},
"icons.ttf": &bintree{iconsTtf, map[string]*bintree{}},
"icons.woff": &bintree{iconsWoff, map[string]*bintree{}},
"icons.woff2": &bintree{iconsWoff2, map[string]*bintree{}},
"index.html": &bintree{indexHtml, map[string]*bintree{}},
}}
// RestoreAsset restores an asset under the given directory
@ -416,4 +417,3 @@ func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
}

Loading…
Cancel
Save