daemon_client.go 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
package daemon

import (
	"bufio"
	"encoding/json"
	"errors"
	"io"
	"os"

	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"

	u "github.com/jbenet/go-ipfs/util"
)

// ErrDaemonNotRunning is returned when attempting to retrieve the daemon's
// address and the daemon is not actually running.
var ErrDaemonNotRunning = errors.New("daemon not running")

func getDaemonAddr(confdir string) (string, error) {
	var err error
	confdir, err = u.TildeExpansion(confdir)
	if err != nil {
		return "", err
	}
	fi, err := os.Open(confdir + "/rpcaddress")
	if err != nil {
		log.Debug("getDaemonAddr failed: %s", err)
		if err == os.ErrNotExist {
			return "", ErrDaemonNotRunning
		}
		return "", err
	}

	read := bufio.NewReader(fi)

	// TODO: operating system agostic line delim
	line, err := read.ReadBytes('\n')
	if err != nil && err != io.EOF {
		return "", err
	}
	return string(line), nil
}

// SendCommand attempts to run the command over a currently-running daemon.
// If there is no running daemon, returns ErrDaemonNotRunning. This is done
// over network RPC API. The address of the daemon is retrieved from the config
// directory, where live daemons write their addresses to special files.
func SendCommand(command *Command, confdir string) error {
	server := os.Getenv("IPFS_ADDRESS_RPC")

	if server == "" {
		//check if daemon is running
		log.Info("Checking if daemon is running...")
		if !serverIsRunning(confdir) {
			return ErrDaemonNotRunning
		}

		log.Info("Daemon is running!")

		var err error
		server, err = getDaemonAddr(confdir)
		if err != nil {
			return err
		}
	}

	return serverComm(server, command)
}

func serverIsRunning(confdir string) bool {
	var err error
	confdir, err = u.TildeExpansion(confdir)
	if err != nil {
		log.Errorf("Tilde Expansion Failed: %s", err)
		return false
	}
	lk, err := daemonLock(confdir)
	if err == nil {
		lk.Close()
		return false
	}
	return true
}

func serverComm(server string, command *Command) error {
	log.Info("Daemon address: %s", server)
	maddr, err := ma.NewMultiaddr(server)
	if err != nil {
		return err
	}

	conn, err := manet.Dial(maddr)
	if err != nil {
		return err
	}

	enc := json.NewEncoder(conn)
	err = enc.Encode(command)
	if err != nil {
		return err
	}

	io.Copy(os.Stdout, conn)

	return nil
}