forked from ipfs/iptb-plugins
/
localipfs.go
522 lines (407 loc) · 10.5 KB
/
localipfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package pluginlocalipfs
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/ipfs/iptb-plugins"
"github.com/ipfs/iptb/testbed/interfaces"
"github.com/ipfs/iptb/util"
"github.com/ipfs/go-cid"
config "github.com/ipfs/go-ipfs-config"
serial "github.com/ipfs/go-ipfs-config/serialize"
"github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
)
var errTimeout = errors.New("timeout")
var PluginName = "localipfs"
type LocalIpfs struct {
dir string
peerid *cid.Cid
apiaddr multiaddr.Multiaddr
swarmaddr multiaddr.Multiaddr
binary string
mdns bool
}
// NewNode creates a localipfs iptb core node that runs ipfs on the local
// system by using random ports for both the api and the swarm.
// Attributes
// - binary: binary to use for Init, Start (defaults to ipfs in path)
// - apiaddr: multiaddr use for the api (defaults to /ip4/127.0.0.1/tcp/0)
// - swarmaddr: multiaddr used for swarm (defaults to /ip4/127.0.0.1/tcp/0)
// - mdns: if present, enables mdns (off by default)
func NewNode(dir string, attrs map[string]string) (testbedi.Core, error) {
mdns := false
binary := ""
var ok bool
if binary, ok = attrs["binary"]; !ok {
var err error
binary, err = exec.LookPath("ipfs")
if err != nil {
return nil, err
}
}
apiaddr, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
if err != nil {
return nil, err
}
swarmaddr, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
if err != nil {
return nil, err
}
if apiaddrstr, ok := attrs["apiaddr"]; ok {
var err error
apiaddr, err = multiaddr.NewMultiaddr(apiaddrstr)
if err != nil {
return nil, err
}
}
if swarmaddrstr, ok := attrs["swarmaddr"]; ok {
var err error
swarmaddr, err = multiaddr.NewMultiaddr(swarmaddrstr)
if err != nil {
return nil, err
}
}
if _, ok := attrs["mdns"]; ok {
mdns = true
}
return &LocalIpfs{
dir: dir,
apiaddr: apiaddr,
swarmaddr: swarmaddr,
binary: binary,
mdns: mdns,
}, nil
}
func GetAttrList() []string {
return ipfs.GetAttrList()
}
func GetAttrDesc(attr string) (string, error) {
return ipfs.GetAttrDesc(attr)
}
func GetMetricList() []string {
return ipfs.GetMetricList()
}
func GetMetricDesc(attr string) (string, error) {
return ipfs.GetMetricDesc(attr)
}
/// TestbedNode Interface
func (l *LocalIpfs) Init(ctx context.Context, agrs ...string) (testbedi.Output, error) {
agrs = append([]string{l.binary, "init"}, agrs...)
output, oerr := l.RunCmd(ctx, nil, agrs...)
if oerr != nil {
return nil, oerr
}
icfg, err := l.Config()
if err != nil {
return nil, err
}
lcfg := icfg.(*config.Config)
lcfg.Bootstrap = []string{}
lcfg.Addresses.Swarm = []string{l.swarmaddr.String()}
lcfg.Addresses.API = []string{l.apiaddr.String()}
lcfg.Addresses.Gateway = []string{""}
lcfg.Discovery.MDNS.Enabled = l.mdns
err = l.WriteConfig(lcfg)
if err != nil {
return nil, err
}
return output, oerr
}
func (l *LocalIpfs) Start(ctx context.Context, wait bool, args ...string) (testbedi.Output, error) {
alive, err := l.isAlive()
if err != nil {
return nil, err
}
if alive {
return nil, fmt.Errorf("node is already running")
}
dir := l.dir
dargs := append([]string{"daemon"}, args...)
cmd := exec.Command(l.binary, dargs...)
cmd.Dir = dir
cmd.Env, err = l.env()
if err != nil {
return nil, err
}
iptbutil.SetupOpt(cmd)
stdout, err := os.Create(filepath.Join(dir, "daemon.stdout"))
if err != nil {
return nil, err
}
stderr, err := os.Create(filepath.Join(dir, "daemon.stderr"))
if err != nil {
return nil, err
}
cmd.Stdout = stdout
cmd.Stderr = stderr
err = cmd.Start()
if err != nil {
return nil, err
}
pid := cmd.Process.Pid
err = ioutil.WriteFile(filepath.Join(dir, "daemon.pid"), []byte(fmt.Sprint(pid)), 0666)
if err != nil {
return nil, err
}
if wait {
return nil, ipfs.WaitOnAPI(l)
}
return nil, nil
}
func (l *LocalIpfs) Stop(ctx context.Context) error {
pid, err := l.getPID()
if err != nil {
return fmt.Errorf("error killing daemon %s: %s", l.dir, err)
}
p, err := os.FindProcess(pid)
if err != nil {
return fmt.Errorf("error killing daemon %s: %s", l.dir, err)
}
waitch := make(chan struct{}, 1)
go func() {
p.Wait()
waitch <- struct{}{}
}()
defer func() {
err := os.Remove(filepath.Join(l.dir, "daemon.pid"))
if err != nil && !os.IsNotExist(err) {
panic(fmt.Errorf("error removing pid file for daemon at %s: %s", l.dir, err))
}
}()
if err := l.signalAndWait(p, waitch, syscall.SIGTERM, 1*time.Second); err != errTimeout {
return err
}
if err := l.signalAndWait(p, waitch, syscall.SIGTERM, 2*time.Second); err != errTimeout {
return err
}
if err := l.signalAndWait(p, waitch, syscall.SIGQUIT, 5*time.Second); err != errTimeout {
return err
}
if err := l.signalAndWait(p, waitch, syscall.SIGKILL, 5*time.Second); err != errTimeout {
return err
}
return fmt.Errorf("Could not stop localipfs node with pid %d", pid)
}
func (l *LocalIpfs) RunCmd(ctx context.Context, stdin io.Reader, args ...string) (testbedi.Output, error) {
env, err := l.env()
if err != nil {
return nil, fmt.Errorf("error getting env: %s", err)
}
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd.Env = env
cmd.Stdin = stdin
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
err = cmd.Start()
if err != nil {
return nil, err
}
stderrbytes, err := ioutil.ReadAll(stderr)
if err != nil {
return nil, err
}
stdoutbytes, err := ioutil.ReadAll(stdout)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
exiterr := cmd.Wait()
var exitcode = 0
switch oerr := exiterr.(type) {
case *exec.ExitError:
if ctx.Err() == context.DeadlineExceeded {
err = errors.Wrapf(oerr, "context deadline exceeded for command: %q", strings.Join(cmd.Args, " "))
}
exitcode = 1
case nil:
err = oerr
}
return iptbutil.NewOutput(args, stdoutbytes, stderrbytes, exitcode, err), nil
}
func (l *LocalIpfs) Connect(ctx context.Context, tbn testbedi.Core) error {
swarmaddrs, err := tbn.SwarmAddrs()
if err != nil {
return err
}
output, err := l.RunCmd(ctx, nil, "ipfs", "swarm", "connect", swarmaddrs[0])
if err != nil {
return err
}
if output.ExitCode() != 0 {
out, err := ioutil.ReadAll(output.Stderr())
if err != nil {
return err
}
return fmt.Errorf("%s", string(out))
}
return nil
}
func (l *LocalIpfs) Shell(ctx context.Context, nodes []testbedi.Core) error {
shell := os.Getenv("SHELL")
if shell == "" {
return fmt.Errorf("no shell found")
}
if len(os.Getenv("IPFS_PATH")) != 0 {
// If the users shell sets IPFS_PATH, it will just be overridden by the shell again
return fmt.Errorf("shell has IPFS_PATH set, please unset before trying to use iptb shell")
}
nenvs, err := l.env()
if err != nil {
return err
}
// TODO(tperson): It would be great if we could guarantee that the shell
// is using the same binary. However, the users shell may prepend anything
// we change in the PATH
for i, n := range nodes {
peerid, err := n.PeerID()
if err != nil {
return err
}
nenvs = append(nenvs, fmt.Sprintf("NODE%d=%s", i, peerid))
}
return syscall.Exec(shell, []string{shell}, nenvs)
}
func (l *LocalIpfs) String() string {
pcid, err := l.PeerID()
if err != nil {
return fmt.Sprintf("%s", l.Type())
}
return fmt.Sprintf("%s", pcid[0:12])
}
func (l *LocalIpfs) APIAddr() (string, error) {
return ipfs.GetAPIAddrFromRepo(l.dir)
}
func (l *LocalIpfs) SwarmAddrs() ([]string, error) {
return ipfs.SwarmAddrs(l)
}
func (l *LocalIpfs) Dir() string {
return l.dir
}
func (l *LocalIpfs) PeerID() (string, error) {
if l.peerid != nil {
return l.peerid.String(), nil
}
var err error
l.peerid, err = ipfs.GetPeerID(l)
if err != nil {
return "", err
}
return l.peerid.String(), nil
}
/// Metric Interface
func (l *LocalIpfs) GetMetricList() []string {
return GetMetricList()
}
func (l *LocalIpfs) GetMetricDesc(attr string) (string, error) {
return GetMetricDesc(attr)
}
func (l *LocalIpfs) Metric(metric string) (string, error) {
return ipfs.GetMetric(l, metric)
}
func (l *LocalIpfs) Heartbeat() (map[string]string, error) {
return nil, nil
}
func (l *LocalIpfs) Events() (io.ReadCloser, error) {
return ipfs.ReadLogs(l)
}
func (l *LocalIpfs) Logs() (io.ReadCloser, error) {
return nil, fmt.Errorf("not implemented")
}
// Attribute Interface
func (l *LocalIpfs) GetAttrList() []string {
return GetAttrList()
}
func (l *LocalIpfs) GetAttrDesc(attr string) (string, error) {
return GetAttrDesc(attr)
}
func (l *LocalIpfs) Attr(attr string) (string, error) {
return ipfs.GetAttr(l, attr)
}
func (l *LocalIpfs) SetAttr(string, string) error {
return fmt.Errorf("no attribute to set")
}
func (l *LocalIpfs) StderrReader() (io.ReadCloser, error) {
return l.readerFor("daemon.stderr")
}
func (l *LocalIpfs) StdoutReader() (io.ReadCloser, error) {
return l.readerFor("daemon.stdout")
}
func (l *LocalIpfs) Config() (interface{}, error) {
return serial.Load(filepath.Join(l.dir, "config"))
}
func (l *LocalIpfs) WriteConfig(cfg interface{}) error {
return serial.WriteConfigFile(filepath.Join(l.dir, "config"), cfg)
}
func (l *LocalIpfs) Type() string {
return "ipfs"
}
func (l *LocalIpfs) Deployment() string {
return "local"
}
func (l *LocalIpfs) readerFor(file string) (io.ReadCloser, error) {
return os.OpenFile(filepath.Join(l.dir, file), os.O_RDONLY, 0)
}
func (l *LocalIpfs) signalAndWait(p *os.Process, waitch <-chan struct{}, signal os.Signal, t time.Duration) error {
err := p.Signal(signal)
if err != nil {
return fmt.Errorf("error killing daemon %s: %s", l.dir, err)
}
select {
case <-waitch:
return nil
case <-time.After(t):
return errTimeout
}
}
func (l *LocalIpfs) getPID() (int, error) {
b, err := ioutil.ReadFile(filepath.Join(l.dir, "daemon.pid"))
if err != nil {
return -1, err
}
return strconv.Atoi(string(b))
}
func (l *LocalIpfs) isAlive() (bool, error) {
pid, err := l.getPID()
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
proc, err := os.FindProcess(pid)
if err != nil {
return false, nil
}
err = proc.Signal(syscall.Signal(0))
if err == nil {
return true, nil
}
return false, nil
}
func (l *LocalIpfs) env() ([]string, error) {
envs := os.Environ()
ipfspath := "IPFS_PATH=" + l.dir
for i, e := range envs {
if strings.HasPrefix(e, "IPFS_PATH=") {
envs[i] = ipfspath
return envs, nil
}
}
return append(envs, ipfspath), nil
}