Skip to content

Commit

Permalink
[docker] Run openwisp-monitor in docker (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
atb00ker authored Jul 17, 2020
1 parent 57b0ded commit df41092
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 11 deletions.
18 changes: 12 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,23 @@ before_install:

install:
- pip install -e .
# TODO: removed when openwisp-controller 0.8.0 is released
- pip install -U https://github.com/openwisp/openwisp-controller/tarball/master
# TODO: removed when openwisp-users 0.3.0 is released
- pip install -U https://github.com/openwisp/openwisp-users/tarball/master
# TODO: remove when openwisp-notifications 0.1 is released
- pip install -U https://github.com/openwisp/openwisp-notifications/tarball/master
- sh install-dev.sh

script:
- ./run-qa-checks
- SAMPLE_APP=1 coverage run --source=openwisp_monitoring runtests.py
- coverage run -a --source=openwisp_monitoring runtests.py

jobs:
include:
- stage: Deploy
before_install: skip
install: skip
if: type = push AND branch = master
script:
- echo "$DOCKER_TOKEN" | docker login --username $DOCKER_USERNAME --password-stdin
- docker build . -t openwisp/openwisp-monitoring:develop
- docker push openwisp/openwisp-monitoring:develop

after_success:
coveralls
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM python:3.7.5-slim-buster

RUN apt update && \
apt install --yes zlib1g-dev libjpeg-dev gdal-bin libproj-dev \
libgeos-dev libspatialite-dev libsqlite3-mod-spatialite \
sqlite3 libsqlite3-dev openssl libssl-dev fping && \
rm -rf /var/lib/apt/lists/* /root/.cache/pip/* /tmp/*

COPY install-dev.sh requirements-test.txt requirements.txt /opt/openwisp/
RUN pip install -r /opt/openwisp/requirements.txt && \
pip install -r /opt/openwisp/requirements-test.txt && \
sh /opt/openwisp/install-dev.sh && \
rm -rf /var/lib/apt/lists/* /root/.cache/pip/* /tmp/*

ADD . /opt/openwisp
RUN pip install -U /opt/openwisp && \
rm -rf /var/lib/apt/lists/* /root/.cache/pip/* /tmp/*
WORKDIR /opt/openwisp/tests/
ENV NAME=openwisp-monitoring \
PYTHONBUFFERED=1 \
INFLUXDB_HOST=influxdb \
REDIS_HOST=redis
CMD ["sh", "docker-entrypoint.sh"]
EXPOSE 8000
17 changes: 16 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ Install your forked repo:
cd openwisp-monitoring/
python setup.py develop
Install development dependencies:

.. code-block:: shell
./install-dev.sh
Install test requirements:

.. code-block:: shell
Expand All @@ -624,7 +630,7 @@ Start Redis and InfluxDB using docker-compose:

.. code-block:: shell
docker-compose up -d
docker-compose up -d redis influxdb
Create the Django database:

Expand Down Expand Up @@ -670,6 +676,15 @@ which are simple django apps that extend ``openwisp-monitoring`` with
the sole purpose of testing its extensibility, for more information regarding
this concept, read the following section.

Install and run on docker
-------------------------

.. code-block:: shell
# ``--build`` parameter is useful when you want to
# rebuild the openwisp-monitoring image with your changes.
docker-compose up --build
Extending openwisp-monitoring
-----------------------------

Expand Down
13 changes: 12 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
version: "3"

services:
monitoring:
image: openwisp/openwisp-monitoring:develop
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:8000"
depends_on:
- influxdb
- redis

influxdb:
image: influxdb:1.8-alpine
volumes:
Expand All @@ -19,4 +30,4 @@ services:
entrypoint: redis-server --appendonly yes

volumes:
influxdb-data:
influxdb-data: {}
8 changes: 8 additions & 0 deletions install-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# TODO: removed when openwisp-controller 0.8.0 is released
pip install -U https://github.com/openwisp/openwisp-controller/tarball/master
# TODO: removed when openwisp-users 0.3.0 is released
pip install -U https://github.com/openwisp/openwisp-users/tarball/master
# TODO: remove when openwisp-notifications 0.1 is released
pip install -U https://github.com/openwisp/openwisp-notifications/tarball/master
23 changes: 23 additions & 0 deletions tests/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

create_superuser() {
local username="$1"
local email="$2"
local password="$3"
cat <<EOF | python manage.py shell
from django.contrib.auth import get_user_model
User = get_user_model()
if not User.objects.filter(username="$username").exists():
User.objects.create_superuser("$username", "$email", "$password")
else:
print('User "{}" exists already, not created'.format("$username"))
EOF
}

python manage.py migrate --no-input
create_superuser admin [email protected] admin
celery -A openwisp2 worker -l info &
celery -A openwisp2 beat -l info &
python manage.py runserver 0.0.0.0:8000
7 changes: 4 additions & 3 deletions tests/openwisp2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'USER': 'openwisp',
'PASSWORD': 'openwisp',
'NAME': 'openwisp2',
'HOST': 'localhost',
'HOST': os.getenv('INFLUXDB_HOST', 'localhost'),
'PORT': '8086',
}

Expand Down Expand Up @@ -144,10 +144,11 @@

OPENWISP_MONITORING_MANAGEMENT_IP_ONLY = False

redis_host = os.getenv('REDIS_HOST', 'localhost')
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://localhost/0',
'LOCATION': f'redis://{redis_host}/0',
'OPTIONS': {'CLIENT_CLASS': 'django_redis.client.DefaultClient',},
}
}
Expand All @@ -156,7 +157,7 @@
SESSION_CACHE_ALIAS = 'default'

if not TESTING:
CELERY_BROKER_URL = 'redis://localhost/1'
CELERY_BROKER_URL = f'redis://{redis_host}/1'
else:
CELERY_TASK_ALWAYS_EAGER = True
CELERY_TASK_EAGER_PROPAGATES = True
Expand Down

0 comments on commit df41092

Please sign in to comment.