Skip to content

Commit

Permalink
chore: create pid file upon rollapp start
Browse files Browse the repository at this point in the history
chore: fix import alias
chore: improve output handling
chore: move home path helper to filesystem utils
  • Loading branch information
artemijspavlovs committed Jul 30, 2024
1 parent cf84f05 commit fa59992
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
42 changes: 18 additions & 24 deletions cmd/rollapp/init/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"errors"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
datalayer "github.com/dymensionxyz/roller/data_layer"
global_utils "github.com/dymensionxyz/roller/utils"
globalutils "github.com/dymensionxyz/roller/utils"
"github.com/dymensionxyz/roller/utils/archives"
)

Expand Down Expand Up @@ -53,14 +53,19 @@ func runInit(cmd *cobra.Command, opts ...Option) error {
opt(&options)
}

home := utils.GetRollerRootDir()
home, err := globalutils.ExpandHomePath(cmd.Flag(utils.FlagNames.Home).Value.String())
if err != nil {
pterm.Error.Println("failed to expand home directory")
return err
}

outputHandler := initconfig.NewOutputHandler(false)
configArchivePath := options.configArchivePath

defer outputHandler.StopSpinner()

// TODO: extract into util
isRootExist, err := global_utils.DirNotEmpty(home)
isRootExist, err := globalutils.DirNotEmpty(home)
if err != nil {
utils.PrettifyErrorIfExists(err)
return err
Expand Down Expand Up @@ -125,7 +130,7 @@ func runInit(cmd *cobra.Command, opts ...Option) error {
}

// TODO: create all dirs here
outputHandler.StartSpinner(" Initializing RollApp configuration files...")
outputHandler.StartSpinner(" Initializing RollApp configuration files...\n")

/* ------------------------------ Generate keys ----------------------------- */
addresses, err := initconfig.GenerateSequencersKeys(initConfig)
Expand Down Expand Up @@ -180,20 +185,20 @@ func runInit(cmd *cobra.Command, opts ...Option) error {
return err
}

rollerConfigFilePath := filepath.Join(utils.GetRollerRootDir(), "roller.toml")
err = global_utils.UpdateFieldInToml(rollerConfigFilePath, "home", utils.GetRollerRootDir())
rollerConfigFilePath := filepath.Join(home, "roller.toml")
err = globalutils.UpdateFieldInToml(rollerConfigFilePath, "home", home)
if err != nil {
fmt.Println("failed to add home to roller.toml: ", err)
return err
}

err = global_utils.UpdateFieldInToml(rollerConfigFilePath, "HubData.ID", initConfig.HubData.ID)
err = globalutils.UpdateFieldInToml(rollerConfigFilePath, "HubData.ID", initConfig.HubData.ID)
if err != nil {
fmt.Println("failed to add HubData.ID to roller.toml: ", err)
return err
}

err = global_utils.UpdateFieldInToml(
err = globalutils.UpdateFieldInToml(
rollerConfigFilePath,
"HubData.rpc_url",
initConfig.HubData.RPC_URL,
Expand All @@ -203,7 +208,7 @@ func runInit(cmd *cobra.Command, opts ...Option) error {
return err
}

err = global_utils.UpdateFieldInToml(
err = globalutils.UpdateFieldInToml(
rollerConfigFilePath,
"HubData.gas_price",
initConfig.HubData.GAS_PRICE,
Expand All @@ -213,7 +218,7 @@ func runInit(cmd *cobra.Command, opts ...Option) error {
return err
}

err = global_utils.UpdateFieldInToml(
err = globalutils.UpdateFieldInToml(
rollerConfigFilePath,
"da",
strings.ToLower(string(initConfig.DA)),
Expand All @@ -223,7 +228,7 @@ func runInit(cmd *cobra.Command, opts ...Option) error {
return err
}

err = global_utils.UpdateFieldInToml(
err = globalutils.UpdateFieldInToml(
rollerConfigFilePath,
"rollapp_binary",
strings.ToLower(consts.Executables.RollappEVM),
Expand Down Expand Up @@ -276,7 +281,7 @@ func checkConfigArchive(path string) (string, error) {
return "", errors.New("invalid or no input")
}

archivePath, err := expandHomePath(path)
archivePath, err := globalutils.ExpandHomePath(path)
if err != nil {
return "", err
}
Expand All @@ -287,14 +292,3 @@ func checkConfigArchive(path string) (string, error) {

return archivePath, nil
}

func expandHomePath(path string) (string, error) {
if path[:2] == "~/" {
usr, err := user.Current()
if err != nil {
return "", err
}
path = filepath.Join(usr.HomeDir, path[2:])
}
return path, nil
}
50 changes: 45 additions & 5 deletions cmd/rollapp/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import (
"context"
"fmt"
"math/big"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/config"
"github.com/dymensionxyz/roller/sequencer"
globalutils "github.com/dymensionxyz/roller/utils"
)

// TODO: Test sequencing on 35-C and update the price
Expand All @@ -29,7 +33,17 @@ func Cmd() *cobra.Command {
Use: "start",
Short: "Show the status of the sequencer on the local machine.",
Run: func(cmd *cobra.Command, args []string) {
home := cmd.Flag(utils.FlagNames.Home).Value.String()
err := initconfig.AddFlags(cmd)
if err != nil {
pterm.Error.Println("failed to add flags")
return
}
home, err := globalutils.ExpandHomePath(cmd.Flag(utils.FlagNames.Home).Value.String())
if err != nil {
pterm.Error.Println("failed to expand home directory")
return
}

rollappConfig, err := config.LoadConfigFromTOML(home)
utils.PrettifyErrorIfExists(err)

Expand All @@ -41,10 +55,16 @@ func Cmd() *cobra.Command {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go utils.RunBashCmdAsync(ctx, startRollappCmd, func() {
printOutput(rollappConfig, startRollappCmd)
}, parseError,
utils.WithLogging(utils.GetSequencerLogPath(rollappConfig)))
go utils.RunBashCmdAsync(
ctx, startRollappCmd, func() {
printOutput(rollappConfig, startRollappCmd)
err := createPidFile(RollappDirPath, startRollappCmd)
if err != nil {
pterm.Warning.Println("failed to create pid file")
}
}, parseError,
utils.WithLogging(utils.GetSequencerLogPath(rollappConfig)),
)
select {}
},
}
Expand All @@ -65,6 +85,26 @@ func printOutput(rlpCfg config.RollappConfig, cmd *exec.Cmd) {
fmt.Println("💈 PID: ", cmd.Process.Pid)
}

func createPidFile(path string, cmd *exec.Cmd) error {
pidPath := filepath.Join(path, "rollapp.pid")
file, err := os.Create(pidPath)
if err != nil {
fmt.Println("Error creating file:", err)
return err
}
// nolint errcheck
defer file.Close()

pid := cmd.Process.Pid
_, err = file.WriteString(fmt.Sprintf("%d", pid))
if err != nil {
fmt.Println("Error writing to file:", err)
return err
}

return nil
}

func parseError(errMsg string) string {
lines := strings.Split(errMsg, "\n")
if len(lines) > 0 &&
Expand Down
12 changes: 12 additions & 0 deletions utils/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
)

Expand Down Expand Up @@ -50,3 +51,14 @@ func MoveFile(src, dst string) error {
}
return nil
}

func ExpandHomePath(path string) (string, error) {
if path[:2] == "~/" {
usr, err := user.Current()
if err != nil {
return "", err
}
path = filepath.Join(usr.HomeDir, path[2:])
}
return path, nil
}

0 comments on commit fa59992

Please sign in to comment.