Ejemplo n.º 1
0
// setup initial things, spawn server if needed, any prereqs
func phase1Setup(ctx context.Context, server, spawnClientserver, clientserver bool, dspath string, home, serverhash string, port int) (*core.IpfsNode, string, int, string, bool) {

	var n *core.IpfsNode
	var err error

	// grab or update configs
	filepath := fmt.Sprintf("%s%s.ipbohrc", home, string(os.PathSeparator))
	serverhash, port = getUpdateConfig(filepath, serverhash, port)
	csBaseUrl := fmt.Sprintf("http://localhost:%d", port)

	if server {
		n, err = startupIPFS(dspath, &ctx)
		if err != nil {
			panic(err)
		}

		for _, addr := range n.PeerHost.Addrs() {
			fmt.Printf("Swarm listening on %s\n", addr.String())
		}
	} else if clientserver {
		n, err = startupIPFS(dspath, &ctx)
		if err != nil {
			panic(err)
		}
	} else {
		resp, err := http.Get(fmt.Sprintf("%s/areuthere", csBaseUrl))
		if err != nil {
			spawnClientserver = true
		} else if resp.StatusCode == 200 {
			spawnClientserver = false
		} else {
			spawnClientserver = true
		}

	}

	// spawn a separate process to launch client server if it is not already running
	if spawnClientserver {
		var exePath string
		exePath, err := godaemon.GetExecutablePath()
		if err != nil {
			err = fmt.Errorf("failed to get pid: %v", err)
		}

		files := make([]*os.File, 3, 3)
		files[0], files[1], files[2] = os.Stdin, os.Stdout, os.Stderr
		attrs := os.ProcAttr{Dir: ".", Env: os.Environ(), Files: files}
		_, err = os.StartProcess(exePath, []string{exePath, "-c", "-p", fmt.Sprintf("%d", port)}, &attrs)
		if err != nil {
			panic(err)
		}
	}

	return n, serverhash, port, csBaseUrl, spawnClientserver
}
Ejemplo n.º 2
0
// Reach tries to Dial() the daemon, if not there it Launch()'es one.
func Reach(pwd, repoPath string, port int) (*Client, error) {
	// Try to Dial directly first:
	if client, err := Dial(port); err == nil {
		return client, nil
	}

	// Probably not running, find out our own binary:
	exePath, err := godaemon.GetExecutablePath()
	if err != nil {
		return nil, err
	}

	// Start a new daemon process:
	log.Info("Starting daemon: ", exePath)

	// TODO: pass real log location:
	proc := exec.Command(exePath, "-l", "/tmp/brig.log", "-x", pwd, "daemon", "launch")

	if err := proc.Start(); err != nil {
		log.Infof("Failed to start the daemon: %v", err)
		return nil, err
	}

	// Make sure it it's still referenced:
	go func() {
		pid := 0
		if proc.Process != nil {
			pid = proc.Process.Pid
		}

		log.Info("Daemon has PID: ", pid)

		if err := proc.Wait(); err != nil {
			log.Warning("Bad exit state: ", err)
		}
	}()

	// Wait at max 15 seconds for the daemon to start up:
	// (this means, wait till it's network interface is started)
	for i := 0; i < 15; i++ {
		client, err := Dial(port)
		fmt.Println("Try dial", client)
		if err != nil {
			time.Sleep(1 * time.Second)
			continue
		}

		return client, nil
	}

	return nil, fmt.Errorf("Daemon could not be started or took to long.")
}