update iptb dependency and use different ports for each iptb cluster
update iptb dependency again, and pick different ports for each iptb cluster try and fix godeps crap
Jeromy committed
May 3, 2015 at 16:39 UTC
9cf8c5cbc9c4c4679daf1f8b42739eb0e663c50b
4 files changed
+90
-22
Godeps/Godeps.json
+1
-1
@@ -256,7 +256,7 @@
256
},
257
{
258
"ImportPath": "github.com/whyrusleeping/iptb",
259
- "Rev": "4fa36405d0baea7773676f83fba9695e9a560473"
259
+ "Rev": "3970c95a864f1a40037f796ff596607ce8ae43be"
260
},
261
{
262
"ImportPath": "golang.org/x/crypto/blowfish",
Godeps/_workspace/src/github.com/whyrusleeping/iptb/main.go
+87
-19
@@ -1,12 +1,13 @@
1
package main
2
3
import (
4
+ "encoding/json"
5
"errors"
6
"flag"
7
"fmt"
8
"io/ioutil"
9
"log"
9
- "net"
10
+ "net/http"
11
"os"
12
"os/exec"
13
"path"
@@ -15,7 +16,9 @@ import (
16
"syscall"
17
"time"
18
19
+ ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
20
serial "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
21
+ manet "github.com/jbenet/go-multiaddr-net"
22
)
23
24
// GetNumNodes returns the number of testbed nodes configured in the testbed directory
@@ -66,6 +69,15 @@ type initCfg struct {
69
Count int
70
Force bool
71
Bootstrap string
72
+ PortStart int
73
+}
74
+
75
+func (c *initCfg) swarmAddrForPeer(i int) string {
76
+ return fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", c.PortStart+i)
77
+}
78
+
79
+func (c *initCfg) apiAddrForPeer(i int) string {
80
+ return fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", c.PortStart+1000+i)
81
}
82
83
func IpfsInit(cfg *initCfg) error {
@@ -121,7 +133,7 @@ func IpfsInit(cfg *initCfg) error {
133
return nil
134
}
135
124
-func starBootstrap(cfg *initCfg) error {
136
+func starBootstrap(icfg *initCfg) error {
137
// '0' node is the bootstrap node
138
cfgpath := path.Join(IpfsDirN(0), "config")
139
bcfg, err := serial.Load(cfgpath)
@@ -129,15 +141,15 @@ func starBootstrap(cfg *initCfg) error {
141
return err
142
}
143
bcfg.Bootstrap = nil
132
- bcfg.Addresses.Swarm = []string{"/ip4/127.0.0.1/tcp/4002"}
133
- bcfg.Addresses.API = "/ip4/127.0.0.1/tcp/5002"
144
+ bcfg.Addresses.Swarm = []string{icfg.swarmAddrForPeer(0)}
145
+ bcfg.Addresses.API = icfg.apiAddrForPeer(0)
146
bcfg.Addresses.Gateway = ""
147
err = serial.WriteConfigFile(cfgpath, bcfg)
148
if err != nil {
149
return err
150
}
151
140
- for i := 1; i < cfg.Count; i++ {
152
+ for i := 1; i < icfg.Count; i++ {
153
cfgpath := path.Join(IpfsDirN(i), "config")
154
cfg, err := serial.Load(cfgpath)
155
if err != nil {
@@ -147,9 +159,9 @@ func starBootstrap(cfg *initCfg) error {
159
cfg.Bootstrap = []string{fmt.Sprintf("%s/ipfs/%s", bcfg.Addresses.Swarm[0], bcfg.Identity.PeerID)}
160
cfg.Addresses.Gateway = ""
161
cfg.Addresses.Swarm = []string{
150
- fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", 4002+i),
162
+ icfg.swarmAddrForPeer(i),
163
}
152
- cfg.Addresses.API = fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5002+i)
164
+ cfg.Addresses.API = icfg.apiAddrForPeer(i)
165
err = serial.WriteConfigFile(cfgpath, cfg)
166
if err != nil {
167
return err
@@ -158,8 +170,8 @@ func starBootstrap(cfg *initCfg) error {
170
return nil
171
}
172
161
-func clearBootstrapping(cfg *initCfg) error {
162
- for i := 0; i < cfg.Count; i++ {
173
+func clearBootstrapping(icfg *initCfg) error {
174
+ for i := 0; i < icfg.Count; i++ {
175
cfgpath := path.Join(IpfsDirN(i), "config")
176
cfg, err := serial.Load(cfgpath)
177
if err != nil {
@@ -168,10 +180,8 @@ func clearBootstrapping(cfg *initCfg) error {
180
181
cfg.Bootstrap = nil
182
cfg.Addresses.Gateway = ""
171
- cfg.Addresses.Swarm = []string{
172
- fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", 4002+i),
173
- }
174
- cfg.Addresses.API = fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5002+i)
183
+ cfg.Addresses.Swarm = []string{icfg.swarmAddrForPeer(i)}
184
+ cfg.Addresses.API = icfg.apiAddrForPeer(i)
185
err = serial.WriteConfigFile(cfgpath, cfg)
186
if err != nil {
187
return err
@@ -222,6 +232,7 @@ func IpfsKill() error {
232
}
233
234
func IpfsStart(waitall bool) error {
235
+ var addrs []string
236
n := GetNumNodes()
237
for i := 0; i < n; i++ {
238
dir := IpfsDirN(i)
@@ -259,22 +270,55 @@ func IpfsStart(waitall bool) error {
270
// Make sure node 0 is up before starting the rest so
271
// bootstrapping works properly
272
if i == 0 || waitall {
262
- err := waitForLive(fmt.Sprintf("localhost:%d", 5002+i))
273
+ cfg, err := serial.Load(path.Join(IpfsDirN(i), "config"))
274
+ if err != nil {
275
+ return err
276
+ }
277
+
278
+ maddr := ma.StringCast(cfg.Addresses.API)
279
+ _, addr, err := manet.DialArgs(maddr)
280
+ if err != nil {
281
+ return err
282
+ }
283
+
284
+ addrs = append(addrs, addr)
285
+
286
+ err = waitOnAPI(cfg.Identity.PeerID, addr)
287
+ if err != nil {
288
+ return err
289
+ }
290
+ }
291
+ }
292
+ if waitall {
293
+ for i := 0; i < n; i++ {
294
+ err := waitOnSwarmPeers(addrs[i])
295
if err != nil {
296
return err
297
}
298
}
299
+
300
}
301
return nil
302
}
303
271
-// waitForLive polls the given endpoint until it is up, or until
272
-// a timeout
273
-func waitForLive(addr string) error {
304
+func waitOnAPI(peerid, addr string) error {
305
for i := 0; i < 50; i++ {
275
- c, err := net.Dial("tcp", addr)
306
+ resp, err := http.Get("http://" + addr + "/api/v0/id")
307
if err == nil {
277
- c.Close()
308
+ out := make(map[string]interface{})
309
+ err := json.NewDecoder(resp.Body).Decode(&out)
310
+ if err != nil {
311
+ return fmt.Errorf("liveness check failed: %s", err)
312
+ }
313
+ id, ok := out["ID"]
314
+ if !ok {
315
+ return fmt.Errorf("liveness check failed: ID field not present in output")
316
+ }
317
+ idstr := id.(string)
318
+ if idstr != peerid {
319
+ return fmt.Errorf("liveness check failed: unexpected peer at endpoint")
320
+ }
321
+
322
return nil
323
}
324
time.Sleep(time.Millisecond * 200)
@@ -282,6 +326,29 @@ func waitForLive(addr string) error {
326
return fmt.Errorf("node at %s failed to come online in given time period", addr)
327
}
328
329
+func waitOnSwarmPeers(addr string) error {
330
+ for i := 0; i < 50; i++ {
331
+ resp, err := http.Get("http://" + addr + "/api/v0/swarm/peers")
332
+ if err == nil {
333
+ out := make(map[string]interface{})
334
+ err := json.NewDecoder(resp.Body).Decode(&out)
335
+ if err != nil {
336
+ return fmt.Errorf("liveness check failed: %s", err)
337
+ }
338
+
339
+ peers := out["Strings"].([]interface{})
340
+ if len(peers) == 0 {
341
+ time.Sleep(time.Millisecond * 200)
342
+ continue
343
+ }
344
+
345
+ return nil
346
+ }
347
+ time.Sleep(time.Millisecond * 200)
348
+ }
349
+ return fmt.Errorf("node at %s failed to bootstrap in given time period", addr)
350
+}
351
+
352
// GetPeerID reads the config of node 'n' and returns its peer ID
353
func GetPeerID(n int) (string, error) {
354
cfg, err := serial.Load(path.Join(IpfsDirN(n), "config"))
@@ -371,6 +438,7 @@ func handleErr(s string, err error) {
438
func main() {
439
cfg := new(initCfg)
440
flag.IntVar(&cfg.Count, "n", 0, "number of ipfs nodes to initialize")
441
+ flag.IntVar(&cfg.PortStart, "p", 4002, "port to start allocations from")
442
flag.BoolVar(&cfg.Force, "f", false, "force initialization (overwrite existing configs)")
443
flag.StringVar(&cfg.Bootstrap, "bootstrap", "star", "select bootstrapping style for cluster")
444
test/sharness/t0101-iptb-name.sh
+1
-1
@@ -11,7 +11,7 @@ test_description="Test ipfs repo operations"
11
export IPTB_ROOT="`pwd`/.iptb"
12
13
test_expect_success "set up an iptb cluster" '
14
- iptb -n=4 init &&
14
+ iptb -n=4 -p=9000 init &&
15
iptb -wait start
16
'
17
test/sharness/t0130-multinode.sh
+1
-1
@@ -11,7 +11,7 @@ test_description="Test multiple ipfs nodes"
11
export IPTB_ROOT="`pwd`/.iptb"
12
13
test_expect_success "set up a few nodes" '
14
- iptb -n=3 init &&
14
+ iptb -n=3 -p=9200 init &&
15
iptb -wait start
16
'
17