main.go 11.6 KB
Newer Older
1 2 3
package main

import (
4
	"errors"
5 6 7
	"fmt"
	"io"
	"os"
Matt Bell's avatar
Matt Bell committed
8
	"os/signal"
9
	"runtime/pprof"
10
	"syscall"
11

12
	// TODO rm direct reference to go-logging
13
	logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
14 15 16
	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"

17 18 19
	cmds "github.com/jbenet/go-ipfs/commands"
	cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
	cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21
	config "github.com/jbenet/go-ipfs/config"
	core "github.com/jbenet/go-ipfs/core"
22
	daemon "github.com/jbenet/go-ipfs/daemon2"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23
	updates "github.com/jbenet/go-ipfs/updates"
24
	u "github.com/jbenet/go-ipfs/util"
25
	"github.com/jbenet/go-ipfs/util/debugerror"
26
	elog "github.com/jbenet/go-ipfs/util/elog"
27 28 29
)

// log is the command logger
30
var log = elog.Logger("cmd/ipfs")
31

32 33 34
// signal to output help
var errHelpRequested = errors.New("Help Requested")

35
const (
36 37
	cpuProfile  = "ipfs.cpuprof"
	heapProfile = "ipfs.memprof"
38 39
	errorFormat = "ERROR: %v\n\n"
)
40

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42 43 44 45 46 47 48 49 50 51 52
type cmdInvocation struct {
	path []string
	cmd  *cmds.Command
	req  cmds.Request
}

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

	// this is a local helper to print out help text.
	// there's some considerations that this makes easier.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
	printHelp := func(long bool, w io.Writer) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66 67 68 69 70
		helpFunc := cmdsCli.ShortHelp
		if long {
			helpFunc = cmdsCli.LongHelp
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71
		helpFunc("ipfs", Root, invoc.path, w)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72 73 74
	}

	// parse the commandline into a command invocation
75
	parseErr := invoc.Parse(os.Args[1:])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76 77 78

	// BEFORE handling the parse error, if we have enough information
	// AND the user requested help, print it out and exit
79
	if invoc.req != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82 83 84 85
		longH, shortH, err := invoc.requestedHelp()
		if err != nil {
			printErr(err)
			os.Exit(1)
		}
		if longH || shortH {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86
			printHelp(longH, os.Stdout)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87 88 89 90
			os.Exit(0)
		}
	}

91 92 93 94
	// here we handle the cases where
	// - commands with no Run func are invoked directly.
	// - the main command is invoked.
	if invoc.cmd == nil || invoc.cmd.Run == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95
		printHelp(false, os.Stdout)
96 97 98 99
		os.Exit(0)
	}

	// ok now handle parse error (which means cli input was wrong,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100
	// e.g. incorrect number of args, or nonexistent subcommand)
101 102
	if parseErr != nil {
		printErr(parseErr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103 104 105 106 107

		// this was a user error, print help.
		if invoc.cmd != nil {
			// we need a newline space.
			fmt.Fprintf(os.Stderr, "\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108
			printHelp(false, os.Stderr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
109
		}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
110 111
		os.Exit(1)
	}
Matt Bell's avatar
Matt Bell committed
112

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113 114
	// ok, finally, run the command invocation.
	output, err := invoc.Run()
115
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116 117 118 119
		printErr(err)

		// if this error was a client error, print short help too.
		if isClientError(err) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
120
			printHelp(false, os.Stderr)
121
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122
		os.Exit(1)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
123 124
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
125 126 127 128 129
	// everything went better than expected :)
	io.Copy(os.Stdout, output)
}

func (i *cmdInvocation) Run() (output io.Reader, err error) {
130 131
	// setup our global interrupt handler.
	i.setupInterruptHandler()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132 133 134

	// check if user wants to debug. option OR env var.
	debug, _, err := i.req.Option("debug").Bool()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
135
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
136
		return nil, err
Brian Tiger Chow's avatar
Brian Tiger Chow committed
137
	}
138
	if debug || u.GetenvBool("DEBUG") || os.Getenv("IPFS_LOGGING") == "debug" {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
139 140
		u.Debug = true
		u.SetAllLoggers(logging.DEBUG)
141
	}
142

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
143 144
	// if debugging, let's profile.
	// TODO maybe change this to its own option... profiling makes it slower.
145
	if u.Debug {
146
		stopProfilingFunc, err := startProfiling()
147
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
148
			return nil, err
149
		}
150
		defer stopProfilingFunc() // to be executed as late as possible
151
	}
152

Matt Bell's avatar
Matt Bell committed
153
	res, err := callCommand(i.req, Root)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
154
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
155
		return nil, err
156
	}
157

158 159 160 161
	if err := res.Error(); err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
162
	return res.Reader()
163
}
164

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165 166
func (i *cmdInvocation) Parse(args []string) error {
	var err error
167

168
	i.req, i.cmd, i.path, err = cmdsCli.Parse(args, os.Stdin, Root)
169
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
170
		return err
171 172
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
173
	configPath, err := getConfigRoot(i.req)
174
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
175
		return err
176
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
177
	log.Debugf("config path is %s", configPath)
178

179
	// this sets up the function that will initialize the config lazily.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
180
	ctx := i.req.Context()
181
	ctx.ConfigRoot = configPath
182
	ctx.LoadConfig = loadConfig
183

184 185
	// if no encoding was specified by user, default to plaintext encoding
	// (if command doesn't support plaintext, use JSON instead)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
186
	if !i.req.Option("encoding").Found() {
187
		if i.req.Command().Marshalers != nil && i.req.Command().Marshalers[cmds.Text] != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
188
			i.req.SetOption("encoding", cmds.Text)
189
		} else {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
190
			i.req.SetOption("encoding", cmds.JSON)
191 192 193
		}
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
194
	return nil
195 196
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
197 198
func (i *cmdInvocation) requestedHelp() (short bool, long bool, err error) {
	longHelp, _, err := i.req.Option("help").Bool()
199
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
200
		return false, false, err
201
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
202
	shortHelp, _, err := i.req.Option("h").Bool()
203
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
204
		return false, false, err
205
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
206
	return longHelp, shortHelp, nil
207
}
208

209 210 211 212 213 214 215 216 217 218 219 220 221
func callPreCommandHooks(details cmdDetails, req cmds.Request, root *cmds.Command) error {

	log.Debug("Calling pre-command hooks...")

	// some hooks only run when the command is executed locally
	daemon, err := commandShouldRunOnDaemon(details, req, root)
	if err != nil {
		return err
	}

	// check for updates when 1) commands is going to be run locally, 2) the
	// command does not initialize the config, and 3) the command does not
	// pre-empt updates
222
	if !daemon && details.usesConfigAsInput() && !details.preemptsAutoUpdate {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

		log.Debug("Calling hook: Check for updates")

		cfg, err := req.Context().GetConfig()
		if err != nil {
			return err
		}
		// Check for updates and potentially install one.
		if err := updates.CliCheckForUpdates(cfg, req.Context().ConfigRoot); err != nil {
			return err
		}
	}

	return nil
}

239
func callCommand(req cmds.Request, root *cmds.Command) (cmds.Response, error) {
240
	var res cmds.Response
241

242 243 244 245 246 247
	details, err := commandDetails(req.Path(), root)
	if err != nil {
		return nil, err
	}

	useDaemon, err := commandShouldRunOnDaemon(*details, req, root)
248 249 250
	if err != nil {
		return nil, err
	}
251

252
	err = callPreCommandHooks(*details, req, root)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
253 254 255
	if err != nil {
		return nil, err
	}
256

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
257
	if useDaemon {
258

259 260 261 262 263
		cfg, err := req.Context().GetConfig()
		if err != nil {
			return nil, err
		}

264 265 266 267
		addr, err := ma.NewMultiaddr(cfg.Addresses.API)
		if err != nil {
			return nil, err
		}
268

269
		log.Infof("Executing command on daemon running at %s", addr)
270 271 272 273
		_, host, err := manet.DialArgs(addr)
		if err != nil {
			return nil, err
		}
274

275
		client := cmdsHttp.NewClient(host)
276

277 278 279 280
		res, err = client.Send(req)
		if err != nil {
			return nil, err
		}
281

282
	} else {
283
		log.Info("Executing command locally")
284

285 286 287
		// this sets up the function that will initialize the node
		// this is so that we can construct the node lazily.
		ctx := req.Context()
288

289 290
		ctx.ConstructNode = func() (*core.IpfsNode, error) {
			cfg, err := ctx.GetConfig()
291
			if err != nil {
292
				return nil, err
293
			}
294 295 296 297 298
			return core.NewIpfsNode(cfg, false)
		}

		// Okay!!!!! NOW we can call the command.
		res = root.Call(req)
299

300 301 302 303 304
		// let's not forget teardown. If a node was initialized, we must close it.
		// Note that this means the underlying req.Context().Node variable is exposed.
		// this is gross, and should be changed when we extract out the exec Context.
		node := req.Context().NodeWithoutConstructing()
		if node != nil {
305
			log.Info("Shutting down node...")
306
			node.Close()
307 308
		}
	}
309
	return res, nil
310 311
}

312 313 314 315 316
// commandDetails returns a command's details for the command given by |path|
// within the |root| command tree.
//
// Returns an error if the command is not found in the Command tree.
func commandDetails(path []string, root *cmds.Command) (*cmdDetails, error) {
317 318 319 320 321 322 323
	var details cmdDetails
	// find the last command in path that has a cmdDetailsMap entry
	cmd := root
	for _, cmp := range path {
		var found bool
		cmd, found = cmd.Subcommands[cmp]
		if !found {
324
			return nil, debugerror.Errorf("subcommand %s should be in root", cmp)
325
		}
326

327 328 329
		if cmdDetails, found := cmdDetailsMap[cmd]; found {
			details = cmdDetails
		}
330
	}
331
	log.Debugf("cmd perms for +%v: %s", path, details.String())
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	return &details, nil
}

// commandShouldRunOnDaemon determines, from commmand details, whether a
// command ought to be executed on an IPFS daemon.
//
// It returns true if the command should be executed on a daemon and false if
// it should be executed on a client. It returns an error if the command must
// NOT be executed on either.
func commandShouldRunOnDaemon(details cmdDetails, req cmds.Request, root *cmds.Command) (bool, error) {
	path := req.Path()
	// root command.
	if len(path) < 1 {
		return false, nil
	}
347 348 349 350 351

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

352
	if details.doesNotUseRepo && details.canRunOnClient() {
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
		return false, nil
	}

	// at this point need to know whether daemon is running. we defer
	// to this point so that some commands dont open files unnecessarily.
	daemonLocked := daemon.Locked(req.Context().ConfigRoot)
	log.Info("Daemon is running.")

	if daemonLocked {

		if details.cannotRunOnDaemon {
			e := "ipfs daemon is running. please stop it to run this command"
			return false, cmds.ClientError(e)
		}

		return true, nil
	}

	if details.cannotRunOnClient {
		return false, cmds.ClientError("must run on the ipfs daemon")
	}

	return false, nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
378
func isClientError(err error) bool {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
379 380 381 382

	// Somewhat suprisingly, the pointer cast fails to recognize commands.Error
	// passed as values, so we check both.

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
383
	// cast to cmds.Error
Brian Tiger Chow's avatar
Brian Tiger Chow committed
384 385 386 387 388
	switch e := err.(type) {
	case *cmds.Error:
		return e.Code == cmds.ErrClient
	case cmds.Error:
		return e.Code == cmds.ErrClient
389
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
390
	return false
391 392 393
}

func getConfigRoot(req cmds.Request) (string, error) {
394
	configOpt, found, err := req.Option("config").String()
395 396 397
	if err != nil {
		return "", err
	}
398
	if found && configOpt != "" {
399
		return configOpt, nil
400 401 402 403 404 405 406 407 408
	}

	configPath, err := config.PathRoot()
	if err != nil {
		return "", err
	}
	return configPath, nil
}

409
func loadConfig(path string) (*config.Config, error) {
410 411 412 413 414 415 416
	configFile, err := config.Filename(path)
	if err != nil {
		return nil, err
	}

	return config.Load(configFile)
}
417

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
// 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)

	stopProfiling := func() {
		pprof.StopCPUProfile()
		defer ofi.Close() // captured by the closure
		err := writeHeapProfileToFile()
		if err != nil {
			log.Critical(err)
		}
	}
	return stopProfiling, nil
}

440 441 442
func writeHeapProfileToFile() error {
	mprof, err := os.Create(heapProfile)
	if err != nil {
443
		return err
444
	}
445
	defer mprof.Close() // _after_ writing the heap profile
446 447
	return pprof.WriteHeapProfile(mprof)
}
448

Matt Bell's avatar
Matt Bell committed
449
// listen for and handle SIGTERM
450 451 452 453
func (i *cmdInvocation) setupInterruptHandler() {

	ctx := i.req.Context()
	sig := allInterruptSignals()
Matt Bell's avatar
Matt Bell committed
454 455

	go func() {
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

		for {
			// first time, try to shut down.
			<-sig
			log.Critical("Received interrupt signal, shutting down...")

			n, err := ctx.GetNode()
			if err == nil {
				go n.Close()
				select {
				case <-n.Closed():
				case <-sig:
					log.Critical("Received another interrupt signal, terminating...")
				}
			}

472
			os.Exit(0)
Matt Bell's avatar
Matt Bell committed
473
		}
474

Matt Bell's avatar
Matt Bell committed
475 476
	}()
}
477 478 479 480 481 482 483

func allInterruptSignals() chan os.Signal {
	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT,
		syscall.SIGTERM, syscall.SIGQUIT)
	return sigc
}