Skip to content

Commit

Permalink
feat: Add installer configuration to services
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 19, 2024
1 parent 403366e commit 5eb50c4
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 18 deletions.
5 changes: 4 additions & 1 deletion cmd/cli/commands/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/cli/commands/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/cli/commands/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
},
Expand Down
22 changes: 22 additions & 0 deletions internal/installer/config.go
Original file line number Diff line number Diff line change
@@ -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",
}
}
13 changes: 10 additions & 3 deletions internal/service/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"strconv"
"strings"
"time"

"github.com/ethpandaops/contributoor-installer/internal/installer"
"github.com/sirupsen/logrus"
)

var (
Expand Down Expand Up @@ -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,
},
Expand Down
7 changes: 5 additions & 2 deletions internal/service/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
13 changes: 9 additions & 4 deletions internal/sidecar/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand All @@ -41,6 +43,7 @@ func NewDockerSidecar(logger *logrus.Logger, configService ConfigManager) (Docke
composePath: filepath.Clean(composePath),
configPath: configService.GetConfigPath(),
configService: configService,
installerCfg: installerCfg,
}, nil
}

Expand Down Expand Up @@ -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,
)

Expand Down
12 changes: 7 additions & 5 deletions internal/sidecar/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5eb50c4

Please sign in to comment.