storj/cmd/overlay/main.go
JT Olio 2744a26b60
pkg/cfgstruct: tie defaults to releases (#1787)
* tie defaults to releases

this change makes it so that by default, the flag defaults are
chosen based on whether the build was built as a release build or
an ordinary build. release builds by default get release defaults,
whereas ordinary builds by default get dev defaults.

any binary can have its defaults changed by specifying

 --defaults=dev

or

 --defaults=release

Change-Id: I6d216aa345d211c69ad913159d492fac77b12c64

* make release defaults more clear

this change extends cfgstruct structs to support either
a 'default' tag, or a pair of 'devDefault' and 'releaseDefault'
tags, but not both, for added clarity

Change-Id: Ia098be1fa84b932fdfe90a4a4d027ffb95e249c6

* clarify cfgstruct.DefaultsFlag

Change-Id: I55f2ff9080ebbc0ce83abf956e085242a92f883e
2019-04-19 12:17:30 -06:00

135 lines
2.6 KiB
Go

// 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
}
)
func init() {
defaults := cfgstruct.DefaultsFlag(rootCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(listCmd)
cfgstruct.Bind(addCmd.Flags(), &cacheCfg, defaults)
cfgstruct.Bind(listCmd.Flags(), &cacheCfg, defaults)
}
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)
}