2018-08-20 19:21:41 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
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"
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
|
2018-08-20 19:21:41 +01:00
|
|
|
"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-08-20 19:21:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-08-27 18:28:16 +01:00
|
|
|
// Error is the error class for overlays
|
2018-08-20 19:21:41 +01:00
|
|
|
Error = errs.Class("overlay error")
|
|
|
|
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() {
|
|
|
|
rootCmd.AddCommand(addCmd)
|
|
|
|
rootCmd.AddCommand(listCmd)
|
|
|
|
cfgstruct.Bind(addCmd.Flags(), &cacheCfg)
|
|
|
|
cfgstruct.Bind(listCmd.Flags(), &cacheCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdList(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
c, err := cacheCfg.open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err := c.DB.List(nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeIDs, err := storj.NodeIDsFromBytes(keys.ByteSlices())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, id := range nodeIDs {
|
|
|
|
n, err := c.Get(process.Ctx(cmd), id)
|
2018-08-20 19:21:41 +01:00
|
|
|
if err != nil {
|
2018-11-29 18:39:27 +00:00
|
|
|
zap.S().Infof("ID: %s; error getting value\n", id.String())
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
if n != nil {
|
2018-11-29 18:39:27 +00:00
|
|
|
zap.S().Infof("ID: %s; Address: %s\n", id.String(), n.Address.Address)
|
2018-08-20 19:21:41 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
zap.S().Infof("ID: %s: nil\n", id.String())
|
2018-08-20 19:21:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdAdd(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
j, err := ioutil.ReadFile(cacheCfg.NodesPath)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodes map[string]string
|
|
|
|
if err := json.Unmarshal(j, &nodes); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := cacheCfg.open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-20 19:21:41 +01:00
|
|
|
zap.S().Infof("adding node ID: %s; Address: %s", i, a)
|
2018-11-29 18:39:27 +00:00
|
|
|
err = c.Put(id, pb.Node{
|
|
|
|
Id: id,
|
2018-11-29 14:57:00 +00:00
|
|
|
// TODO: NodeType is missing
|
2018-09-18 05:39:06 +01:00
|
|
|
Address: &pb.NodeAddress{
|
2018-08-20 19:21:41 +01:00
|
|
|
Transport: 0,
|
|
|
|
Address: a,
|
|
|
|
},
|
2018-09-18 05:39:06 +01:00
|
|
|
Restrictions: &pb.NodeRestrictions{
|
2018-08-27 18:28:16 +01:00
|
|
|
FreeBandwidth: 2000000000,
|
|
|
|
FreeDisk: 2000000000,
|
|
|
|
},
|
2018-08-20 19:21:41 +01:00
|
|
|
Type: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
process.Exec(rootCmd)
|
|
|
|
}
|