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(cmd): add node type page to the startup assistant #1431

Merged
merged 24 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bc1c59c
feat: add prune node widget in assitants GUI
Ja7ad Jul 20, 2024
8c69c82
fix: passed metadata with ref
Ja7ad Jul 20, 2024
e760744
Merge branch 'main' into feat/importing-data
Ja7ad Jul 20, 2024
df46aa8
fix: fmt error
Ja7ad Jul 20, 2024
0ba26eb
fix: add configurable struct for download manager
Ja7ad Jul 20, 2024
4967605
fix: update download manager with new structure
Ja7ad Jul 20, 2024
887169f
fix: update download manager and add total item in download state
Ja7ad Jul 20, 2024
37556a5
fix: fmt and lint errors
Ja7ad Jul 20, 2024
1320865
fix: add new line for progress
Ja7ad Jul 21, 2024
d5af7be
Merge branch 'main' into feat/importing-data
Ja7ad Jul 21, 2024
b7fcbcf
fix: remove show bytes make confused
Ja7ad Jul 21, 2024
9187cb6
fix: update progress bar describe
Ja7ad Jul 21, 2024
da0c81c
fix: remove show bytes param
Ja7ad Jul 21, 2024
213a580
fix: add cleanup method for dm
Ja7ad Jul 21, 2024
7f2646f
feat: add MoveDirectory util
Ja7ad Jul 22, 2024
545d681
fix: make single file in compression
Ja7ad Jul 22, 2024
f524f92
fix: import data from single file and move after extract
Ja7ad Jul 22, 2024
55745db
fix: change base url of snapshots
Ja7ad Jul 22, 2024
e3d2990
Merge branch 'main' into feat/importing-data
Ja7ad Jul 22, 2024
b495c49
fix(cmd): fix importer issues
b00f Jul 24, 2024
8cd64f3
Merge pull request #5 from b00f/feat/importing-data
Ja7ad Jul 24, 2024
a0d8bb3
fix: sort items in metadata
Ja7ad Jul 24, 2024
0e4cb7b
fix: remove check abs fullPath
Ja7ad Jul 24, 2024
fdd810a
fix: don't run test on windows
Ja7ad Jul 24, 2024
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
6 changes: 3 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,18 +634,18 @@ func MakeValidatorKey(walletInstance *wallet.Wallet, valAddrsInfo []vault.Addres
return valKeys, nil
}

func TerminalProgressBar(totalSize, barWidth int, showBytes bool) *progressbar.ProgressBar {
func TerminalProgressBar(totalSize int64, barWidth int) *progressbar.ProgressBar {
if barWidth < 15 {
barWidth = 15
}

opts := []progressbar.Option{
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionShowBytes(showBytes),
progressbar.OptionSetWidth(barWidth),
progressbar.OptionSetElapsedTime(false),
progressbar.OptionSetPredictTime(false),
progressbar.OptionShowDescriptionAtLineEnd(),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
Expand All @@ -655,5 +655,5 @@ func TerminalProgressBar(totalSize, barWidth int, showBytes bool) *progressbar.P
}),
}

return progressbar.NewOptions(totalSize, opts...)
return progressbar.NewOptions64(totalSize, opts...)
}
87 changes: 31 additions & 56 deletions cmd/daemon/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/gofrs/flock"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/util"
"github.com/spf13/cobra"
)
Expand All @@ -21,7 +19,7 @@ func buildImportCmd(parentCmd *cobra.Command) {
parentCmd.AddCommand(importCmd)

workingDirOpt := addWorkingDirOption(importCmd)
serverAddrOpt := importCmd.Flags().String("server-addr", "https://download.pactus.org",
serverAddrOpt := importCmd.Flags().String("server-addr", cmd.DefaultSnapshotURL,
"import server address")

importCmd.Run = func(c *cobra.Command, _ []string) {
Expand All @@ -46,39 +44,25 @@ func buildImportCmd(parentCmd *cobra.Command) {
return
}

storeDir, _ := filepath.Abs(conf.Store.StorePath())
if !util.IsDirNotExistsOrEmpty(storeDir) {
cmd.PrintErrorMsgf("The data directory is not empty: %s", conf.Store.StorePath())

return
}
cmd.PrintLine()

snapshotURL := *serverAddrOpt
importer, err := cmd.NewImporter(
gen.ChainType(),
snapshotURL,
conf.Store.DataPath(),
)
cmd.FatalErrorCheck(err)

switch gen.ChainType() {
case genesis.Mainnet:
snapshotURL += "/mainnet/"
case genesis.Testnet:
snapshotURL += "/testnet/"
case genesis.Localnet:
cmd.PrintErrorMsgf("Unsupported chain type: %s", gen.ChainType())

return
}

metadata, err := cmd.GetSnapshotMetadata(c.Context(), snapshotURL)
if err != nil {
cmd.PrintErrorMsgf("Failed to get snapshot metadata: %s", err)

return
}
metadata, err := importer.GetMetadata(c.Context())
cmd.FatalErrorCheck(err)

snapshots := make([]string, 0, len(metadata))

for _, m := range metadata {
item := fmt.Sprintf("snapshot %s (%s)",
parseTime(m.CreatedAt).Format("2006-01-02"),
util.FormatBytesToHumanReadable(m.TotalSize),
m.CreatedAtTime().Format("2006-01-02"),
util.FormatBytesToHumanReadable(m.Data.Size),
)

snapshots = append(snapshots, item)
Expand All @@ -89,34 +73,32 @@ func buildImportCmd(parentCmd *cobra.Command) {
choice := cmd.PromptSelect("Please select a snapshot", snapshots)

selected := metadata[choice]
tmpDir := util.TempDirPath()
extractPath := fmt.Sprintf("%s/data", tmpDir)

err = os.MkdirAll(extractPath, 0o750)
cmd.FatalErrorCheck(err)
cmd.TrapSignal(func() {
_ = fileLock.Unlock()
_ = importer.Cleanup()
})

cmd.PrintLine()

zipFileList := cmd.DownloadManager(
importer.Download(
c.Context(),
&selected,
snapshotURL,
tmpDir,
downloadProgressBar,
)

for _, zFile := range zipFileList {
err := cmd.ExtractAndStoreFile(zFile, extractPath)
cmd.FatalErrorCheck(err)
}
cmd.PrintLine()
cmd.PrintLine()
cmd.PrintInfoMsgf("Extracting files...")

err = os.MkdirAll(filepath.Dir(conf.Store.StorePath()), 0o750)
err = importer.ExtractAndStoreFiles()
cmd.FatalErrorCheck(err)

err = cmd.CopyAllFiles(extractPath, conf.Store.StorePath())
cmd.PrintInfoMsgf("Moving data...")
err = importer.MoveStore()
cmd.FatalErrorCheck(err)

err = os.RemoveAll(tmpDir)
err = importer.Cleanup()
cmd.FatalErrorCheck(err)

_ = fileLock.Unlock()
Expand All @@ -131,19 +113,12 @@ func buildImportCmd(parentCmd *cobra.Command) {
}

func downloadProgressBar(fileName string, totalSize, downloaded int64, _ float64) {
bar := cmd.TerminalProgressBar(int(totalSize), 30, true)
bar.Describe(fileName)
err := bar.Add(int(downloaded))
bar := cmd.TerminalProgressBar(totalSize, 30)
bar.Describe(fmt.Sprintf("%s (%s/%s)",
fileName,
util.FormatBytesToHumanReadable(uint64(downloaded)),
util.FormatBytesToHumanReadable(uint64(totalSize)),
))
err := bar.Add64(downloaded)
cmd.FatalErrorCheck(err)
}

func parseTime(dateString string) time.Time {
const layout = "2006-01-02T15:04:05.000000"

parsedTime, err := time.Parse(layout, dateString)
if err != nil {
return time.Time{}
}

return parsedTime
}
7 changes: 2 additions & 5 deletions cmd/daemon/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ func buildPruneCmd(parentCmd *cobra.Command) {
fileLock := flock.New(lockFilePath)

locked, err := fileLock.TryLock()
if err != nil {
// handle unable to attempt to acquire lock
cmd.FatalErrorCheck(err)
}
cmd.FatalErrorCheck(err)

if !locked {
cmd.PrintWarnMsgf("Could not lock '%s', another instance is running?", lockFilePath)
Expand Down Expand Up @@ -114,7 +111,7 @@ func buildPruneCmd(parentCmd *cobra.Command) {
}

func pruningProgressBar(prunedCount, skippedCount, totalCount uint32) {
bar := cmd.TerminalProgressBar(int(totalCount), 30, false)
bar := cmd.TerminalProgressBar(int64(totalCount), 30)
bar.Describe(fmt.Sprintf("Pruned: %d | Skipped: %d", prunedCount, skippedCount))
err := bar.Add(int(prunedCount + skippedCount))
cmd.FatalErrorCheck(err)
Expand Down
5 changes: 1 addition & 4 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ func buildStartCmd(parentCmd *cobra.Command) {
fileLock := flock.New(lockFilePath)

locked, err := fileLock.TryLock()
if err != nil {
// handle unable to attempt to acquire lock
cmd.FatalErrorCheck(err)
}
cmd.FatalErrorCheck(err)

if !locked {
cmd.PrintWarnMsgf("Could not lock '%s', another instance is running?", lockFilePath)
Expand Down
Loading
Loading