-
Notifications
You must be signed in to change notification settings - Fork 397
/
main.go
135 lines (117 loc) · 2.65 KB
/
main.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/storj"
)
var (
rootCmd = &cobra.Command{
Use: "overlay",
Short: "Overlay cache management",
}
addCmd = &cobra.Command{
Use: "add",
Short: "Add nodes to the overlay cache",
RunE: cmdAdd,
}
listCmd = &cobra.Command{
Use: "list",
Short: "List nodes in the overlay cache",
RunE: cmdList,
}
cacheCfg struct {
cacheConfig
}
isDev bool
)
func init() {
cfgstruct.DevFlag(rootCmd, &isDev, false, "use development and test configuration settings")
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(listCmd)
cfgstruct.Bind(addCmd.Flags(), &cacheCfg, isDev)
cfgstruct.Bind(listCmd.Flags(), &cacheCfg, isDev)
}
func cmdList(cmd *cobra.Command, args []string) (err error) {
ctx := process.Ctx(cmd)
cache, dbClose, err := cacheCfg.open(ctx)
if err != nil {
return err
}
defer dbClose()
keys, err := cache.Inspect(ctx)
if err != nil {
return err
}
nodeIDs, err := storj.NodeIDsFromBytes(keys.ByteSlices())
if err != nil {
return err
}
const padding = 3
w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', tabwriter.Debug)
fmt.Fprintln(w, "Node ID\t Address")
for _, id := range nodeIDs {
n, err := cache.Get(process.Ctx(cmd), id)
if err != nil {
fmt.Fprintln(w, id.String(), "\t", "error getting value")
}
if n != nil {
fmt.Fprintln(w, id.String(), "\t", n.Address.Address)
continue
}
fmt.Fprintln(w, id.String(), "\tnil")
}
return w.Flush()
}
func cmdAdd(cmd *cobra.Command, args []string) (err error) {
ctx := process.Ctx(cmd)
j, err := ioutil.ReadFile(cacheCfg.NodesPath)
if err != nil {
return errs.New("Unable to read file with nodes: %+v", err)
}
var nodes map[string]string
if err := json.Unmarshal(j, &nodes); err != nil {
return errs.Wrap(err)
}
cache, dbClose, err := cacheCfg.open(ctx)
if err != nil {
return err
}
defer dbClose()
for i, a := range nodes {
id, err := storj.NodeIDFromString(i)
if err != nil {
zap.S().Error(err)
}
fmt.Printf("adding node ID: %s; Address: %s", i, a)
err = cache.Put(process.Ctx(cmd), id, pb.Node{
Id: id,
Address: &pb.NodeAddress{
Transport: 0,
Address: a,
},
Restrictions: &pb.NodeRestrictions{
FreeBandwidth: 2000000000,
FreeDisk: 2000000000,
},
Type: pb.NodeType_STORAGE,
})
if err != nil {
return err
}
}
return nil
}
func main() {
process.Exec(rootCmd)
}