Don't create identity when running dash (#1098)

* Don't create identity when running dash

* only use configs for connecting with dash, delete unused client example

* use server address
This commit is contained in:
Alexander Leitner 2019-01-18 12:08:45 -05:00 committed by Stefan Benten
parent ecd704692e
commit aabcceef52
3 changed files with 33 additions and 285 deletions

View File

@ -31,6 +31,7 @@ import (
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/server"
"storj.io/storj/pkg/storj"
"storj.io/storj/pkg/transport"
)
// StorageNode defines storage node configuration
@ -75,8 +76,9 @@ var (
Short: "Display a dashbaord",
RunE: dashCmd,
}
runCfg StorageNode
setupCfg StorageNode
runCfg StorageNode
setupCfg StorageNode
dashboardCfg struct {
Address string `default:":28967" help:"address for dashboard service"`
}
@ -122,7 +124,7 @@ func init() {
cfgstruct.BindSetup(setupCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir))
cfgstruct.BindSetup(configCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir))
cfgstruct.Bind(diagCmd.Flags(), &diagCfg, cfgstruct.ConfDir(defaultDiagDir))
cfgstruct.Bind(dashboardCmd.Flags(), &dashboardCfg)
cfgstruct.Bind(dashboardCmd.Flags(), &dashboardCfg, cfgstruct.ConfDir(defaultDiagDir))
}
func cmdRun(cmd *cobra.Command, args []string) (err error) {
@ -311,8 +313,34 @@ func cmdDiag(cmd *cobra.Command, args []string) (err error) {
func dashCmd(cmd *cobra.Command, args []string) (err error) {
ctx := context.Background()
ident, err := runCfg.Server.Identity.Load()
if err != nil {
zap.S().Fatal(err)
} else {
zap.S().Info("Node ID: ", ident.ID)
}
// address of node to create client connection
address := dashboardCfg.Address
if dashboardCfg.Address == "" {
if runCfg.Server.Address == "" {
return fmt.Errorf("Storage Node address isn't specified")
}
address = runCfg.Server.Address
}
tc := transport.NewClient(ident)
n := &pb.Node{
Address: &pb.NodeAddress{
Address: address,
Transport: 0,
},
Type: pb.NodeType_STORAGE,
}
// create new client
lc, err := psclient.NewLiteClient(ctx, dashboardCfg.Address)
lc, err := psclient.NewLiteClient(ctx, tc, n)
if err != nil {
return err
}

View File

@ -1,260 +0,0 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"time"
"github.com/gogo/protobuf/proto"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/piecestore/psclient"
"storj.io/storj/pkg/provider"
"storj.io/storj/pkg/transport"
)
var ctx = context.Background()
var argError = errs.Class("argError")
func main() {
cobra.EnableCommandSorting = false
clientIdent, err := provider.NewFullIdentity(ctx, 12, 4)
if err != nil {
log.Fatal(err)
}
serverIdent, err := provider.NewFullIdentity(ctx, 12, 4)
if err != nil {
log.Fatal(err)
}
// Set up connection with rpc server
n := &pb.Node{
Address: &pb.NodeAddress{
Address: ":7777",
Transport: 0,
},
Id: serverIdent.ID,
Type: pb.NodeType_STORAGE,
}
tc := transport.NewClient(clientIdent)
// create a new liteclient for dashboard on localhost port 7777
liteClient, err := psclient.NewLiteClient(ctx, ":7777")
if err != nil {
log.Fatalf("could not initialize lite client: %s", err)
}
psClient, err := psclient.NewPSClient(ctx, tc, n, 0)
if err != nil {
log.Fatalf("could not initialize Client: %s", err)
}
defer printError(psClient.Close)
root := &cobra.Command{
Use: "piecestore-client",
Short: "piecestore example client",
}
root.AddCommand(&cobra.Command{
Use: "upload [input-file]",
Short: "Upload data",
Aliases: []string{"u"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inputfile := args[0]
file, err := os.Open(inputfile)
if err != nil {
return err
}
// Close the file when we are done
defer printError(file.Close)
fileInfo, err := file.Stat()
if err != nil {
return err
}
if fileInfo.IsDir() {
return argError.New(fmt.Sprintf("path (%s) is a directory, not a file", inputfile))
}
satelliteIdent, err := provider.NewFullIdentity(ctx, 12, 4)
if err != nil {
return err
}
var length = fileInfo.Size()
var ttl = time.Now().Add(24 * time.Hour)
// Created a section reader so that we can concurrently retrieve the same file.
dataSection := io.NewSectionReader(file, 0, length)
id := psclient.NewPieceID()
allocationData := &pb.PayerBandwidthAllocation_Data{
SatelliteId: satelliteIdent.ID,
Action: pb.PayerBandwidthAllocation_PUT,
CreatedUnixSec: time.Now().Unix(),
}
serializedAllocation, err := proto.Marshal(allocationData)
if err != nil {
return err
}
pba := &pb.PayerBandwidthAllocation{
Data: serializedAllocation,
}
if err := psClient.Put(context.Background(), id, dataSection, ttl, pba, nil); err != nil {
fmt.Printf("Failed to Store data of id: %s\n", id)
return err
}
fmt.Printf("Successfully stored file of id: %s\n", id)
return nil
},
})
root.AddCommand(&cobra.Command{
Use: "download [id] [output-dir]",
Short: "Download data",
Aliases: []string{"d"},
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
id := args[0]
outputDir := args[1]
_, err := os.Stat(outputDir)
if err != nil && !os.IsNotExist(err) {
return err
}
if err == nil {
return argError.New("File already exists")
}
if err = os.MkdirAll(filepath.Dir(outputDir), 0700); err != nil {
return err
}
// Create File on file system
dataFile, err := os.OpenFile(outputDir, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer printError(dataFile.Close)
pieceInfo, err := psClient.Meta(context.Background(), psclient.PieceID(id))
if err != nil {
errRemove := os.Remove(outputDir)
if errRemove != nil {
log.Println(errRemove)
}
return err
}
satelliteIdent, err := provider.NewFullIdentity(ctx, 12, 4)
if err != nil {
return err
}
allocationData := &pb.PayerBandwidthAllocation_Data{
SatelliteId: satelliteIdent.ID,
Action: pb.PayerBandwidthAllocation_GET,
CreatedUnixSec: time.Now().Unix(),
}
serializedAllocation, err := proto.Marshal(allocationData)
if err != nil {
return err
}
pba := &pb.PayerBandwidthAllocation{
Data: serializedAllocation,
}
rr, err := psClient.Get(ctx, psclient.PieceID(id), pieceInfo.PieceSize, pba, nil)
if err != nil {
fmt.Printf("Failed to retrieve file of id: %s\n", id)
errRemove := os.Remove(outputDir)
if errRemove != nil {
log.Println(errRemove)
}
return err
}
reader, err := rr.Range(ctx, 0, pieceInfo.PieceSize)
if err != nil {
fmt.Printf("Failed to retrieve file of id: %s\n", id)
errRemove := os.Remove(outputDir)
if errRemove != nil {
log.Println(errRemove)
}
return err
}
_, err = io.Copy(dataFile, reader)
if err != nil {
fmt.Printf("Failed to retrieve file of id: %s\n", id)
errRemove := os.Remove(outputDir)
if errRemove != nil {
log.Println(errRemove)
}
} else {
fmt.Printf("Successfully retrieved file of id: %s\n", id)
}
return reader.Close()
},
})
root.AddCommand(&cobra.Command{
Use: "delete [id]",
Short: "Delete data",
Args: cobra.ExactArgs(1),
Aliases: []string{"x"},
RunE: func(cmd *cobra.Command, args []string) error {
id := args[0]
return psClient.Delete(context.Background(), psclient.PieceID(id), nil)
},
})
root.AddCommand(&cobra.Command{
Use: "stat",
Aliases: []string{"s"},
Short: "Retrieve statistics",
RunE: func(cmd *cobra.Command, args []string) error {
var summary *pb.StatSummary
summary, err := liteClient.Stats(context.Background())
if err != nil {
return err
}
log.Printf("Space Used: %v, Space Available: %v\nBandwidth Available: %v, Bandwidth Used: %v\n", summary.GetUsedSpace(), summary.GetAvailableSpace(), summary.GetAvailableBandwidth(), summary.GetUsedBandwidth())
return nil
},
})
if err := root.Execute(); err != nil {
log.Fatal(err)
}
}
func printError(fn func() error) {
err := fn()
if err != nil {
fmt.Println(err)
}
}

View File

@ -7,7 +7,6 @@ import (
"context"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/provider"
"storj.io/storj/pkg/transport"
)
@ -33,26 +32,7 @@ func (psl *PieceStoreLite) Stats(ctx context.Context) (*pb.StatSummary, error) {
}
// NewLiteClient returns a new LiteClient
func NewLiteClient(ctx context.Context, addr string) (LiteClient, error) {
clientIdent, err := provider.NewFullIdentity(ctx, 12, 4)
if err != nil {
return nil, err
}
// address of node to create client connection
if addr == "" {
addr = ":7777"
}
tc := transport.NewClient(clientIdent)
n := &pb.Node{
Address: &pb.NodeAddress{
Address: addr,
Transport: 0,
},
Type: pb.NodeType_STORAGE,
}
func NewLiteClient(ctx context.Context, tc transport.Client, n *pb.Node) (LiteClient, error) {
conn, err := tc.DialNode(ctx, n)
if err != nil {
return nil, err