From 5eb50c4011fd544d8547079dca8e975003b67b72 Mon Sep 17 00:00:00 2001 From: Matty Evans Date: Fri, 20 Dec 2024 07:39:57 +1000 Subject: [PATCH] feat: Add installer configuration to services --- cmd/cli/commands/start/start.go | 5 ++++- cmd/cli/commands/stop/stop.go | 5 ++++- cmd/cli/commands/update/update.go | 7 +++++-- internal/installer/config.go | 22 ++++++++++++++++++++++ internal/service/github.go | 13 ++++++++++--- internal/service/github_test.go | 7 +++++-- internal/sidecar/docker.go | 13 +++++++++---- internal/sidecar/docker_test.go | 12 +++++++----- 8 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 internal/installer/config.go diff --git a/cmd/cli/commands/start/start.go b/cmd/cli/commands/start/start.go index 27b13ad..0ac2f75 100644 --- a/cmd/cli/commands/start/start.go +++ b/cmd/cli/commands/start/start.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ethpandaops/contributoor-installer/cmd/cli/options" + "github.com/ethpandaops/contributoor-installer/internal/installer" "github.com/ethpandaops/contributoor-installer/internal/sidecar" "github.com/ethpandaops/contributoor-installer/internal/tui" "github.com/sirupsen/logrus" @@ -19,12 +20,14 @@ func RegisterCommands(app *cli.App, opts *options.CommandOpts) error { Action: func(c *cli.Context) error { log := opts.Logger() + installerCfg := installer.NewConfig() + sidecarConfig, err := sidecar.NewConfigService(log, c.GlobalString("config-path")) if err != nil { return fmt.Errorf("error loading config: %w", err) } - dockerSidecar, err := sidecar.NewDockerSidecar(log, sidecarConfig) + dockerSidecar, err := sidecar.NewDockerSidecar(log, sidecarConfig, installerCfg) if err != nil { return fmt.Errorf("error creating docker sidecar service: %w", err) } diff --git a/cmd/cli/commands/stop/stop.go b/cmd/cli/commands/stop/stop.go index d884aab..47a1a5f 100644 --- a/cmd/cli/commands/stop/stop.go +++ b/cmd/cli/commands/stop/stop.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ethpandaops/contributoor-installer/cmd/cli/options" + "github.com/ethpandaops/contributoor-installer/internal/installer" "github.com/ethpandaops/contributoor-installer/internal/sidecar" "github.com/ethpandaops/contributoor-installer/internal/tui" "github.com/sirupsen/logrus" @@ -19,12 +20,14 @@ func RegisterCommands(app *cli.App, opts *options.CommandOpts) error { Action: func(c *cli.Context) error { log := opts.Logger() + installerCfg := installer.NewConfig() + sidecarConfig, err := sidecar.NewConfigService(log, c.GlobalString("config-path")) if err != nil { return fmt.Errorf("error loading config: %w", err) } - dockerSidecar, err := sidecar.NewDockerSidecar(log, sidecarConfig) + dockerSidecar, err := sidecar.NewDockerSidecar(log, sidecarConfig, installerCfg) if err != nil { return fmt.Errorf("error creating docker sidecar service: %w", err) } diff --git a/cmd/cli/commands/update/update.go b/cmd/cli/commands/update/update.go index cdab444..55cf21b 100644 --- a/cmd/cli/commands/update/update.go +++ b/cmd/cli/commands/update/update.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ethpandaops/contributoor-installer/cmd/cli/options" + "github.com/ethpandaops/contributoor-installer/internal/installer" "github.com/ethpandaops/contributoor-installer/internal/service" "github.com/ethpandaops/contributoor-installer/internal/sidecar" "github.com/ethpandaops/contributoor-installer/internal/tui" @@ -27,18 +28,20 @@ func RegisterCommands(app *cli.App, opts *options.CommandOpts) error { Action: func(c *cli.Context) error { log := opts.Logger() + installerCfg := installer.NewConfig() + sidecarConfig, err := sidecar.NewConfigService(log, c.GlobalString("config-path")) if err != nil { return fmt.Errorf("error loading config: %w", err) } - dockerSidecar, err := sidecar.NewDockerSidecar(log, sidecarConfig) + dockerSidecar, err := sidecar.NewDockerSidecar(log, sidecarConfig, installerCfg) if err != nil { return fmt.Errorf("error creating docker sidecar service: %w", err) } binarySidecar := sidecar.NewBinarySidecar(log, sidecarConfig) - githubService := service.NewGitHubService("ethpandaops", "contributoor") + githubService := service.NewGitHubService(log, installerCfg) return updateContributoor(c, log, sidecarConfig, dockerSidecar, binarySidecar, githubService) }, diff --git a/internal/installer/config.go b/internal/installer/config.go new file mode 100644 index 0000000..7fe0a4e --- /dev/null +++ b/internal/installer/config.go @@ -0,0 +1,22 @@ +package installer + +// Config holds installer-specific configuration that isn't exposed to the sidecar. +type Config struct { + // DockerImage is the image name of the sidecar. + DockerImage string + // DockerTag is the tag of the sidecar. + DockerTag string + // GithubOrg is the organization name housing the sidecar repository. + GithubOrg string + // GithubRepo is the repository name of the sidecar repository. + GithubRepo string +} + +// NewConfig returns the default installer configuration. +func NewConfig() *Config { + return &Config{ + DockerImage: "ethpandaops/contributoor", + GithubOrg: "ethpandaops", + GithubRepo: "contributoor", + } +} diff --git a/internal/service/github.go b/internal/service/github.go index 475f11f..d7bac6c 100644 --- a/internal/service/github.go +++ b/internal/service/github.go @@ -9,6 +9,9 @@ import ( "strconv" "strings" "time" + + "github.com/ethpandaops/contributoor-installer/internal/installer" + "github.com/sirupsen/logrus" ) var ( @@ -55,16 +58,20 @@ type GitHubRelease struct { // githubService is a basic service for interacting with the GitHub API. type githubService struct { + log *logrus.Logger owner string repo string client *http.Client + config *installer.Config } // NewGitHubService creates a new GitHubService. -func NewGitHubService(owner, repo string) GitHubService { +func NewGitHubService(log *logrus.Logger, config *installer.Config) GitHubService { return &githubService{ - owner: owner, - repo: repo, + log: log, + owner: config.GithubOrg, + repo: config.GithubRepo, + config: config, client: &http.Client{ Timeout: 10 * time.Second, }, diff --git a/internal/service/github_test.go b/internal/service/github_test.go index 4733156..f1ee0cc 100644 --- a/internal/service/github_test.go +++ b/internal/service/github_test.go @@ -7,6 +7,9 @@ import ( "net/url" "strings" "testing" + + "github.com/ethpandaops/contributoor-installer/internal/installer" + "github.com/sirupsen/logrus" ) func TestGitHubService_GetLatestVersion(t *testing.T) { @@ -69,7 +72,7 @@ func TestGitHubService_GetLatestVersion(t *testing.T) { } defer func() { validateGitHubURL = validate }() - svc := NewGitHubService("test", "repo") + svc := NewGitHubService(logrus.New(), installer.NewConfig()) got, err := svc.GetLatestVersion() if (err != nil) != tt.wantErr { @@ -140,7 +143,7 @@ func TestGitHubService_VersionExists(t *testing.T) { } defer func() { validateGitHubURL = validate }() - svc := NewGitHubService("test", "repo") + svc := NewGitHubService(logrus.New(), installer.NewConfig()) exists, err := svc.VersionExists(tt.version) diff --git a/internal/sidecar/docker.go b/internal/sidecar/docker.go index 4c1fd05..433e8dc 100644 --- a/internal/sidecar/docker.go +++ b/internal/sidecar/docker.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strings" + "github.com/ethpandaops/contributoor-installer/internal/installer" "github.com/ethpandaops/contributoor-installer/internal/tui" "github.com/sirupsen/logrus" ) @@ -23,10 +24,11 @@ type dockerSidecar struct { composePath string configPath string configService ConfigManager + installerCfg *installer.Config } // NewDockerSidecar creates a new DockerSidecar. -func NewDockerSidecar(logger *logrus.Logger, configService ConfigManager) (DockerSidecar, error) { +func NewDockerSidecar(logger *logrus.Logger, configService ConfigManager, installerCfg *installer.Config) (DockerSidecar, error) { composePath, err := findComposeFile() if err != nil { return nil, fmt.Errorf("failed to find docker-compose.yml: %w", err) @@ -41,6 +43,7 @@ func NewDockerSidecar(logger *logrus.Logger, configService ConfigManager) (Docke composePath: filepath.Clean(composePath), configPath: configService.GetConfigPath(), configService: configService, + installerCfg: installerCfg, }, nil } @@ -105,14 +108,16 @@ func (s *dockerSidecar) Update() error { cfg := s.configService.Get() //nolint:gosec // validateComposePath() and filepath.Clean() in-use. - cmd := exec.Command("docker", "pull", fmt.Sprintf("ethpandaops/contributoor:%s", cfg.Version)) + image := fmt.Sprintf("%s:%s", s.installerCfg.DockerImage, cfg.Version) + cmd := exec.Command("docker", "pull", image) if output, err := cmd.CombinedOutput(); err != nil { - return fmt.Errorf("failed to pull image: %w\nOutput: %s", err, string(output)) + return fmt.Errorf("failed to pull image %s: %w\nOutput: %s", image, err, string(output)) } s.logger.WithField("version", cfg.Version).Infof( - "%sImage updated successfully%s", + "%sImage %s updated successfully%s", tui.TerminalColorGreen, + image, tui.TerminalColorReset, ) diff --git a/internal/sidecar/docker_test.go b/internal/sidecar/docker_test.go index f5dfbbb..c20319a 100644 --- a/internal/sidecar/docker_test.go +++ b/internal/sidecar/docker_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/docker/go-connections/nat" + "github.com/ethpandaops/contributoor-installer/internal/installer" "github.com/ethpandaops/contributoor-installer/internal/sidecar" "github.com/ethpandaops/contributoor-installer/internal/sidecar/mock" "github.com/sirupsen/logrus" @@ -57,10 +58,11 @@ func TestDockerService_Integration(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() - mockConfig := mock.NewMockConfigManager(ctrl) - mockConfig.EXPECT().Get().Return(cfg).AnyTimes() - mockConfig.EXPECT().GetConfigDir().Return(tmpDir).AnyTimes() - mockConfig.EXPECT().GetConfigPath().Return(filepath.Join(tmpDir, "config.yaml")).AnyTimes() + mockInstallerConfig := installer.NewConfig() + mockSidecarConfig := mock.NewMockConfigManager(ctrl) + mockSidecarConfig.EXPECT().Get().Return(cfg).AnyTimes() + mockSidecarConfig.EXPECT().GetConfigDir().Return(tmpDir).AnyTimes() + mockSidecarConfig.EXPECT().GetConfigPath().Return(filepath.Join(tmpDir, "config.yaml")).AnyTimes() container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ContainerRequest: testcontainers.ContainerRequest{ @@ -95,7 +97,7 @@ func TestDockerService_Integration(t *testing.T) { require.NoError(t, err) // Create docker service with mock config - ds, err := sidecar.NewDockerSidecar(logger, mockConfig) + ds, err := sidecar.NewDockerSidecar(logger, mockSidecarConfig, mockInstallerConfig) require.NoError(t, err) // Set docker host to test container.