Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .env

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Project files
etc/config/config.yml
.env
etc/logs

# Extractor
Expand Down
141 changes: 114 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,135 @@

DOCKER_COMPOSE_FILE ?= docker-compose.yml
DOCKER_COMPOSE ?= docker compose
UTILITY := utility

# Well documented Makefiles
DEFAULT_GOAL := help

help:
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-40s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@echo "Available commands:"
@echo ""
@echo "=== Setup ==="
@echo " make config - Install default config to alpha-core"
@echo " make setup-all - Run config, db-setup, and up"
@echo " make external_db - Switch to external DB and disable local sql"
@echo " make internal_db - Switch to local sql and enable localdb profile"
@echo ""
@echo "=== Database ==="
@echo " make db-user - Create DB user (if not root)"
@echo " make db-create - Create databases"
@echo " make db-drop - Drop databases"
@echo " make db-populate - Populate databases (schema only)"
@echo " make db-update - Apply database updates"
@echo " make db-setup - Create, populate, then update databases"
@echo " make db-backup - Backup databases (optional BACKUP=folder)"
@echo " make db-restore - Restore databases (optional BACKUP=folder)"
@echo ""
@echo "=== Docker ==="
@echo " make up - Build and start all containers"
@echo " make start - Start all containers"
@echo " make stop - Stop all containers (not dev)"
@echo " make restart - Restart all containers (not dev)"
@echo " make down - Stop and remove containers (not dev)"
@echo " make build-utility - Build utility image"
@echo " make connect <name> - Connect to container"
@echo " make list - List running containers"
@echo ""
@echo "=== Logs ==="
@echo " make log <name> - Show container log"
@echo " make logs - Show all containers logs"

##@ [Run Firsttime]
.PHONY: config
config: ## Install default config to alpha-core
cp etc/config/config.yml.dist etc/config/config.yml
@if [ -e etc/config/config.yml ] && [ ! -w etc/config/config.yml ]; then \
echo "etc/config/config.yml is not writable; run: sudo chown $$USER:$$USER etc/config/config.yml"; \
exit 1; \
fi
@cp etc/config/config.yml.dist etc/config/config.yml
@echo "Config installed to etc/config/config.yml"

setup-all: ## Run config, db-setup, and up
$(MAKE) config
$(MAKE) db-setup
$(MAKE) up

external_db: ## Switch to external DB and disable local sql
@set -a; . ./.env; set +a; python3 etc/docker/external_db.py

internal_db: ## Switch to local sql and enable localdb profile
@set -a; . ./.env; set +a; python3 etc/docker/internal_db.py
##@ [Database]
db-create: build-utility db-start-local db-user ## Create databases
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/create_databases.sh

db-drop: build-utility db-start-local ## Drop databases
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/drop_databases.sh

db-populate: build-utility db-start-local ## Populate databases (schema only)
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/populate_databases.sh

db-update: build-utility db-start-local ## Apply database updates
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/update_databases.sh

db-setup: ## Create, populate, then update databases
$(MAKE) db-create
$(MAKE) db-populate
$(MAKE) db-update

db-backup: build-utility db-start-local ## Backup databases (optional BACKUP=folder)
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/backup_db.sh

db-restore: build-utility db-start-local ## Restore databases (optional BACKUP=folder)
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/restore_db.sh

##@ [Docker]
build-utility: ## Build utility image
$(DOCKER_COMPOSE) build $(UTILITY)

db-user: build-utility db-start-local ## Create DB user (if not root)
$(DOCKER_COMPOSE) run --rm $(UTILITY) /bin/commands/create_db_user.sh

db-start-local: ## Start local sql if MYSQL_HOST=sql
@set -a; . ./.env; set +a; \
if [ "$$MYSQL_HOST" = "sql" ] || [ "$$MYSQL_HOST" = "127.0.0.1" ] || [ "$$MYSQL_HOST" = "localhost" ]; then \
$(DOCKER_COMPOSE) up -d sql; \
echo "Waiting for sql to be ready..."; \
host="$$MYSQL_HOST"; \
port="$$MYSQL_PORT"; \
if [ "$$MYSQL_HOST" = "sql" ]; then \
host="127.0.0.1"; \
port="$$MYSQL_HOST_PORT"; \
fi; \
i=0; \
while [ $$i -lt 30 ]; do \
if bash -c "</dev/tcp/$$host/$$port" >/dev/null 2>&1; then \
echo "sql is ready."; \
break; \
fi; \
i=$$((i+1)); \
sleep 2; \
done; \
if [ $$i -ge 30 ]; then \
echo "Timed out waiting for sql."; \
exit 1; \
fi; \
fi

up: ## Build and start all containers
docker compose -f $(DOCKER_COMPOSE_FILE) up -d
$(DOCKER_COMPOSE) up -d

start: ## Start all containers
docker compose -f $(DOCKER_COMPOSE_FILE) start
$(DOCKER_COMPOSE) start

stop: ## Stop all containers (not dev)
docker compose -f $(DOCKER_COMPOSE_FILE) stop
$(DOCKER_COMPOSE) stop

restart: ## Restart all containers (not dev)
docker compose -f $(DOCKER_COMPOSE_FILE) restart
$(DOCKER_COMPOSE) restart

down: ## Stop and remove containers (not dev)
docker compose -f $(DOCKER_COMPOSE_FILE) down

build: ## Just build all docker images
docker compose -f $(DOCKER_COMPOSE_FILE) build
$(DOCKER_COMPOSE) down

connect: ## Connect to container. usage: make connect <container>
docker exec -it $(filter-out $@,$(MAKECMDGOALS)) /bin/sh
Expand All @@ -37,22 +138,8 @@ list: ## List all runnning containers
docker ps -a --format="table {{.Names}}\t{{.Image}}\t{{.Status}}"

##@ [Logs]
log: ## show one contaienr log. usage: make log <container>
log: ## show one contaienr log. usage: make log <contai ner>
docker logs $(filter-out $@,$(MAKECMDGOALS)) -f

all-logs: ## Show all containers logs
docker-compose logs -f

##@ [Profiles: dev]
up-dev: ## Build and start dev profile with PhpMyAdmin, Inotify (run it after make up)
docker compose -f $(DOCKER_COMPOSE_FILE) --profile dev up -d

start-dev: ## Start built containers in dev profile
docker start alpha-core-phpmyadmin-1 alpha-core-inotify-1

stop-dev: ## Stop built containers in dev profile
docker stop alpha-core-phpmyadmin-1 alpha-core-inotify-1

down-dev: ## Stop and remove dev profile containers
docker stop alpha-core-phpmyadmin-1 alpha-core-inotify-1
docker rm alpha-core-phpmyadmin-1 alpha-core-inotify-1
logs: ## Show all containers logs
$(DOCKER_COMPOSE) logs -f
128 changes: 128 additions & 0 deletions README.docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# ![logo](.github/logo-small.png) Alpha Core - Docker & Makefile Guide

---

## ✅ Requirements

- [Docker](https://www.docker.com/products/docker-desktop/) `19.03+`
- Docker Compose v2 (`docker compose`)

---

## ⚡ Quick Start (Local DB)

1. Create `.env`:
```bash
cp env.dist .env
```
1. Create `config.yml`:
```bash
make config
```
2. Ensure local DB is selected:
```bash
make internal_db
```
3. Build and start everything:
```bash
make setup-all
```

> [!NOTE]
> If the database is still starting, `make db-setup` may fail the first time.
> Just rerun it after a few seconds.
>
> If you run `docker compose down -v`, the local DB volume is removed and the databases are empty.
> Run `make db-setup` or `make db-restore` after that.

---

## 🗄️ Database Workflow (Makefile)

- Create databases:
```bash
make db-create
```
- Drop databases:
```bash
make db-drop
```
- Populate schema:
```bash
make db-populate
```
- Apply updates:
```bash
make db-update
```
- Full setup (create → populate → update):
```bash
make db-setup
```
- Backup:
```bash
make db-backup
```
- Restore (use a specific backup folder name):
```bash
make db-restore BACKUP=<folder>
```

> [!NOTE]
> All database names use the prefix from `.env` (`DB_PREFIX`).

---

## 🔌 External Database

1. Set these values in `.env`:
```
EXTERNAL_DB_HOST=
EXTERNAL_DB_PORT=3306
EXTERNAL_DB_USERNAME=
EXTERNAL_DB_PASSWORD=
```
2. Apply external DB config:
```bash
make external_db
```
3. Start containers:
```bash
make up
```

This updates `etc/config/config.yml` with concrete values and disables the local `sql`
service by clearing `COMPOSE_PROFILES`.

---

## 🔁 Switch Back to Local DB

```bash
make internal_db
make up
```

This rewrites `etc/config/config.yml` with local values and enables the `localdb` profile.

---

## ⚙️ Important `.env` Values

- `DB_PREFIX=alpha_`
Database names become `${DB_PREFIX}auth`, `${DB_PREFIX}realm`, `${DB_PREFIX}world`, `${DB_PREFIX}dbc`.
- `MYSQL_PORT=3306`
Port used *inside* the Docker network.
- `MYSQL_HOST_PORT=3300`
Port exposed on your host (useful when 3306 is already in use).
- `COMPOSE_PROFILES=localdb`
Enables the local MariaDB container.

---

## 🛠️ Permissions Tip

If `config.yml` is owned by root (often created by a container), fix it once:
```bash
sudo chown $USER:$USER etc/config/config.yml
```
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

## ⚙️ Configuration

For Docker Compose + Makefile usage, see `README.docker.md`.

1. In `etc/config`, create a copy of `config.yml.dist` and rename it to `config.yml`.
Edit the file as needed for your setup.

Expand Down Expand Up @@ -47,19 +49,15 @@
> host: 127.0.0.1
> ```

5. In `etc/databases`, run `create_databases.sql` with a root (or equivalent) user. This creates:
- User: `alphapython`
- Databases: `alpha_auth`, `alpha_realm`, `alpha_world`, `alpha_dbc`

> [!NOTE]
> If you're using Docker and want to use the `alphapython` user, you may need to change
> `'alphapython'@'localhost'` to `'alphapython'@'IPv4OfYourDockerContainer'` in `create_databases.sql`
> if you encounter permission errors.
5. Create the databases using the utility scripts:
- `make db-create`
- Uses `.env` values (`MYSQL_USERNAME`, `MYSQL_PASSWORD`, `DB_PREFIX`)
- Creates `${DB_PREFIX}auth`, `${DB_PREFIX}realm`, `${DB_PREFIX}world`, `${DB_PREFIX}dbc`

6. Each folder (`auth`, `dbc`, `realm`, `world`) in `etc/databases` contains:
- Base SQL files
- Updates in the `/updates` subfolder
Example: `dbc/updates` should be applied to the `alpha_dbc` database.
Example: `dbc/updates` should be applied to the `${DB_PREFIX}dbc` database.

---

Expand All @@ -83,6 +81,8 @@
- [Docker](https://www.docker.com/products/docker-desktop/) `19.03+`
- `docker-compose` `1.28+` (install with `pip3 install docker-compose` if needed)

See `README.docker.md` for Docker Compose and Makefile workflows.

- Start the containers:
```bash
docker-compose up -d
Expand Down
Loading