ddns-scripts: add ddns script to manage the providers
authorFlorian Eckert <fe@dev.tdt.de>
Thu, 17 Sep 2020 12:53:47 +0000 (14:53 +0200)
committerFlorian Eckert <fe@dev.tdt.de>
Tue, 22 Sep 2020 09:56:35 +0000 (11:56 +0200)
Redmine-patch-id: 5298
Signed-off-by: Florian Eckert <fe@dev.tdt.de>
net/ddns-scripts/Makefile
net/ddns-scripts/files/usr/bin/ddns.sh [new file with mode: 0644]

index 07c04251650d95b8e0b2958219a58f51715c3d35..8c5d450f2be48addcddd01141e3d73e9f26c0290 100644 (file)
@@ -199,6 +199,10 @@ define Package/ddns-scripts/install
                $(1)/usr/lib/ddns
        $(INSTALL_BIN) ./files/usr/lib/ddns/dynamic_dns_updater.sh \
                $(1)/usr/lib/ddns
+
+       $(INSTALL_DIR) $(1)/usr/bin
+       $(INSTALL_BIN) ./files/usr/bin/ddns.sh \
+               $(1)/usr/bin/ddns
 endef
 
 define Package/ddns-scripts/postinst
diff --git a/net/ddns-scripts/files/usr/bin/ddns.sh b/net/ddns-scripts/files/usr/bin/ddns.sh
new file mode 100644 (file)
index 0000000..a2b93b0
--- /dev/null
@@ -0,0 +1,171 @@
+#!/bin/sh
+#
+# Copyright (C) 2020 TDT AG <development@tdt.de>
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See https://www.gnu.org/licenses/gpl-2.0.txt for more information.
+#
+
+. /lib/functions.sh
+
+DDNS_PACKAGE_DIR="/usr/share/ddns"
+URL="https://raw.githubusercontent.com/openwrt/packages/master/net/ddns-scripts/files"
+
+usage() {
+       local code="$1"
+       local msg="$2"
+
+       echo "$msg"
+       echo ""
+       echo "Usage: $(basename "$0") <command> <action> <service>"
+       echo ""
+       echo "Supported ddns <command>:"
+       echo "  service:  Command for ddns service provider"
+       echo ""
+       echo "Supported ddns 'service' command <action>:"
+       echo "  update:             Update local ddns service list"
+       echo "  list-available:     List all available service providers"
+       echo "  list-installed:     List all installed service providers"
+       echo "  install <service>:  Install service provider"
+       echo "  remove <service>:   Remove service provider"
+       echo "  purge:              Remove local ddns serivces"
+
+       exit "$code"
+}
+
+action_update() {
+       local cacert
+
+       config_load ddns
+       config_get url global 'url' "${URL}${DDNS_PACKAGE_DIR}"
+       config_get cacert global 'cacert' "IGNORE"
+       url="${url}/list"
+
+       mkdir -p "${DDNS_PACKAGE_DIR}"
+
+       if [ "$cacert" = "IGNORE" ]; then
+               uclient-fetch \
+                       --no-check-certificate \
+                       "$url" \
+                       -O "${DDNS_PACKAGE_DIR}/list"
+       elif [ -f "$cacert" ]; then
+               uclient-fetch \
+                       --ca-certificate="${cacert}" \
+                       "$url" \
+                       -O "${DDNS_PACKAGE_DIR}/list"
+       elif [ -n "$cacert" ]; then
+               echo "Certification file not found ($cacert)"
+               exit 5
+       fi
+}
+
+action_list_available() {
+       if [ -f "${DDNS_PACKAGE_DIR}/list" ]; then
+               cat "${DDNS_PACKAGE_DIR}/list"
+       else
+               echo "No service file found please download first"
+               exit 3
+       fi
+}
+
+action_list_installed() {
+       if [ -d "${DDNS_PACKAGE_DIR}/services" ]; then
+               ls "${DDNS_PACKAGE_DIR}/services"
+       else
+               echo "No services installed"
+               exit 4
+       fi
+}
+
+action_install() {
+       local service="$1"
+
+       local url cacert
+
+       config_load ddns
+       config_get url global 'url' "${URL}${DDNS_PACKAGE_DIR}"
+       config_get cacert global 'cacert' "IGNORE"
+       url="${url}/services/${service}.json"
+
+       if [ -z "$service" ]; then
+               usage "4" "No service specified"
+       fi
+
+       mkdir -p "${DDNS_PACKAGE_DIR}/services"
+       if [ "$cacert" = "IGNORE" ]; then
+               uclient-fetch \
+                       --no-check-certificate \
+                       "${url}" \
+                       -O "${DDNS_PACKAGE_DIR}/services/${service}.json"
+       elif [ -f "$cacert" ]; then
+               uclient-fetch \
+                       --ca-certifcate="${cacert}" \
+                       "${url}" \
+                       -O "${DDNS_PACKAGE_DIR}/services/${service}.json"
+       elif [ -n "$cacert" ]; then
+               echo "Certification file not found ($cacert)"
+               exit 5
+       fi
+}
+
+action_remove() {
+       local service="$1"
+       if [ -z "$service" ]; then
+               usage "4" "No service specified"
+       fi
+
+       rm "${DDNS_PACKAGE_DIR}/services/${service}.json"
+}
+
+action_purge() {
+       rm -rf "${DDNS_PACKAGE_DIR}/services"
+       rm -rf "${DDNS_PACKAGE_DIR}/list"
+}
+
+sub_service() {
+       local action="$1"
+       local service="$2"
+
+       case "$action" in
+               update)
+                       action_update
+                       ;;
+               list-available)
+                       action_list_available
+                       ;;
+               list-installed)
+                       action_list_installed
+                       ;;
+               purge)
+                       action_purge
+                       ;;
+               install)
+                       action_install "$service"
+                       ;;
+               remove)
+                       action_remove "$service"
+                       ;;
+               *)
+                       usage "2" "Action not supported"
+                       ;;
+       esac
+}
+
+main() {
+       local cmd="$1"
+       local action="$2"
+       local service="$3"
+
+       [ "$#" -eq 0 ] && usage "1"
+
+       case "${cmd}" in
+               service)
+                       sub_service "${action}" "${service}"
+                       ;;
+               *)
+                       usage "1" "Command not supported"
+                       ;;
+       esac
+}
+
+main "$@"