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

refactor(build): amd/arm builds #60

Merged
merged 7 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
sentry:
container_name: contributoor
image: ethpandaops/contributoor:${CONTRIBUTOOR_VERSION}
image: ethpandaops/contributoor:${CONTRIBUTOOR_VERSION}-${CONTRIBUTOOR_ARCH_SUFFIX}
entrypoint: ["/usr/local/bin/sentry"]
command: ["--config=/config/config.yaml"]
extra_hosts:
Expand Down
43 changes: 42 additions & 1 deletion install.bats
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,23 @@ EOF
echo "$output" | grep -q "Checksum mismatch"
}

@test "setup_docker_contributoor pulls image" {
@test "setup_docker_contributoor pulls amd64 image" {
# Set required variables
ARCH="amd64"
PLATFORM="linux"
CONTRIBUTOOR_VERSION="1.0.0"

# Mock docker commands
function docker() {
case "$1" in
"system")
return 0
;;
"pull")
if [[ "$2" != "ethpandaops/contributoor:${CONTRIBUTOOR_VERSION}-amd64" ]]; then
echo "Invalid image tag: $2"
return 1
fi
return 0
;;
esac
Expand All @@ -507,6 +516,38 @@ EOF
export -f docker

run setup_docker_contributoor
echo "Status: $status"
echo "Output: $output"
[ "$status" -eq 0 ]
}

@test "setup_docker_contributoor pulls arm64 image" {
# Set required variables
ARCH="arm64"
PLATFORM="linux"
CONTRIBUTOOR_VERSION="1.0.0"

# Mock docker commands
function docker() {
case "$1" in
"system")
return 0
;;
"pull")
if [[ "$2" != "ethpandaops/contributoor:${CONTRIBUTOOR_VERSION}-arm64v8" ]]; then
echo "Invalid image tag: $2"
return 1
fi
return 0
;;
esac
}

export -f docker

run setup_docker_contributoor
echo "Status: $status"
echo "Output: $output"
[ "$status" -eq 0 ]
}

Expand Down
12 changes: 10 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,19 @@ setup_installer() {
setup_docker_contributoor() {
docker system prune -f >/dev/null 2>&1 || true

docker pull "ethpandaops/contributoor:${CONTRIBUTOOR_VERSION}" >/dev/null 2>&1 &
# Map architecture to Docker image suffix
local arch_suffix
case "$ARCH" in
amd64) arch_suffix="amd64" ;;
arm64) arch_suffix="arm64v8" ;;
*) fail "Unsupported architecture for Docker: $ARCH" ;;
esac

docker pull "ethpandaops/contributoor:${CONTRIBUTOOR_VERSION}-${arch_suffix}" >/dev/null 2>&1 &
spinner $!
wait $!
[ $? -ne 0 ] && fail "Failed to pull docker image"
success "Pulled docker image: ethpandaops/contributoor:${CONTRIBUTOOR_VERSION}"
success "Pulled docker image: ethpandaops/contributoor:${CONTRIBUTOOR_VERSION}-${arch_suffix}"
}

setup_binary_contributoor() {
Expand Down
49 changes: 43 additions & 6 deletions internal/sidecar/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (

type DockerSidecar interface {
SidecarRunner
// GetArchSuffix returns the architecture suffix for the docker image.
GetArchSuffix() (string, error)
// GetComposeEnv returns the environment variables for docker-compose.
GetComposeEnv() []string
}

// dockerSidecar is a basic service for interacting with the docker container.
Expand Down Expand Up @@ -81,7 +85,7 @@ func (s *dockerSidecar) Start() error {
args := append(s.getComposeArgs(), "up", "-d", "--pull", "always")

cmd := exec.Command("docker", args...)
cmd.Env = s.getComposeEnv()
cmd.Env = s.GetComposeEnv()

if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to start containers: %w\nOutput: %s", err, string(output))
Expand All @@ -103,7 +107,7 @@ func (s *dockerSidecar) Stop() error {
"--timeout", "30")

cmd := exec.Command("docker", args...)
cmd.Env = s.getComposeEnv()
cmd.Env = s.GetComposeEnv()

if output, err := cmd.CombinedOutput(); err != nil {
// Don't return error here, try our fallback.
Expand All @@ -128,7 +132,7 @@ func (s *dockerSidecar) IsRunning() (bool, error) {
// versions, then this will return a non running state.
args := append(s.getComposeArgs(), "ps", "--format", "{{.State}}")
cmd := exec.Command("docker", args...)
cmd.Env = s.getComposeEnv()
cmd.Env = s.GetComposeEnv()

output, err := cmd.Output()
if err == nil {
Expand Down Expand Up @@ -172,7 +176,12 @@ func (s *dockerSidecar) Update() error {
func (s *dockerSidecar) updateSidecar() error {
cfg := s.sidecarCfg.Get()

image := fmt.Sprintf("%s:%s", s.installerCfg.DockerImage, cfg.Version)
archSuffix, err := s.GetArchSuffix()
if err != nil {
return err
}

image := fmt.Sprintf("%s:%s%s", s.installerCfg.DockerImage, cfg.Version, archSuffix)

cmd := exec.Command("docker", "pull", image)
if output, err := cmd.CombinedOutput(); err != nil {
Expand Down Expand Up @@ -215,13 +224,41 @@ func validateComposePath(path string) error {
return nil
}

func (s *dockerSidecar) getComposeEnv() []string {
// GetArchSuffix determines the Docker image architecture suffix based on system architecture.
func (s *dockerSidecar) GetArchSuffix() (string, error) {
cmd := exec.Command("uname", "-m")

output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to determine system architecture: %w", err)
}

switch strings.TrimSpace(string(output)) {
case "x86_64", "amd64":
return "amd64", nil
case "arm64", "aarch64", "arm64e":
return "arm64v8", nil
default:
return "", fmt.Errorf("unsupported architecture: %s", string(output))
}
}

// GetComposeEnv returns the environment variables for docker-compose.
func (s *dockerSidecar) GetComposeEnv() []string {
cfg := s.sidecarCfg.Get()

archSuffix, err := s.GetArchSuffix()
if err != nil {
s.logger.Errorf("%v", err)

return nil
}

env := append(
os.Environ(),
fmt.Sprintf("CONTRIBUTOOR_CONFIG_PATH=%s", filepath.Dir(s.configPath)),
fmt.Sprintf("CONTRIBUTOOR_VERSION=%s", cfg.Version),
fmt.Sprintf("CONTRIBUTOOR_ARCH_SUFFIX=%s", archSuffix),
)

// Add docker network if using docker
Expand Down Expand Up @@ -263,7 +300,7 @@ func (s *dockerSidecar) Logs(tailLines int, follow bool) error {
}

cmd := exec.Command("docker", args...)
cmd.Env = s.getComposeEnv()
cmd.Env = s.GetComposeEnv()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = filepath.Dir(s.composePath)
Expand Down
Loading
Loading