summaryrefslogtreecommitdiff
path: root/library
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2023-02-07 00:33:42 +0100
committerChristian Pointner <equinox@spreadspace.org>2023-02-07 00:33:42 +0100
commit79ed9e66603642c9b496f2efd4bc52c42383f46d (patch)
treec3cdf070924324083f0d56cb36f6483471f12ebe /library
parentadd dummy setup module for openwrt (diff)
add vastly improved openwrt_setup module
Diffstat (limited to 'library')
-rw-r--r--library/openwrt_setup.sh67
1 files changed, 66 insertions, 1 deletions
diff --git a/library/openwrt_setup.sh b/library/openwrt_setup.sh
index 6f3730f8..a23a2c37 100644
--- a/library/openwrt_setup.sh
+++ b/library/openwrt_setup.sh
@@ -1,5 +1,70 @@
#!/bin/sh
+## This is based on: https://github.com/gekmihesg/ansible-openwrt/blob/master/library/openwrt_setup.sh
-echo '{}'
+
+. /usr/share/libubox/jshn.sh
+
+
+add_ubus_fact() {
+ set -- ${1//!/ }
+ ubus list "$2" > /dev/null 2>&1 || return
+ local json="$($ubus call "$2" "$3" 2>/dev/null)"
+ echo -n "$seperator\"$1\":$json"
+ seperator=","
+}
+
+main() {
+ ubus="/bin/ubus"
+ seperator=","
+ echo '{"changed":false,"ansible_facts":'
+ dist="OpenWRT"
+ dist_version="NA"
+ dist_release="NA"
+ test -f /etc/openwrt_release && {
+ . /etc/openwrt_release
+ dist="${DISTRIB_ID:-$dist}"
+ dist_version="${DISTRIB_RELEASE:-$dist_version}"
+ dist_release="${DISTRIB_CODENAME:-$dist_release}"
+ } || test ! -f /etc/os-release || {
+ . /etc/os-release
+ dist="${NAME:-$dist}"
+ dist_version="${VERSION_ID:-$dist_version}"
+ }
+ dist_major="${dist_version%%.*}"
+ json_set_namespace facts
+ json_init
+ json_add_string ansible_hostname "$(cat /proc/sys/kernel/hostname)"
+ json_add_string ansible_distribution "$dist"
+ json_add_string ansible_distribution_major_version "$dist_major"
+ json_add_string ansible_distribution_release "$dist_release"
+ json_add_string ansible_distribution_version "$dist_version"
+ json_add_string ansible_os_family OpenWRT
+ json_add_boolean ansible_is_chroot "$([ -r /proc/1/root/. ] &&
+ { [ / -ef /proc/1/root/. ]; echo $?; } ||
+ { [ "$(ls -di / | awk '{print $1}')" -eq 2 ]; echo $?; }
+ )"
+ dist_facts="$(json_dump)"
+ json_cleanup
+ json_set_namespace result
+ echo "${dist_facts%\}*}"
+ for fact in \
+ info!system!info \
+ devices!network.device!status \
+ services!service!list \
+ board!system!board \
+ wireless!network.wireless!status \
+ ; do
+ add_ubus_fact "openwrt_$fact"
+ done
+ echo "$seperator"'"openwrt_interfaces":{'
+ seperator=""
+ for net in $($ubus list); do
+ [ "${net#network.interface.}" = "$net" ] ||
+ add_ubus_fact "${net##*.}!$net!status"
+ done
+ echo '}}}'
+}
+
+main
exit 0