main.go 12.6 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
// cmd/ipfs implements the primary CLI binary for ipfs
2 3 4
package main

import (
5
	"context"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
6
	"errors"
7 8
	"fmt"
	"io"
9
	"math/rand"
10
	"os"
Matt Bell's avatar
Matt Bell committed
11
	"os/signal"
12
	"path/filepath"
13
	"runtime/pprof"
14
	"strings"
15
	"sync"
16
	"syscall"
17
	"time"
18

19
	oldcmds "github.com/ipfs/go-ipfs/commands"
20
	core "github.com/ipfs/go-ipfs/core"
Spartucus's avatar
Spartucus committed
21
	corecmds "github.com/ipfs/go-ipfs/core/commands"
keks's avatar
keks committed
22
	corehttp "github.com/ipfs/go-ipfs/core/corehttp"
keks's avatar
keks committed
23
	loader "github.com/ipfs/go-ipfs/plugin/loader"
24
	repo "github.com/ipfs/go-ipfs/repo"
25
	fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
26

Steven Allen's avatar
Steven Allen committed
27 28 29 30
	u "gx/ipfs/QmNohiVssaPw3KVLZik59DBVGTSm2dGvYT9eoXt5DQ36Yz/go-ipfs-util"
	manet "gx/ipfs/QmQVUtnrNGtCRkCMpXgpApfzQjc8FDaDVxHqWH8cnZQeh5/go-multiaddr-net"
	ma "gx/ipfs/QmRKLtwMw131aK7ugC3G7ybpumMz78YrJe5dzneyindvG1/go-multiaddr"
	madns "gx/ipfs/QmT4zgnKCyZBpRyxzsvZqUjzUkMWLJ2pZCw7uk6M6Kto5m/go-multiaddr-dns"
31
	osh "gx/ipfs/QmXuBJ7DR6k3rmUEKtvVMhwjmXDuJgXXPUt4LQXKBMsU93/go-os-helper"
Steven Allen's avatar
Steven Allen committed
32 33 34 35 36 37
	"gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
	"gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds/cli"
	"gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds/http"
	"gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config"
	loggables "gx/ipfs/QmbP5E6C5Ks2b43vTZoyjSUHx2PPgoe78PZpJSJqCJ7LYx/go-libp2p-loggables"
	logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
38 39
)

40
// log is the command logger
Jeromy's avatar
Jeromy committed
41
var log = logging.Logger("cmd/ipfs")
42

43
var errRequestCanceled = errors.New("request canceled")
44

45 46 47
// declared as a var for testing purposes
var dnsResolver = madns.DefaultResolver

48
const (
49 50 51
	EnvEnableProfiling = "IPFS_PROF"
	cpuProfile         = "ipfs.cpuprof"
	heapProfile        = "ipfs.memprof"
52
)
53

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54 55
// main roadmap:
// - parse the commandline to get a cmdInvocation
56
// - if user requests help, print it and exit.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58 59
// - run the command invocation
// - output the response
// - if anything fails, print error, maybe with help
60
func main() {
61 62 63 64
	os.Exit(mainRet())
}

func mainRet() int {
65
	rand.Seed(time.Now().UnixNano())
66
	ctx := logging.ContextWithLoggable(context.Background(), loggables.Uuid("session"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67 68 69 70 71 72 73 74
	var err error

	// we'll call this local helper to output errors.
	// this is so we control how to print errors in one place.
	printErr := func(err error) {
		fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
	}

75 76 77
	stopFunc, err := profileIfEnabled()
	if err != nil {
		printErr(err)
78
		return 1
79 80 81
	}
	defer stopFunc() // to be executed as late as possible

keks's avatar
keks committed
82 83
	intrh, ctx := setupInterruptHandler(ctx)
	defer intrh.Close()
84

Etienne Laurin's avatar
Etienne Laurin committed
85
	// Handle `ipfs help'
86 87
	if len(os.Args) == 2 {
		if os.Args[1] == "help" {
keks's avatar
keks committed
88
			os.Args[1] = "-h"
89 90 91
		} else if os.Args[1] == "--version" {
			os.Args[1] = "version"
		}
Etienne Laurin's avatar
Etienne Laurin committed
92 93
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
94
	// output depends on executable name passed in os.Args
95 96 97
	// so we need to make sure it's stable
	os.Args[0] = "ipfs"

Jeromy's avatar
Jeromy committed
98
	buildEnv := func(ctx context.Context, req *cmds.Request) (cmds.Environment, error) {
99
		checkDebug(req)
keks's avatar
keks committed
100
		repoPath, err := getRepoPath(req)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101
		if err != nil {
keks's avatar
keks committed
102
			return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103
		}
keks's avatar
keks committed
104
		log.Debugf("config path is %s", repoPath)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105

keks's avatar
keks committed
106 107 108 109 110 111 112 113 114 115
		// this sets up the function that will initialize the node
		// this is so that we can construct the node lazily.
		return &oldcmds.Context{
			ConfigRoot: repoPath,
			LoadConfig: loadConfig,
			ReqLog:     &oldcmds.ReqLog{},
			ConstructNode: func() (n *core.IpfsNode, err error) {
				if req == nil {
					return nil, errors.New("constructing node without a request")
				}
Etienne Laurin's avatar
Etienne Laurin committed
116

keks's avatar
keks committed
117 118 119 120
				r, err := fsrepo.Open(repoPath)
				if err != nil { // repo is owned by the node
					return nil, err
				}
Jan Winkelmann's avatar
Jan Winkelmann committed
121

keks's avatar
keks committed
122 123 124 125 126 127 128 129
				// ok everything is good. set it on the invocation (for ownership)
				// and return it.
				n, err = core.NewNode(ctx, &core.BuildCfg{
					Repo: r,
				})
				if err != nil {
					return nil, err
				}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130

keks's avatar
keks committed
131 132 133 134 135 136 137 138
				n.SetLocal(true)
				return n, nil
			},
		}, nil
	}

	err = cli.Run(ctx, Root, os.Args, os.Stdin, os.Stdout, os.Stderr, buildEnv, makeExecutor)
	if err != nil {
139
		return 1
Brian Tiger Chow's avatar
Brian Tiger Chow committed
140 141
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
142
	// everything went better than expected :)
143
	return 0
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
144 145
}

keks's avatar
keks committed
146
func checkDebug(req *cmds.Request) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
147
	// check if user wants to debug. option OR env var.
keks's avatar
keks committed
148
	debug, _ := req.Options["debug"].(bool)
149
	if debug || os.Getenv("IPFS_LOGGING") == "debug" {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
150
		u.Debug = true
Jeromy's avatar
Jeromy committed
151
		logging.SetDebugLogging()
152
	}
153 154 155
	if u.GetenvBool("DEBUG") {
		u.Debug = true
	}
156 157
}

keks's avatar
keks committed
158
func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) {
159
	details := commandDetails(req.Path)
Łukasz Magiera's avatar
Łukasz Magiera committed
160
	client, err := commandShouldRunOnDaemon(*details, req, env.(*oldcmds.Context))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161
	if err != nil {
keks's avatar
keks committed
162
		return nil, err
Jan Winkelmann's avatar
Jan Winkelmann committed
163 164
	}

keks's avatar
keks committed
165 166 167
	var exctr cmds.Executor
	if client != nil && !req.Command.External {
		exctr = client.(cmds.Executor)
168
	} else {
169 170 171 172 173 174 175 176
		cctx := env.(*oldcmds.Context)
		pluginpath := filepath.Join(cctx.ConfigRoot, "plugins")

		// check if repo is accessible before loading plugins
		ok, err := checkPermissions(cctx.ConfigRoot)
		if err != nil {
			return nil, err
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
177 178 179 180 181
		if !ok {
			pluginpath = ""
		}
		if _, err := loader.LoadPlugins(pluginpath); err != nil {
			log.Error("error loading plugins: ", err)
182 183
		}

keks's avatar
keks committed
184
		exctr = cmds.NewExecutor(req.Root)
185 186
	}

keks's avatar
keks committed
187
	return exctr, nil
188 189
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203
func checkPermissions(path string) (bool, error) {
	_, err := os.Open(path)
	if os.IsNotExist(err) {
		// repo does not exist yet - don't load plugins, but also don't fail
		return false, nil
	}
	if os.IsPermission(err) {
		// repo is not accessible. error out.
		return false, fmt.Errorf("error opening repository at %s: permission denied", path)
	}

	return true, nil
}

204 205
// commandDetails returns a command's details for the command given by |path|.
func commandDetails(path []string) *cmdDetails {
206 207
	var details cmdDetails
	// find the last command in path that has a cmdDetailsMap entry
208 209
	for i := range path {
		if cmdDetails, found := cmdDetailsMap[strings.Join(path[:i+1], "/")]; found {
210 211
			details = cmdDetails
		}
212
	}
213
	return &details
214 215
}

Łukasz Magiera's avatar
Łukasz Magiera committed
216
// commandShouldRunOnDaemon determines, from command details, whether a
217
// command ought to be executed on an ipfs daemon.
218
//
219
// It returns a client if the command should be executed on a daemon and nil if
220 221
// it should be executed on a client. It returns an error if the command must
// NOT be executed on either.
Łukasz Magiera's avatar
Łukasz Magiera committed
222
func commandShouldRunOnDaemon(details cmdDetails, req *cmds.Request, cctx *oldcmds.Context) (http.Client, error) {
223
	path := req.Path
224 225
	// root command.
	if len(path) < 1 {
226
		return nil, nil
227
	}
228 229

	if details.cannotRunOnClient && details.cannotRunOnDaemon {
230
		return nil, fmt.Errorf("command disabled: %s", path[0])
231 232
	}

233
	if details.doesNotUseRepo && details.canRunOnClient() {
234
		return nil, nil
235 236
	}

237
	// at this point need to know whether api is running. we defer
Łukasz Magiera's avatar
Łukasz Magiera committed
238
	// to this point so that we don't check unnecessarily
239 240

	// did user specify an api to use for this command?
Spartucus's avatar
Spartucus committed
241
	apiAddrStr, _ := req.Options[corecmds.ApiOption].(string)
242

243
	client, err := getAPIClient(req.Context, cctx.ConfigRoot, apiAddrStr)
244
	if err == repo.ErrApiNotRunning {
245
		if apiAddrStr != "" && req.Command != daemonCmd {
246 247 248 249
			// if user SPECIFIED an api, and this cmd is not daemon
			// we MUST use it. so error out.
			return nil, err
		}
250

251 252 253 254
		// ok for api not to be running
	} else if err != nil { // some other api error
		return nil, err
	}
255

michael's avatar
michael committed
256
	if client != nil {
257
		if details.cannotRunOnDaemon {
258
			// check if daemon locked. legacy error text, for now.
michael's avatar
michael committed
259
			log.Debugf("Command cannot run on daemon. Checking if daemon is locked")
260
			if daemonLocked, _ := fsrepo.LockedByOtherProcess(cctx.ConfigRoot); daemonLocked {
rht's avatar
rht committed
261
				return nil, cmds.ClientError("ipfs daemon is running. please stop it to run this command")
262
			}
rht's avatar
rht committed
263
			return nil, nil
264 265
		}

266
		return client, nil
267 268 269
	}

	if details.cannotRunOnClient {
270
		return nil, cmds.ClientError("must run on the ipfs daemon")
271 272
	}

273
	return nil, nil
274 275
}

276 277
func getRepoPath(req *cmds.Request) (string, error) {
	repoOpt, found := req.Options["config"].(string)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
278 279
	if found && repoOpt != "" {
		return repoOpt, nil
280 281
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
282
	repoPath, err := fsrepo.BestKnownPath()
283 284 285
	if err != nil {
		return "", err
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
286
	return repoPath, nil
287 288
}

289
func loadConfig(path string) (*config.Config, error) {
Brian Tiger Chow's avatar
huh  
Brian Tiger Chow committed
290
	return fsrepo.ConfigAt(path)
291
}
292

293 294 295 296 297 298 299 300 301
// startProfiling begins CPU profiling and returns a `stop` function to be
// executed as late as possible. The stop function captures the memprofile.
func startProfiling() (func(), error) {
	// start CPU profiling as early as possible
	ofi, err := os.Create(cpuProfile)
	if err != nil {
		return nil, err
	}
	pprof.StartCPUProfile(ofi)
302
	go func() {
303
		for range time.NewTicker(time.Second * 30).C {
304 305
			err := writeHeapProfileToFile()
			if err != nil {
rht's avatar
rht committed
306
				log.Error(err)
307 308 309
			}
		}
	}()
310 311 312

	stopProfiling := func() {
		pprof.StopCPUProfile()
313
		ofi.Close() // captured by the closure
314 315 316 317
	}
	return stopProfiling, nil
}

318 319 320
func writeHeapProfileToFile() error {
	mprof, err := os.Create(heapProfile)
	if err != nil {
321
		return err
322
	}
323
	defer mprof.Close() // _after_ writing the heap profile
324 325
	return pprof.WriteHeapProfile(mprof)
}
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
// IntrHandler helps set up an interrupt handler that can
// be cleanly shut down through the io.Closer interface.
type IntrHandler struct {
	sig chan os.Signal
	wg  sync.WaitGroup
}

func NewIntrHandler() *IntrHandler {
	ih := &IntrHandler{}
	ih.sig = make(chan os.Signal, 1)
	return ih
}

func (ih *IntrHandler) Close() error {
	close(ih.sig)
	ih.wg.Wait()
	return nil
}
345

346 347 348 349 350 351 352 353
// Handle starts handling the given signals, and will call the handler
// callback function each time a signal is catched. The function is passed
// the number of times the handler has been triggered in total, as
// well as the handler itself, so that the handling logic can use the
// handler's wait group to ensure clean shutdown when Close() is called.
func (ih *IntrHandler) Handle(handler func(count int, ih *IntrHandler), sigs ...os.Signal) {
	signal.Notify(ih.sig, sigs...)
	ih.wg.Add(1)
Matt Bell's avatar
Matt Bell committed
354
	go func() {
355 356
		defer ih.wg.Done()
		count := 0
357
		for range ih.sig {
358 359 360 361 362 363 364
			count++
			handler(count, ih)
		}
		signal.Stop(ih.sig)
	}()
}

keks's avatar
keks committed
365
func setupInterruptHandler(ctx context.Context) (io.Closer, context.Context) {
366
	intrh := NewIntrHandler()
367 368
	ctx, cancelFunc := context.WithCancel(ctx)

369 370 371
	handlerFunc := func(count int, ih *IntrHandler) {
		switch count {
		case 1:
372
			fmt.Println() // Prevent un-terminated ^C character in terminal
373 374 375 376

			ih.wg.Add(1)
			go func() {
				defer ih.wg.Done()
377
				cancelFunc()
378
			}()
379

380 381 382
		default:
			fmt.Println("Received another interrupt before graceful shutdown, terminating...")
			os.Exit(-1)
Matt Bell's avatar
Matt Bell committed
383
		}
384 385 386
	}

	intrh.Handle(handlerFunc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
387

388
	return intrh, ctx
389
}
390 391 392 393

func profileIfEnabled() (func(), error) {
	// FIXME this is a temporary hack so profiling of asynchronous operations
	// works as intended.
394
	if os.Getenv(EnvEnableProfiling) != "" {
395 396 397 398 399 400 401 402
		stopProfilingFunc, err := startProfiling() // TODO maybe change this to its own option... profiling makes it slower.
		if err != nil {
			return nil, err
		}
		return stopProfilingFunc, nil
	}
	return func() {}, nil
}
403

404 405 406 407
var apiFileErrorFmt string = `Failed to parse '%[1]s/api' file.
	error: %[2]s
If you're sure go-ipfs isn't running, you can just delete it.
`
408 409
var checkIPFSUnixFmt = "Otherwise check:\n\tps aux | grep ipfs"
var checkIPFSWinFmt = "Otherwise check:\n\ttasklist | findstr ipfs"
410

411
// getAPIClient checks the repo, and the given options, checking for
412 413
// a running API service. if there is one, it returns a client.
// otherwise, it returns errApiNotRunning, or another error.
414
func getAPIClient(ctx context.Context, repoPath, apiAddrStr string) (http.Client, error) {
415 416 417 418 419 420 421 422 423
	var apiErrorFmt string
	switch {
	case osh.IsUnix():
		apiErrorFmt = apiFileErrorFmt + checkIPFSUnixFmt
	case osh.IsWindows():
		apiErrorFmt = apiFileErrorFmt + checkIPFSWinFmt
	default:
		apiErrorFmt = apiFileErrorFmt
	}
424

425 426 427 428 429 430 431 432
	var addr ma.Multiaddr
	var err error
	if len(apiAddrStr) != 0 {
		addr, err = ma.NewMultiaddr(apiAddrStr)
		if err != nil {
			return nil, err
		}
		if len(addr.Protocols()) == 0 {
Lars Gierth's avatar
Lars Gierth committed
433
			return nil, fmt.Errorf("multiaddr doesn't provide any protocols")
434 435 436 437
		}
	} else {
		addr, err = fsrepo.APIAddr(repoPath)
		if err == repo.ErrApiNotRunning {
438 439 440
			return nil, err
		}

441 442 443 444 445 446
		if err != nil {
			return nil, fmt.Errorf(apiErrorFmt, repoPath, err.Error())
		}
	}
	if len(addr.Protocols()) == 0 {
		return nil, fmt.Errorf(apiErrorFmt, repoPath, "multiaddr doesn't provide any protocols")
447
	}
448
	return apiClientForAddr(ctx, addr)
449 450
}

451
func apiClientForAddr(ctx context.Context, addr ma.Multiaddr) (http.Client, error) {
452
	addr, err := resolveAddr(ctx, addr)
453 454 455 456
	if err != nil {
		return nil, err
	}

457 458 459 460
	_, host, err := manet.DialArgs(addr)
	if err != nil {
		return nil, err
	}
461

462 463
	return http.NewClient(host, http.ClientWithAPIPrefix(corehttp.APIPath)), nil
}
464

465 466 467 468 469 470 471 472
func resolveAddr(ctx context.Context, addr ma.Multiaddr) (ma.Multiaddr, error) {
	ctx, cancelFunc := context.WithTimeout(ctx, 10*time.Second)
	defer cancelFunc()

	addrs, err := dnsResolver.Resolve(ctx, addr)
	if err != nil {
		return nil, err
	}
473

474 475
	if len(addrs) == 0 {
		return nil, errors.New("non-resolvable API endpoint")
476 477
	}

478
	return addrs[0], nil
479
}