// Available tests if p2p config exists func (ap *AbstractP2P) Available() bool { ap.client = config.GetString("p2p.client") ap.mkseed = config.GetString("p2p.mkseed") if ap.client == "" || ap.mkseed == "" { return false } return true }
func init() { runtime.GOMAXPROCS(runtime.NumCPU()) app = cli.NewApp() app.Name = "gsck" app.Author = "*****@*****.**" app.Version = "2.1.0" app.Usage = "Execute commands on multiple machines over SSH (or other control system)" commands = make([]cli.Command, 0, 10) defaultUser = config.GetString("user") exec = executor.GetExecutor() cl = &cmdline{ pipe: PIPEEMTPY, fifo: PIPEEMTPY, } // Read Data From Pipe/HereDoc fi, _ := os.Stdin.Stat() if (fi.Mode() & (os.ModeCharDevice | os.ModeDir)) == 0 { bytes, _ := ioutil.ReadAll(os.Stdin) cl.pipe = string(bytes) } // Read Data From FIFO for i, arg := range os.Args { if i == 0 { continue } fii, err := os.Stat(arg) if err == nil && (fii.Mode()&os.ModeNamedPipe) != 0 { bytes, _ := ioutil.ReadFile(arg) cl.fifo = string(bytes) cl.fifoFile = arg } } }
// CONFIG Action (gsck config ...) func configAction(c *cli.Context) { if len(c.Args()) == 0 { cli.ShowCommandHelp(c, "config") os.Exit(1) } key := c.Args()[0] if len(c.Args()) == 1 { fmt.Println(config.GetString(key)) } else if len(c.Args()) == 2 { value := c.Args()[1] config.Set(key, value) } }
// CP Action (gsck cp ...) func scpAction(c *cli.Context) { PrepareExecutor(c) src := c.String("src") useScp := func() { exec.SetTransfer(src, c.String("dst")) exec.SetTransferHook(c.String("before"), c.String("after")) } useP2P := func() { p2pMgr := p2p.GetMgr() p2pMgr.SetTransfer(src, c.String("dst")) _ = p2pMgr.Mkseed() if p2pMgr.NeedTransferFile() { remoteTmp := config.GetString("remote.tmpdir") exec.SetTransfer(p2pMgr.TransferFilePath(), remoteTmp) } cmd := util.WrapCmd(p2pMgr.ClientCmd(), c.String("before"), c.String("after")) exec.SetCmd(cmd).SetConcurrency(-1) } if !p2p.Available() { useScp() } else if util.IsDir(src) { useP2P() } else { stat, err := os.Lstat(src) if err != nil { fmt.Println(err) os.Exit(2) } size := stat.Size() sizeInMB := size / 1024 / 1024 transferTotal := sizeInMB * int64(exec.HostCount()) // If transferTotal is less than 1 MB * 10 machine, use scp. if transferTotal < 10 { useScp() } else { useP2P() } } CheckExecutor() err, failed := exec.Run() if err != nil { fmt.Println(err) os.Exit(2) } os.Exit(failed) }
// ClientCmd is `p2p.client remote.tmpdir/path_to_src.torrent` func (ap *AbstractP2P) ClientCmd() string { torrentPath := path.Join(config.GetString("remote.tmpdir"), ap.TransferFilePath()) cmd := fmt.Sprintf("%s %s", ap.client, torrentPath) return cmd }
// TransferFilePath returns local.tmpdir/path_to_src.torrent func (ap *AbstractP2P) TransferFilePath() string { localTmp := config.GetString("local.tmpdir") seperator := string(os.PathSeparator) transSrcPath := strings.Replace(ap.src, seperator, "_", -1) return localTmp + seperator + transSrcPath + ".torrent" }