From 74b7fd28ba284ce59e0f2045e8dcc0292fe865e6 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sun, 15 May 2016 14:12:57 +0200 Subject: [PATCH 1/8] Add offline daemon mode Resolves #2393 License: MIT Signed-off-by: Jakub Sztandera --- cmd/ipfs/daemon.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 825d3fa4918..a42e6cf2460 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -42,6 +42,7 @@ const ( unencryptTransportKwd = "disable-transport-encryption" enableGCKwd = "enable-gc" adjustFDLimitKwd = "manage-fdlimit" + offlineKwd = "offline" // apiAddrKwd = "address-api" // swarmAddrKwd = "address-swarm" ) @@ -136,6 +137,7 @@ future version, along with this notice. Please move to setting the HTTP Headers. cmds.BoolOption(unencryptTransportKwd, "Disable transport encryption (for debugging protocols)").Default(false), cmds.BoolOption(enableGCKwd, "Enable automatic periodic repo garbage collection").Default(false), cmds.BoolOption(adjustFDLimitKwd, "Check and raise file descriptor limits if needed").Default(false), + cmds.BoolOption(offlineKwd, "Run in offline. Do not connect with rest of the network but provide local API.").Default(false), // TODO: add way to override addresses. tricky part: updating the config if also --init. // cmds.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"), @@ -226,9 +228,10 @@ func daemonFunc(req cmds.Request, res cmds.Response) { // Start assembling node config ncfg := &core.BuildCfg{ - Online: true, - Repo: repo, + Repo: repo, } + offline, _, _ := req.Option(offlineKwd).Bool() + ncfg.Online = !offline routingOption, _, err := req.Option(routingOptionKwd).String() if err != nil { @@ -416,6 +419,10 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) { // printSwarmAddrs prints the addresses of the host func printSwarmAddrs(node *core.IpfsNode) { + if !node.OnlineMode() { + fmt.Println("Swarm not listening, running in offline mode.") + return + } var addrs []string for _, addr := range node.PeerHost.Addrs() { addrs = append(addrs, addr.String()) From a77fa94fdfc1151244ddbbe4fea930bf1258049d Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sun, 15 May 2016 14:46:08 +0200 Subject: [PATCH 2/8] Make errors more reasonable in case of node in offline mode License: MIT Signed-off-by: Jakub Sztandera --- core/corehttp/gateway_handler.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index d0366390d41..32269a17029 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -159,7 +159,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request } nd, err := core.Resolve(ctx, i.node, path.Path(urlPath)) - if err != nil { + // If node is in offline mode the error code and message should be different + if err == core.ErrNoNamesys && !i.node.OnlineMode() { + w.WriteHeader(http.StatusServiceUnavailable) + fmt.Fprint(w, "Could not resolve path. Node is in offline mode.") + return + } else if err != nil { webError(w, "Path Resolve error", err, http.StatusBadRequest) return } From 7614f4f700ab550075c30f61982371d01958bd7b Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sun, 15 May 2016 14:59:12 +0200 Subject: [PATCH 3/8] Create shanress test for offline mode node License: MIT Signed-off-by: Jakub Sztandera --- test/sharness/lib/test-lib.sh | 11 ++++++----- test/sharness/t0061-daemon-opts.sh | 13 +++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/test/sharness/lib/test-lib.sh b/test/sharness/lib/test-lib.sh index 674b15882c0..76b1a94d644 100644 --- a/test/sharness/lib/test-lib.sh +++ b/test/sharness/lib/test-lib.sh @@ -194,12 +194,13 @@ test_set_address_vars() { GWAY_PORT=$(port_from_maddr $GWAY_MADDR) ' - - test_expect_success "set swarm address vars" ' + if ipfs swarm addrs local; then + test_expect_success "set swarm address vars" ' ipfs swarm addrs local > addrs_out && - SWARM_MADDR=$(grep "127.0.0.1" addrs_out) && - SWARM_PORT=$(port_from_maddr $SWARM_MADDR) - ' + SWARM_MADDR=$(grep "127.0.0.1" addrs_out) && + SWARM_PORT=$(port_from_maddr $SWARM_MADDR) + ' + fi } test_launch_ipfs_daemon() { diff --git a/test/sharness/t0061-daemon-opts.sh b/test/sharness/t0061-daemon-opts.sh index 0490b40fda3..baf1a51817d 100755 --- a/test/sharness/t0061-daemon-opts.sh +++ b/test/sharness/t0061-daemon-opts.sh @@ -35,4 +35,17 @@ test_expect_success 'transport should be unencrypted' ' test_kill_ipfs_daemon +test_launch_ipfs_daemon --offline + +gwyaddr=$GWAY_ADDR +apiaddr=$API_ADDR + +test_expect_success 'gateway should work in offline mode' ' + echo "hello mars :$gwyaddr :$apiaddr" >expected && + HASH=$(ipfs add -q expected) && + curl -sfo actual1 "http://$gwyaddr/ipfs/$HASH" && + test_cmp expected actual1 +' + +test_kill_ipfs_daemon test_done From bdea10ff0ecc6aeac98761888ce080b66c4f88e6 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sun, 15 May 2016 21:19:44 +0200 Subject: [PATCH 4/8] Make if condition in sharness lib silent License: MIT Signed-off-by: Jakub Sztandera --- test/sharness/lib/test-lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sharness/lib/test-lib.sh b/test/sharness/lib/test-lib.sh index 76b1a94d644..db242146b4f 100644 --- a/test/sharness/lib/test-lib.sh +++ b/test/sharness/lib/test-lib.sh @@ -194,7 +194,7 @@ test_set_address_vars() { GWAY_PORT=$(port_from_maddr $GWAY_MADDR) ' - if ipfs swarm addrs local; then + if ipfs swarm addrs local >/dev/null 2>&1; then test_expect_success "set swarm address vars" ' ipfs swarm addrs local > addrs_out && SWARM_MADDR=$(grep "127.0.0.1" addrs_out) && From e293204666409f231d745eb75f33a3cfdf8269ca Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sun, 15 May 2016 21:32:07 +0200 Subject: [PATCH 5/8] Add t0040 test for offline daemon License: MIT Signed-off-by: Jakub Sztandera --- test/sharness/t0040-add-and-cat.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/sharness/t0040-add-and-cat.sh b/test/sharness/t0040-add-and-cat.sh index a37c605e752..97bc9cddbab 100755 --- a/test/sharness/t0040-add-and-cat.sh +++ b/test/sharness/t0040-add-and-cat.sh @@ -379,4 +379,11 @@ test_expect_success "ipfs cat file fails" ' test_add_named_pipe "" +# Test daemon in offline mode +test_launch_ipfs_daemon --offline + +test_add_cat_file + +test_kill_ipfs_daemon + test_done From e76a53717b857c1baa695fa2f001a7cb6b4d9d44 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 23 May 2016 20:11:40 +0200 Subject: [PATCH 6/8] Prevent repetative initalisations in offline mode License: MIT Signed-off-by: Jakub Sztandera --- core/core.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/core.go b/core/core.go index 3ffc4b69031..f549c1d89ff 100644 --- a/core/core.go +++ b/core/core.go @@ -510,6 +510,10 @@ func (n *IpfsNode) loadFilesRoot() error { // uses it to instantiate a routing system in offline mode. // This is primarily used for offline ipns modifications. func (n *IpfsNode) SetupOfflineRouting() error { + if n.Routing != nil { + // Routing was already set up + return nil + } err := n.LoadPrivateKey() if err != nil { return err From a5a9a9d9067717ebad55ba07c1959b166324570f Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 23 May 2016 20:32:44 +0200 Subject: [PATCH 7/8] Change wording a bit License: MIT Signed-off-by: Jakub Sztandera --- cmd/ipfs/daemon.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index a42e6cf2460..0cb400a2c38 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -137,7 +137,7 @@ future version, along with this notice. Please move to setting the HTTP Headers. cmds.BoolOption(unencryptTransportKwd, "Disable transport encryption (for debugging protocols)").Default(false), cmds.BoolOption(enableGCKwd, "Enable automatic periodic repo garbage collection").Default(false), cmds.BoolOption(adjustFDLimitKwd, "Check and raise file descriptor limits if needed").Default(false), - cmds.BoolOption(offlineKwd, "Run in offline. Do not connect with rest of the network but provide local API.").Default(false), + cmds.BoolOption(offlineKwd, "Run offline. Do not connect with rest of the network but provide local API.").Default(false), // TODO: add way to override addresses. tricky part: updating the config if also --init. // cmds.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"), From 3a89cbbedbca23432c2d2d366e71d7c82decc66f Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 24 May 2016 00:05:36 +0200 Subject: [PATCH 8/8] Improve wording once more License: MIT Signed-off-by: Jakub Sztandera --- cmd/ipfs/daemon.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 0cb400a2c38..eae68b27e7d 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -137,7 +137,7 @@ future version, along with this notice. Please move to setting the HTTP Headers. cmds.BoolOption(unencryptTransportKwd, "Disable transport encryption (for debugging protocols)").Default(false), cmds.BoolOption(enableGCKwd, "Enable automatic periodic repo garbage collection").Default(false), cmds.BoolOption(adjustFDLimitKwd, "Check and raise file descriptor limits if needed").Default(false), - cmds.BoolOption(offlineKwd, "Run offline. Do not connect with rest of the network but provide local API.").Default(false), + cmds.BoolOption(offlineKwd, "Run offline. Do not connect to the rest of the network but provide local API.").Default(false), // TODO: add way to override addresses. tricky part: updating the config if also --init. // cmds.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"),