Skip to content

Commit

Permalink
refactor: cleanup docker ux flow
Browse files Browse the repository at this point in the history
  • Loading branch information
smsunarto committed Nov 8, 2023
1 parent 6645561 commit 4319229
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 653 deletions.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# world-cli binary name
PKGNAME = world

all: test build

build:
go build -o $(PKGNAME) -v ./cmd/$(PKGNAME)

test:
go test -v ./...

clean:
go clean
rm -f $(PKGNAME)

install:
go install ./cmd/$(PKGNAME)

.PHONY: all build test clean install
70 changes: 40 additions & 30 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package cardinal

import (
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"pkg.world.dev/world-cli/tea/style"
"syscall"
"time"

"github.com/magefile/mage/sh"
"github.com/spf13/cobra"
"pkg.world.dev/world-cli/tea/style"
)

const (
CardinalPort = "3333"
RedisPort = "6379"
WebdisPort = "7379"
)

/////////////////
Expand All @@ -27,22 +27,18 @@ const (
// Usage: `world cardinal dev`
var devCmd = &cobra.Command{
Use: "dev",
Short: "TODO",
Long: `TODO`,
Short: "Run Cardinal in development mode",
Long: `Run Cardinal in development mode`,
RunE: func(cmd *cobra.Command, args []string) error {
err := os.Chdir("cardinal")
if err != nil {
return err
}
fmt.Print(style.CLIHeader("Cardinal", "Running Cardinal in dev mode"), "\n")
fmt.Println(style.BoldText.Render("Press Ctrl+C to stop"))
fmt.Println()
fmt.Println(fmt.Sprintf("Redis: localhost:%s", RedisPort))
fmt.Println(fmt.Sprintf("Cardinal: localhost:%s", CardinalPort))
fmt.Println()

// Run Redis container
err = sh.Run("docker", "run", "-d", "-p", fmt.Sprintf("%s:%s", RedisPort, RedisPort), "-e", "LOCAL_REDIS=true", "--name", "cardinal-dev-redis", "redis")
if err != nil {
return err
}

// Run Webdis container - this provides a REST wrapper around Redis
err = sh.Run("docker", "run", "-d", "-p", fmt.Sprintf("%s:%s", WebdisPort, WebdisPort), "--link", "cardinal-dev-redis:redis", "--name", "cardinal-dev-webdis", "anapsix/webdis")
err := runRedis()
if err != nil {
return err
}
Expand Down Expand Up @@ -105,16 +101,37 @@ var devCmd = &cobra.Command{
},
}

// runRedis runs Redis in a Docker container
func runRedis() error {
fmt.Println("Starting Redis container...")
cmd := exec.Command("docker", "run", "-d", "-p", fmt.Sprintf("%s:%s", RedisPort, RedisPort), "--name", "cardinal-dev-redis", "redis")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err := cmd.Start()
if err != nil {
fmt.Println("Failed to start Redis container. Retrying after cleanup...")
cleanupErr := cleanup()
if cleanupErr != nil {
return err
}

err := sh.Run("docker", "run", "-d", "-p", fmt.Sprintf("%s:%s", RedisPort, RedisPort), "--name", "cardinal-dev-redis", "redis")
if err != nil {
return err
}
}

return nil
}

// runCardinal runs cardinal in dev mode.
// We run cardinal without docker to make it easier to debug and skip the docker image build step
func runCardinal() (*exec.Cmd, error) {
fmt.Print(style.CLIHeader("Cardinal", "Running Cardinal in dev mode"), "\n")
fmt.Println(style.BoldText.Render("Press Ctrl+C to stop"))
fmt.Println()
fmt.Println(fmt.Sprintf("Redis: localhost:%s", RedisPort))
fmt.Println(fmt.Sprintf("Webdis: localhost:%s", WebdisPort))
fmt.Println(fmt.Sprintf("Cardinal: localhost:%s", CardinalPort))
fmt.Println()
err := os.Chdir("cardinal")
if err != nil {
return nil, errors.New("can't find cardinal directory. Are you in the root of a World Engine project")
}

env := map[string]string{
"REDIS_MODE": "normal",
Expand All @@ -131,7 +148,7 @@ func runCardinal() (*exec.Cmd, error) {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}

err := cmd.Start()
err = cmd.Start()
if err != nil {
return cmd, err
}
Expand All @@ -148,12 +165,5 @@ func cleanup() error {
return err
}

err = sh.Run("docker", "rm", "-f", "cardinal-dev-webdis")
if err != nil {
fmt.Println("Failed to delete Webdis container automatically")
fmt.Println("Please delete it manually with `docker rm -f cardinal-dev-webdis`")
return err
}

return nil
}
49 changes: 14 additions & 35 deletions cmd/world/cardinal/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
/////////////////

func init() {
startCmd.Flags().Bool("build", true, "Rebuild the Docker images before starting")
startCmd.Flags().Bool("debug", false, "Enable debug mode")
startCmd.Flags().String("mode", "", "Run with special mode [detach/integration-test]")
startCmd.Flags().Bool("build", true, "Rebuild Docker images before starting")
startCmd.Flags().Bool("debug", false, "Run in debug mode")
startCmd.Flags().Bool("detach", false, "Run in detached mode")
}

// startCmd starts your Cardinal game shard stack
Expand All @@ -23,9 +23,11 @@ var startCmd = &cobra.Command{
Short: "Start your Cardinal game shard stack",
Long: `Start your Cardinal game shard stack.
This will start the following Docker services:
This will start the following Docker services and its dependencies:
- Cardinal (Core game logic)
- Nakama (Relay)`,
- Nakama (Relay)
- Redis (Cardinal dependency)
- Postgres (Nakama dependency)`,
RunE: func(cmd *cobra.Command, args []string) error {
buildFlag, err := cmd.Flags().GetBool("build")
if err != nil {
Expand All @@ -37,41 +39,18 @@ This will start the following Docker services:
return err
}

modeFlag, err := cmd.Flags().GetString("mode")
detachFlag, err := cmd.Flags().GetBool("detach")
if err != nil {
return err
}

// Don't allow running with special mode and debug mode
if modeFlag != "" && debugFlag {
return fmt.Errorf("cannot run with special mode and debug mode at the same time")
}
fmt.Println("Starting Cardinal game shard...")
fmt.Println("This may take a few minutes to rebuild the Docker images.")
fmt.Println("Use `world cardinal dev` to run Cardinal faster/easier in development mode.")

switch modeFlag {
case "":
if debugFlag {
err = tea_cmd.DockerStartDebug()
if err != nil {
return err
}
} else {
err = tea_cmd.DockerStart(buildFlag, []tea_cmd.DockerService{tea_cmd.DockerServiceCardinal, tea_cmd.DockerServiceNakama})
if err != nil {
return err
}
}
case "detach":
err = tea_cmd.DockerStartDetach()
if err != nil {
return err
}
case "integration-test":
err = tea_cmd.DockerStartTest()
if err != nil {
return err
}
default:
return fmt.Errorf("unknown mode %s", modeFlag)
err = tea_cmd.DockerStartAll(buildFlag, debugFlag, detachFlag, -1)
if err != nil {
return err
}

return nil
Expand Down
12 changes: 4 additions & 8 deletions cmd/world/cardinal/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ var stopCmd = &cobra.Command{
This will stop the following Docker services:
- Cardinal (Core game logic)
- Nakama (Relay)`,
- Nakama (Relay)
- Redis (Cardinal dependency)
- Postgres (Nakama dependency)`,
RunE: func(cmd *cobra.Command, args []string) error {
err := tea_cmd.DockerStop([]tea_cmd.DockerService{
tea_cmd.DockerServiceCardinal,
tea_cmd.DockerServiceNakama,
tea_cmd.DockerServicePostgres,
tea_cmd.DockerServiceRedis,
tea_cmd.DockerServiceTestsuite,
})
err := tea_cmd.DockerStopAll()
if err != nil {
return err
}
Expand Down
47 changes: 0 additions & 47 deletions common/log_byte_buffer.go

This file was deleted.

Loading

0 comments on commit 4319229

Please sign in to comment.