diff --git a/CHANGELOG.md b/CHANGELOG.md index a04eae355..83850a265 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cli/actions/actions.go b/cli/actions/actions.go index 856544886..bd3974674 100644 --- a/cli/actions/actions.go +++ b/cli/actions/actions.go @@ -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" @@ -39,12 +40,14 @@ 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 { @@ -52,6 +55,7 @@ func NewSedgeActions(options SedgeActionsOptions) SedgeActions { dockerClient: options.DockerClient, dockerServiceManager: options.DockerServiceManager, commandRunner: options.CommandRunner, + composeManager: options.ComposeManager, } } diff --git a/cli/actions/run.go b/cli/actions/run.go index 62823eb52..e97136746 100644 --- a/cli/actions/run.go +++ b/cli/actions/run.go @@ -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 { @@ -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{ 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 diff --git a/cli/actions/run_test.go b/cli/actions/run_test.go index f4addc448..36fa4c0b8 100644 --- a/cli/actions/run_test.go +++ b/cli/actions/run_test.go @@ -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" @@ -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) diff --git a/cli/actions/setup.go b/cli/actions/setup.go index c2123ddf1..4561d12fe 100644 --- a/cli/actions/setup.go +++ b/cli/actions/setup.go @@ -16,6 +16,7 @@ limitations under the License. package actions import ( + "fmt" "path/filepath" "github.com/NethermindEth/sedge/configs" @@ -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 } diff --git a/cli/actions/setup_test.go b/cli/actions/setup_test.go index b1fdcef33..46519cf71 100644 --- a/cli/actions/setup_test.go +++ b/cli/actions/setup_test.go @@ -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" @@ -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) }) diff --git a/cli/down.go b/cli/down.go index 1cc7451fd..701044944 100644 --- a/cli/down.go +++ b/cli/down.go @@ -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" @@ -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 @@ -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 diff --git a/cli/down_test.go b/cli/down_test.go index e5ca9f8b8..64c07e84f 100644 --- a/cli/down_test.go +++ b/cli/down_test.go @@ -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" @@ -39,6 +40,7 @@ import ( type downCmdTestCase struct { generationPath string + composeMgr compose.ComposeManager runner commands.CommandRunner depsMgr dependencies.DependenciesManager sedgeActions actions.SedgeActions @@ -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) @@ -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) @@ -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() diff --git a/cli/logs.go b/cli/logs.go index be56fd4e3..94508cc10 100644 --- a/cli/logs.go +++ b/cli/logs.go @@ -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" @@ -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 @@ -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) } diff --git a/cli/logs_test.go b/cli/logs_test.go index 46b56476f..e20f3894f 100644 --- a/cli/logs_test.go +++ b/cli/logs_test.go @@ -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" @@ -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", @@ -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() diff --git a/cmd/sedge/main.go b/cmd/sedge/main.go index 0fba9000a..9d58655aa 100644 --- a/cmd/sedge/main.go +++ b/cmd/sedge/main.go @@ -88,6 +88,7 @@ func main() { DockerClient: dockerClient, DockerServiceManager: dockerServiceManager, CommandRunner: cmdRunner, + ComposeManager: *composeManager, } sedgeActions := actions.NewSedgeActions(sdgOpts) @@ -95,10 +96,10 @@ func main() { 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), diff --git a/configs/errors.go b/configs/errors.go index 8b82002f7..e7d774ac6 100644 --- a/configs/errors.go +++ b/configs/errors.go @@ -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" ) diff --git a/configs/messages.go b/configs/messages.go index d4b9be4f6..615458c80 100644 --- a/configs/messages.go +++ b/configs/messages.go @@ -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. @@ -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) } diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 147d42dd9..a130685cd 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -20,7 +20,9 @@ import ( "fmt" "strings" + "github.com/NethermindEth/sedge/configs" "github.com/NethermindEth/sedge/internal/pkg/commands" + log "github.com/sirupsen/logrus" ) // DockerComposeCmdError represents an error that occurs when running a Docker Compose command. @@ -49,6 +51,7 @@ func NewComposeManager(runner commands.CommandRunner) *ComposeManager { func (cm *ComposeManager) Up(opts commands.DockerComposeUpOptions) error { upCmd := cm.cmdRunner.BuildDockerComposeUpCMD(opts) + log.Infof(configs.RunningCommand, upCmd.Cmd) if out, exitCode, err := cm.cmdRunner.RunCMD(upCmd); err != nil || exitCode != 0 { return fmt.Errorf("%w: %s. Output: %s", DockerComposeCmdError{Cmd: "up"}, err, out) } @@ -59,6 +62,7 @@ func (cm *ComposeManager) Up(opts commands.DockerComposeUpOptions) error { func (cm *ComposeManager) Pull(opts commands.DockerComposePullOptions) error { pullCmd := cm.cmdRunner.BuildDockerComposePullCMD(opts) + log.Infof(configs.RunningCommand, pullCmd.Cmd) if out, exitCode, err := cm.cmdRunner.RunCMD(pullCmd); err != nil || exitCode != 0 { return fmt.Errorf("%w: %s. Output: %s", DockerComposeCmdError{Cmd: "pull"}, err, out) } @@ -69,6 +73,7 @@ func (cm *ComposeManager) Pull(opts commands.DockerComposePullOptions) error { func (cm *ComposeManager) Create(opts commands.DockerComposeCreateOptions) error { createCmd := cm.cmdRunner.BuildDockerComposeCreateCMD(opts) + log.Infof(configs.RunningCommand, createCmd.Cmd) if out, exitCode, err := cm.cmdRunner.RunCMD(createCmd); err != nil || exitCode != 0 { return fmt.Errorf("%w: %s. Output: %s", DockerComposeCmdError{Cmd: "create"}, err, out) } @@ -79,6 +84,7 @@ func (cm *ComposeManager) Create(opts commands.DockerComposeCreateOptions) error func (cm *ComposeManager) Build(opts commands.DockerComposeBuildOptions) error { buildCmd := cm.cmdRunner.BuildDockerComposeBuildCMD(opts) + log.Infof(configs.RunningCommand, buildCmd.Cmd) if out, exitCode, err := cm.cmdRunner.RunCMD(buildCmd); err != nil || exitCode != 0 { return fmt.Errorf("%w: %s. Output: %s", DockerComposeCmdError{Cmd: "build"}, err, out) } @@ -90,6 +96,7 @@ func (cm *ComposeManager) Build(opts commands.DockerComposeBuildOptions) error { func (c *ComposeManager) PS(opts commands.DockerComposePsOptions) ([]ComposeService, error) { psCmd := c.cmdRunner.BuildDockerComposePSCMD(opts) + log.Infof(configs.RunningCommand, psCmd.Cmd) out, exitCode, err := c.cmdRunner.RunCMD(psCmd) if err != nil || exitCode != 0 { return nil, fmt.Errorf("%w: %s. Output: %s", DockerComposeCmdError{Cmd: "ps"}, err, out) @@ -130,7 +137,11 @@ func (c *ComposeManager) PS(opts commands.DockerComposePsOptions) ([]ComposeServ func (cm *ComposeManager) Logs(opts commands.DockerComposeLogsOptions) error { logsCmd := cm.cmdRunner.BuildDockerComposeLogsCMD(opts) + log.Infof(configs.RunningCommand, logsCmd.Cmd) if out, exitCode, err := cm.cmdRunner.RunCMD(logsCmd); err != nil || exitCode != 0 { + if exitCode == 130 { + return nil + } return fmt.Errorf("%w: %s. Output: %s", DockerComposeCmdError{Cmd: "logs"}, err, out) } return nil @@ -140,6 +151,7 @@ func (cm *ComposeManager) Logs(opts commands.DockerComposeLogsOptions) error { func (cm *ComposeManager) Stop(opts DockerComposeStopOptions) error { stopCmd := fmt.Sprintf("docker compose -f %s stop", opts.Path) + log.Infof(configs.RunningCommand, stopCmd) if out, exitCode, err := cm.cmdRunner.RunCMD(commands.Command{Cmd: stopCmd, GetOutput: true}); err != nil || exitCode != 0 { return fmt.Errorf("%w: %s. Output: %s", DockerComposeCmdError{Cmd: "stop"}, err, out) } @@ -150,6 +162,7 @@ func (cm *ComposeManager) Stop(opts DockerComposeStopOptions) error { func (cm *ComposeManager) Down(opts commands.DockerComposeDownOptions) error { downCmd := cm.cmdRunner.BuildDockerComposeDownCMD(opts) + log.Infof(configs.RunningCommand, downCmd.Cmd) if out, exitCode, err := cm.cmdRunner.RunCMD(downCmd); err != nil || exitCode != 0 { return fmt.Errorf("%w: %s. Output: %s", DockerComposeCmdError{Cmd: "down"}, err, out) }