Commit e12edd20 authored by Brian Tiger Chow's avatar Brian Tiger Chow

fix(daemon) ensure IPFS is initialized before starting the daemon

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent e24b09f8
...@@ -46,19 +46,9 @@ the daemon. ...@@ -46,19 +46,9 @@ the daemon.
} }
func daemonFunc(req cmds.Request) (interface{}, error) { func daemonFunc(req cmds.Request) (interface{}, error) {
ctx := req.Context()
cfg, err := ctx.GetConfig()
if err != nil {
return nil, err
}
// make sure we construct online node.
ctx.Online = true
node, err := ctx.GetNode()
if err != nil {
return nil, err
}
// first, whether user has provided the initialization flag. we may be
// running in an uninitialized state.
initialize, _, err := req.Option(initOptionKwd).Bool() initialize, _, err := req.Option(initOptionKwd).Bool()
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -77,12 +67,32 @@ func daemonFunc(req cmds.Request) (interface{}, error) { ...@@ -77,12 +67,32 @@ func daemonFunc(req cmds.Request) (interface{}, error) {
} }
} }
// To ensure that IPFS has been initialized, fetch the config. Do this
// _before_ acquiring the daemon lock so the user gets an appropriate error
// message.
// NB: It's safe to read the config without the daemon lock, but not safe
// to write.
ctx := req.Context()
cfg, err := ctx.GetConfig()
if err != nil {
return nil, err
}
// acquire the daemon lock _before_ constructing a node. we need to make
// sure we are permitted to access the resources (datastore, etc.)
lock, err := daemon.Lock(req.Context().ConfigRoot) lock, err := daemon.Lock(req.Context().ConfigRoot)
if err != nil { if err != nil {
return nil, debugerror.Errorf("Couldn't obtain lock. Is another daemon already running?") return nil, debugerror.Errorf("Couldn't obtain lock. Is another daemon already running?")
} }
defer lock.Close() defer lock.Close()
// make sure we construct online node.
ctx.Online = true
node, err := ctx.GetNode()
if err != nil {
return nil, err
}
addr, err := ma.NewMultiaddr(cfg.Addresses.API) addr, err := ma.NewMultiaddr(cfg.Addresses.API)
if err != nil { if err != nil {
return nil, err return nil, err
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment