2018-10-02 15:47:39 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2018-10-30 19:03:41 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-10-02 15:47:39 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-06 17:49:17 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psclient"
|
2018-10-02 15:47:39 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-11-06 17:49:17 +00:00
|
|
|
"storj.io/storj/pkg/transport"
|
2018-10-02 15:47:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var ctx = context.Background()
|
|
|
|
var argError = errs.Class("argError")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cobra.EnableCommandSorting = false
|
|
|
|
|
2018-11-01 15:48:43 +00:00
|
|
|
ca, err := provider.NewTestCA(ctx)
|
2018-10-02 15:47:39 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
identity, err := ca.NewIdentity()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up connection with rpc server
|
2018-11-06 17:49:17 +00:00
|
|
|
n := &pb.Node{
|
2018-11-29 14:57:00 +00:00
|
|
|
// TODO: NodeType is missing
|
2018-11-06 17:49:17 +00:00
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: ":7777",
|
|
|
|
Transport: 0,
|
|
|
|
},
|
|
|
|
Id: "test-node-id-1234567",
|
2018-10-02 15:47:39 +01:00
|
|
|
}
|
2018-11-06 17:49:17 +00:00
|
|
|
tc := transport.NewClient(identity)
|
|
|
|
psClient, err := psclient.NewPSClient(ctx, tc, n, 0)
|
2018-10-02 15:47:39 +01:00
|
|
|
if err != nil {
|
2018-11-06 17:49:17 +00:00
|
|
|
log.Fatalf("could not initialize Client: %s", err)
|
2018-10-02 15:47:39 +01:00
|
|
|
}
|
2018-11-06 17:49:17 +00:00
|
|
|
defer printError(psClient.Close)
|
2018-10-02 15:47:39 +01:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
id := psclient.NewPieceID()
|
2018-10-02 15:47:39 +01:00
|
|
|
|
2018-10-30 19:03:41 +00:00
|
|
|
allocationData := &pb.PayerBandwidthAllocation_Data{
|
2018-11-29 14:57:00 +00:00
|
|
|
SatelliteId: []byte("OhHeyThisIsAnUnrealFakeSatellite"),
|
2018-11-08 16:04:52 +00:00
|
|
|
Action: pb.PayerBandwidthAllocation_PUT,
|
|
|
|
CreatedUnixSec: time.Now().Unix(),
|
2018-10-30 19:03:41 +00:00
|
|
|
}
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2018-10-30 19:03:41 +00:00
|
|
|
serializedAllocation, err := proto.Marshal(allocationData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2018-10-30 19:03:41 +00:00
|
|
|
pba := &pb.PayerBandwidthAllocation{
|
|
|
|
Data: serializedAllocation,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := psClient.Put(context.Background(), id, dataSection, ttl, pba, nil); err != nil {
|
2018-10-02 15:47:39 +01:00
|
|
|
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)
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
pieceInfo, err := psClient.Meta(context.Background(), psclient.PieceID(id))
|
2018-10-02 15:47:39 +01:00
|
|
|
if err != nil {
|
|
|
|
errRemove := os.Remove(outputDir)
|
|
|
|
if errRemove != nil {
|
|
|
|
log.Println(errRemove)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-30 19:03:41 +00:00
|
|
|
allocationData := &pb.PayerBandwidthAllocation_Data{
|
2018-11-29 14:57:00 +00:00
|
|
|
SatelliteId: []byte("OhHeyThisIsAnUnrealFakeSatellite"),
|
2018-11-08 16:04:52 +00:00
|
|
|
Action: pb.PayerBandwidthAllocation_GET,
|
|
|
|
CreatedUnixSec: time.Now().Unix(),
|
2018-10-30 19:03:41 +00:00
|
|
|
}
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2018-10-30 19:03:41 +00:00
|
|
|
serializedAllocation, err := proto.Marshal(allocationData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2018-10-30 19:03:41 +00:00
|
|
|
pba := &pb.PayerBandwidthAllocation{
|
|
|
|
Data: serializedAllocation,
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:35 +00:00
|
|
|
rr, err := psClient.Get(ctx, psclient.PieceID(id), pieceInfo.PieceSize, pba, nil)
|
2018-10-02 15:47:39 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:35 +00:00
|
|
|
reader, err := rr.Range(ctx, 0, pieceInfo.PieceSize)
|
2018-10-02 15:47:39 +01:00
|
|
|
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]
|
2018-11-06 17:49:17 +00:00
|
|
|
return psClient.Delete(context.Background(), psclient.PieceID(id), nil)
|
2018-10-02 15:47:39 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
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 := psClient.Stats(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:59:05 +01:00
|
|
|
log.Printf("Space Used: %v, Space Available: %v\nBandwidth Available: %v, Bandwidth Used: %v\n", summary.GetUsedSpace(), summary.GetAvailableSpace(), summary.GetAvailableBandwidth(), summary.GetUsedBandwidth())
|
2018-10-02 15:47:39 +01:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := root.Execute(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printError(fn func() error) {
|
|
|
|
err := fn()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|