Skip to content

Commit

Permalink
Migration DB env
Browse files Browse the repository at this point in the history
  • Loading branch information
snadrus committed May 28, 2024
1 parent f6af707 commit 629c5ea
Show file tree
Hide file tree
Showing 7 changed files with 719 additions and 529 deletions.
119 changes: 79 additions & 40 deletions cmd/curio/guidedsetup/guidedsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/filecoin-project/curio/deps"
"io"
"math/bits"
"net/http"
"os"
"os/signal"
"path"
"reflect"
"strconv"
"strings"
"syscall"
"time"

"github.com/BurntSushi/toml"
"github.com/charmbracelet/lipgloss"
"github.com/docker/go-units"
"github.com/manifoldco/promptui"
"github.com/mitchellh/go-homedir"
"github.com/samber/lo"
Expand All @@ -40,15 +37,22 @@ import (
"github.com/filecoin-project/go-state-types/abi"

_ "github.com/filecoin-project/curio/cmd/curio/internal/translations"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/v1api"
"github.com/filecoin-project/curio/deps"
"github.com/filecoin-project/curio/harmony/harmonydb"

"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/cli/spcli"

cliutil "github.com/filecoin-project/lotus/cli/util"
"github.com/filecoin-project/curio/harmony/harmonydb"
"github.com/filecoin-project/lotus/lib/must"
"github.com/filecoin-project/lotus/node/config"
"github.com/filecoin-project/lotus/node/repo"

"github.com/filecoin-project/lotus/cli/spcli"

"github.com/filecoin-project/lotus/chain/types"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/v1api"
)

// URL to upload user-selected fields to help direct developer's focus.
Expand Down Expand Up @@ -209,6 +213,7 @@ var migrationSteps = []migrationStep{
doc,
oneLastThing,
complete,
afterRan,
}

type newMinerStep func(data *MigrationData)
Expand All @@ -220,6 +225,7 @@ var newMinerSteps = []newMinerStep{
doc,
oneLastThing,
completeInit,
afterRan,
}

type MigrationData struct {
Expand All @@ -229,6 +235,7 @@ type MigrationData struct {
MinerConfigPath string
MinerConfig *config.StorageMiner
DB *harmonydb.DB
HarmonyCfg config.HarmonyDB
MinerID address.Address
full v1api.FullNode
cctx *cli.Context
Expand All @@ -237,20 +244,54 @@ type MigrationData struct {
owner address.Address
worker address.Address
sender address.Address
ssize abi.SectorSize
ssize string
confidence uint64
init bool
}

func complete(d *MigrationData) {
stepCompleted(d, d.T("Lotus-Miner to Curio Migration."))
d.say(plain, "Try the web interface with %s for further guided improvements.", code.Render("curio run --layers=gui"))
}

var EnvFiles = []string{"/etc/curio/config.env", "./curio/curio.env", "~/config/curio.env"}

func afterRan(d *MigrationData) {
// Write curio.env file. TODO!!!!!!!!!!
// Inform users they need to copy this to /etc/curio.env or ~/.config/curio.env to run Curio.
places := append([]string{"/tmp/curio.env",
must.One(os.Getwd()) + "/curio.env",
must.One(os.UserHomeDir()) + "/curio.env"}, EnvFiles...)
saveConfigFile:
_, where, err := (&promptui.Select{
Label: d.T("Where should we save your database config file?"),
Items: places,
Templates: d.selectTemplates,
}).Run()
if err != nil {
d.say(notice, "Aborting migration.", err.Error())
os.Exit(1)
}
args := []string{fmt.Sprintf("CURIO_DB=postgres://%s:%s@%s:%s/%s",
d.HarmonyCfg.Username,
d.HarmonyCfg.Password,
d.HarmonyCfg.Hosts[0],
d.HarmonyCfg.Port,
d.HarmonyCfg.Database)}

// Write the file
err = os.WriteFile(where, []byte(strings.Join(args, "\n")), 0644)
if err != nil {
d.say(notice, "Error writing file: %s", err.Error())
goto saveConfigFile
}

d.say(plain, "Try the web interface with %s ", code.Render("curio run --layers=gui"))
d.say(plain, "For more servers, copy curio.env to /etc/curio.env and add the CURIO_LAYERS env to assign purposes.")
d.say(plain, "You can now migrate your market node (%s), if applicable.", "Boost")
}

func completeInit(d *MigrationData) {
stepCompleted(d, d.T("New Miner initialization complete."))
d.say(plain, "Try the web interface with %s for further guided improvements.", code.Render("curio run --layers=gui"))
}

func configToDB(d *MigrationData) {
Expand Down Expand Up @@ -458,6 +499,8 @@ func yugabyteConnect(d *MigrationData) {
if err != nil {
hcfg := getDBDetails(d)
harmonyCfg = *hcfg
} else {
d.HarmonyCfg = harmonyCfg
}

d.say(plain, "Connected to Yugabyte. Schema is current.")
Expand Down Expand Up @@ -608,15 +651,15 @@ func stepCompleted(d *MigrationData, step string) {
func stepCreateActor(d *MigrationData) {
d.say(plain, "Initializing a new miner actor.")

d.ssize = "32 GiB"
for {
i, _, err := (&promptui.Select{
Label: d.T("Enter the info to create a new miner"),
Items: []string{
d.T("Owner Address: %s", d.owner.String()),
d.T("Worker Address: %s", d.worker.String()),
d.T("Sender Address: %s", d.sender.String()),
d.T("Owner Wallet: %s", d.owner.String()),
d.T("Worker Wallet: %s", d.worker.String()),
d.T("Sender Wallet: %s", d.sender.String()),
d.T("Sector Size: %d", d.ssize),
d.T("Confidence epochs: %d", d.confidence),
d.T("Continue to verify the addresses and create a new miner actor.")},
Size: 6,
Templates: d.selectTemplates,
Expand Down Expand Up @@ -660,41 +703,33 @@ func stepCreateActor(d *MigrationData) {
}
continue
case 3:
val, err := (&promptui.Prompt{
Label: d.T("Enter the sector size"),
i, _, err := (&promptui.Select{
Label: d.T("Select the Sector Size"),
Items: []string{
d.T("64 GiB"),
d.T("32 GiB"),
d.T("8 MiB"),
d.T("2 KiB"),
},
Size: 4,
Templates: d.selectTemplates,
}).Run()
if err != nil {
d.say(notice, "No value provided")
continue
}
sectorSize, err := units.RAMInBytes(val)
if err != nil {
d.say(notice, "Failed to parse sector size: %s", err.Error())
continue
d.say(notice, "Sector selection failed: %s ", err.Error())
os.Exit(1)
}
d.ssize = abi.SectorSize(sectorSize)

d.ssize = []string{"64 GiB", "32 GiB", "8 MiB", "2 KiB"}[i]
continue
case 4:
confidenceStr, err := (&promptui.Prompt{
Label: d.T("Confidence epochs"),
Default: strconv.Itoa(5),
}).Run()
if err != nil {
d.say(notice, err.Error())
continue
}
confidence, err := strconv.ParseUint(confidenceStr, 10, 64)
if err != nil {
d.say(notice, "Failed to parse confidence: %s", err.Error())
continue
}
d.confidence = confidence
goto minerInit // break out of the for loop once we have all the values
}
}

minerInit:
miner, err := spcli.CreateStorageMiner(d.ctx, d.full, d.owner, d.worker, d.sender, d.ssize, d.confidence)
var ss abi.SectorSize

miner, err := spcli.CreateStorageMiner(d.ctx, d.full, d.owner, d.worker, d.sender, ss, CONFIDENCE)
if err != nil {
d.say(notice, "Failed to create the miner actor: %s", err.Error())
os.Exit(1)
Expand All @@ -704,6 +739,8 @@ minerInit:
stepCompleted(d, d.T("Miner %s created successfully", miner.String()))
}

const CONFIDENCE = 5

func stepPresteps(d *MigrationData) {

// Setup and connect to YugabyteDB
Expand Down Expand Up @@ -890,6 +927,8 @@ func getDBDetails(d *MigrationData) *config.HarmonyDB {
continue
}
d.DB = db
d.HarmonyCfg = harmonyCfg

return &harmonyCfg
}
}
Expand Down
Loading

0 comments on commit 629c5ea

Please sign in to comment.