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

Offline daemon mode #2696

Merged
merged 8 commits into from
May 31, 2016
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
11 changes: 9 additions & 2 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
unencryptTransportKwd = "disable-transport-encryption"
enableGCKwd = "enable-gc"
adjustFDLimitKwd = "manage-fdlimit"
offlineKwd = "offline"
// apiAddrKwd = "address-api"
// swarmAddrKwd = "address-swarm"
)
Expand Down Expand Up @@ -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 offline. Do not connect to the rest of the network but provide local API.").Default(false),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provide the local API, maybe?


// 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)"),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think there's other errors here to check alongside !i.node.OnlineMode(). for example, if a /ipfs/... path fails to resolve, it's likely because of no network.

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
}
Expand Down
11 changes: 6 additions & 5 deletions test/sharness/lib/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 >/dev/null 2>&1; then
test_expect_success "set swarm address vars" '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation problem here, i'd say indent the whole block.

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() {
Expand Down
7 changes: 7 additions & 0 deletions test/sharness/t0040-add-and-cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great. i think we want to test --offline with almost every other test too. We don't want nasty surprises.


test_add_cat_file

test_kill_ipfs_daemon

test_done
13 changes: 13 additions & 0 deletions test/sharness/t0061-daemon-opts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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