Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var BackupCmd = &cobra.Command{ Use: "backup", Short: "backup job & group data", Run: func(cmd *cobra.Command, args []string) { var err error var ea = NewExitAction() backupDir = strings.TrimSpace(backupDir) if len(backupDir) > 0 { err = os.MkdirAll(backupDir, 0755) if err != nil { ea.Exit("failed to make directory %s, err: %s", backupDir, err) } } backupFile = strings.TrimSpace(backupFile) if len(backupFile) == 0 { backupFile = time.Now().Format("20060102_150405") } backupFile += ".zip" name := path.Join(backupDir, backupFile) f, err := os.OpenFile(name, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0600) ea.ExitOnErr(err) ea.Defer = func() { f.Close() if err != nil { os.Remove(name) } } var waitForStore = [][]string{ []string{"job", conf.Config.Cmd}, []string{"node_group", conf.Config.Group}, } zw := zip.NewWriter(f) for i := range waitForStore { zf, err := zw.Create(waitForStore[i][0]) ea.ExitOnErr(err) storeKvs(zf, waitForStore[i][1]) } ea.ExitOnErr(zw.Close()) }, }
View Source
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) { var nodeInclude []string if len(importNodes) > 0 { nodeInclude = strings.Split(importNodes, spliter) } ea := NewExitAction() crons, err := loadCrons() if err != nil { ea.Exit("load crontab failed,err:%s", err.Error()) } total := len(crons) var successCount int ea.After = func() { fmt.Printf("total:%d,success:%d,failed:%d\n", total, successCount, total-successCount) if err := cmd.Help(); err != nil { return } } 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,succes:%d\n", successCount) }, }
View Source
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") } } }, }
View Source
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) }, }
View Source
var RestoreCmd = &cobra.Command{ Use: "restore", Short: "restore job & group data", Run: func(cmd *cobra.Command, args []string) { var err error var ea = NewExitAction() restoreFile = strings.TrimSpace(restoreFile) if len(restoreFile) == 0 { ea.Exit("backup file is required") } r, err := zip.OpenReader(restoreFile) ea.ExitOnErr(err) ea.Defer = func() { r.Close() } restoreChan, wg := startRestoreProcess() for _, f := range r.File { var keyPrefix string switch f.Name { case "job": keyPrefix = conf.Config.Cmd case "node_group": keyPrefix = conf.Config.Group } rc, err := f.Open() ea.ExitOnErr(err) ea.ExitOnErr(restoreKvs(rc, keyPrefix, restoreChan, wg)) rc.Close() } wg.Wait() close(restoreChan) }, }
View Source
var UpgradeCmd = &cobra.Command{ Use: "upgrade", Short: "upgrade will upgrade data to the current version(" + cronsun.VersionNumber + ")", Run: func(cmd *cobra.Command, args []string) { var ea = NewExitAction() prever = strings.TrimLeft(strings.TrimSpace(prever), "v") if len(prever) < 5 { ea.Exit("invalid version number") } nodesById := getIPMapper(ea, prever) if prever < "0.3.0" { fmt.Println("upgrading data to version 0.3.0") if to_0_3_0(ea, nodesById) { return } } if prever < "0.3.1" { fmt.Println("upgrading data to version 0.3.1") if to_0_3_1(ea, nodesById) { return } } }, }
Functions ¶
This section is empty.
Types ¶
type ExitAction ¶
type ExitAction struct { Defer func() After func() }
func NewExitAction ¶
func NewExitAction() *ExitAction
func (*ExitAction) Exit ¶
func (ea *ExitAction) Exit(format string, v ...interface{})
func (*ExitAction) ExitOnErr ¶
func (ea *ExitAction) ExitOnErr(err error)
Click to show internal directories.
Click to hide internal directories.