Skip to content

Commit d3c1f36

Browse files
committed
Run two chains
1 parent 9620350 commit d3c1f36

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

examples/running-two-chains.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Running two chains
2+
3+
This example shows how to run two chains on the same machine.
4+
5+
First, we need to deploy the first chain:
6+
7+
```bash
8+
$ go run main.go cook opstack
9+
```
10+
11+
This chain is going to run under the default `ethplayground` Docker network. Playground uses DNS resolution to discover services in the same network.
12+
13+
In order to run a second chain, we can use the same command and specify a different network name:
14+
15+
```bash
16+
$ go run main.go cook opstack --network eth2
17+
```
18+
19+
This will deploy the second chain under the `eth2` Docker network.

internal/local_runner.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"gopkg.in/yaml.v2"
2626
)
2727

28-
const networkName = "ethplayground"
28+
const defaultNetworkName = "ethplayground"
2929

3030
// LocalRunner is a component that runs the services from the manifest on the local host machine.
3131
// By default, it uses docker and docker compose to run all the services.
@@ -67,6 +67,9 @@ type LocalRunner struct {
6767
// sessionID is a random sequence that is used to identify the session
6868
// it is used to identify the containers in the cleanup process
6969
sessionID string
70+
71+
// networkName is the name of the network to use for the services
72+
networkName string
7073
}
7174

7275
type task struct {
@@ -96,7 +99,8 @@ func newDockerClient() (*client.Client, error) {
9699
return client, nil
97100
}
98101

99-
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool) (*LocalRunner, error) {
102+
// TODO: add a runner config struct
103+
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool, networkName string) (*LocalRunner, error) {
100104
client, err := newDockerClient()
101105
if err != nil {
102106
return nil, fmt.Errorf("failed to create docker client: %w", err)
@@ -141,6 +145,9 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
141145
}
142146
}
143147

148+
if networkName == "" {
149+
networkName = defaultNetworkName
150+
}
144151
d := &LocalRunner{
145152
out: out,
146153
manifest: manifest,
@@ -153,6 +160,7 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
153160
exitErr: make(chan error, 2),
154161
bindHostPortsLocally: bindHostPortsLocally,
155162
sessionID: uuid.New().String(),
163+
networkName: networkName,
156164
}
157165

158166
if interactive {
@@ -563,7 +571,7 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
563571
fmt.Sprintf("%s:/artifacts", outputFolder),
564572
},
565573
// Add the ethereum network
566-
"networks": []string{networkName},
574+
"networks": []string{d.networkName},
567575
"labels": labels,
568576
}
569577

@@ -648,8 +656,8 @@ func (d *LocalRunner) generateDockerCompose() ([]byte, error) {
648656
// We create a new network to be used by all the services so that
649657
// we can do DNS discovery between them.
650658
"networks": map[string]interface{}{
651-
networkName: map[string]interface{}{
652-
"name": networkName,
659+
d.networkName: map[string]interface{}{
660+
"name": d.networkName,
653661
},
654662
},
655663
}

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var timeout time.Duration
2626
var logLevelFlag string
2727
var bindExternal bool
2828
var withPrometheus bool
29+
var networkName string
2930

3031
var rootCmd = &cobra.Command{
3132
Use: "playground",
@@ -171,6 +172,7 @@ func main() {
171172
recipeCmd.Flags().StringVar(&logLevelFlag, "log-level", "info", "log level")
172173
recipeCmd.Flags().BoolVar(&bindExternal, "bind-external", false, "bind host ports to external interface")
173174
recipeCmd.Flags().BoolVar(&withPrometheus, "with-prometheus", false, "whether to gather the Prometheus metrics")
175+
recipeCmd.Flags().StringVar(&networkName, "network", "", "network name")
174176

175177
cookCmd.AddCommand(recipeCmd)
176178
}
@@ -244,7 +246,7 @@ func runIt(recipe internal.Recipe) error {
244246
}
245247
}
246248

247-
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal)
249+
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal, networkName)
248250
if err != nil {
249251
return fmt.Errorf("failed to create docker runner: %w", err)
250252
}

0 commit comments

Comments
 (0)