Skip to content

Commit

Permalink
have a version working with a database
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Feb 3, 2024
1 parent 6e53485 commit 7c1bc4d
Show file tree
Hide file tree
Showing 17 changed files with 279 additions and 48 deletions.
2 changes: 1 addition & 1 deletion app/services/engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func run(ctx context.Context, log *logger.Logger) error {
DB struct {
User string `conf:"default:postgres"`
Password string `conf:"default:postgres,mask"`
HostPort string `conf:"default:database-service.sales-system.svc.cluster.local"`
HostPort string `conf:"default:database-service.liars-system.svc.cluster.local"`
Name string `conf:"default:postgres"`
MaxIdleConns int `conf:"default:2"`
MaxOpenConns int `conf:"default:0"`
Expand Down
1 change: 0 additions & 1 deletion app/tooling/admin/cmd/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ardanlabs/liarsdice/business/core/bank"
)

// contractCmd represents the contract command
var contractCmd = &cobra.Command{
Use: "contract",
Short: "Manage contract related items",
Expand Down
1 change: 0 additions & 1 deletion app/tooling/admin/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/spf13/cobra"
)

// keyCmd represents the key command
var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy the bank contract",
Expand Down
52 changes: 52 additions & 0 deletions app/tooling/admin/cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"context"
"fmt"
"time"

"github.com/ardanlabs/liarsdice/business/data/migrate"
"github.com/ardanlabs/liarsdice/business/data/sqldb"
"github.com/spf13/cobra"
)

var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Migrate the database",
Long: `Migrates the database to its most current schema.`,
RunE: func(cmd *cobra.Command, args []string) error {
dbConfig := sqldb.Config{
User: "postgres",
Password: "postgres",
HostPort: "database-service.liars-system.svc.cluster.local",
Name: "postgres",
MaxIdleConns: 2,
MaxOpenConns: 0,
DisableTLS: true,
}

return performMigrate(dbConfig)
},
}

func init() {
rootCmd.AddCommand(migrateCmd)
}

func performMigrate(cfg sqldb.Config) error {
db, err := sqldb.Open(cfg)
if err != nil {
return fmt.Errorf("connect database: %w", err)
}
defer db.Close()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := migrate.Migrate(ctx, db); err != nil {
return fmt.Errorf("migrate database: %w", err)
}

fmt.Println("migrations complete")
return nil
}
2 changes: 1 addition & 1 deletion app/tooling/admin/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/ardanlabs/liarsdice/foundation/web"
)

// rootCmd represents the base command when called without any subcommands
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "admin",
Short: "A small tool to manage liars dice",
Expand Down
4 changes: 2 additions & 2 deletions app/tooling/admin/cmd/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/ardanlabs/ethereum"
"github.com/ardanlabs/ethereum/currency"
"time"

"github.com/spf13/cobra"

"github.com/ethereum/go-ethereum/common"
)

// transactionCmd represents the transaction command
var transactionCmd = &cobra.Command{
Use: "transaction",
Short: "Examine transaction",
Expand Down
4 changes: 2 additions & 2 deletions app/tooling/admin/cmd/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package cmd
import (
"context"
"fmt"
"time"

"github.com/ardanlabs/ethereum"
"github.com/ardanlabs/ethereum/currency"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"time"
)

// walletCmd represents the wallet command
var walletCmd = &cobra.Command{
Use: "wallet",
Short: "Show the wallet balance",
Expand Down
19 changes: 12 additions & 7 deletions business/core/game/stores/gamedb/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type dbState struct {
}

type dbCup struct {
ID uuid.UUID `db:"game_id"`
Round int `db:"round"`
Player string `db:"player"`
OrderIdx int `db:"order_idx"`
Outs int `db:"outs"`
Dice any `db:"dice"`
ID uuid.UUID `db:"game_id"`
Round int `db:"round"`
Player string `db:"player"`
OrderIdx int `db:"order_idx"`
Outs int `db:"outs"`
Dice dbarray.Int64 `db:"dice"`
}

type dbBet struct {
Expand All @@ -53,13 +53,18 @@ type dbBalance struct {
func toDBState(state game.State) dbState {
cups := make([]dbCup, 0, len(state.Cups))
for _, cup := range state.Cups {
dice := make([]int64, len(cup.Dice))
for i, d := range cup.Dice {
dice[i] = int64(d)
}

cups = append(cups, dbCup{
ID: state.GameID,
Round: state.Round,
Player: cup.Player.String(),
OrderIdx: cup.OrderIdx,
Outs: cup.Outs,
Dice: dbarray.Array(cup.Dice),
Dice: dice,
})
}

Expand Down
67 changes: 45 additions & 22 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@
# The coinbase address is given a LOT of money to start.
#

GOLANG := golang:1.21.6
NODE := node:16
ALPINE := alpine:3.19
CADDY := caddy:2.6-alpine
KIND := kindest/node:v1.29.0@sha256:eaa1450915475849a73a9227b8f201df25e55e268e5d619312131292e324d570
GETH := ethereum/client-go:stable

KIND_CLUSTER := liars-game-cluster
VERSION := 1.0
GOLANG := golang:1.22rc
NODE := node:16
ALPINE := alpine:3.19
CADDY := caddy:2.6-alpine
KIND := kindest/node:v1.29.0@sha256:eaa1450915475849a73a9227b8f201df25e55e268e5d619312131292e324d570
BUSYBOX := busybox:stable
GETH := ethereum/client-go:stable
POSTGRES := postgres:16.1

KIND_CLUSTER := liars-game-cluster
NAMESPACE := liars-system
APP := engine
BASE_IMAGE_NAME := ardanlabs/liars
SERVICE_NAME := engine
VERSION := 0.0.1
SERVICE_IMAGE := $(BASE_IMAGE_NAME)/$(SERVICE_NAME):$(VERSION)

# ==============================================================================
# Install dependencies
Expand All @@ -29,14 +36,17 @@ dev-setup:
brew list kustomize || brew install kustomize
brew list ethereum || brew install ethereum
brew list solidity || brew install solidity
brew list pgcli || brew install pgcli

dev-docker:
docker pull $(GOLANG)
docker pull $(NODE)
docker pull $(ALPINE)
docker pull $(CADDY)
docker pull $(KIND)
docker pull $(BUSYBOX)
docker pull $(GETH)
docker pull $(POSTGRES)

# ==============================================================================
# Game UI
Expand All @@ -52,16 +62,13 @@ game-tuio:

# ==============================================================================
# Building containers
#
# The new docker buildx build system is required for these docker build commands On systems other than Docker Desktop
# buildx is not the default build system. You will need to enable it with: docker buildx install

all: game-engine

game-engine:
docker build \
-f zarf/docker/dockerfile.engine \
-t liarsdice-game-engine:$(VERSION) \
-t $(SERVICE_IMAGE) \
--build-arg BUILD_REF=$(VERSION) \
--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
.
Expand All @@ -72,7 +79,6 @@ game-engine:
# To start the system for the first time, run these two commands:
# make dev-up
# make dev-update-apply
# Expect the building of the FE to take a wee bit of time :(

dev-up:
kind create cluster \
Expand All @@ -81,14 +87,16 @@ dev-up:
--config zarf/k8s/dev/kind-config.yaml
kubectl wait --timeout=120s --namespace=local-path-storage --for=condition=Available deployment/local-path-provisioner

kind load docker-image $(BUSYBOX) --name $(KIND_CLUSTER)
kind load docker-image $(GETH) --name $(KIND_CLUSTER)
kind load docker-image $(POSTGRES) --name $(KIND_CLUSTER)

dev-down:
kind delete cluster --name $(KIND_CLUSTER)
rm -f /tmp/credentials.json

dev-load:
kind load docker-image liarsdice-game-engine:$(VERSION) --name $(KIND_CLUSTER)
kind load docker-image $(SERVICE_IMAGE) --name $(KIND_CLUSTER)

dev-deploy:
@zarf/k8s/dev/geth/setup-contract-k8s
Expand All @@ -98,14 +106,17 @@ dev-deploy-force:

dev-apply:
go build -o admin app/tooling/admin/main.go

kustomize build zarf/k8s/dev/geth | kubectl apply -f -
kubectl wait --timeout=120s --namespace=liars-system --for=condition=Available deployment/geth
kubectl wait --timeout=120s --namespace=$(NAMESPACE) --for=condition=Available deployment/geth

@zarf/k8s/dev/geth/setup-contract-k8s.sh

kustomize build zarf/k8s/dev/database | kubectl apply -f -
kubectl rollout status --namespace=$(NAMESPACE) --watch --timeout=120s sts/database

kustomize build zarf/k8s/dev/engine | kubectl apply -f -
kubectl wait --timeout=120s --namespace=liars-system --for=condition=Available deployment/engine
kubectl wait --timeout=120s --namespace=$(NAMESPACE) --for=condition=Available deployment/engine

dev-restart:
kubectl rollout restart deployment engine --namespace=liars-system
Expand All @@ -114,11 +125,14 @@ dev-update: all dev-load dev-restart

dev-update-apply: all dev-load dev-apply

dev-logs-init:
kubectl logs --namespace=$(NAMESPACE) -l app=$(APP) -f --tail=100 -c init-ge-migrate

dev-logs:
kubectl logs --namespace=liars-system -l app=engine --all-containers=true -f --tail=100 | go run app/tooling/logfmt/main.go
kubectl logs --namespace=$(NAMESPACE) -l app=$(APP) --all-containers=true -f --tail=100 | go run app/tooling/logfmt/main.go

dev-logs-geth:
kubectl logs --namespace=liars-system -l app=geth --all-containers=true -f --tail=1000
kubectl logs --namespace=$(NAMESPACE) -l app=geth --all-containers=true -f --tail=1000

dev-status:
kubectl get nodes -o wide
Expand All @@ -130,10 +144,19 @@ dev-describe:
kubectl describe svc

dev-describe-deployment-engine:
kubectl describe deployment --namespace=liars-system engine
kubectl describe deployment --namespace=$(NAMESPACE) $(APP)

dev-describe-engine:
kubectl describe pod --namespace=liars-system -l app=engine
kubectl describe pod --namespace=$(NAMESPACE) -l app=$(APP)

# ==============================================================================
# Administration

migrate:
export SALES_DB_HOST=localhost; go run app/tooling/sales-admin/main.go migrate

pgcli:
pgcli postgresql://postgres:postgres@localhost

# ==============================================================================
# Running tests within the local computer
Expand Down
2 changes: 1 addition & 1 deletion zarf/docker/dockerfile.engine
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the Go Binary.
FROM golang:1.21.6 as build_engine
FROM golang:1.22rc2 as build_engine
ENV CGO_ENABLED 0
ARG BUILD_REF

Expand Down
19 changes: 17 additions & 2 deletions zarf/k8s/base/engine/base-engine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,35 @@ kind: Deployment
metadata:
name: engine
namespace: liars-system

spec:
selector:
matchLabels:
app: engine

template:
metadata:
labels:
app: engine

spec:
terminationGracePeriodSeconds: 60

initContainers:
- name: init-ge-migrate
image: engine-image
command: ['./admin', 'migrate']

containers:
- name: game-engine
image: game-engine-image
image: engine-image

ports:
- name: game-engine
containerPort: 3000
- name: ge-debug
- name: game-engine-dbg
containerPort: 4000

readinessProbe:
httpGet:
path: /v1/readiness
Expand All @@ -35,6 +46,7 @@ spec:
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 2

livenessProbe:
httpGet:
path: /v1/liveness
Expand All @@ -44,6 +56,7 @@ spec:
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 2

env:
- name: GAME_CONTRACT_ID
valueFrom:
Expand All @@ -66,7 +79,9 @@ spec:
valueFrom:
fieldRef:
fieldPath: spec.nodeName

---

apiVersion: v1
kind: Service
metadata:
Expand Down
Loading

0 comments on commit 7c1bc4d

Please sign in to comment.