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

Refac/integrate monitoring pkg #464

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- Updated Sedge's Docker commands internal functionality.

## [v1.7.0] - 2024-10-24

### Added
Expand Down
4 changes: 4 additions & 0 deletions cli/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package actions

import (
"github.com/NethermindEth/sedge/internal/compose"
"github.com/NethermindEth/sedge/internal/pkg/commands"
"github.com/NethermindEth/sedge/internal/pkg/generate"
"github.com/docker/docker/client"
Expand All @@ -39,19 +40,22 @@ type sedgeActions struct {
dockerClient client.APIClient
dockerServiceManager DockerServiceManager
commandRunner commands.CommandRunner
composeManager compose.ComposeManager
}

type SedgeActionsOptions struct {
DockerClient client.APIClient
DockerServiceManager DockerServiceManager
CommandRunner commands.CommandRunner
ComposeManager compose.ComposeManager
}

func NewSedgeActions(options SedgeActionsOptions) SedgeActions {
return &sedgeActions{
dockerClient: options.DockerClient,
dockerServiceManager: options.DockerServiceManager,
commandRunner: options.CommandRunner,
composeManager: options.ComposeManager,
}
}

Expand Down
14 changes: 5 additions & 9 deletions cli/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/NethermindEth/sedge/configs"
"github.com/NethermindEth/sedge/internal/pkg/commands"
log "github.com/sirupsen/logrus"
)

type RunContainersOptions struct {
Expand All @@ -31,24 +30,21 @@ type RunContainersOptions struct {
}

func (s *sedgeActions) RunContainers(options RunContainersOptions) error {
upCmd := s.commandRunner.BuildDockerComposeUpCMD(commands.DockerComposeUpOptions{
err := s.composeManager.Up(commands.DockerComposeUpOptions{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the DockerCompose*Options should fall under the compose package rather than under the commands (after this refactor). WDYT

Copy link
Contributor Author

@khalifaa55 khalifaa55 Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ComposeManager uses the CommandRunner interface (in commands pkg) to build the docker compose commands (which use these options). I think it would be better if we keep the DockerCompose*Options in the commands pkg to also prevent an "import cycle" issue.

Path: filepath.Join(options.GenerationPath, configs.DefaultDockerComposeScriptName),
Services: options.Services,
})
log.Infof(configs.RunningCommand, upCmd.Cmd)
_, _, err := s.commandRunner.RunCMD(upCmd)
if err != nil {
return fmt.Errorf(configs.CommandError, upCmd.Cmd, err)
return fmt.Errorf(configs.RunContainersErr, err)
}
if !options.SkipDockerPs {
// Run docker compose ps --filter status=running to show script running containers
dcpsCMD := s.commandRunner.BuildDockerComposePSCMD(commands.DockerComposePsOptions{
_, err := s.composeManager.PS(commands.DockerComposePsOptions{
Path: filepath.Join(options.GenerationPath, configs.DefaultDockerComposeScriptName),
FilterRunning: true,
})
log.Infof(configs.RunningCommand, dcpsCMD.Cmd)
if _, _, err := s.commandRunner.RunCMD(dcpsCMD); err != nil {
return fmt.Errorf(configs.CommandError, dcpsCMD.Cmd, err)
if err != nil {
return fmt.Errorf(configs.RunContainersErr, err)
}
}
return nil
Expand Down
5 changes: 4 additions & 1 deletion cli/actions/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/NethermindEth/sedge/cli/actions"
"github.com/NethermindEth/sedge/internal/compose"
"github.com/NethermindEth/sedge/internal/pkg/commands"
"github.com/NethermindEth/sedge/test"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -66,8 +67,10 @@ func TestRunContainers(t *testing.T) {
return "", 0, nil
},
}
composeMgr := compose.NewComposeManager(commandRunner)
sedgeActions := actions.NewSedgeActions(actions.SedgeActionsOptions{
CommandRunner: commandRunner,
CommandRunner: commandRunner,
ComposeManager: *composeMgr,
})
sedgeActions.RunContainers(tc.options)
assert.Equal(t, 1, up)
Expand Down
22 changes: 10 additions & 12 deletions cli/actions/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package actions

import (
"fmt"
"path/filepath"

"github.com/NethermindEth/sedge/configs"
Expand All @@ -31,33 +32,30 @@ type SetupContainersOptions struct {

func (s *sedgeActions) SetupContainers(options SetupContainersOptions) error {
log.Info("Setting up containers")
buildCmd := s.commandRunner.BuildDockerComposeBuildCMD(commands.DockerComposeBuildOptions{
err := s.composeManager.Build(commands.DockerComposeBuildOptions{
Path: filepath.Join(options.GenerationPath, configs.DefaultDockerComposeScriptName),
Services: options.Services,
})
log.Infof(configs.RunningCommand, buildCmd.Cmd)
if _, _, err := s.commandRunner.RunCMD(buildCmd); err != nil {
return err
if err != nil {
return fmt.Errorf(configs.SetUpContainersErr, err)
}
if !options.SkipPull {
pullCmd := s.commandRunner.BuildDockerComposePullCMD(commands.DockerComposePullOptions{
err := s.composeManager.Pull(commands.DockerComposePullOptions{
Path: filepath.Join(options.GenerationPath, configs.DefaultDockerComposeScriptName),
Services: options.Services,
})
log.Infof(configs.RunningCommand, pullCmd.Cmd)
if _, _, err := s.commandRunner.RunCMD(pullCmd); err != nil {
return err
if err != nil {
return fmt.Errorf(configs.SetUpContainersErr, err)
}
} else {
log.Warn("Skipping 'docker compose pull' step")
}
createCmd := s.commandRunner.BuildDockerComposeCreateCMD(commands.DockerComposeCreateOptions{
err = s.composeManager.Create(commands.DockerComposeCreateOptions{
Path: filepath.Join(options.GenerationPath, configs.DefaultDockerComposeScriptName),
Services: options.Services,
})
log.Infof(configs.RunningCommand, createCmd.Cmd)
if _, _, err := s.commandRunner.RunCMD(createCmd); err != nil {
return err
if err != nil {
return fmt.Errorf(configs.SetUpContainersErr, err)
}
return nil
}
5 changes: 4 additions & 1 deletion cli/actions/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/NethermindEth/sedge/cli/actions"
"github.com/NethermindEth/sedge/internal/compose"
"github.com/NethermindEth/sedge/internal/pkg/commands"
"github.com/NethermindEth/sedge/test"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -74,8 +75,10 @@ func TestSetupContainers(t *testing.T) {
return "", 0, nil
},
}
composeMgr := compose.NewComposeManager(commandRunner)
sedgeActions := actions.NewSedgeActions(actions.SedgeActionsOptions{
CommandRunner: commandRunner,
CommandRunner: commandRunner,
ComposeManager: *composeMgr,
})
sedgeActions.SetupContainers(tc.options)
})
Expand Down
11 changes: 5 additions & 6 deletions cli/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/NethermindEth/sedge/cli/actions"
"github.com/NethermindEth/sedge/configs"
"github.com/NethermindEth/sedge/internal/compose"
"github.com/NethermindEth/sedge/internal/pkg/commands"
"github.com/NethermindEth/sedge/internal/pkg/dependencies"
"github.com/NethermindEth/sedge/internal/utils"
Expand All @@ -29,7 +30,7 @@ import (
log "github.com/sirupsen/logrus"
)

func DownCmd(cmdRunner commands.CommandRunner, a actions.SedgeActions, depsMgr dependencies.DependenciesManager) *cobra.Command {
func DownCmd(composeManager compose.ComposeManager, cmdRunner commands.CommandRunner, a actions.SedgeActions, depsMgr dependencies.DependenciesManager) *cobra.Command {
// Flags
var generationPath string
// Build command
Expand All @@ -49,13 +50,11 @@ func DownCmd(cmdRunner commands.CommandRunner, a actions.SedgeActions, depsMgr d
return err
}

downCMD := cmdRunner.BuildDockerComposeDownCMD(commands.DockerComposeDownOptions{
err := composeManager.Down(commands.DockerComposeDownOptions{
Path: filepath.Join(generationPath, configs.DefaultDockerComposeScriptName),
})

log.Debugf(configs.RunningCommand, downCMD.Cmd)
if _, _, err := cmdRunner.RunCMD(downCMD); err != nil {
return fmt.Errorf(configs.CommandError, downCMD.Cmd, err)
if err != nil {
return fmt.Errorf("error shutting down contaiers %w", err)
}

return nil
Expand Down
8 changes: 6 additions & 2 deletions cli/down_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/NethermindEth/sedge/internal/compose"
"github.com/NethermindEth/sedge/internal/pkg/commands"
"github.com/NethermindEth/sedge/internal/pkg/dependencies"
"github.com/NethermindEth/sedge/test"
Expand All @@ -39,6 +40,7 @@ import (

type downCmdTestCase struct {
generationPath string
composeMgr compose.ComposeManager
runner commands.CommandRunner
depsMgr dependencies.DependenciesManager
sedgeActions actions.SedgeActions
Expand Down Expand Up @@ -78,6 +80,8 @@ func buildDownTestCase(t *testing.T, caseName string, isErr bool, path string) *
return "", nil
},
}
composeMgr := compose.NewComposeManager(tc.runner)
tc.composeMgr = *composeMgr

tc.generationPath = path
tc.fdOut = new(bytes.Buffer)
Expand All @@ -93,7 +97,7 @@ func TestDownCmd(t *testing.T) {

for _, tc := range tcs {
rootCmd := RootCmd()
rootCmd.AddCommand(DownCmd(tc.runner, tc.sedgeActions, tc.depsMgr))
rootCmd.AddCommand(DownCmd(tc.composeMgr, tc.runner, tc.sedgeActions, tc.depsMgr))
rootCmd.SetArgs([]string{"down", "--path", tc.generationPath})
rootCmd.SetOut(tc.fdOut)
log.SetOutput(tc.fdOut)
Expand Down Expand Up @@ -196,7 +200,7 @@ func TestDown_Error(t *testing.T) {
pathFlag = tc.customPath
}
tt := buildDownTestCase(t, "case_1", true, pathFlag)
downCmd := DownCmd(tc.runner, tt.sedgeActions, tt.depsMgr)
downCmd := DownCmd(tt.composeMgr, tc.runner, tt.sedgeActions, tt.depsMgr)
downCmd.SetArgs([]string{"--path", pathFlag})
downCmd.SetOut(io.Discard)
err := downCmd.Execute()
Expand Down
13 changes: 3 additions & 10 deletions cli/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/NethermindEth/sedge/cli/actions"
"github.com/NethermindEth/sedge/configs"
"github.com/NethermindEth/sedge/internal/compose"
"github.com/NethermindEth/sedge/internal/pkg/commands"
"github.com/NethermindEth/sedge/internal/pkg/dependencies"
"github.com/NethermindEth/sedge/internal/utils"
Expand All @@ -30,7 +31,7 @@ import (
log "github.com/sirupsen/logrus"
)

func LogsCmd(cmdRunner commands.CommandRunner, sedgeActions actions.SedgeActions, depsMgr dependencies.DependenciesManager) *cobra.Command {
func LogsCmd(composeManager compose.ComposeManager, cmdRunner commands.CommandRunner, sedgeActions actions.SedgeActions, depsMgr dependencies.DependenciesManager) *cobra.Command {
// Flags
var (
generationPath string
Expand Down Expand Up @@ -65,20 +66,12 @@ func LogsCmd(cmdRunner commands.CommandRunner, sedgeActions actions.SedgeActions
services = args
}

logsCMD := cmdRunner.BuildDockerComposeLogsCMD(commands.DockerComposeLogsOptions{
err = composeManager.Logs(commands.DockerComposeLogsOptions{
Path: file,
Services: services,
Follow: tail == 0,
Tail: tail,
})

log.Debugf(configs.RunningCommand, logsCMD.Cmd)
_, exitCode, err := cmdRunner.RunCMD(logsCMD)
if exitCode == 130 {
// A job with exit code 130 was terminated with signal 2 (SIGINT on most systems).
// Process interrupted by user (Ctrl+C)
return nil
}
if err != nil {
return fmt.Errorf(configs.GettingLogsError, strings.Join(services, " "), err)
}
Expand Down
6 changes: 4 additions & 2 deletions cli/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/NethermindEth/sedge/internal/compose"
"github.com/NethermindEth/sedge/internal/pkg/commands"
"github.com/NethermindEth/sedge/internal/pkg/dependencies"
"github.com/NethermindEth/sedge/test"
Expand Down Expand Up @@ -115,7 +116,7 @@ func TestLogs(t *testing.T) {
return "", nil
},
},
err: "failed to get logs for services . Error: error",
err: "failed to get logs for services . Error: Docker Compose Manager running 'docker compose logs': error. Output: ",
},
{
name: "services arg",
Expand Down Expand Up @@ -145,13 +146,14 @@ func TestLogs(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
composeMgr := compose.NewComposeManager(tc.cmd)

depsMgr := sedge_mocks.NewMockDependenciesManager(ctrl)
sedgeActions := sedge_mocks.NewMockSedgeActions(ctrl)

tc.setup(depsMgr, sedgeActions)

cmd := LogsCmd(tc.cmd, sedgeActions, depsMgr)
cmd := LogsCmd(*composeMgr, tc.cmd, sedgeActions, depsMgr)
cmd.SetOutput(io.Discard)
cmd.SetArgs(tc.args)
err := cmd.Execute()
Expand Down
5 changes: 3 additions & 2 deletions cmd/sedge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,18 @@ func main() {
DockerClient: dockerClient,
DockerServiceManager: dockerServiceManager,
CommandRunner: cmdRunner,
ComposeManager: *composeManager,
}
sedgeActions := actions.NewSedgeActions(sdgOpts)

sedgeCmd := cli.RootCmd()
sedgeCmd.AddCommand(
cli.CliCmd(prompt, sedgeActions, depsMgr, monitoringMgr),
cli.KeysCmd(cmdRunner, prompt),
cli.DownCmd(cmdRunner, sedgeActions, depsMgr),
cli.DownCmd(*composeManager, cmdRunner, sedgeActions, depsMgr),
cli.ClientsCmd(),
cli.NetworksCmd(),
cli.LogsCmd(cmdRunner, sedgeActions, depsMgr),
cli.LogsCmd(*composeManager, cmdRunner, sedgeActions, depsMgr),
cli.VersionCmd(),
cli.SlashingExportCmd(sedgeActions, depsMgr),
cli.SlashingImportCmd(sedgeActions, depsMgr),
Expand Down
2 changes: 2 additions & 0 deletions configs/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ const (
InvalidNetworkForLido = "invalid network: Choose valid network for Lido: %v"
InvalidNetworkForLidoMevBoost = "invalid network: Choose valid network for Lido with MEV-Boost: %v"
InvalidNetworkForLidoKeys = "invalid network: Choose valid network for Lido Withdrawal Address: %v"
SetUpContainersErr = "error setting up containers %w"
RunContainersErr = "error running containers %w"
)
18 changes: 14 additions & 4 deletions configs/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ package configs

import (
"fmt"
"os"
"path/filepath"

"github.com/NethermindEth/sedge/internal/locker"
"github.com/NethermindEth/sedge/internal/monitoring/data"
"github.com/spf13/afero"
)

// All the strings that are needed for debugging and info logging, and constant strings.
Expand Down Expand Up @@ -120,10 +123,17 @@ Happy Staking!
var DefaultAbsSedgeDataPath string

func init() {
cwd, err := os.Getwd()
// Set filesystem
// fs := afero.NewMemMapFs() // Uncomment this line if you want to use the in-memory filesystem
// fs := afero.NewBasePathFs(afero.NewOsFs(), "/tmp") // Uncomment this line if you want to use the real filesystem with a base path
fs := afero.NewOsFs() // Uncomment this line if you want to use the real filesystem

// Set locker
locker := locker.NewFLock()
dataDir, err := data.NewDataDirDefault(fs, locker)
if err != nil {
// notest
fmt.Println(err)
}
DefaultAbsSedgeDataPath = filepath.Join(cwd, DefaultSedgeDataFolderName)

DefaultAbsSedgeDataPath = filepath.Join(dataDir.Path(), DefaultSedgeDataFolderName)
}
Loading