Skip to content

Commit 0b7f850

Browse files
authored
Add ready fn (#58)
* Add ready fn * Fix * Fix CI * Fix logs upload * Fix ci * Add checks * Add * Add more * One more * Explicit log * More * God * Go * Add * Try again * Add last? * Cleaning
1 parent 8cea709 commit 0b7f850

File tree

9 files changed

+110
-11
lines changed

9 files changed

+110
-11
lines changed

.github/workflows/checks.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ jobs:
3636
- name: Run playground
3737
run: go run main.go cook ${{ matrix.flags }} --output /tmp/playground --timeout 4m --watchdog
3838

39-
- name: Move playground logs
39+
- name: Copy playground logs
4040
if: ${{ failure() }}
41-
run: mv /tmp/playground.log /tmp/playground/logs
41+
run: ./scripts/ci-copy-playground-logs.sh /tmp/playground /tmp/playground-logs
4242

4343
- name: Archive playground logs
4444
uses: actions/upload-artifact@v4
4545
if: ${{ failure() }}
4646
with:
4747
name: playground-logs-${{ matrix.flags }}
48-
path: /tmp/playground/logs
48+
path: /tmp/playground-logs
4949
retention-days: 5

internal/components.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package internal
22

3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"time"
8+
)
9+
310
var defaultJWTToken = "04592280e1778419b7aa954d43871cb2cfb2ebda754fb735e8adeb293a88f9bf"
411

512
var (
@@ -267,6 +274,17 @@ func (l *LighthouseBeaconNode) Run(svc *service, ctx *ExContext) {
267274
}
268275
}
269276

277+
var _ ServiceReady = &LighthouseBeaconNode{}
278+
279+
func (l *LighthouseBeaconNode) Ready(logOutput io.Writer, service *service, ctx context.Context) error {
280+
beaconNodeURL := fmt.Sprintf("http://localhost:%d", service.MustGetPort("http").HostPort)
281+
282+
if err := waitForChainAlive(logOutput, beaconNodeURL, 30*time.Second); err != nil {
283+
return err
284+
}
285+
return nil
286+
}
287+
270288
type LighthouseValidator struct {
271289
BeaconNode string
272290
}

internal/local_runner.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"os"
99
"os/exec"
10+
"runtime"
1011
"strings"
1112
"sync"
1213
"text/template"
@@ -374,6 +375,16 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
374375
"labels": map[string]string{"playground": "true"},
375376
}
376377

378+
if runtime.GOOS == "linux" {
379+
// We rely on host.docker.internal as the DNS address for the host inside
380+
// the container. But, this is only available on Macos and Windows.
381+
// On Linux, you can use the IP address 172.17.0.1 to access the host.
382+
// Thus, if we are running on Linux, we need to add an extra host entry.
383+
service["extra_hosts"] = map[string]string{
384+
"host.docker.internal": "172.17.0.1",
385+
}
386+
}
387+
377388
if s.entrypoint != "" {
378389
service["entrypoint"] = s.entrypoint
379390
}
@@ -428,7 +439,6 @@ func (d *LocalRunner) generateDockerCompose() ([]byte, error) {
428439
}
429440

430441
compose["services"] = services
431-
432442
yamlData, err := yaml.Marshal(compose)
433443
if err != nil {
434444
return nil, fmt.Errorf("failed to marshal docker compose: %w", err)

internal/manifest.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package internal
22

33
import (
4+
"context"
45
"fmt"
6+
"io"
57
"strings"
8+
"sync"
69
"text/template"
710

811
flag "github.com/spf13/pflag"
@@ -74,6 +77,43 @@ type Service interface {
7477
Run(service *service, ctx *ExContext)
7578
}
7679

80+
type ServiceReady interface {
81+
Ready(out io.Writer, service *service, ctx context.Context) error
82+
}
83+
84+
func WaitForReady(manifest *Manifest) error {
85+
var wg sync.WaitGroup
86+
readyErr := make(chan error, len(manifest.Services()))
87+
88+
output, err := manifest.out.LogOutput("ready")
89+
if err != nil {
90+
return fmt.Errorf("failed to create log output: %w", err)
91+
}
92+
93+
for _, s := range manifest.Services() {
94+
if readyFn, ok := s.component.(ServiceReady); ok {
95+
wg.Add(1)
96+
97+
go func() {
98+
defer wg.Done()
99+
100+
if err := readyFn.Ready(output, s, context.Background()); err != nil {
101+
readyErr <- fmt.Errorf("service %s failed to start: %w", s.Name, err)
102+
}
103+
}()
104+
}
105+
}
106+
wg.Wait()
107+
108+
close(readyErr)
109+
for err := range readyErr {
110+
if err != nil {
111+
return err
112+
}
113+
}
114+
return nil
115+
}
116+
77117
func (s *Manifest) Services() []*service {
78118
return s.services
79119
}

internal/recipe_l1.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (l *L1Recipe) Flags() *flag.FlagSet {
3838
flags := flag.NewFlagSet("l1", flag.ContinueOnError)
3939
flags.BoolVar(&l.latestFork, "latest-fork", false, "use the latest fork")
4040
flags.BoolVar(&l.useRethForValidation, "use-reth-for-validation", false, "use reth for validation")
41-
flags.Uint64Var(&l.secondaryELPort, "secondary-el", 1234, "port to use for the secondary builder")
41+
flags.Uint64Var(&l.secondaryELPort, "secondary-el", 0, "port to use for the secondary builder")
4242
flags.BoolVar(&l.useNativeReth, "use-native-reth", false, "use the native reth binary")
4343
return flags
4444
}
@@ -100,9 +100,6 @@ func (l *L1Recipe) Watchdog(manifest *Manifest, out *output) error {
100100
}
101101

102102
beaconNodeURL := fmt.Sprintf("http://localhost:%d", beaconNode.MustGetPort("http").HostPort)
103-
if err := waitForChainAlive(watchDogOut, beaconNodeURL, 30*time.Second); err != nil {
104-
return err
105-
}
106103
beaconNodeELURL := fmt.Sprintf("http://localhost:%d", beaconNodeEL.MustGetPort("http").HostPort)
107104

108105
watchGroup := newWatchGroup()

internal/releases.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"compress/gzip"
66
"fmt"
77
"io"
8+
"log"
89
"net/http"
910
"os"
1011
"os/exec"
@@ -40,17 +41,17 @@ func downloadRelease(outputFolder string, artifact *release) (string, error) {
4041
archVersion := artifact.Arch(goos, goarch)
4142
if archVersion == "" {
4243
// Case 2. The architecture is not supported.
43-
fmt.Printf("unsupported OS/Arch: %s/%s\n", goos, goarch)
44+
log.Printf("unsupported OS/Arch: %s/%s\n", goos, goarch)
4445
if _, err := exec.LookPath(artifact.Name); err != nil {
4546
return "", fmt.Errorf("error looking up binary in PATH: %v", err)
4647
} else {
4748
outPath = artifact.Name
48-
fmt.Printf("Using %s from PATH\n", artifact.Name)
49+
log.Printf("Using %s from PATH\n", artifact.Name)
4950
}
5051
} else {
5152
// Case 3. Download the binary from the release page
5253
releasesURL := fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s-%s-%s.tar.gz", artifact.Org, artifact.Name, artifact.Version, artifact.Name, artifact.Version, archVersion)
53-
fmt.Printf("Downloading %s: %s\n", outPath, releasesURL)
54+
log.Printf("Downloading %s: %s\n", outPath, releasesURL)
5455

5556
if err := downloadArtifact(releasesURL, artifact.Name, outPath); err != nil {
5657
return "", fmt.Errorf("error downloading artifact: %v", err)

internal/watchers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ func watchChainHead(logOutput io.Writer, elURL string, blockTime time.Duration)
190190
if latestBlock != nil && num <= *latestBlock {
191191
continue
192192
}
193+
log.Infof("Chain head: %d", num)
193194
latestBlock = &num
194195

195196
// Reset timeout since we saw a new block

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ func runIt(recipe internal.Recipe) error {
138138
}
139139
}
140140

141+
// wait for service readiness
142+
if err := internal.WaitForReady(svcManager); err != nil {
143+
dockerRunner.Stop()
144+
return fmt.Errorf("failed to wait for service readiness: %w", err)
145+
}
146+
141147
watchdogErr := make(chan error, 1)
142148
if watchdog {
143149
go func() {

scripts/ci-copy-playground-logs.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
if [ "$#" -ne 2 ]; then
4+
echo "Usage: $0 source_folder destination_folder"
5+
exit 1
6+
fi
7+
8+
source_dir="$1"
9+
dest_dir="$2"
10+
11+
# Check if source directory exists
12+
if [ ! -d "$source_dir" ]; then
13+
echo "Error: Source directory '$source_dir' does not exist"
14+
exit 1
15+
fi
16+
17+
# Create destination directory if it doesn't exist
18+
mkdir -p "$dest_dir"
19+
20+
# First, copy everything
21+
cp -r "$source_dir"/* "$dest_dir"/ 2>/dev/null || true
22+
23+
# Remove any data_* directories from the destination
24+
find "$dest_dir" -type d -name "data_*" -exec rm -rf {} +
25+
26+
echo "Copied contents from '$source_dir' to '$dest_dir' (excluding data_* folders)"

0 commit comments

Comments
 (0)