-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3405 from filecoin-project/feat/worker-storage-cli
worker: Cli to attach storage paths
- Loading branch information
Showing
7 changed files
with
260 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/urfave/cli/v2" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/filecoin-project/lotus/chain/types" | ||
lcli "github.com/filecoin-project/lotus/cli" | ||
) | ||
|
||
var infoCmd = &cli.Command{ | ||
Name: "info", | ||
Usage: "Print worker info", | ||
Action: func(cctx *cli.Context) error { | ||
api, closer, err := lcli.GetWorkerAPI(cctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer closer() | ||
|
||
ctx := lcli.ReqContext(cctx) | ||
|
||
ver, err := api.Version(ctx) | ||
if err != nil { | ||
return xerrors.Errorf("getting version: %w", err) | ||
} | ||
|
||
fmt.Println("Worker version: ", ver) | ||
fmt.Print("CLI version: ") | ||
cli.VersionPrinter(cctx) | ||
fmt.Println() | ||
|
||
info, err := api.Info(ctx) | ||
if err != nil { | ||
return xerrors.Errorf("getting info: %w", err) | ||
} | ||
|
||
fmt.Printf("Hostname: %s\n", info.Hostname) | ||
fmt.Printf("CPUs: %d; GPUs: %v\n", info.Resources.CPUs, info.Resources.GPUs) | ||
fmt.Printf("RAM: %s; Swap: %s\n", types.SizeStr(types.NewInt(info.Resources.MemPhysical)), types.SizeStr(types.NewInt(info.Resources.MemSwap))) | ||
fmt.Printf("Reserved memory: %s\n", types.SizeStr(types.NewInt(info.Resources.MemReserved))) | ||
fmt.Println() | ||
|
||
paths, err := api.Paths(ctx) | ||
if err != nil { | ||
return xerrors.Errorf("getting path info: %w", err) | ||
} | ||
|
||
for _, path := range paths { | ||
fmt.Printf("%s:\n", path.ID) | ||
fmt.Printf("\tWeight: %d; Use: ", path.Weight) | ||
if path.CanSeal || path.CanStore { | ||
fmt.Printf("Weight: %d; Use: ", path.Weight) | ||
if path.CanSeal { | ||
fmt.Print("Seal ") | ||
} | ||
if path.CanStore { | ||
fmt.Print("Store") | ||
} | ||
fmt.Println("") | ||
} else { | ||
fmt.Print("Use: ReadOnly") | ||
} | ||
fmt.Printf("\tLocal: %s\n", path.LocalPath) | ||
} | ||
|
||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/google/uuid" | ||
"github.com/mitchellh/go-homedir" | ||
"github.com/urfave/cli/v2" | ||
"golang.org/x/xerrors" | ||
|
||
lcli "github.com/filecoin-project/lotus/cli" | ||
"github.com/filecoin-project/lotus/extern/sector-storage/stores" | ||
) | ||
|
||
const metaFile = "sectorstore.json" | ||
|
||
var storageCmd = &cli.Command{ | ||
Name: "storage", | ||
Usage: "manage sector storage", | ||
Subcommands: []*cli.Command{ | ||
storageAttachCmd, | ||
}, | ||
} | ||
|
||
var storageAttachCmd = &cli.Command{ | ||
Name: "attach", | ||
Usage: "attach local storage path", | ||
Flags: []cli.Flag{ | ||
&cli.BoolFlag{ | ||
Name: "init", | ||
Usage: "initialize the path first", | ||
}, | ||
&cli.Uint64Flag{ | ||
Name: "weight", | ||
Usage: "(for init) path weight", | ||
Value: 10, | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "seal", | ||
Usage: "(for init) use path for sealing", | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "store", | ||
Usage: "(for init) use path for long-term storage", | ||
}, | ||
}, | ||
Action: func(cctx *cli.Context) error { | ||
nodeApi, closer, err := lcli.GetWorkerAPI(cctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer closer() | ||
ctx := lcli.ReqContext(cctx) | ||
|
||
if !cctx.Args().Present() { | ||
return xerrors.Errorf("must specify storage path to attach") | ||
} | ||
|
||
p, err := homedir.Expand(cctx.Args().First()) | ||
if err != nil { | ||
return xerrors.Errorf("expanding path: %w", err) | ||
} | ||
|
||
if cctx.Bool("init") { | ||
if err := os.MkdirAll(p, 0755); err != nil { | ||
if !os.IsExist(err) { | ||
return err | ||
} | ||
} | ||
|
||
_, err := os.Stat(filepath.Join(p, metaFile)) | ||
if !os.IsNotExist(err) { | ||
if err == nil { | ||
return xerrors.Errorf("path is already initialized") | ||
} | ||
return err | ||
} | ||
|
||
cfg := &stores.LocalStorageMeta{ | ||
ID: stores.ID(uuid.New().String()), | ||
Weight: cctx.Uint64("weight"), | ||
CanSeal: cctx.Bool("seal"), | ||
CanStore: cctx.Bool("store"), | ||
} | ||
|
||
if !(cfg.CanStore || cfg.CanSeal) { | ||
return xerrors.Errorf("must specify at least one of --store of --seal") | ||
} | ||
|
||
b, err := json.MarshalIndent(cfg, "", " ") | ||
if err != nil { | ||
return xerrors.Errorf("marshaling storage config: %w", err) | ||
} | ||
|
||
if err := ioutil.WriteFile(filepath.Join(p, metaFile), b, 0644); err != nil { | ||
return xerrors.Errorf("persisting storage metadata (%s): %w", filepath.Join(p, metaFile), err) | ||
} | ||
} | ||
|
||
return nodeApi.StorageAddLocal(ctx, p) | ||
}, | ||
} |