提交 6a894d6e 作者: Jeromy Johnson

Merge pull request #2696 from ipfs/feature/Offline-2393

Offline daemon mode
...@@ -42,6 +42,7 @@ const ( ...@@ -42,6 +42,7 @@ const (
unencryptTransportKwd = "disable-transport-encryption" unencryptTransportKwd = "disable-transport-encryption"
enableGCKwd = "enable-gc" enableGCKwd = "enable-gc"
adjustFDLimitKwd = "manage-fdlimit" adjustFDLimitKwd = "manage-fdlimit"
offlineKwd = "offline"
// apiAddrKwd = "address-api" // apiAddrKwd = "address-api"
// swarmAddrKwd = "address-swarm" // swarmAddrKwd = "address-swarm"
) )
...@@ -136,6 +137,7 @@ future version, along with this notice. Please move to setting the HTTP Headers. ...@@ -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(unencryptTransportKwd, "Disable transport encryption (for debugging protocols)").Default(false),
cmds.BoolOption(enableGCKwd, "Enable automatic periodic repo garbage collection").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(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),
// TODO: add way to override addresses. tricky part: updating the config if also --init. // 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)"), // cmds.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"),
...@@ -226,9 +228,10 @@ func daemonFunc(req cmds.Request, res cmds.Response) { ...@@ -226,9 +228,10 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
// Start assembling node config // Start assembling node config
ncfg := &core.BuildCfg{ ncfg := &core.BuildCfg{
Online: true, Repo: repo,
Repo: repo,
} }
offline, _, _ := req.Option(offlineKwd).Bool()
ncfg.Online = !offline
routingOption, _, err := req.Option(routingOptionKwd).String() routingOption, _, err := req.Option(routingOptionKwd).String()
if err != nil { if err != nil {
...@@ -416,6 +419,10 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) { ...@@ -416,6 +419,10 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) {
// printSwarmAddrs prints the addresses of the host // printSwarmAddrs prints the addresses of the host
func printSwarmAddrs(node *core.IpfsNode) { func printSwarmAddrs(node *core.IpfsNode) {
if !node.OnlineMode() {
fmt.Println("Swarm not listening, running in offline mode.")
return
}
var addrs []string var addrs []string
for _, addr := range node.PeerHost.Addrs() { for _, addr := range node.PeerHost.Addrs() {
addrs = append(addrs, addr.String()) addrs = append(addrs, addr.String())
......
...@@ -510,6 +510,10 @@ func (n *IpfsNode) loadFilesRoot() error { ...@@ -510,6 +510,10 @@ func (n *IpfsNode) loadFilesRoot() error {
// uses it to instantiate a routing system in offline mode. // uses it to instantiate a routing system in offline mode.
// This is primarily used for offline ipns modifications. // This is primarily used for offline ipns modifications.
func (n *IpfsNode) SetupOfflineRouting() error { func (n *IpfsNode) SetupOfflineRouting() error {
if n.Routing != nil {
// Routing was already set up
return nil
}
err := n.LoadPrivateKey() err := n.LoadPrivateKey()
if err != nil { if err != nil {
return err return err
......
...@@ -159,7 +159,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request ...@@ -159,7 +159,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
} }
nd, err := core.Resolve(ctx, i.node, path.Path(urlPath)) 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) webError(w, "Path Resolve error", err, http.StatusBadRequest)
return return
} }
......
...@@ -194,12 +194,13 @@ test_set_address_vars() { ...@@ -194,12 +194,13 @@ test_set_address_vars() {
GWAY_PORT=$(port_from_maddr $GWAY_MADDR) GWAY_PORT=$(port_from_maddr $GWAY_MADDR)
' '
if ipfs swarm addrs local >/dev/null 2>&1; then
test_expect_success "set swarm address vars" ' test_expect_success "set swarm address vars" '
ipfs swarm addrs local > addrs_out && ipfs swarm addrs local > addrs_out &&
SWARM_MADDR=$(grep "127.0.0.1" addrs_out) && SWARM_MADDR=$(grep "127.0.0.1" addrs_out) &&
SWARM_PORT=$(port_from_maddr $SWARM_MADDR) SWARM_PORT=$(port_from_maddr $SWARM_MADDR)
' '
fi
} }
test_launch_ipfs_daemon() { test_launch_ipfs_daemon() {
......
...@@ -392,4 +392,11 @@ test_expect_success "ipfs cat file fails" ' ...@@ -392,4 +392,11 @@ test_expect_success "ipfs cat file fails" '
test_add_named_pipe "" test_add_named_pipe ""
# Test daemon in offline mode
test_launch_ipfs_daemon --offline
test_add_cat_file
test_kill_ipfs_daemon
test_done test_done
...@@ -35,4 +35,17 @@ test_expect_success 'transport should be unencrypted' ' ...@@ -35,4 +35,17 @@ test_expect_success 'transport should be unencrypted' '
test_kill_ipfs_daemon 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 test_done
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论