fix(daemon) ensure IPFS is initialized before starting the daemon
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 18, 2014 at 06:42 UTC
e12edd205384d4c571f79e0299741a3d7b03a497
1 file changed
+22
-12
cmd/ipfs/daemon.go
+22
-12
@@ -46,19 +46,9 @@ the daemon.
46
}
47
48
func daemonFunc(req cmds.Request) (interface{}, error) {
49
- ctx := req.Context()
50
- cfg, err := ctx.GetConfig()
51
- if err != nil {
52
- return nil, err
53
- }
54
-
55
- // make sure we construct online node.
56
- ctx.Online = true
57
- node, err := ctx.GetNode()
58
- if err != nil {
59
- return nil, err
60
- }
49
50
+ // first, whether user has provided the initialization flag. we may be
51
+ // running in an uninitialized state.
52
initialize, _, err := req.Option(initOptionKwd).Bool()
53
if err != nil {
54
return nil, err
@@ -77,12 +67,32 @@ func daemonFunc(req cmds.Request) (interface{}, error) {
67
}
68
}
69
70
+ // To ensure that IPFS has been initialized, fetch the config. Do this
71
+ // _before_ acquiring the daemon lock so the user gets an appropriate error
72
+ // message.
73
+ // NB: It's safe to read the config without the daemon lock, but not safe
74
+ // to write.
75
+ ctx := req.Context()
76
+ cfg, err := ctx.GetConfig()
77
+ if err != nil {
78
+ return nil, err
79
+ }
80
+
81
+ // acquire the daemon lock _before_ constructing a node. we need to make
82
+ // sure we are permitted to access the resources (datastore, etc.)
83
lock, err := daemon.Lock(req.Context().ConfigRoot)
84
if err != nil {
85
return nil, debugerror.Errorf("Couldn't obtain lock. Is another daemon already running?")
86
}
87
defer lock.Close()
88
89
+ // make sure we construct online node.
90
+ ctx.Online = true
91
+ node, err := ctx.GetNode()
92
+ if err != nil {
93
+ return nil, err
94
+ }
95
+
96
addr, err := ma.NewMultiaddr(cfg.Addresses.API)
97
if err != nil {
98
return nil, err