diff --git a/Dockerfile b/Dockerfile index 670eea2..feb06dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,11 @@ -FROM python:alpine3.18 as builder +FROM python:3.9.18-alpine3.18 as builder LABEL maintainer "tuan t. pham" ENV PKGS="py3-virtualenv" RUN apk update && apk add ${PKGS} \ && mkdir -p /opt/pp-code-metrics - COPY ./requirements.txt /opt/pp-code-metrics - RUN cd /opt/pp-code-metrics \ && virtualenv metrics \ && source /opt/pp-code-metrics/metrics/bin/activate \ @@ -17,9 +15,10 @@ RUN cd /opt/pp-code-metrics \ FROM mikefarah/yq:latest as yq_base -FROM python:alpine3.18 as deploy +FROM python:3.9.18-alpine3.18 as deploy ENV PKGS="py3-virtualenv" +ENV DOCKERIZE_URL="https://github.com/jwilder/dockerize/releases/download/v0.7.0/dockerize-alpine-linux-amd64-v0.7.0.tar.gz" RUN apk update && apk add ${PKGS} \ && mkdir -p /opt/pp-code-metrics @@ -34,4 +33,11 @@ COPY --from=yq_base /usr/bin/yq /usr/bin/yq WORKDIR /opt/pp-code-metrics ENV VIRT_PYTHON=/opt/pp-code-metrics/metrics/bin/python3 -CMD ["/opt/pp-code-metrics/run_get_metrics.sh", "my_settings.yml"] +RUN wget -O - ${DOCKERIZE_URL} | tar xzf - -C /usr/local/bin + +RUN mkdir -p /etc/cron.d +COPY crontab/pp-code-metrics.tmpl /etc/cron.d/ +ENTRYPOINT dockerize --template /etc/cron.d/pp-code-metrics.tmpl:/etc/cron.d/pp-code-metrics crontab /etc/cron.d/pp-code-metrics && \ + /usr/sbin/crond -f -d 0 + +#CMD ["/opt/pp-code-metrics/run_get_metrics.sh", "my_settings.yml"] diff --git a/crontab/pp-code-metrics.tmpl b/crontab/pp-code-metrics.tmpl new file mode 100644 index 0000000..5cba576 --- /dev/null +++ b/crontab/pp-code-metrics.tmpl @@ -0,0 +1,5 @@ +# Run get_metrics.py every 10 minutes + +# + +*/{{ .Env.INTERVAL }} * * * * /opt/pp-code-metrics/run_get_metrics.sh my_settings.yml diff --git a/docker-compose.yml b/docker-compose.yml index defe506..c8c8c6d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,12 +7,14 @@ services: hostname: pp-code-metrics image: neofob/pp-code-metrics:latest restart: always + environment: + INTERVAL: 10 volumes: - ./my_settings.yml:/opt/pp-code-metrics/my_settings.yml:ro deploy: resources: limits: - memory: 32M + memory: 64M logging: driver: "json-file" options: diff --git a/get_metrics.py b/get_metrics.py index 87c34bc..51298c4 100755 --- a/get_metrics.py +++ b/get_metrics.py @@ -142,7 +142,7 @@ def getNodeMetrics(node): for i in range(3): try: write_api.write(bucket=influxdb_bucket, record=p) - except NewConnectionError: + except ConnectionError: time.sleep(5) countinue if try_again: diff --git a/run_get_metrics.sh b/run_get_metrics.sh index 13dccbe..b62754a 100755 --- a/run_get_metrics.sh +++ b/run_get_metrics.sh @@ -1,13 +1,11 @@ #!/usr/bin/env sh set -o nounset # Treat unset variables as an error -INTERVAL=$(cat $1 | yq '.General.interval') -echo "INTERVAL=$INTERVAL" +#INTERVAL=$(cat $1 | yq '.General.interval') +#echo "INTERVAL=$INTERVAL" #INTERVAL=${INTERVAL:=600} VIRT_PYTHON=${VIRT_PYTHON:=/home/vagrant/.virtualenvs/metrics/bin/python3} -while true; do - ${VIRT_PYTHON} ./get_metrics.py --config $1 - sleep ${INTERVAL}s -done +cd /opt/pp-code-metrics +${VIRT_PYTHON} ./get_metrics.py --config $1 diff --git a/sample_settings.yml b/sample_settings.yml index e72c2dc..59b70d5 100644 --- a/sample_settings.yml +++ b/sample_settings.yml @@ -1,7 +1,8 @@ # Default settings --- General: - Interval: 600 + # It is in minutes for cron job config now + Interval: 10 InfluxDB: # avoid using 'localhost' when this is deployed to be used by get_metrics.py