Skip to content

Commit

Permalink
[fix] Corrections in docker-compose #274
Browse files Browse the repository at this point in the history
Fixes #274
  • Loading branch information
praptisharma28 committed Jun 8, 2024
1 parent 4a6c3aa commit a219447
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 152 deletions.
22 changes: 5 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,17 @@ 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 /root/.cache/pip/* /tmp/*
rm -rf /var/lib/apt/lists/* /root/.cache/pip/* /tmp/*

# Copy project files and install the project
COPY . /opt/openwisp
ADD . /opt/openwisp
RUN pip install -U /opt/openwisp && \
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
rm -rf /var/lib/apt/lists/* /root/.cache/pip/* /tmp/*
WORKDIR /opt/openwisp/tests/

# Set environment variables
ENV NAME=openwisp-monitoring \
PYTHONBUFFERED=1 \
INFLUXDB1_HOST=influxdb \
INFLUXDB2_HOST=influxdb2 \
INFLUXDB_HOST=influxdb \
REDIS_HOST=redis

# Expose the application port
CMD ["sh", "docker-entrypoint.sh"]
EXPOSE 8000

# Command to run the application
ENTRYPOINT ["/opt/openwisp/docker-entrypoint.sh"]
32 changes: 13 additions & 19 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ services:
- "8000:8000"
depends_on:
- influxdb
- influxdb2
- redis

influxdb:
Expand All @@ -30,32 +29,27 @@ services:
INFLUXDB_USER_PASSWORD: openwisp

influxdb2:
image: influxdb:2.0-alpine
volumes:
- influxdb2-data:/var/lib/influxdb2
image: influxdb:2.0
container_name: influxdb2
ports:
- "9999:9999"
# Map the 9086 port on host machine to 8086 in container
- "9086:8086"
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_USERNAME: myuser
DOCKER_INFLUXDB_INIT_PASSWORD: mypassword
DOCKER_INFLUXDB_INIT_ORG: myorg
DOCKER_INFLUXDB_INIT_BUCKET: mybucket
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
volumes:
- influxdb-storage:/var/lib/influxdb2

redis:
image: redis:5.0-alpine
ports:
- "6379:6379"
entrypoint: ["redis-server", "--appendonly", "yes"]
entrypoint: redis-server --appendonly yes

volumes:
influxdb-data:
influxdb2-data:
influxdb-data: {}
influxdb-storage:
42 changes: 0 additions & 42 deletions openwisp_monitoring/db/backends/base.py

This file was deleted.

Empty file.
79 changes: 23 additions & 56 deletions openwisp_monitoring/db/backends/influxdb2/client.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,45 @@
import logging

import influxdb_client
from django.conf import settings
from django.utils.functional import cached_property
from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.exceptions import InfluxDBError
from influxdb_client.client.write_api import SYNCHRONOUS

from openwisp_monitoring.utils import retry

from ...exceptions import TimeseriesWriteException
from .. import TIMESERIES_DB
from ..base import BaseDatabaseClient

logger = logging.getLogger(__name__)


class DatabaseClient(BaseDatabaseClient):
class DatabaseClient:
backend_name = 'influxdb2'

def __init__(self, db_name=None):
super().__init__(db_name)
self.client_error = InfluxDBError
def __init__(self):
self.token = settings.TIMESERIES_DB['TOKEN']
self.org = settings.TIMESERIES_DB['ORG']
self.bucket = settings.TIMESERIES_DB['BUCKET']
self.url = (
f"http://{settings.TIMESERIES_DB['HOST']}:{settings.TIMESERIES_DB['PORT']}"
)

@cached_property
def db(self):
return InfluxDBClient(
url=f"http://{TIMESERIES_DB['HOST']}:{TIMESERIES_DB['PORT']}",
token=TIMESERIES_DB['TOKEN'],
org=TIMESERIES_DB['ORG'],
bucket=self.db_name,
def client(self):
return influxdb_client.InfluxDBClient(
url=self.url, token=self.token, org=self.org
)

@retry
def create_database(self):
self.write_api = self.db.write_api(write_options=SYNCHRONOUS)
self.query_api = self.db.query_api()
logger.debug('Initialized APIs for InfluxDB 2.0')

@retry
def drop_database(self):
pass # Implement as needed for InfluxDB 2.0
@cached_property
def write_api(self):
return self.client.write_api(write_options=SYNCHRONOUS)

@retry
def query(self, query):
return self.query_api.query(query)

def write(self, name, values, **kwargs):
point = Point(name).time(self._get_timestamp(kwargs.get('timestamp')))
tags = kwargs.get('tags', {})
for tag, value in tags.items():
point.tag(tag, value)
for field, value in values.items():
point.field(field, value)
try:
self.write_api.write(bucket=self.db_name, record=point)
except InfluxDBError as e:
raise TimeseriesWriteException(str(e))
point = influxdb_client.Point(name).fields(values)
self.write_api.write(bucket=self.bucket, org=self.org, record=point)

@retry
def get_list_retention_policies(self, name=None):
bucket = self.db.buckets_api().find_bucket_by_name(name)
if bucket:
return bucket.retention_rules
return []
@cached_property
def query_api(self):
return self.client.query_api()

@retry
def create_or_alter_retention_policy(self, name, duration):
bucket = self.db.buckets_api().find_bucket_by_name(name)
retention_rules = [{"type": "expire", "everySeconds": duration}]
if bucket:
bucket.retention_rules = retention_rules
self.db.buckets_api().update_bucket(bucket=bucket)
else:
self.db.buckets_api().create_bucket(
bucket_name=name,
retention_rules=retention_rules,
org=TIMESERIES_DB["ORG"],
)
def query(self, query):
return self.query_api.query(org=self.org, query=query)
Loading

0 comments on commit a219447

Please sign in to comment.