Skip to content

Commit

Permalink
fix bug finding on ui/ux and docker built image issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed Oct 17, 2024
1 parent 0ded692 commit d098f5f
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 20 deletions.
1 change: 1 addition & 0 deletions cmd/world/cardinal/cardinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var BaseCmd = &cobra.Command{
func init() {
// Register subcommands - `world cardinal [subcommand]`
BaseCmd.AddCommand(startCmd, devCmd, restartCmd, purgeCmd, stopCmd)
registerConfigAndVerboseFlags(startCmd, devCmd, restartCmd, purgeCmd, stopCmd)
}

func getServices(cfg *config.Config) []service.Builder {
Expand Down
14 changes: 13 additions & 1 deletion cmd/world/cardinal/common_flag.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package cardinal

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/config"
"pkg.world.dev/world-cli/common/logger"
)

func registerEditorFlag(cmd *cobra.Command, defaultEnable bool) {
cmd.Flags().Bool("editor", defaultEnable, "Run Cardinal Editor, useful for prototyping and debugging")
}

func registerConfigAndVerboseFlags(cmds ...*cobra.Command) {
for _, cmd := range cmds {
config.AddConfigFlag(cmd)
logger.AddVerboseFlag(cmd)
}
}
5 changes: 3 additions & 2 deletions cmd/world/cardinal/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const (
var (
// ValidLogLevels Valid log levels for zerolog
validLogLevels = strings.Join([]string{
zerolog.DebugLevel.String(),
zerolog.InfoLevel.String(),
zerolog.DebugLevel.String(),
zerolog.WarnLevel.String(),
zerolog.ErrorLevel.String(),
zerolog.FatalLevel.String(),
Expand Down Expand Up @@ -158,7 +158,8 @@ func init() {
registerEditorFlag(startCmd, true)
startCmd.Flags().Bool(flagBuild, true, "Rebuild Docker images before starting")
startCmd.Flags().Bool(flagDetach, false, "Run in detached mode")
startCmd.Flags().String(flagLogLevel, "", "Set the log level")
startCmd.Flags().String(flagLogLevel, "",
fmt.Sprintf("Set the log level for Cardinal. Must be one of (%v)", validLogLevels))
startCmd.Flags().Bool(flagDebug, false, "Enable delve debugging")
startCmd.Flags().Bool(flagTelemetry, false, "Enable tracing, metrics, and profiling")
}
Expand Down
10 changes: 10 additions & 0 deletions cmd/world/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package evm
import (
"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/config"
"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
"pkg.world.dev/world-cli/tea/style"
)
Expand All @@ -27,4 +29,12 @@ var BaseCmd = &cobra.Command{
func init() {
// Register subcommands - `world evm [subcommand]`
BaseCmd.AddCommand(startCmd, stopCmd)
registerConfigAndVerboseFlags(startCmd, stopCmd)
}

func registerConfigAndVerboseFlags(cmds ...*cobra.Command) {
for _, cmd := range cmds {
config.AddConfigFlag(cmd)
logger.AddVerboseFlag(cmd)
}
}
4 changes: 0 additions & 4 deletions cmd/world/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"pkg.world.dev/world-cli/cmd/world/cardinal"
"pkg.world.dev/world-cli/cmd/world/evm"
"pkg.world.dev/world-cli/common/config"
"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/tea/style"
)
Expand Down Expand Up @@ -74,9 +73,6 @@ func init() {

// Remove completion subcommand
rootCmd.CompletionOptions.DisableDefaultCmd = true

config.AddConfigFlag(rootCmd)
logger.AddVerboseFlag(rootCmd)
}

func checkLatestVersion() error {
Expand Down
2 changes: 1 addition & 1 deletion common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Config struct {
}

func AddConfigFlag(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&configFile, flagForConfigFile, "c", "", "a toml encoded config file")
cmd.Flags().StringVarP(&configFile, flagForConfigFile, "c", "", "a toml encoded config file")
}

func GetConfig() (*Config, error) {
Expand Down
25 changes: 19 additions & 6 deletions common/docker/client_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"regexp"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -155,7 +154,9 @@ func (c *Client) stopContainer(ctx context.Context, containerName string) error
}

// Stop the container
err = c.client.ContainerStop(ctx, containerName, container.StopOptions{})
err = c.client.ContainerStop(ctx, containerName, container.StopOptions{
Signal: "SIGINT",
})
if err != nil {
return eris.Wrapf(err, "Failed to stop container %s", containerName)
}
Expand Down Expand Up @@ -198,7 +199,7 @@ func (c *Client) logMultipleContainers(ctx context.Context, services ...service.
case <-ctx.Done():
return
default:
err := c.logContainerOutput(ctx, id, strconv.Itoa(i))
err := c.logContainerOutput(ctx, id, i)
if err != nil && !errors.Is(err, context.Canceled) {
fmt.Printf("Error logging container %s: %v. Reattaching...\n", id, err)
time.Sleep(2 * time.Second) //nolint:gomnd // Sleep for 2 seconds before reattaching
Expand All @@ -212,7 +213,19 @@ func (c *Client) logMultipleContainers(ctx context.Context, services ...service.
wg.Wait()
}

func (c *Client) logContainerOutput(ctx context.Context, containerID, styleNumber string) error {
func (c *Client) logContainerOutput(ctx context.Context, containerID string, styleNumber int) error {
colors := []string{
"#00FF00", // Green
"#0000FF", // Blue
"#00FFFF", // Cyan
"#FF00FF", // Magenta
"#FFA500", // Orange
"#800080", // Purple
"#FFC0CB", // Pink
"#87CEEB", // Sky Blue
"#32CD32", // Lime Green
}

// Create options for logs
options := container.LogsOptions{
ShowStdout: true,
Expand Down Expand Up @@ -258,10 +271,10 @@ func (c *Client) logContainerOutput(ctx context.Context, containerID, styleNumbe
// Print the cleaned log message
if streamType == 1 { // Stdout
// TODO: what content should be printed for stdout?
fmt.Printf("[%s] %s", style.ForegroundPrint(containerID, styleNumber), cleanLog)
fmt.Printf("[%s] %s", style.ForegroundPrint(containerID, colors[styleNumber]), cleanLog)
} else if streamType == 2 { //nolint:gomnd // Stderr
// TODO: what content should be printed for stderr?
fmt.Printf("[%s] %s", style.ForegroundPrint(containerID, styleNumber), cleanLog)
fmt.Printf("[%s] %s", style.ForegroundPrint(containerID, colors[styleNumber]), cleanLog)
}
}

Expand Down
16 changes: 16 additions & 0 deletions common/docker/client_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ func (c *Client) buildImages(ctx context.Context, dockerServices ...service.Serv
Name: dockerService.Image,
})

// Remove the container
err := c.removeContainer(ctx, dockerService.Name)
fmt.Printf("Removing container %s\n", dockerService.Image)
if err != nil {
p.Send(multispinner.ProcessState{
Icon: style.CrossIcon.Render(),
State: "building",
Type: "image",
Name: dockerService.Image,
Detail: err.Error(),
Done: true,
})
errChan <- err
return
}

// Start the build process
buildResponse, err := c.buildImage(ctx, dockerService)
if err != nil {
Expand Down
54 changes: 53 additions & 1 deletion common/docker/service/cardinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func getCardinalContainerName(cfg *config.Config) string {
return fmt.Sprintf("%s-cardinal", cfg.DockerEnv["CARDINAL_NAMESPACE"])
}

func Cardinal(cfg *config.Config) Service {
func Cardinal(cfg *config.Config) Service { //nolint:funlen // this function is long but it's necessary

Check failure on line 27 in common/docker/service/cardinal.go

View workflow job for this annotation

GitHub Actions / Go

directive `//nolint:funlen // this function is long but it's necessary` is unused for linter "funlen" (nolintlint)

Check failure on line 27 in common/docker/service/cardinal.go

View workflow job for this annotation

GitHub Actions / Go

directive `//nolint:funlen // this function is long but it's necessary` is unused for linter "funlen" (nolintlint)
// Check cardinal namespace
checkCardinalNamespace(cfg)

Expand All @@ -40,13 +40,65 @@ func Cardinal(cfg *config.Config) Service {
dockerfile = strings.ReplaceAll(dockerfile, mountCacheScript, "")
}

// Set env variables
const falseValue = "false"

// Set Base Shard Router Key
baseShardRouterKey := cfg.DockerEnv["BASE_SHARD_ROUTER_KEY"]
if baseShardRouterKey == "" {
baseShardRouterKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ01"
}

// Set Cardinal Log Level
cardinalLogLevel := cfg.DockerEnv["CARDINAL_LOG_LEVEL"]
if cardinalLogLevel == "" {
cardinalLogLevel = "info"
}

// Set Cardinal Log Pretty
cardinalLogPretty := cfg.DockerEnv["CARDINAL_LOG_PRETTY"]
if cardinalLogPretty == "" {
cardinalLogPretty = "true"
}

// Set Cardinal Rollup Enabled
cardinalRollupEnabled := cfg.DockerEnv["CARDINAL_ROLLUP_ENABLED"]
if cardinalRollupEnabled == "" {
cardinalRollupEnabled = falseValue
}

// Set Telemetry Profiler Enabled
telemetryProfilerEnabled := cfg.DockerEnv["TELEMETRY_PROFILER_ENABLED"]
if telemetryProfilerEnabled == "" {
telemetryProfilerEnabled = falseValue
}

// Set telemetry trace enabled
telemetryTraceEnabled := cfg.DockerEnv["TELEMETRY_TRACE_ENABLED"]
if telemetryTraceEnabled == "" {
telemetryTraceEnabled = falseValue
}

// Set router key
routerKey := cfg.DockerEnv["ROUTER_KEY"]
if routerKey == "" {
routerKey = "25a0f627050d11b1461b2728ea3f704e141312b1d4f2a21edcec4eccddd940c2"
}

service := Service{
Name: getCardinalContainerName(cfg),
Config: container.Config{
Image: cfg.DockerEnv["CARDINAL_NAMESPACE"],
Env: []string{
fmt.Sprintf("REDIS_ADDRESS=%s:6379", getRedisContainerName(cfg)),
fmt.Sprintf("BASE_SHARD_SEQUENCER_ADDRESS=%s:9601", getEVMContainerName(cfg)),
fmt.Sprintf("BASE_SHARD_ROUTER_KEY=%s", baseShardRouterKey),
fmt.Sprintf("CARDINAL_LOG_LEVEL=%s", cardinalLogLevel),
fmt.Sprintf("CARDINAL_LOG_PRETTY=%s", cardinalLogPretty),
fmt.Sprintf("CARDINAL_ROLLUP_ENABLED=%s", cardinalRollupEnabled),
fmt.Sprintf("TELEMETRY_PROFILER_ENABLED=%s", telemetryProfilerEnabled),
fmt.Sprintf("TELEMETRY_TRACE_ENABLED=%s", telemetryTraceEnabled),
fmt.Sprintf("ROUTER_KEY=%s", routerKey),
},
ExposedPorts: getExposedPorts(exposedPorts),
},
Expand Down
25 changes: 25 additions & 0 deletions common/docker/service/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func EVM(cfg *config.Config) Service {
daBaseURL = fmt.Sprintf("http://%s", getCelestiaDevNetContainerName(cfg))
}

daNamespaceID := cfg.DockerEnv["DA_NAMESPACE_ID"]
if daNamespaceID == "" {
daNamespaceID = "67480c4a88c4d12935d4"
}

faucetEnabled := cfg.DockerEnv["FAUCET_ENABLED"]
if faucetEnabled == "" {
faucetEnabled = "false" //nolint:goconst // default values should be local to the service
Expand All @@ -41,17 +46,37 @@ func EVM(cfg *config.Config) Service {
baseShardRouterKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ01"
}

routerKey := cfg.DockerEnv["ROUTER_KEY"]
if routerKey == "" {
routerKey = "25a0f627050d11b1461b2728ea3f704e141312b1d4f2a21edcec4eccddd940c2"
}

chainID := cfg.DockerEnv["CHAIN_ID"]
if chainID == "" {
chainID = "world-420"
}

chainKeyMnemonic := cfg.DockerEnv["CHAIN_KEY_MNEMONIC"]
if chainKeyMnemonic == "" {
chainKeyMnemonic = "enact adjust liberty squirrel bulk ticket invest tissue antique window" +
"thank slam unknown fury script among bread social switch glide wool clog flag enroll"
}

return Service{
Name: getEVMContainerName(cfg),
Config: container.Config{
Image: "ghcr.io/argus-labs/world-engine-evm:1.4.1",
Env: []string{
fmt.Sprintf("DA_BASE_URL=%s", daBaseURL),
fmt.Sprintf("DA_AUTH_TOKEN=%s", cfg.DockerEnv["DA_AUTH_TOKEN"]),
fmt.Sprintf("DA_NAMESPACE_ID=%s", daNamespaceID),
fmt.Sprintf("FAUCET_ENABLED=%s", faucetEnabled),
fmt.Sprintf("FAUCET_ADDRESS=%s", faucetAddress),
fmt.Sprintf("FAUCET_AMOUNT=%s", faucetAmount),
fmt.Sprintf("BASE_SHARD_ROUTER_KEY=%s", baseShardRouterKey),
fmt.Sprintf("ROUTER_KEY=%s", routerKey),
fmt.Sprintf("CHAIN_ID=%s", chainID),
fmt.Sprintf("CHAIN_KEY_MNEMONIC=%s", chainKeyMnemonic),
},
ExposedPorts: getExposedPorts([]int{1317, 26657, 9090, 9601}),
},
Expand Down
5 changes: 1 addition & 4 deletions common/docker/service/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ func Redis(cfg *config.Config) Service {
return Service{
Name: getRedisContainerName(cfg),
Config: container.Config{
Image: "redis:latest",
Env: []string{
fmt.Sprintf("REDIS_PASSWORD=%s", cfg.DockerEnv["REDIS_PASSWORD"]),
},
Image: "redis:latest",
ExposedPorts: getExposedPorts(exposedPorts),
},
HostConfig: container.HostConfig{
Expand Down
2 changes: 1 addition & 1 deletion common/logger/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ func PrintLogs() {
// AddVerboseFlag set flag --log-debug
func AddVerboseFlag(cmd ...*cobra.Command) {
for _, c := range cmd {
c.PersistentFlags().BoolVarP(&verboseMode, "verbose", "v", false, "Enable World CLI debug logs")
c.Flags().BoolVarP(&verboseMode, "verbose", "v", false, "Enable World CLI debug logs")
}
}

0 comments on commit d098f5f

Please sign in to comment.