Skip to content

Commit

Permalink
feat: Add command to run dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoura committed Jan 11, 2024
1 parent 4eb5c28 commit 86e781a
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 0 deletions.
77 changes: 77 additions & 0 deletions cmd/cartesi-rollups-cli/root/deps/deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package deps

import (
"os/signal"
"syscall"

"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/internal/deps"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "run-deps",
Short: "Run node dependencies with Docker",
Example: examples,
Run: run,
}

const examples = `# Run all deps:
cartesi-rollups-cli run-deps`

var (
postgresDockerImage string
postgresPort string
postgresPassword string

devnetDockerImage string
devnetPort string
)

func init() {
Cmd.Flags().StringVar(&postgresDockerImage, "postgres-docker-image",
deps.DefaultPostgresDockerImage,
"Postgress docker image name")

Cmd.Flags().StringVar(&postgresPort, "postgres-mapped-port",
deps.DefaultPostgresPort,
"Postgres local listening port number")

Cmd.Flags().StringVar(&postgresPassword, "postgres-password",
deps.DefaultPostgresPassword,
"Postgres password")

Cmd.Flags().StringVar(&devnetDockerImage, "devnet-docker-image",
deps.DefaultDevnetDockerImage,
"Devnet docker image name")

Cmd.Flags().StringVar(&devnetPort, "devnet-mapped-port",
deps.DefaultDevnetPort,
"devnet local listening port number")
}

func run(cmd *cobra.Command, args []string) {

ctx, cancel := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

depsConfig := deps.NewDefaultDepsConfig()
depsConfig.PostgresDockerImage = postgresDockerImage
depsConfig.PostgresPort = postgresPort
depsConfig.PostgresPassword = postgresPassword
depsConfig.DevnetDockerImage = devnetDockerImage
depsConfig.DevnetPort = devnetPort

_, err := deps.Run(ctx, *depsConfig)
cobra.CheckErr(err)

config.InfoLogger.Println("all deps are up")

<-ctx.Done()

config.InfoLogger.Println("killing deps")

}
2 changes: 2 additions & 0 deletions cmd/cartesi-rollups-cli/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package root

import (
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/deps"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/read"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/savesnapshot"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/send"
Expand All @@ -21,5 +22,6 @@ func init() {
Cmd.AddCommand(send.Cmd)
Cmd.AddCommand(read.Cmd)
Cmd.AddCommand(savesnapshot.Cmd)
Cmd.AddCommand(deps.Cmd)
Cmd.DisableAutoGenTag = true
}
1 change: 1 addition & 0 deletions docs/cartesi-rollups-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Cartesi Rollups node.

### SEE ALSO

* [cartesi-rollups-cli deps](cartesi-rollups-cli_deps.md) - Read the node state from the GraphQL API
* [cartesi-rollups-cli read](cartesi-rollups-cli_read.md) - Read the node state from the GraphQL API
* [cartesi-rollups-cli save-snapshot](cartesi-rollups-cli_save-snapshot.md) - Saves the testing Cartesi machine snapshot to the designated folder
* [cartesi-rollups-cli send](cartesi-rollups-cli_send.md) - Send a rollups input to the Ethereum node
Expand Down
15 changes: 15 additions & 0 deletions docs/cartesi-rollups-cli_deps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## cartesi-rollups-cli deps

Read the node state from the GraphQL API

### Options

```
-h, --help help for deps
```

### SEE ALSO

* [cartesi-rollups-cli](cartesi-rollups-cli.md) - Command line interface for Cartesi Rollups
* [cartesi-rollups-cli deps start](cartesi-rollups-cli_deps_start.md) - start all deps

30 changes: 30 additions & 0 deletions docs/cartesi-rollups-cli_deps_start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## cartesi-rollups-cli deps start

start all deps

```
cartesi-rollups-cli deps start [flags]
```

### Examples

```
# Start all deps:
cartesi-rollups-cli deps start
```

### Options

```
--devnet-docker-image string Devnet docker image name
--devnet-mapped-port string devnet local listening port number
-h, --help help for start
--postgres-docker-image string Postgress docker image name
--postgres-mapped-port string Postgres local listening port number
--postgres-password string Postgres password
```

### SEE ALSO

* [cartesi-rollups-cli deps](cartesi-rollups-cli_deps.md) - Read the node state from the GraphQL API

115 changes: 115 additions & 0 deletions internal/deps/deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

// Package deps provides mechanisms to Node dependencies using docker
package deps

import (
"context"
"strings"
"time"

"github.com/cartesi/rollups-node/internal/config"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
DefaultPostgresDockerImage = "postgres:13-alpine"
DefaultPostgresPort = "5432"
DefaultPostgresPassword = "password"
DefaultDevnetDockerImage = "sunodo/devnet:1.1.1"
DefaultDevnetPort = "8545"

numPostgresCheckReadyAttempts = 2
fiveSeconds = 5 * time.Second
)

type DepsConfig struct {
PostgresDockerImage string
PostgresPort string
PostgresPassword string
DevnetDockerImage string
DevnetPort string
}

func NewDefaultDepsConfig() *DepsConfig {
return &DepsConfig{
DefaultPostgresDockerImage,
DefaultPostgresPort,
DefaultPostgresPassword,
DefaultDevnetDockerImage,
DefaultDevnetPort,
}
}

type DepsContainers struct {
postgres testcontainers.Container
devnet testcontainers.Container
}

func Run(ctx context.Context, depsConfig DepsConfig) (*DepsContainers, error) {

// wait strategy copied from testcontainers docs
postgresWaitStrategy := wait.ForLog("database system is ready to accept connections").
WithOccurrence(numPostgresCheckReadyAttempts).
WithPollInterval(fiveSeconds)

postgresReq := testcontainers.ContainerRequest{
Image: depsConfig.PostgresDockerImage,
ExposedPorts: []string{strings.Join([]string{
depsConfig.PostgresPort, ":5432/tcp"}, "")},
WaitingFor: postgresWaitStrategy,
Name: "rollups-node-dep-postgres",
Env: map[string]string{
"POSTGRES_PASSWORD": depsConfig.PostgresPassword,
},
}

postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: postgresReq,
Started: true,
})

terminateDepContainer := func(depContainer testcontainers.Container) {
name, err := depContainer.Name(ctx)
if err != nil {
name = "N/A"
}
<-ctx.Done()
config.InfoLogger.Printf("Terminating %s", name)
terr := depContainer.Terminate(context.Background())
if terr != nil {
config.ErrorLogger.Printf("Error terminating %s", name)
}

}
go terminateDepContainer(postgres)

if err != nil {
return nil, err
}

devNetReq := testcontainers.ContainerRequest{
Image: depsConfig.DevnetDockerImage,
ExposedPorts: []string{strings.Join([]string{depsConfig.DevnetPort, ":8545/tcp"}, "")},
WaitingFor: wait.ForExec([]string{"eth_isready"}),
Name: "rollups-node-dep-devnet",
Env: map[string]string{
"ANVIL_IP_ADDR": "0.0.0.0",
},
}

devnet, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: devNetReq,
Started: true,
})

go terminateDepContainer(devnet)

if err != nil {
return nil, err
}

return &DepsContainers{postgres, devnet}, nil
}

0 comments on commit 86e781a

Please sign in to comment.