feat(cmd/routingd) add executable
Brian Tiger Chow committed
Feb 12, 2015 at 09:04 UTC
0918636651b398324d218a400b603a451ee62233
1 file changed
+121
cmd/routingd/main.go
new
+121
@@ -0,0 +1,121 @@
1
+package main
2
+
3
+import (
4
+ "flag"
5
+ "log"
6
+ "os"
7
+
8
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
9
+ aws "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/crowdmob/goamz/aws"
10
+ s3 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/crowdmob/goamz/s3"
11
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
12
+ syncds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
13
+ core "github.com/jbenet/go-ipfs/core"
14
+ corehttp "github.com/jbenet/go-ipfs/core/corehttp"
15
+ "github.com/jbenet/go-ipfs/core/corerouting"
16
+ config "github.com/jbenet/go-ipfs/repo/config"
17
+ fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
18
+ s3datastore "github.com/jbenet/go-ipfs/thirdparty/s3-datastore"
19
+ ds2 "github.com/jbenet/go-ipfs/util/datastore2"
20
+)
21
+
22
+var (
23
+ host = flag.String("host", "/ip4/0.0.0.0/tcp/8080", "override the HTTP host listening address")
24
+ s3bucket = flag.String("aws-bucket", "", "S3 bucket for routing datastore")
25
+ s3region = flag.String("aws-region", aws.USWest2.Name, "S3 region")
26
+ nBitsForKeypair = flag.Int("b", 1024, "number of bits for keypair (if repo is uninitialized)")
27
+)
28
+
29
+func main() {
30
+ flag.Parse()
31
+ if *s3bucket == "" {
32
+ log.Fatal("bucket is required")
33
+ }
34
+ if err := run(); err != nil {
35
+ log.Println(err)
36
+ }
37
+}
38
+
39
+func run() error {
40
+ ctx, cancel := context.WithCancel(context.Background())
41
+ defer cancel()
42
+ repoPath, err := fsrepo.BestKnownPath()
43
+ if err != nil {
44
+ return err
45
+ }
46
+
47
+ if !fsrepo.IsInitialized(repoPath) {
48
+ conf, err := config.Init(os.Stdout, *nBitsForKeypair)
49
+ if err != nil {
50
+ return err
51
+ }
52
+ if err := fsrepo.Init(repoPath, conf); err != nil {
53
+ return err
54
+ }
55
+ }
56
+ repo := fsrepo.At(repoPath)
57
+ if err := repo.Open(); err != nil { // owned by node
58
+ return err
59
+ }
60
+ s3, err := makeS3Datastore()
61
+ if err != nil {
62
+ return err
63
+ }
64
+ enhanced, err := enhanceDatastore(s3)
65
+ if err != nil {
66
+ return err
67
+ }
68
+ node, err := core.NewIPFSNode(ctx,
69
+ core.OnlineWithOptions(
70
+ repo,
71
+ corerouting.GrandCentralServer(enhanced),
72
+ core.DefaultHostOption),
73
+ )
74
+ if err != nil {
75
+ return err
76
+ }
77
+ defer node.Close()
78
+
79
+ opts := []corehttp.ServeOption{}
80
+ return corehttp.ListenAndServe(node, *host, opts...) // TODO rm
81
+}
82
+
83
+func makeS3Datastore() (*s3datastore.S3Datastore, error) {
84
+
85
+ // FIXME get ENV through flags?
86
+
87
+ auth, err := aws.EnvAuth()
88
+ if err != nil {
89
+ return nil, err
90
+ }
91
+
92
+ s3c := s3.New(auth, aws.Regions[*s3region])
93
+ b := s3c.Bucket(*s3bucket)
94
+ exists, err := b.Exists("initialized") // TODO lazily instantiate
95
+ if err != nil {
96
+ return nil, err
97
+ }
98
+
99
+ if !exists {
100
+ if err := b.PutBucket(s3.PublicRead); err != nil {
101
+ switch e := err.(type) {
102
+ case *s3.Error:
103
+ log.Println(e.Code)
104
+ default:
105
+ return nil, err
106
+ }
107
+ }
108
+
109
+ // TODO create the initial value
110
+ }
111
+
112
+ return &s3datastore.S3Datastore{
113
+ Bucket: *s3bucket,
114
+ Client: s3c,
115
+ }, nil
116
+}
117
+
118
+func enhanceDatastore(d datastore.Datastore) (datastore.ThreadSafeDatastore, error) {
119
+ // TODO cache
120
+ return ds2.CloserWrap(syncds.MutexWrap(d)), nil
121
+}