Exemple #1
0
func NewSyncContext() *SyncContext {
	ctx := &SyncContext{}
	ctx.ExecContext = bismuth.NewExecContext()
	AllSyncContexts = append(AllSyncContexts, ctx)
	return ctx
}
Exemple #2
0
func connectChild(remoteHost, remoteRoot string) {
	lg := alog.New(os.Stderr, "@(dim:{isodate} [remote]) ", 0)
	exePath, err := exec.LookPath(os.Args[0])
	// alog.Println("My path is", exePath)
	ctx := bismuth.NewExecContext()
	ctx.Connect()
	ctx.Quote("copy-mkdir", "ssh", remoteHost, "mkdir -p ~/.nsync")
	retCode, err := ctx.Quote("copy", "rsync", "-a", exePath, remoteHost+":~/.nsync/nsync")
	if retCode != 0 {
		alog.Println("copy retCode", retCode)
	}
	if err != nil {
		alog.Println(err)
		return
	}
	cmd := exec.Command("ssh", remoteHost, "sudo", "~/.nsync/nsync", "--child", remoteRoot)
	if Opts.Verbose {
		cmd.Args = append(cmd.Args, "--verbose")
	}
	if Opts.IgnorePart != "" {
		cmd.Args = append(cmd.Args, "--ignore-part", Opts.IgnorePart)
	}
	if Opts.IgnoreSuffix != "" {
		cmd.Args = append(cmd.Args, "--ignore-suffix", Opts.IgnoreSuffix)
	}
	if Opts.IgnoreSubstring != "" {
		cmd.Args = append(cmd.Args, "--ignore-substring", Opts.IgnoreSubstring)
	}
	if Opts.DeletePart != "" {
		cmd.Args = append(cmd.Args, "--delete-part", Opts.DeletePart)
	}
	if Opts.DeleteSuffix != "" {
		cmd.Args = append(cmd.Args, "--delete-suffix", Opts.DeleteSuffix)
	}
	if Opts.DeleteSubstring != "" {
		cmd.Args = append(cmd.Args, "--delete-substring", Opts.DeleteSubstring)
	}
	stdin, err := cmd.StdinPipe()
	if err != nil {
		lg.Printf("@(error:Error getting stdin pipe: %v)\n", err)
		return
	}
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		lg.Printf("@(error:Error getting stdout pipe: %v)\n", err)
		return
	}
	stderr, err := cmd.StderrPipe()
	if err != nil {
		lg.Printf("@(error:Error getting stderr pipe: %v)\n", err)
		return
	}
	if err := cmd.Start(); err != nil {
		lg.Printf("@(error:Error starting %s: %v)\n", cmd.Args[0], err)
		return
	}
	childSshProcessMutex.Lock()
	childSshProcess = cmd.Process
	childSshProcessMutex.Unlock()
	done := make(chan error)
	go sendMessages(stdin, MessagesToChild, done)
	go receiveMessages(stdout, MessagesToParent, done)
	go func() {
		_, err := io.Copy(lg, stderr)
		done <- err
	}()
	for i := 0; i < 2; i++ {
		err := <-done
		if err != nil && err != io.EOF {
			lg.Printf("@(error:Error reading subprocess output: %v)\n", err)

			return
		}
	}
	if err := cmd.Wait(); err != nil {
		lg.Printf("@(error:Error on subprocess exit: %s)\n", err)
	}
	childSshProcessMutex.Lock()
	childSshProcess = nil
	childSshProcessMutex.Unlock()
}