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)
|
Command line flags features and cleanup (#2068)
* change BindSetup to be an option to Bind
* add process.Bind to allow composite structures
* hack fix for noprefix flags
* used tagged version of structs
Before this PR, some flags were created by calling `cfgstruct.Bind` and having their fields create a flag. Once the flags were parsed, `viper` was used to acquire all the values from them and config files, and the fields in the struct were set through the flag interface.
This doesn't work for slices of things on config structs very well, since it can only set strings, and for a string slice, it turns out that the implementation in `pflag` appends an entry rather than setting it.
This changes three things:
1. Only have a `Bind` call instead of `Bind` and `BindSetup`, and make `BindSetup` an option instead.
2. Add a `process.Bind` call that takes in a `*cobra.Cmd`, binds the struct to the command's flags, and keeps track of that struct in a global map keyed by the command.
3. Use `viper` to get the values and load them into the bound configuration structs instead of using the flags to propagate the changes.
In this way, we can support whatever rich configuration we want in the config yaml files, while still getting command like flags when important.
2019-05-29 18:56:22 +01:00
|
|
|
process.Bind(addCmd, &cacheCfg, defaults)
|
|
|
|
process.Bind(listCmd, &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)
|
|
|
|
}
|