From 360644f64bc65ae65c44d64c8a63cf95f52fbefa Mon Sep 17 00:00:00 2001 From: artpav <19916123+artemijspavlovs@users.noreply.github.com> Date: Sun, 15 Sep 2024 13:38:49 +0300 Subject: [PATCH] feat: add logs command for services (#943) --- cmd/eibc/eibc.go | 5 +- cmd/relayer/relayer.go | 2 + cmd/rollapp/rollapp.go | 2 + cmd/services/logs/logs.go | 143 +++++++++++++++++++++++++++++++++ cmd/services/services.go | 4 +- go.mod | 2 + utils/filesystem/filesystem.go | 20 +++++ 7 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 cmd/services/logs/logs.go diff --git a/cmd/eibc/eibc.go b/cmd/eibc/eibc.go index 8793c652..604116bb 100644 --- a/cmd/eibc/eibc.go +++ b/cmd/eibc/eibc.go @@ -1,6 +1,8 @@ package eibc import ( + "github.com/spf13/cobra" + "github.com/dymensionxyz/roller/cmd/eibc/fulfill" "github.com/dymensionxyz/roller/cmd/eibc/funds" eibcinit "github.com/dymensionxyz/roller/cmd/eibc/init" @@ -8,10 +10,10 @@ import ( "github.com/dymensionxyz/roller/cmd/eibc/start" "github.com/dymensionxyz/roller/cmd/services" loadservices "github.com/dymensionxyz/roller/cmd/services/load" + logservices "github.com/dymensionxyz/roller/cmd/services/logs" restartservices "github.com/dymensionxyz/roller/cmd/services/restart" startservices "github.com/dymensionxyz/roller/cmd/services/start" stopservices "github.com/dymensionxyz/roller/cmd/services/stop" - "github.com/spf13/cobra" ) func Cmd() *cobra.Command { @@ -33,6 +35,7 @@ func Cmd() *cobra.Command { startservices.EibcCmd(), restartservices.Cmd(sl), stopservices.Cmd(sl), + logservices.EibcCmd(), ), ) diff --git a/cmd/relayer/relayer.go b/cmd/relayer/relayer.go index 27bda112..ae678bfd 100644 --- a/cmd/relayer/relayer.go +++ b/cmd/relayer/relayer.go @@ -6,6 +6,7 @@ import ( "github.com/dymensionxyz/roller/cmd/relayer/status" "github.com/dymensionxyz/roller/cmd/services" loadservices "github.com/dymensionxyz/roller/cmd/services/load" + logservices "github.com/dymensionxyz/roller/cmd/services/logs" restartservices "github.com/dymensionxyz/roller/cmd/services/restart" startservices "github.com/dymensionxyz/roller/cmd/services/start" stopservices "github.com/dymensionxyz/roller/cmd/services/stop" @@ -29,6 +30,7 @@ func Cmd() *cobra.Command { startservices.RelayerCmd(), restartservices.Cmd(sl), stopservices.Cmd(sl), + logservices.RelayerCmd(), ), ) diff --git a/cmd/rollapp/rollapp.go b/cmd/rollapp/rollapp.go index 34232ba4..418b52a6 100644 --- a/cmd/rollapp/rollapp.go +++ b/cmd/rollapp/rollapp.go @@ -9,6 +9,7 @@ import ( "github.com/dymensionxyz/roller/cmd/rollapp/status" "github.com/dymensionxyz/roller/cmd/services" loadservices "github.com/dymensionxyz/roller/cmd/services/load" + logservices "github.com/dymensionxyz/roller/cmd/services/logs" restartservices "github.com/dymensionxyz/roller/cmd/services/restart" startservices "github.com/dymensionxyz/roller/cmd/services/start" stopservices "github.com/dymensionxyz/roller/cmd/services/stop" @@ -35,6 +36,7 @@ func Cmd() *cobra.Command { startservices.RollappCmd(), restartservices.Cmd(sl), stopservices.Cmd(sl), + logservices.RollappCmd(), ), ) diff --git a/cmd/services/logs/logs.go b/cmd/services/logs/logs.go new file mode 100644 index 00000000..56ca34b5 --- /dev/null +++ b/cmd/services/logs/logs.go @@ -0,0 +1,143 @@ +package logs + +import ( + "fmt" + "os" + "path/filepath" + "time" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/dymensionxyz/roller/cmd/consts" + "github.com/dymensionxyz/roller/cmd/utils" + "github.com/dymensionxyz/roller/utils/config/tomlconfig" + "github.com/dymensionxyz/roller/utils/filesystem" +) + +// TODO: refactor +func RollappCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "logs", + Short: "Follow the logs for rollapp and da light client", + Run: func(cmd *cobra.Command, args []string) { + home := cmd.Flag(utils.FlagNames.Home).Value.String() + + rollerData, err := tomlconfig.LoadRollerConfig(home) + if err != nil { + pterm.Error.Println("failed to load roller config file", err) + return + } + + raLogFilePath := filepath.Join( + rollerData.Home, + consts.ConfigDirName.Rollapp, + "rollapp.log", + ) + + daLogFilePath := filepath.Join( + rollerData.Home, + consts.ConfigDirName.DALightNode, + "light_client.log", + ) + pterm.Info.Println("Follow the logs for rollapp: ", raLogFilePath) + pterm.Info.Println("Follow the logs for da light client: ", daLogFilePath) + + errChan := make(chan error, 2) + doneChan := make(chan bool) + + go func() { + err := filesystem.TailFile(raLogFilePath, "rollapp") + if err != nil { + pterm.Error.Println("failed to tail file", err) + errChan <- fmt.Errorf("failed to tail RA file: %w", err) + return + } + }() + go func() { + err := filesystem.TailFile(daLogFilePath, "da light client") + if err != nil { + pterm.Error.Println("failed to tail file", err) + errChan <- fmt.Errorf("failed to tail DA file: %w", err) + return + } + }() + + // Keep the program running + go func() { + time.Sleep(time.Hour) // Adjust this duration as needed + doneChan <- true + }() + + select { + case err := <-errChan: + pterm.Error.Println(err) + os.Exit(1) + default: + select {} + } + }, + } + return cmd +} + +func RelayerCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "logs", + Short: "Follow the logs for relayer", + Run: func(cmd *cobra.Command, args []string) { + home := cmd.Flag(utils.FlagNames.Home).Value.String() + + rollerData, err := tomlconfig.LoadRollerConfig(home) + if err != nil { + pterm.Error.Println("failed to load roller config file", err) + return + } + + rlyLogFilePath := filepath.Join( + rollerData.Home, + consts.ConfigDirName.Relayer, + "relayer.log", + ) + + pterm.Info.Println("Follow the logs for da light client: ", rlyLogFilePath) + + errChan := make(chan error, 2) + doneChan := make(chan bool) + + go func() { + err := filesystem.TailFile(rlyLogFilePath, "relayer") + if err != nil { + pterm.Error.Println("failed to tail file", err) + errChan <- fmt.Errorf("failed to tail RA file: %w", err) + return + } + }() + + go func() { + time.Sleep(time.Hour) + doneChan <- true + }() + + select { + case err := <-errChan: + pterm.Error.Println(err) + os.Exit(1) + default: + select {} + } + }, + } + return cmd +} + +func EibcCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "logs", + Short: "Follow the logs for eibc", + Run: func(cmd *cobra.Command, args []string) { + pterm.Info.Println("not implemented") + }, + } + return cmd +} diff --git a/cmd/services/services.go b/cmd/services/services.go index 66ce863b..ba37b69e 100644 --- a/cmd/services/services.go +++ b/cmd/services/services.go @@ -2,7 +2,8 @@ package services import "github.com/spf13/cobra" -func Cmd(loadCmd, startCmd, restartCmd, stopCmd *cobra.Command) *cobra.Command { +// TODO: use options instead +func Cmd(loadCmd, startCmd, restartCmd, stopCmd, logsCmd *cobra.Command) *cobra.Command { cmd := &cobra.Command{ Use: "services [command]", Short: "Commands for managing systemd services.", @@ -11,5 +12,6 @@ func Cmd(loadCmd, startCmd, restartCmd, stopCmd *cobra.Command) *cobra.Command { cmd.AddCommand(startCmd) cmd.AddCommand(restartCmd) cmd.AddCommand(stopCmd) + cmd.AddCommand(logsCmd) return cmd } diff --git a/go.mod b/go.mod index 62ca06ea..5c221490 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/lib/pq v1.10.9 github.com/manifoldco/promptui v0.9.0 github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 + github.com/nxadm/tail v1.4.8 github.com/olekukonko/tablewriter v0.0.5 github.com/pelletier/go-toml v1.9.5 github.com/pelletier/go-toml/v2 v2.1.0 @@ -219,6 +220,7 @@ require ( google.golang.org/protobuf v1.34.2 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect + gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect nhooyr.io/websocket v1.8.7 // indirect pgregory.net/rapid v1.1.0 // indirect sigs.k8s.io/yaml v1.4.0 // indirect diff --git a/utils/filesystem/filesystem.go b/utils/filesystem/filesystem.go index 60966d38..4254a408 100644 --- a/utils/filesystem/filesystem.go +++ b/utils/filesystem/filesystem.go @@ -12,6 +12,7 @@ import ( "os/user" "path/filepath" + "github.com/nxadm/tail" "github.com/pterm/pterm" "github.com/dymensionxyz/roller/utils/bash" @@ -232,3 +233,22 @@ func RemoveFileIfExists(filePath string) error { } return nil } + +func TailFile(fp, svcName string) error { + t, err := tail.TailFile(fp, tail.Config{Follow: true, ReOpen: false}) + if err != nil { + return fmt.Errorf("failed to tail file: %v", err) + } + + infoPrefix := pterm.Info.Prefix + infoPrefix.Text = svcName + cp := pterm.PrefixPrinter{ + Prefix: infoPrefix, + } + + for line := range t.Lines { + cp.Println(line.Text) + } + + return nil +}