Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup daemon online logic #6050

Merged
merged 2 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
log.Error("error from node construction: ", err)
return err
}
node.SetLocal(false)
node.IsDaemon = true

if node.PNetFingerprint != nil {
fmt.Println("Swarm is limited to private network of peers with the swarm key")
Expand Down Expand Up @@ -517,7 +517,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error

// printSwarmAddrs prints the addresses of the host
func printSwarmAddrs(node *core.IpfsNode) {
if !node.OnlineMode() {
if !node.IsOnline {
fmt.Println("Swarm not listening, running in offline mode.")
return
}
Expand Down
4 changes: 0 additions & 4 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ environment variable:
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cctx := env.(*oldcmds.Context)
if cctx.Online {
return cmdkit.Error{Message: "init must be run offline only"}
}

empty, _ := req.Options[emptyRepoOptionName].(bool)
nBitsForKeypair, _ := req.Options[bitsOptionName].(int)

Expand Down
1 change: 0 additions & 1 deletion cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ func mainRet() int {
return nil, err
}

n.SetLocal(true)
return n, nil
},
}, nil
Expand Down
1 change: 0 additions & 1 deletion cmd/ipfswatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ func IsHidden(path string) bool {

func cmdCtx(node *core.IpfsNode, repoPath string) commands.Context {
return commands.Context{
Online: true,
ConfigRoot: repoPath,
LoadConfig: func(path string) (*config.Config, error) {
return node.Repo.Config()
Expand Down
1 change: 0 additions & 1 deletion commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var log = logging.Logger("command")

// Context represents request context
type Context struct {
Online bool
ConfigRoot string
ReqLog *ReqLog

Expand Down
6 changes: 1 addition & 5 deletions core/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
ctx = metrics.CtxScope(ctx, "ipfs")

n := &IpfsNode{
mode: offlineMode,
IsOnline: cfg.Online,
Repo: cfg.Repo,
ctx: ctx,
Peerstore: pstoremem.NewPeerstore(),
Expand All @@ -153,10 +153,6 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
"ipns": ipns.Validator{KeyBook: n.Peerstore},
}

if cfg.Online {
n.mode = onlineMode
}

// TODO: this is a weird circular-ish dependency, rework it
n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)

Expand Down
8 changes: 4 additions & 4 deletions core/commands/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Print out all blocks currently on the bitswap wantlist for the local peer.`,
return err
}

if !nd.OnlineMode() {
if !nd.IsOnline {
return ErrNotOnline
}

Expand Down Expand Up @@ -100,7 +100,7 @@ var bitswapStatCmd = &cmds.Command{
return err
}

if !nd.OnlineMode() {
if !nd.IsOnline {
return cmdkit.Errorf(cmdkit.ErrClient, ErrNotOnline.Error())
}

Expand Down Expand Up @@ -163,7 +163,7 @@ prints the ledger associated with a given peer.
return err
}

if !nd.OnlineMode() {
if !nd.IsOnline {
return ErrNotOnline
}

Expand Down Expand Up @@ -206,7 +206,7 @@ Trigger reprovider to announce our data to network.
return err
}

if !nd.OnlineMode() {
if !nd.IsOnline {
return ErrNotOnline
}

Expand Down
10 changes: 5 additions & 5 deletions core/commands/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var findProvidersDhtCmd = &cmds.Command{
return err
}

if !n.OnlineMode() {
if !n.IsOnline {
return ErrNotOnline
}

Expand Down Expand Up @@ -232,7 +232,7 @@ var provideRefDhtCmd = &cmds.Command{
return err
}

if !nd.OnlineMode() {
if !nd.IsOnline {
return ErrNotOnline
}

Expand Down Expand Up @@ -361,7 +361,7 @@ var findPeerDhtCmd = &cmds.Command{
return err
}

if !nd.OnlineMode() {
if !nd.IsOnline {
return ErrNotOnline
}

Expand Down Expand Up @@ -445,7 +445,7 @@ Different key types can specify other 'best' rules.
return err
}

if !nd.OnlineMode() {
if !nd.IsOnline {
return ErrNotOnline
}

Expand Down Expand Up @@ -539,7 +539,7 @@ NOTE: A value may not exceed 2048 bytes.
return err
}

if !nd.OnlineMode() {
if !nd.IsOnline {
return ErrNotOnline
}

Expand Down
11 changes: 0 additions & 11 deletions core/commands/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"os/exec"
"strings"

commands "github.com/ipfs/go-ipfs/commands"

cmds "gx/ipfs/QmQkW9fnCsg9SLHdViiAh6qfBppodsPZVpU92dZLqYtEfs/go-ipfs-cmds"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)
Expand Down Expand Up @@ -51,15 +49,6 @@ func ExternalBinary() *cmds.Command {
// setup env of child program
osenv := os.Environ()

// Get the node iff already defined.
if cctx, ok := env.(*commands.Context); ok && cctx.Online {
nd, err := cctx.GetNode()
if err != nil {
return fmt.Errorf("failed to start ipfs node: %s", err)
}
osenv = append(osenv, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode()))
}

cmd.Env = osenv

err = cmd.Start()
Expand Down
2 changes: 1 addition & 1 deletion core/commands/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ EXAMPLE:
}

// TODO handle offline mode with polymorphism instead of conditionals
if !n.OnlineMode() {
if !n.IsOnline {
return errors.New(offlineIdErrorMessage)
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ baz
}

// error if we aren't running node in online mode
if nd.LocalMode() {
if !nd.IsOnline {
return ErrNotOnline
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func p2pGetNode(env cmds.Environment) (*core.IpfsNode, error) {
return nil, errors.New("libp2p stream mounting not enabled")
}

if !nd.OnlineMode() {
if !nd.IsOnline {
return nil, ErrNotOnline
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ trip latency information.
}

// Must be online!
if !n.OnlineMode() {
if !n.IsOnline {
return ErrNotOnline
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var daemonShutdownCmd = &cmds.Command{
return err
}

if nd.LocalMode() {
if !nd.IsDaemon {
return cmdkit.Errorf(cmdkit.ErrClient, "daemon not running")
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Example:
}

// Must be online!
if !nd.OnlineMode() {
if !nd.IsOnline {
return cmdkit.Errorf(cmdkit.ErrClient, ErrNotOnline.Error())
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/sysdiag.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Prints out information about your computer to aid in easier debugging.
return err
}

err = netInfo(nd.OnlineMode(), info)
err = netInfo(nd.IsOnline, info)
if err != nil {
return err
}
Expand Down
36 changes: 3 additions & 33 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ const DefaultIpnsCacheSize = 128

var log = logging.Logger("core")

type mode int

const (
// zero value is not a valid mode, must be explicitly set
localMode mode = iota
offlineMode
onlineMode
)

func init() {
identify.ClientVersion = "go-ipfs/" + version.CurrentVersionNumber + "/" + version.CurrentCommit
}
Expand Down Expand Up @@ -145,8 +136,9 @@ type IpfsNode struct {
proc goprocess.Process
ctx context.Context

mode mode
localModeSet bool
// Flags
IsOnline bool // Online is set when networking is enabled.
IsDaemon bool // Daemon is set when running on a long-running daemon.
}

// Mounts defines what the node's mount state is. This should
Expand Down Expand Up @@ -721,28 +713,6 @@ func (n *IpfsNode) teardown() error {
return nil
}

// OnlineMode returns whether or not the IpfsNode is in OnlineMode.
func (n *IpfsNode) OnlineMode() bool {
return n.mode == onlineMode
}

// SetLocal will set the IpfsNode to local mode
func (n *IpfsNode) SetLocal(isLocal bool) {
if isLocal {
n.mode = localMode
}
n.localModeSet = true
}

// LocalMode returns whether or not the IpfsNode is in LocalMode
func (n *IpfsNode) LocalMode() bool {
if !n.localModeSet {
// programmer error should not happen
panic("local mode not set")
}
return n.mode == localMode
}

// Bootstrap will set and call the IpfsNodes bootstrap function.
func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
// TODO what should return value be when in offlineMode?
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
}

subApi.checkOnline = func(allowOffline bool) error {
if !n.OnlineMode() && !allowOffline {
if !n.IsOnline && !allowOffline {
return coreiface.ErrOffline
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr

// Resolve path to the final DAG node for the ETag
resolvedPath, err := i.api.ResolvePath(ctx, parsedPath)
if err == coreiface.ErrOffline && !i.node.OnlineMode() {
if err == coreiface.ErrOffline && !i.node.IsOnline {
webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusServiceUnavailable)
return
} else if err != nil {
Expand Down
1 change: 0 additions & 1 deletion core/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func MockCmdsCtx() (commands.Context, error) {
}

return commands.Context{
Online: true,
ConfigRoot: "/tmp/.mockipfsconfig",
LoadConfig: func(path string) (*config.Config, error) {
return &conf, nil
Expand Down
2 changes: 1 addition & 1 deletion fuse/node/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func doMount(node *core.IpfsNode, fsdir, nsdir string) error {
fsmount, err1 = rofs.Mount(node, fsdir)
}()

if node.OnlineMode() {
if node.IsOnline {
wg.Add(1)
go func() {
defer wg.Done()
Expand Down