Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #4910 venus-market,venus : add sub command to mannage piece storage #153

Merged
merged 23 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ build: $(BUILD_DEPS)
go build -o ./venus-market $(GOFLAGS) ./cmd/venus-market


# docker
# docker
.PHONY: docker

docker:
Expand Down
71 changes: 71 additions & 0 deletions api/impl/venus_market.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package impl

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/filecoin-project/go-state-types/abi"

clients2 "github.com/filecoin-project/venus-market/v2/api/clients"
"github.com/filecoin-project/venus-market/v2/cli/tablewriter"
"github.com/filecoin-project/venus-market/v2/config"
"github.com/filecoin-project/venus-market/v2/minermgr"
"github.com/filecoin-project/venus-market/v2/models/repo"
Expand Down Expand Up @@ -66,6 +68,7 @@ type MarketNodeImpl struct {
MinerMgr minermgr.IAddrMgr
PaychAPI *paychmgr.PaychAPI
Repo repo.Repo
Config *config.MarketConfig
ConsiderOnlineStorageDealsConfigFunc config.ConsiderOnlineStorageDealsConfigFunc
SetConsiderOnlineStorageDealsConfigFunc config.SetConsiderOnlineStorageDealsConfigFunc
ConsiderOnlineRetrievalDealsConfigFunc config.ConsiderOnlineRetrievalDealsConfigFunc
Expand Down Expand Up @@ -825,3 +828,71 @@ func (m MarketNodeImpl) GetReadUrl(ctx context.Context, s string) (string, error
func (m MarketNodeImpl) GetWriteUrl(ctx context.Context, s2 string) (string, error) {
panic("not support")
}

func (m MarketNodeImpl) AddFsPieceStorage(ctx context.Context, readonly bool, path string, name string) error {
ifs := &config.FsPieceStorage{ReadOnly: readonly, Path: path, Name: name}
fsps, err := piecestorage.NewFsPieceStorage(ifs)
if err != nil {
return err
}
// add in memory
m.PieceStorageMgr.AddPieceStorage(fsps)

// add to config
return m.Config.AddFsPieceStorage(ifs)
}

func (m MarketNodeImpl) AddS3PieceStorage(ctx context.Context, readonly bool, endpoit string, name string, accessKeyID string, secretAccessKey string, token string) error {
ifs := &config.S3PieceStorage{ReadOnly: readonly, EndPoint: endpoit, Name: name, AccessKey: accessKeyID, SecretKey: secretAccessKey, Token: token}
s3ps, err := piecestorage.NewS3PieceStorage(ifs)
if err != nil {
return err
}
// add in memory
m.PieceStorageMgr.AddPieceStorage(s3ps)

// add to config
return m.Config.AddS3PieceStorage(ifs)
}

func (m MarketNodeImpl) ListPieceStorage(ctx context.Context) (string, error) {
LinZexiao marked this conversation as resolved.
Show resolved Hide resolved
storages := m.PieceStorageMgr.GetStorages()

w := tablewriter.New(tablewriter.Col("index"),
tablewriter.Col("name"),
tablewriter.Col("readonly"),
tablewriter.Col("type"),
)

for idx, storage := range storages {

w.Write(map[string]interface{}{
"index": idx,
"name": storage.GetName(),
"readonly": storage.ReadOnly(),
"type": storage.Type(),
})
}

buf := new(bytes.Buffer)
err := w.Flush(buf)
if err != nil {
return "", err
}
return buf.String(), nil
}

func (m MarketNodeImpl) GetPieceStorages(ctx context.Context) types.PieceStorageList {
return m.PieceStorageMgr.ListStorages()
}

func (m MarketNodeImpl) RemovePieceStorage(ctx context.Context, name string) error {

hunjixin marked this conversation as resolved.
Show resolved Hide resolved
err := m.PieceStorageMgr.RemovePieceStorage(name)
if err != nil {
return err
}

hunjixin marked this conversation as resolved.
Show resolved Hide resolved
return m.Config.RemovePieceStorage(name)

}
218 changes: 218 additions & 0 deletions cli/piece-storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package cli

import (
"fmt"
"os"

"github.com/filecoin-project/venus-market/v2/cli/tablewriter"
"github.com/urfave/cli/v2"
)

var PieceStorageCmd = &cli.Command{
Name: "piece-storage",
Usage: "Manage piece storage ",
Description: "The piece storage will decide where to store pieces and how to store them",
Subcommands: []*cli.Command{
addFsPieceStorageCmd,
addS3PieceStorageCmd,
PieceStorageListCmd,
LinZexiao marked this conversation as resolved.
Show resolved Hide resolved
PieceStorageRemoveCmd,
},
}

var addFsPieceStorageCmd = &cli.Command{
Name: "add-fs",
Usage: "add a local filesystem piece storage",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Usage: "path to the filesystem piece storage",
},
&cli.BoolFlag{
Name: "read-only",
Aliases: []string{"r"},
Usage: "read-only filesystem piece storage",
DefaultText: "false",
},
// name
LinZexiao marked this conversation as resolved.
Show resolved Hide resolved
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "name of the filesystem piece storage",
},
},
Action: func(cctx *cli.Context) error {
nodeApi, closer, err := NewMarketNode(cctx)
if err != nil {
return err
}
defer closer()

ctx := ReqContext(cctx)
path := cctx.String("path")
readOnly := cctx.Bool("read-only")
name := cctx.String("name")

if path == "" {
LinZexiao marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("path is required")
}
if name == "" {
return fmt.Errorf("name is required")
}

err = nodeApi.AddFsPieceStorage(ctx, readOnly, path, name)
if err != nil {
return err
}
fmt.Println("Adding filesystem piece storage:", path)

return nil
},
}

var addS3PieceStorageCmd = &cli.Command{
Name: "add-s3",
Usage: "add a object storage for piece storage",
Flags: []cli.Flag{
// read only
&cli.BoolFlag{
Name: "readonly",
Aliases: []string{"r"},
Usage: "set true if you want the piece storage only fro reading",
DefaultText: "false",
},
// Endpoint
&cli.StringFlag{
Name: "endpoint",
Aliases: []string{"e"},
Usage: "endpoint of the S3 bucket",
},
// access key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下面三个量最好使用交互式的输入方式,避免在history里面留下记录

&cli.StringFlag{
Name: "access-key",
Aliases: []string{"a"},
Usage: "access key of the S3 bucket",
},
// secret key
&cli.StringFlag{
Name: "secret-key",
Aliases: []string{"s"},
Usage: "secret key of the S3 bucket",
},
// token
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Usage: "token of the S3 bucket",
},
// name
LinZexiao marked this conversation as resolved.
Show resolved Hide resolved
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "name of the S3 bucket",
},
},
Action: func(cctx *cli.Context) error {
nodeApi, closer, err := NewMarketNode(cctx)
if err != nil {
return err
}
defer closer()

ctx := ReqContext(cctx)

readOnly := cctx.Bool("readonly")
endpoint := cctx.String("endpoint")
accessKey := cctx.String("access-key")
secretKey := cctx.String("secret-key")
token := cctx.String("token")
name := cctx.String("name")

if endpoint == "" {
return fmt.Errorf("endpoint are required")
}
if accessKey == "" {
return fmt.Errorf("access key are required")
}
if secretKey == "" {
return fmt.Errorf("secret key are required")
}
if name == "" {
return fmt.Errorf("name are required")
}

err = nodeApi.AddS3PieceStorage(ctx, readOnly, endpoint, name, accessKey, secretKey, token)
if err != nil {
return err
}
fmt.Println("Adding S3 piece storage:", endpoint)

return nil
},
}

var PieceStorageListCmd = &cli.Command{
Name: "list",
Usage: "list piece storages",
Action: func(cctx *cli.Context) error {
nodeApi, closer, err := NewMarketNode(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)

storagelist := nodeApi.GetPieceStorages(ctx)

w := tablewriter.New(
tablewriter.Col("name"),
tablewriter.Col("readonly"),
tablewriter.Col("path/enter point"),
tablewriter.Col("type"),
)

for _, storage := range storagelist.FsStorage {

w.Write(map[string]interface{}{
"Name": storage.Name,
"Readonly": storage.ReadOnly,
"Path or Enter point": storage.Path,
"Type": "file system",
})
}

for _, storage := range storagelist.S3Storage {
w.Write(map[string]interface{}{
"Name": storage.Name,
"Readonly": storage.ReadOnly,
"Path or Enter point": storage.EndPoint,
"Type": "S3",
})
}

w.Flush(os.Stdout)

return nil
},
}

var PieceStorageRemoveCmd = &cli.Command{
Name: "remove",
Usage: "remove a piece storage",
Action: func(cctx *cli.Context) error {
// get idx
name := cctx.Args().Get(0)
LinZexiao marked this conversation as resolved.
Show resolved Hide resolved
if name == "" {
return fmt.Errorf("piece storage name is required")
}

nodeApi, closer, err := NewMarketNode(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
return nodeApi.RemovePieceStorage(ctx, name)
},
}
1 change: 1 addition & 0 deletions cmd/venus-market/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func main() {
cli2.DataTransfersCmd,
cli2.DagstoreCmd,
cli2.MigrateCmd,
cli2.PieceStorageCmd,
},
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/venus-market/pool-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func poolDaemon(cctx *cli.Context) error {
builder.Override(new(metrics.MetricsCtx), func() context.Context {
return metrics2.CtxScope(context.Background(), "venus-market")
}),
// override marketconfig
builder.Override(new(config.MarketConfig), cfg),
builder.Override(new(types2.ShutdownChan), shutdownChan),
//config
config.ConfigServerOpts(cfg),
Expand Down
4 changes: 4 additions & 0 deletions cmd/venus-market/solo-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func soloDaemon(cctx *cli.Context) error {
return metrics2.CtxScope(context.Background(), "venus-market")
}),
builder.Override(new(types2.ShutdownChan), shutdownChan),

// override marketconfig
builder.Override(new(config.MarketConfig), cfg),

//config
config.ConfigServerOpts(cfg),

Expand Down
Loading