2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-08-20 19:21:41 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-12-18 20:41:31 +00:00
|
|
|
"fmt"
|
2018-08-20 19:21:41 +01:00
|
|
|
"io/ioutil"
|
2018-12-18 20:41:31 +00:00
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
2018-08-27 18:28:16 +01:00
|
|
|
|
2018-08-20 19:21:41 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-10-11 21:25:54 +01:00
|
|
|
"storj.io/storj/pkg/process"
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-08-20 19:21:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-04-19 19:17:30 +01:00
|
|
|
defaults := cfgstruct.DefaultsFlag(rootCmd)
|
2018-08-20 19:21:41 +01:00
|
|
|
rootCmd.AddCommand(addCmd)
|
|
|
|
rootCmd.AddCommand(listCmd)
|
2019-04-19 19:17:30 +01:00
|
|
|
cfgstruct.Bind(addCmd.Flags(), &cacheCfg, defaults)
|
|
|
|
cfgstruct.Bind(listCmd.Flags(), &cacheCfg, defaults)
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdList(cmd *cobra.Command, args []string) (err error) {
|
2018-12-14 20:17:30 +00:00
|
|
|
ctx := process.Ctx(cmd)
|
2018-12-20 13:57:54 +00:00
|
|
|
cache, dbClose, err := cacheCfg.open(ctx)
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-18 20:41:31 +00:00
|
|
|
defer dbClose()
|
2018-08-20 19:21:41 +01:00
|
|
|
|
2018-12-20 13:57:54 +00:00
|
|
|
keys, err := cache.Inspect(ctx)
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeIDs, err := storj.NodeIDsFromBytes(keys.ByteSlices())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-18 20:41:31 +00:00
|
|
|
|
|
|
|
const padding = 3
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', tabwriter.Debug)
|
|
|
|
fmt.Fprintln(w, "Node ID\t Address")
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
for _, id := range nodeIDs {
|
2018-12-20 13:57:54 +00:00
|
|
|
n, err := cache.Get(process.Ctx(cmd), id)
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
2018-12-18 20:41:31 +00:00
|
|
|
fmt.Fprintln(w, id.String(), "\t", "error getting value")
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
if n != nil {
|
2018-12-18 20:41:31 +00:00
|
|
|
fmt.Fprintln(w, id.String(), "\t", n.Address.Address)
|
2018-08-20 19:21:41 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-12-18 20:41:31 +00:00
|
|
|
fmt.Fprintln(w, id.String(), "\tnil")
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 20:41:31 +00:00
|
|
|
return w.Flush()
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdAdd(cmd *cobra.Command, args []string) (err error) {
|
2018-12-14 20:17:30 +00:00
|
|
|
ctx := process.Ctx(cmd)
|
2018-08-20 19:21:41 +01:00
|
|
|
j, err := ioutil.ReadFile(cacheCfg.NodesPath)
|
|
|
|
if err != nil {
|
2018-12-18 20:41:31 +00:00
|
|
|
return errs.New("Unable to read file with nodes: %+v", err)
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var nodes map[string]string
|
|
|
|
if err := json.Unmarshal(j, &nodes); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:57:54 +00:00
|
|
|
cache, dbClose, err := cacheCfg.open(ctx)
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-18 20:41:31 +00:00
|
|
|
defer dbClose()
|
2018-08-20 19:21:41 +01:00
|
|
|
|
|
|
|
for i, a := range nodes {
|
2018-11-29 18:39:27 +00:00
|
|
|
id, err := storj.NodeIDFromString(i)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Error(err)
|
|
|
|
}
|
2018-12-18 20:41:31 +00:00
|
|
|
fmt.Printf("adding node ID: %s; Address: %s", i, a)
|
2018-12-20 13:57:54 +00:00
|
|
|
err = cache.Put(process.Ctx(cmd), id, pb.Node{
|
2018-11-29 18:39:27 +00:00
|
|
|
Id: id,
|
2018-09-18 05:39:06 +01:00
|
|
|
Address: &pb.NodeAddress{
|
2018-08-20 19:21:41 +01:00
|
|
|
Transport: 0,
|
|
|
|
Address: a,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
process.Exec(rootCmd)
|
|
|
|
}
|