Skip to content

Commit

Permalink
refactor: remove cron & ajust timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
Couapy committed May 16, 2021
1 parent eaa1e5b commit bcae3db
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
8 changes: 3 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
FROM python:3.9

# Install CRON
RUN apt-get -qq update && apt-get install -yqq cron
COPY config/cron.conf /etc/cron.d/dynhostupdater
RUN chmod +x /etc/cron.d/dynhostupdater
RUN crontab /etc/cron.d/dynhostupdater
# Configure locale (log date & time)
ENV TZ=Europe/Paris
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install application
WORKDIR /app
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ The recommanded way to execute this tool is docker.
This tool can be executed in a container with docker :

```bash
docker build -t dynhostupdater .
docker run dynhostupdater -d
docker-compose up -d
```

### Locally
Expand All @@ -29,7 +28,7 @@ source .venv/bin/activate
pip install -r requirements.txt
# To execute the script once:
python main.py
python3 main.py
```

To execute automatically the script, you can use the `config/con.conf` cron file.
Expand Down
4 changes: 0 additions & 4 deletions config/cron.conf

This file was deleted.

2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3.9"
services:
dynhost:
build: .
command: "cron -f"
command: "python3 /app/main.py"
volumes:
- ./logs:/app/logs

Expand Down
33 changes: 24 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import os
import signal
import sys
import time
from configparser import ConfigParser

import dns.resolver
Expand All @@ -10,6 +12,7 @@
URI_UPDATE = 'http://www.ovh.com/nic/update?system=dyndns&hostname=%(hostname)s&myip=%(ip)s'
URI_GET_IP = 'https://ifconfig.me/ip'
DNS_RESOLVERS = ['8.8.8.8']
UPDATE_INTERVAL = 60 * 10 # Seconds


class Domain:
Expand Down Expand Up @@ -62,6 +65,12 @@ def get_current_ip() -> str:
return req.text


def signal_handler(signum: int, frame):
print(end="\r")
logging.info("stopped")
sys.exit(0)


def main():
"""Configure logs, and update the domains configured in SITES_CONFIG file."""
os.makedirs('logs', exist_ok=True)
Expand All @@ -80,15 +89,21 @@ def main():
config = ConfigParser()
config.read(os.path.join(SITES_CONFIG))

current_ip = get_current_ip()

for section in config.sections():
domain = Domain(
domain=section,
user=config.get(section, 'user'),
password=config.get(section, 'password'),
)
domain.update(current_ip)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
logging.info("started")

while True:
current_ip = get_current_ip()

for section in config.sections():
domain = Domain(
domain=section,
user=config.get(section, 'user'),
password=config.get(section, 'password'),
)
domain.update(current_ip)
time.sleep(UPDATE_INTERVAL)


if __name__ == '__main__':
Expand Down

0 comments on commit bcae3db

Please sign in to comment.