Skip to content

Ease onboarding of new developers using devcontainers #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions .devcontainer/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
PUBLIC_SERVER_URL='http://127.0.0.1:8000'
ORIGIN='http://127.0.0.1:5173'
__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS='127.0.0.1'
BODY_SIZE_LIMIT=Infinity
POSTGRES_DB=database
POSTGRES_USER=adventure
POSTGRES_PASSWORD=changeme123
PGHOST=localhost
PGDATABASE=database
PGUSER=adventure
PGPASSWORD=changeme123
SECRET_KEY=changeme123
DJANGO_ADMIN_USERNAME=admin
DJANGO_ADMIN_PASSWORD=admin
DJANGO_ADMIN_EMAIL='[email protected]'
PUBLIC_URL='http://127.0.0.1:8000'
CSRF_TRUSTED_ORIGINS='http://127.0.0.1:8000,http://127.0.0.1:5173'
DEBUG=True
FRONTEND_URL='http://127.0.0.1:5173'
EMAIL_BACKEND=console
SECRET_KEY='pleasechangethisbecauseifyoudontitwillbeverybadandyouwillgethackedinlessthanaminuteguaranteed'
17 changes: 13 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers/features/python:1": {}
}
},

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
"forwardPorts": [5173,8000],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "uname -a",
"postCreateCommand": "./.devcontainer/postcreate.sh",

"postStartCommand": "./.devcontainer/poststart.sh",

// Configure tool-specific properties.
// "customizations": {},
"customizations": {
"vscode": {
"extensions": [
"svelte.svelte-vscode",
"Lokalise.i18n-ally"
]
}
}

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
Expand Down
11 changes: 11 additions & 0 deletions .devcontainer/postcreate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# Install dependencies for backend and poststart.sh
sudo apt update && sudo apt install -y python3-gdal postgresql-client
pip install -r ./backend/server/requirements.txt
cd ./backend/server
# Create static files on backend
python manage.py collectstatic --noinput
cd ../../frontend
# Install frontend dependencies
npm install
71 changes: 71 additions & 0 deletions .devcontainer/poststart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/bin/bash

set -a # Make sure to export vars to the whole environment
source ./.devcontainer/.env
set +a

# Run PostGIS with "docker in docker" on port 5432
docker run --rm --name adventurelog-devdb -e POSTGRES_USER -e POSTGRES_PASSWORD -e POSTGRES_DB -p 5432:5432 -d postgis/postgis:15-3.3

cd ./backend/server

# Function to check PostgreSQL availability
check_postgres() {
psql -c '\q' >/dev/null 2>&1
}

# Wait for PostgreSQL to become available
until check_postgres; do
>&2 echo "PostgreSQL is unavailable - sleeping"
sleep 1
done

>&2 echo "PostgreSQL is up - continuing..."

# Apply Django migrations
python manage.py migrate

# Create superuser if environment variables are set and there are no users present at all.
if [ -n "$DJANGO_ADMIN_USERNAME" ] && [ -n "$DJANGO_ADMIN_PASSWORD" ] && [ -n "$DJANGO_ADMIN_EMAIL" ]; then
echo "Creating superuser..."
python manage.py shell << EOF
from django.contrib.auth import get_user_model
from allauth.account.models import EmailAddress
User = get_user_model()
# Check if the user already exists
if not User.objects.filter(username='$DJANGO_ADMIN_USERNAME').exists():
# Create the superuser
superuser = User.objects.create_superuser(
username='$DJANGO_ADMIN_USERNAME',
email='$DJANGO_ADMIN_EMAIL',
password='$DJANGO_ADMIN_PASSWORD'
)
print("Superuser created successfully.")
# Create the EmailAddress object for AllAuth
EmailAddress.objects.create(
user=superuser,
email='$DJANGO_ADMIN_EMAIL',
verified=True,
primary=True
)
print("EmailAddress object created successfully for AllAuth.")
else:
print("Superuser already exists.")
EOF
fi

# Sync the countries and world travel regions
mkdir media
python manage.py download-countries
if [ $? -eq 137 ]; then
>&2 echo "WARNING: The download-countries command was interrupted. This is likely due to lack of memory allocated to the container or the host. Please try again with more memory."
exit 1
fi

# Run backend in dev mode
nohup $SHELL -c "python manage.py runserver 2>&1 | tee -a /tmp/servers.log >> /tmp/backend.log" > /dev/null &

cd ../../frontend

# Run frontend in dev mode
nohup $SHELL -c "npm run dev 2>&1 | tee -a /tmp/servers.log >> /tmp/frontend.log" > /dev/null &
7 changes: 6 additions & 1 deletion frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ export default defineConfig({
Icons({
compiler: 'svelte'
})
]
],
server: {
watch: {
usePolling: true
}
}
});