-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add command to run dependencies
- Loading branch information
Showing
6 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |