Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions utils/auto-timezone/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=auto-timezone
PKG_VERSION:=1.0.0
PKG_RELEASE:=1

PKG_MAINTAINER:=Renn Akaza <openwrt.org-auto-timezone@akaza.ren>
PKG_LICENSE:=GPL-3.0-or-later

include $(INCLUDE_DIR)/package.mk

define Package/auto-timezone
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Auto set timezone by IP
PKGARCH:=all
DEPENDS:=+zoneinfo-all
endef

define Package/auto-timezone/description
Hotplug script to automatically configure the timezone UCI settings based on the IP address.

The user needs to set their own endpoint to get the IANA timezone string, e.g.:
uci set auto-timezone.config.endpoint="https://domain.tld/json"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be made more clear?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the description on the expected response from the endpoint in 1dbe849. Please let me know if you expect something else.

uci commit
endef

define Build/Compile
endef

define Package/auto-timezone/install
$(INSTALL_DIR) $(1)/etc/hotplug.d/iface/
$(INSTALL_DATA) ./files/99-auto-timezone $(1)/etc/hotplug.d/iface/
$(INSTALL_DIR) $(1)/etc/config
$(INSTALL_CONF) ./files/auto-timezone.config $(1)/etc/config/auto-timezone
endef

$(eval $(call BuildPackage,auto-timezone))
45 changes: 45 additions & 0 deletions utils/auto-timezone/files/99-auto-timezone
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh

if [ "${INTERFACE}" = "loopback" ]; then
exit 0
fi
if [ "${ACTION}" != "ifup" ] && [ "${ACTION}" != "ifupdate" ]; then
exit 0
fi
if [ "${ACTION}" = "ifupdate" ] && [ -z "${IFUPDATE_ADDRESSES}" ] && [ -z "${IFUPDATE_DATA}" ]; then
exit 0
fi

log() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would pass priority as well.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added two functions log_info and log_error to handle the priorities in 1dbe849.

logger -t "auto-timezone" "$*"
}

endpoint="$(uci -q get auto-timezone.config.endpoint)"
if [ $? -ne 0 ]; then
log "Endpoint not set"
exit 1
fi

for i in {1..5}; do
timezone_iana="$(wget --timeout=5 -qO- "${endpoint}")" && break || sleep 1
done

if [ -z "${timezone_iana}" ]; then
log "Failed to get timezone"
exit 1
fi

timezone_posix="$(tail -n1 /usr/share/zoneinfo/${timezone_iana})"
Comment thread
pprindeville marked this conversation as resolved.
Outdated
if [ $? -ne 0 ]; then
log "Invalid timezone"
exit 1
fi

if [ "${timezone_iana}" != "$(uci get system.@system[0].zonename)" ] || [ "${timezone_posix}" != "$(uci get system.@system[0].timezone)" ]; then
log "Updating timezone, IANA: ${timezone_iana}, POSIX: ${timezone_posix}"
Comment thread
pprindeville marked this conversation as resolved.
Outdated
uci set system.@system[0].zonename="${timezone_iana}"
uci set system.@system[0].timezone="${timezone_posix}"
uci commit system
/etc/init.d/system reload
/etc/init.d/cron restart
fi
1 change: 1 addition & 0 deletions utils/auto-timezone/files/auto-timezone.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config auto-timezone 'config'
Loading