From df6b8a381a39c5a0d949fa16300f2f549d0feb55 Mon Sep 17 00:00:00 2001 From: timothymiller Date: Wed, 5 Aug 2020 03:25:13 -0400 Subject: [PATCH] Minimal multi-stage docker build --- .github/ISSUE_TEMPLATE/bug_report.md | 16 +++---- .gitignore | 71 +--------------------------- .vscode/settings.json | 18 +++++++ Dockerfile | 18 +++++++ build-docker-image.sh | 1 + cloudflare-ddns.py | 27 +++++++++-- requirements.txt | 1 + start-sync.sh | 2 +- 8 files changed, 73 insertions(+), 81 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 Dockerfile create mode 100755 build-docker-image.sh create mode 100644 requirements.txt diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index dd84ea7..e66c16d 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -3,7 +3,7 @@ name: Bug report about: Create a report to help us improve title: '' labels: '' -assignees: '' +assignees: 'timothymiller' --- @@ -24,15 +24,15 @@ A clear and concise description of what you expected to happen. If applicable, add screenshots to help explain your problem. **Desktop (please complete the following information):** - - OS: [e.g. iOS] - - Browser [e.g. chrome, safari] - - Version [e.g. 22] + - OS: [e.g. Arch Linux] + - Browser [e.g. Chrome, Safari] + - Version [e.g. 60] **Smartphone (please complete the following information):** - - Device: [e.g. iPhone6] - - OS: [e.g. iOS8.1] - - Browser [e.g. stock browser, safari] - - Version [e.g. 22] + - Device: [e.g. iPhone X] + - OS: [e.g. iOS 14.0] + - Browser [e.g. stock browser, Safari] + - Version [e.g. 60] **Additional context** Add any other context about the problem here. diff --git a/.gitignore b/.gitignore index 2e96c5c..843b7e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +# Private API keys for updating IPv4 & IPv6 addresses on Cloudflare config.json # Byte-compiled / optimized / DLL files @@ -39,43 +40,6 @@ MANIFEST pip-log.txt pip-delete-this-directory.txt -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - # Jupyter Notebook .ipynb_checkpoints @@ -86,19 +50,6 @@ ipython_config.py # pyenv .python-version -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# celery beat schedule file -celerybeat-schedule - -# SageMath parsed files -*.sage.py - # Environments .env .venv @@ -106,22 +57,4 @@ env/ venv/ ENV/ env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ +venv.bak/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c65f915 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + "files.exclude": { + "**/.git": true, + "**/.svn": true, + "**/.hg": true, + "**/CVS": true, + "**/.DS_Store": true, + ".github": true, + ".vscode": true, + "LICENSE": true, + "requirements.txt": true, + "build-docker-image.sh": true, + ".gitignore": true, + "Dockerfile": false, + "start-sync.sh": false + }, + "explorerExclude.backup": null +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f5d2006 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# ---- Base ---- +FROM python:alpine AS base + +# +# ---- Dependencies ---- +FROM base AS dependencies +# install dependencies +COPY requirements.txt . +RUN pip install -r requirements.txt + +# +# ---- Release ---- +FROM dependencies AS release +# copy project file(s) +WORKDIR / +COPY cloudflare-ddns.py . +COPY config.json . +CMD ["python", "/cloudflare-ddns.py"] \ No newline at end of file diff --git a/build-docker-image.sh b/build-docker-image.sh new file mode 100755 index 0000000..fabbd86 --- /dev/null +++ b/build-docker-image.sh @@ -0,0 +1 @@ +docker build -t timothymiller/cloudflare-ddns:latest . \ No newline at end of file diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 038b09c..f8181ab 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -104,6 +104,27 @@ def cf_api(endpoint, method, config, headers={}, data=False): return response.json() -for ip in getIPs(): - print("Checking " + ip["type"] + " records") - commitRecord(ip) +# Scheduling +import time, traceback + +def every(delay, task): + next_time = time.time() + delay + while True: + time.sleep(max(0, next_time - time.time())) + try: + task() + except Exception: + traceback.print_exc() + # in production code you might want to have this instead of course: + # logger.exception("Problem while executing repetitive task.") + # skip tasks if we are behind schedule: + next_time += (time.time() - next_time) // delay * delay + delay + +def updateIPs(): + for ip in getIPs(): + print("Checking " + ip["type"] + " records") + commitRecord(ip) + +updateIPs() +import threading +threading.Thread(target=lambda: every(60*15, updateIPs)).start() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4261e68 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests==2.24.0 \ No newline at end of file diff --git a/start-sync.sh b/start-sync.sh index 92872eb..0cfec0f 100755 --- a/start-sync.sh +++ b/start-sync.sh @@ -4,7 +4,7 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" python3 -m venv venv source ./venv/bin/activate -pip install requests +pip3 install requests cd $DIR python3 cloudflare-ddns.py