daemon_client.go 2.35 KB
Newer Older
1 2 3
package daemon

import (
4
	"bufio"
5
	"encoding/json"
6
	"errors"
7 8
	"io"
	"os"
9

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
11
	manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
12

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

16 17 18
// 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")
Siraj Ravel's avatar
Siraj Ravel committed
19

20 21 22 23 24 25 26 27
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 {
28
		log.Debugf("getDaemonAddr failed: %s", err)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
		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
}

45 46 47 48
// 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.
49
func SendCommand(command *Command, confdir string) error {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	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 {
72 73 74
	var err error
	confdir, err = u.TildeExpansion(confdir)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75
		log.Errorf("Tilde Expansion Failed: %s", err)
76
		return false
77
	}
78
	lk, err := daemonLock(confdir)
79 80
	if err == nil {
		lk.Close()
81
		return false
82
	}
83 84
	return true
}
85

86
func serverComm(server string, command *Command) error {
87
	log.Info("Daemon address: %s", server)
88
	maddr, err := ma.NewMultiaddr(server)
89 90 91
	if err != nil {
		return err
	}
Siraj Ravel's avatar
Siraj Ravel committed
92

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93
	conn, err := manet.Dial(maddr)
94
	if err != nil {
95
		return err
96 97
	}

Siraj Ravel's avatar
Siraj Ravel committed
98 99
	enc := json.NewEncoder(conn)
	err = enc.Encode(command)
100
	if err != nil {
101
		return err
102 103
	}

Siraj Ravel's avatar
Siraj Ravel committed
104
	io.Copy(os.Stdout, conn)
105

106
	return nil
107
}