diff --git a/.gitignore b/.gitignore index 02f32301335a..ec87f83efcad 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,6 @@ docker-cache-new/ # Ignore the version.json as this should be created during the build version.json + +# Ignore db backup files +backup-olympia.*.sql diff --git a/Makefile-docker b/Makefile-docker index 3bb64f42f5ec..60b986667131 100644 --- a/Makefile-docker +++ b/Makefile-docker @@ -96,7 +96,10 @@ initialize_db: ## create a new database $(PYTHON_COMMAND) manage.py migrate --noinput $(PYTHON_COMMAND) manage.py loaddata initial.json $(PYTHON_COMMAND) manage.py import_prod_versions - $(PYTHON_COMMAND) manage.py createsuperuser + $(PYTHON_COMMAND) manage.py createsuperuser \ + --no-input \ + --username "$(SUPERUSER_USERNAME)" \ + --email $(SUPERUSER_EMAIL) $(PYTHON_COMMAND) manage.py loaddata zadmin/users .PHONY: populate_data diff --git a/Makefile-os b/Makefile-os index c06f3bcd27a5..1405ac08085c 100644 --- a/Makefile-os +++ b/Makefile-os @@ -9,6 +9,11 @@ VERSION_BUILD_URL ?= build # Exporting these variables make them default values for docker-compose*.yml files export DOCKER_VERSION ?= local +EXPORT_FILE ?= backup-olympia.$(shell date +%Y%m%d%H%M%S).sql +# define the username and email for the superuser in the container +SUPERUSER_EMAIL=$(shell git config user.email) +SUPERUSER_USERNAME=$(shell git config user.name) + .PHONY: help_redirect help_redirect: @$(MAKE) help --no-print-directory @@ -41,7 +46,22 @@ rootshell: ## connect to a running addons-server docker shell with root user .PHONY: create_env_file create_env_file: - echo "HOST_UID=${HOST_UID}" > .env + @ rm -f .env + echo "HOST_UID=${HOST_UID}" >> .env + echo "SUPERUSER_EMAIL=${SUPERUSER_EMAIL}" >> .env + echo "SUPERUSER_USERNAME=${SUPERUSER_USERNAME}" >> .env + +.PHONY: db_export +db_export: + @echo "Exporting database to $(EXPORT_FILE)" + docker compose exec mysqld /usr/bin/mysqldump olympia > $(EXPORT_FILE) + +.PHONY: db_restore +db_restore: + @if [ ! -f "$(RESTORE_FILE)" ]; then echo "File $(RESTORE_FILE) does not exist"; exit 1; fi + @echo "Restoring database from $(RESTORE_FILE)" + cat $(RESTORE_FILE) | docker compose exec -T mysqld /usr/bin/mysql olympia + rm $(RESTORE_FILE) .PHONY: create_docker_builder create_docker_builder: ## Create a custom builder for buildkit to efficiently build local images diff --git a/docker-compose.yml b/docker-compose.yml index ae38d7c2e41c..69c7c24c1762 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,6 +21,9 @@ x-env-mapping: &env # it exists. ./docker/entrypoint.sh uses this variable to fix # the uid of the olympia user to match the host user id if necessary. - HOST_UID=${HOST_UID:-} + # This is the email address of the superuser created when initializing the db + - SUPERUSER_EMAIL=${SUPERUSER_EMAIL:-admin@mozilla.com} + - SUPERUSER_USERNAME=${SUPERUSER_USERNAME:-admin} services: worker: &worker @@ -89,6 +92,8 @@ services: - MYSQL_DATABASE=olympia ports: - "3306:3306" + volumes: + - data:/var/lib/mysql elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:7.17.3 @@ -144,3 +149,6 @@ services: networks: default: + +volumes: + data: