diff --git a/Dockerfile b/Dockerfile index 53db803d..0c3808f6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,12 +14,16 @@ RUN pip install -U pip setuptools wheel COPY requirements-test.txt requirements.txt /opt/openwisp/ RUN pip install -r /opt/openwisp/requirements.txt && \ pip install -r /opt/openwisp/requirements-test.txt && \ - rm -rf /var/lib/apt/lists/* /root/.cache/pip/* /tmp/* + rm -rf /root/.cache/pip/* /tmp/* # Copy project files and install the project -ADD . /opt/openwisp +COPY . /opt/openwisp RUN pip install -U /opt/openwisp && \ - rm -rf /var/lib/apt/lists/* /root/.cache/pip/* /tmp/* + rm -rf /root/.cache/pip/* /tmp/* + +# Copy entrypoint script +COPY docker-entrypoint.sh /opt/openwisp/docker-entrypoint.sh +RUN chmod +x /opt/openwisp/docker-entrypoint.sh # Set working directory WORKDIR /opt/openwisp/tests/ @@ -35,4 +39,4 @@ ENV NAME=openwisp-monitoring \ EXPOSE 8000 # Command to run the application -CMD ["sh", "docker-entrypoint.sh"] +ENTRYPOINT ["/opt/openwisp/docker-entrypoint.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index 4df296e0..f3b9aed0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,12 +8,9 @@ services: dockerfile: Dockerfile ports: - "8000:8000" - - "8089:8089/udp" - - "8090:8090/udp" - - "8091:8091/udp" - - "8092:8092/udp" depends_on: - influxdb + - influxdb2 - redis influxdb: @@ -32,11 +29,33 @@ services: INFLUXDB_USER: openwisp INFLUXDB_USER_PASSWORD: openwisp + influxdb2: + image: influxdb:2.0-alpine + volumes: + - influxdb2-data:/var/lib/influxdb2 + ports: + - "9999:9999" + environment: + DOCKER_INFLUXDB_INIT_MODE: setup + DOCKER_INFLUXDB_INIT_USERNAME: openwisp + DOCKER_INFLUXDB_INIT_PASSWORD: openwisp + DOCKER_INFLUXDB_INIT_ORG: openwisp + DOCKER_INFLUXDB_INIT_BUCKET: openwisp2 + DOCKER_INFLUXDB_INIT_RETENTION: 1w + DOCKER_INFLUXDB_INIT_ADMIN_TOKEN: my-super-secret-auth-token + INFLUXD_LOG_LEVEL: debug + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:9999/health"] + interval: 30s + timeout: 10s + retries: 5 + redis: image: redis:5.0-alpine ports: - "6379:6379" - entrypoint: redis-server --appendonly yes + entrypoint: ["redis-server", "--appendonly", "yes"] volumes: - influxdb-data: {} + influxdb-data: + influxdb2-data: diff --git a/openwisp_monitoring/db/backends/__init__.py b/openwisp_monitoring/db/backends/__init__.py index bac780e7..43963926 100644 --- a/openwisp_monitoring/db/backends/__init__.py +++ b/openwisp_monitoring/db/backends/__init__.py @@ -40,9 +40,6 @@ def load_backend_module(backend_name=TIMESERIES_DB['BACKEND'], module=None): assert 'USER' in TIMESERIES_DB, 'USER' assert 'PASSWORD' in TIMESERIES_DB, 'PASSWORD' assert 'NAME' in TIMESERIES_DB, 'NAME' - assert 'USER' in TIMESERIES_DB, 'USER' - assert 'PASSWORD' in TIMESERIES_DB, 'PASSWORD' - assert 'NAME' in TIMESERIES_DB, 'NAME' assert 'HOST' in TIMESERIES_DB, 'HOST' assert 'PORT' in TIMESERIES_DB, 'PORT' if module: diff --git a/tests/docker-entrypoint.sh b/tests/docker-entrypoint.sh old mode 100644 new mode 100755 index 20766cf4..ffb8aacf --- a/tests/docker-entrypoint.sh +++ b/tests/docker-entrypoint.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -e + create_superuser() { local username="$1" local email="$2" @@ -16,8 +18,21 @@ else: EOF } +# Run migrations +echo "Running database migrations..." python manage.py migrate --no-input + +# Create superuser if it doesn't exist +echo "Creating superuser if it doesn't exist..." create_superuser admin admin@example.com admin + +# Start Celery worker and beat in the background +echo "Starting Celery worker..." celery -A openwisp2 worker -l info & + +echo "Starting Celery beat..." celery -A openwisp2 beat -l info & -python manage.py runserver 0.0.0.0:8000 + +# Start the Django development server +echo "Starting Django server..." +exec python manage.py runserver 0.0.0.0:8000 diff --git a/tests/openwisp2/settings.py b/tests/openwisp2/settings.py index 3d3dbf30..8092a856 100644 --- a/tests/openwisp2/settings.py +++ b/tests/openwisp2/settings.py @@ -26,22 +26,23 @@ 'USER': 'openwisp', 'PASSWORD': 'openwisp', 'NAME': 'openwisp2', - 'HOST': os.getenv('INFLUXDB_HOST', 'localhost'), + 'HOST': 'influxdb', 'PORT': '8086', # UDP writes are disabled by default 'OPTIONS': {'udp_writes': False, 'udp_port': 8089}, } +# For InfluxDB 2.x INFLUXDB_2x_DATABASE = { 'BACKEND': 'openwisp_monitoring.db.backends.influxdb2', - 'TOKEN': 'your-influxdb-2.0-token', - 'ORG': 'your-org', - 'BUCKET': 'your-bucket', - 'HOST': os.getenv('INFLUXDB2_HOST', 'localhost'), - 'PORT': '8087', + 'TOKEN': 'my-super-secret-auth-token', + 'ORG': 'openwisp', + 'BUCKET': 'openwisp2', + 'HOST': 'influxdb2', + 'PORT': '9999', } -if os.environ.get('USE_INFLUXDB2', False): +if os.environ.get('USE_INFLUXDB2', 'False') == 'True': TIMESERIES_DATABASE = INFLUXDB_2x_DATABASE else: TIMESERIES_DATABASE = INFLUXDB_1x_DATABASE