From 19c2ddc6f5b0da0060f60d20a4560b83f1f6d9b1 Mon Sep 17 00:00:00 2001 From: jung-geun Date: Tue, 24 Sep 2024 22:50:43 +0900 Subject: [PATCH] Refactor Dockerfile to use Python 3.11-slim --- Dockerfile | 3 +-- Makefile | 42 +++++++++++++----------------------------- requirements.txt | 4 +++- scripts/certbot.sh | 2 -- scripts/install.sh | 31 ++++++++++++++++++++++++------- scripts/run_script.sh | 4 ---- scripts/uninstall.sh | 23 +++++++++++++++++------ src/update_dns.py | 4 ++-- 8 files changed, 60 insertions(+), 53 deletions(-) diff --git a/Dockerfile b/Dockerfile index c88a61f..42c5fd8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9-slim +FROM python:3.11-slim RUN mkdir -p /app/cloudflare-ddns/ WORKDIR /app/cloudflare-ddns @@ -15,7 +15,6 @@ RUN apt-get update && apt-get --no-install-recommends install -y \ cron \ libaugeas0 \ make \ - python3.11 python3.11-dev python3.11-pip python3.11-venv \ && rm -rf /var/lib/apt/lists/* \ cp /app/cloudflare-ddns/cron/cronjob-ddns /etc/cron.d/cloudflare-ddns \ pip install --no-cache-dir -r requirements.txt \ diff --git a/Makefile b/Makefile index 1668639..ca62848 100644 --- a/Makefile +++ b/Makefile @@ -1,41 +1,25 @@ -IMAGE_NAME = "cloudflare-ddns" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" -default: build +.DEFAULT_GOAL := help -.PHONY: build -build: stop - docker build -t $(IMAGE_NAME) . - -.PHONY: run -run: - docker run --rm --privileged=true -d -v /opt/cloudflare-ddns/config:/app/cloudflare-ddns/config $(IMAGE_NAME) - -.PHONY: stop -stop: - @container_id=$$(docker ps -q -f ancestor=$(IMAGE_NAME)); \ - if [ -n "$$container_id" ]; then \ - echo "Stopping container $$container_id"; \ - docker stop $$container_id; \ - else \ - echo "No running container found for image $(IMAGE_NAME)"; \ - fi +.PHONY: help +help: + @echo "Usage: make [target]" + @echo "" + @echo "Targets:" + @echo " install Install the required dependencies" + @echo " certbot Install certbot" + @echo " uninstall Uninstall the installed dependencies" + @echo "" .PHONY: install install: - @echo "Installing cloudflare-ddns" @scripts/install.sh .PHONY: certbot certbot: - @echo "Installing certbot" @scripts/certbot.sh -.PHONY: clean -clean: - @rm -rf /app/cloudflare-ddns - @rm -rf /app/certbot - @rm /etc/cron.d/cloudflare-ddns - @rm /var/log/cloudflare_ddns.log - @rm /var/log/cloudflare_ddns.log.* - @echo "Done" \ No newline at end of file +.PHONY: uninstall +uninstall: + @scripts/uninstall.sh diff --git a/requirements.txt b/requirements.txt index 6b1425c..6d10a1c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ -requests==2.32.2 \ No newline at end of file +requests==2.32.2 +certbot==2.11.0 +certbot-dns-cloudflare==1.18.0 \ No newline at end of file diff --git a/scripts/certbot.sh b/scripts/certbot.sh index ad9fec6..cf9bcf6 100755 --- a/scripts/certbot.sh +++ b/scripts/certbot.sh @@ -1,8 +1,6 @@ #!/bin/bash # Certbot 및 Cloudflare API 도구 설치 -sudo apt update -sudo apt install -y certbot python3-certbot-dns-cloudflare jq # 필요한 변수 설정 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" diff --git a/scripts/install.sh b/scripts/install.sh index 97b4845..412c76c 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,19 +1,36 @@ #!/bin/bash DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" -@echo off -echo "Certbot and Cloudflare API tools installation" -sudo apt update -sudo apt install -y certbot python3-certbot-dns-cloudflare jq +echo -e "Certbot and Cloudflare API tools installation started. \n" +sudo apt -qq update +PKG_LIST=(certbot python3-certbot-dns-cloudflare jq) +for pkg in ${PKG_LIST[@]}; do + dpkg -l | grep -q $pkg + if [ $? -eq 0 ]; then + echo "$pkg is already installed." + read -p "Do you want to reinstall it? (y/N): " reinstall + reinstall=${reinstall:-n} + if [ "$reinstall" == "y" ]; then + sudo apt install --reinstall -y $pkg + fi + echo -e "\n\n" + else + echo "$pkg is not installed." + sudo apt install -y $pkg + fi +done sudo mkdir -p /app/cloudflare-ddns sudo cp -r $DIR/../* /app/cloudflare-ddns if [ -f /app/cloudflare-ddns/config/env.json ]; then - read -p "Environment configuration file already exists. Do you want to overwrite it? (y/n): " overwrite + read -p "Environment configuration file already exists. Do you want to overwrite it? (y/N): " overwrite + overwrite=${overwrite:-n} if [ "$overwrite" == "y" ]; then sudo mv /app/cloudflare-ddns/config/env.json /app/cloudflare-ddns/config/env.json.bak sudo cp $DIR/../config/env_example.json /app/cloudflare-ddns/config/env.json sudo chmod 600 /app/cloudflare-ddns/config/env.json + else + echo -e "Environment configuration file is not overwritten. \n" fi else sudo mkdir -p /app/cloudflare-ddns/config @@ -21,6 +38,6 @@ else sudo chmod 600 /app/cloudflare-ddns/config/env.json fi sudo cp $DIR/../cron/cronjob-ddns /etc/cron.d/cloudflare-ddns -echo "Please modify the environment configuration file and save it in the /app/cloudflare-ddns/config/env.json path." +echo -e "Please modify the environment configuration file and save it in the /app/cloudflare-ddns/config/env.json path. \n" -echo "Certbot and Cloudflare API tools installation completed." \ No newline at end of file +echo -e "Certbot and Cloudflare API tools installation completed. \n" \ No newline at end of file diff --git a/scripts/run_script.sh b/scripts/run_script.sh index 36681e4..70fb5fe 100755 --- a/scripts/run_script.sh +++ b/scripts/run_script.sh @@ -1,9 +1,5 @@ #!/bin/bash -export CLOUDFLARE_API_KEY=$API_KEY -export CLOUDFLARE_DOMAIN=$DOMAIN_NAME -export CLOUDFLARE_ZONE_ID=$ZONE_ID - DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # Run the script diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh index dd85e15..96e03f5 100644 --- a/scripts/uninstall.sh +++ b/scripts/uninstall.sh @@ -1,7 +1,18 @@ #!/bin/bash -@echo "Uninstalling Cloudflare DDNS and Certbot..." -@sudo rm -rf /app/cloudflare-ddns -@sudo rm /etc/cron.d/cloudflare-ddns -@sudo rm /var/log/cloudflare_ddns.log -@sudo rm /var/log/cloudflare_ddns.log.* -@echo "Done" \ No newline at end of file + +echo "Uninstalling Cloudflare DDNS and Certbot..." + +read -p "Do you want to uninstall Certbot and Cloudflare API tools? (y/N): " uninstall +uninstall=${uninstall:-n} +echo -e "\n" +if [ "$uninstall" == "y" ]; then + echo "Uninstalling Certbot and Cloudflare API tools..." + sudo apt remove --purge -y certbot python3-certbot-dns-cloudflare jq + sudo rm -rf /app/cloudflare-ddns + sudo rm /etc/cron.d/cloudflare-ddns + sudo rm /var/log/cloudflare_ddns.log + sudo rm /var/log/cloudflare_ddns.log.* + echo "Done" +else + echo "Uninstallation is cancelled." +fi diff --git a/src/update_dns.py b/src/update_dns.py index 4fed954..6b03df8 100644 --- a/src/update_dns.py +++ b/src/update_dns.py @@ -23,12 +23,13 @@ logger.addHandler(logging.StreamHandler()) DEFAULT_CONFIG_PATH = os.path.join(os.path.dirname(__file__), "../config/env.json") PWD = os.path.dirname(os.path.abspath(__file__)) -TMP_PATH = os.path.join(PWD, "tmp") +TMP_PATH = os.path.join(PWD, "../tmp") class DDNS: def __init__(self, config_path=DEFAULT_CONFIG_PATH): self.config = self.load_config(config_path) + self.external_ip_path = os.path.join(TMP_PATH, "external_ip") current_ip = self.get_ip() previous_ip = self.previous_ip() @@ -36,7 +37,6 @@ class DDNS: logger.info(f"Previous IP: {previous_ip}") self.current_ip = current_ip self.cname_list = self.config["CLOUDFLARE_CNAME"] - self.external_ip_path = os.path.join(TMP_PATH, "external_ip.txt") self.HEADERS = { "Content-Type": "application/json",