Repository : ssh://git@open-mesh.org/openwrt-feed-devel
On branch : master
commit 6667bea3a5bc1614d47351eb5b2a045a4ace9b73 Author: Marek Lindner mareklindner@neomailbox.ch Date: Thu May 5 22:34:55 2016 +0800
alfred-devel: initial commit
Signed-off-by: Marek Lindner mareklindner@neomailbox.ch
6667bea3a5bc1614d47351eb5b2a045a4ace9b73 alfred-devel/Config.in | 22 ++++++++ alfred-devel/Makefile | 70 +++++++++++++++++++++++ alfred-devel/files/alfred.config | 8 +++ alfred-devel/files/alfred.init | 118 +++++++++++++++++++++++++++++++++++++++ alfred-devel/files/bat-hosts.lua | 107 +++++++++++++++++++++++++++++++++++ 5 files changed, 325 insertions(+)
diff --git a/alfred-devel/Config.in b/alfred-devel/Config.in new file mode 100644 index 0000000..98c409b --- /dev/null +++ b/alfred-devel/Config.in @@ -0,0 +1,22 @@ +config ALFRED_DEVEL_NEEDS_lua + bool + +config ALFRED_DEVEL_NEEDS_libgps + bool + +config PACKAGE_ALFRED_DEVEL_VIS + bool "enable vis server for alfred" + depends on PACKAGE_alfred-devel + default y + +config PACKAGE_ALFRED_DEVEL_BATHOSTS + bool "enable autogeneration of /etc/bat-hosts" + depends on PACKAGE_alfred-devel + select ALFRED_DEVEL_NEEDS_lua + default n + +config PACKAGE_ALFRED_DEVEL_GPSD + bool "enable gpsd service for alfred" + depends on PACKAGE_alfred-devel + select ALFRED_DEVEL_NEEDS_libgps + default n diff --git a/alfred-devel/Makefile b/alfred-devel/Makefile new file mode 100644 index 0000000..df52b2a --- /dev/null +++ b/alfred-devel/Makefile @@ -0,0 +1,70 @@ +# +# Copyright (C) 2013 OpenWrt.org +# +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=alfred-devel + +PKG_BRANCH:=master +PKG_VERSION:=$(shell git ls-remote https://git@git.open-mesh.org/alfred.git $(PKG_BRANCH) | cut -c1-7) +PKG_ALFRED_FNAME:=alfred-devel-$(PKG_VERSION).tar.gz +PKG_ALFRED_DIR:=alfred-$(PKG_VERSION) + +PKG_SOURCE_URL:=https://git.open-mesh.org/snapshot/ +PKG_SOURCE:=$(PKG_ALFRED_FNAME) + +PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)/$(PKG_ALFRED_DIR) + +include $(INCLUDE_DIR)/package.mk + +init-y := alfred + +define Package/alfred-devel + SECTION:=net + CATEGORY:=Network + TITLE:=Almighty Lightweight Fact Remote Exchange Daemon + DEPENDS:= +libc +kmod-ipv6 +librt \ + +ALFRED_DEVEL_NEEDS_lua:lua \ + +ALFRED_DEVEL_NEEDS_libgps:libgps +endef + +define Package/alfred-devel/conffiles +/etc/config/alfred +endef + +define Package/alfred-devel/config + source "$(SOURCE)/Config.in" +endef + +MAKE_ALFRED_FLAGS=\ + CONFIG_ALFRED_VIS=$(if $(CONFIG_PACKAGE_ALFRED_DEVEL_VIS),y,n) \ + CONFIG_ALFRED_GPSD=$(if $(CONFIG_PACKAGE_ALFRED_DEVEL_GPSD),y,n) \ + CONFIG_ALFRED_CAPABILITIES=n + +TARGET_CFLAGS += -ffunction-sections -fdata-sections -flto +TARGET_LDFLAGS += -Wl,--gc-sections -fuse-linker-plugin + +define Build/Compile + CFLAGS="$(TARGET_CPPFLAGS) $(TARGET_CFLAGS)" \ + LDFLAGS="$(TARGET_LDFLAGS)" \ + $(MAKE) -C $(PKG_BUILD_DIR) $(MAKE_FLAGS) $(MAKE_ALFRED_FLAGS) all +endef + +define Package/alfred-devel/install + $(INSTALL_DIR) $(1)/usr/sbin + cp -fpR $(PKG_BUILD_DIR)/alfred $(1)/usr/sbin/ + [ "x$(CONFIG_PACKAGE_ALFRED_DEVEL_VIS)" == "xy" ] && cp -fpR $(PKG_BUILD_DIR)/vis/batadv-vis $(1)/usr/sbin/ ; true + [ "x$(CONFIG_PACKAGE_ALFRED_DEVEL_GPSD)" == "xy" ] && cp -fpR $(PKG_BUILD_DIR)/gpsd/alfred-gpsd $(1)/usr/sbin/ ; true + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_BIN) ./files/alfred.init $(1)/etc/init.d/alfred + $(INSTALL_DIR) $(1)/etc/config + $(INSTALL_DATA) ./files/alfred.config $(1)/etc/config/alfred + $(INSTALL_DIR) $(1)/etc/alfred + [ "x$(CONFIG_PACKAGE_ALFRED_DEVEL_BATHOSTS)" == "xy" ] && $(INSTALL_BIN) ./files/bat-hosts.lua $(1)/etc/alfred/bat-hosts.lua ; true +endef + +$(eval $(call BuildPackage,alfred-devel)) diff --git a/alfred-devel/files/alfred.config b/alfred-devel/files/alfred.config new file mode 100644 index 0000000..9d3fff6 --- /dev/null +++ b/alfred-devel/files/alfred.config @@ -0,0 +1,8 @@ +config 'alfred' 'alfred' + option interface 'br-lan' + option mode 'master' + option batmanif 'bat0' + option start_vis '1' + option run_facters '1' +# REMOVE THIS LINE TO ENABLE ALFRED + option disabled '1' diff --git a/alfred-devel/files/alfred.init b/alfred-devel/files/alfred.init new file mode 100755 index 0000000..5ce06c0 --- /dev/null +++ b/alfred-devel/files/alfred.init @@ -0,0 +1,118 @@ +#!/bin/sh /etc/rc.common + +# +# Copyright (C) 2013 OpenWrt.org +# +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +START=99 +STOP=99 +alfred_args="" +vis_args="" +facters_dir="/etc/alfred" +pid_file_alfred="/var/run/alfred.pid" +pid_file_vis="/var/run/batadv-vis.pid" +enable=0 +vis_enable=0 +SERVICE_DAEMONIZE=1 +SERVICE_WRITE_PID=1 + +wait_for_dir() +{ + local ifce="$1" dir="$2" + + if ! [ -d "$dir" ] ; then + timeout=30 + echo "${initscript}: waiting $timeout secs for $ifce interface..." + for i in $(seq $timeout); do + sleep 1 + [ -d "$dir" ] && break + if [ $i == $timeout ] ; then + echo "${initscript}: $ifce not detected, alfred not starting." + exit 1 + fi + done + fi +} + +alfred_start() +{ + local args="" + local section="$1" + local disabled interface mode + + # check if section is disabled + config_get_bool disabled "$section" disabled 0 + [ $disabled = 0 ] || return 1 + + args="" + + config_get interface "$section" interface + append args "-i $interface" + + config_get mode "$section" mode + [ "$mode" = "master" ] && append args "-m" + + config_get batmanif "$section" batmanif + append args "-b $batmanif" + + if [ "$batmanif" != "none" ]; then + wait_for_dir "$batmanif" "/sys/class/net/$batmanif/mesh" + fi + + wait_for_dir "$interface" "/sys/class/net/$interface/" + + append alfred_args "$args" + enable=1 + + config_get_bool start_vis "$section" start_vis 0 + if [ "$start_vis" = 1 ] && [ -x /usr/sbin/batadv-vis ]; then + vis_enable=1 + append vis_args "-i $batmanif -s" + fi + + config_get_bool run_facters "$section" run_facters 0 + + return 0 +} + +start() +{ + config_load "alfred" + config_foreach alfred_start alfred + + if [ "$enable" = "0" ]; then + exit 0 + fi + + echo "${initscript}: starting alfred" + SERVICE_PID_FILE="$pid_file_alfred" + service_start /usr/sbin/alfred ${alfred_args} + + if [ "$vis_enable" = "1" ]; then + echo "${initscript}: starting batadv-vis" + SERVICE_PID_FILE="$pid_file_vis" + service_start /usr/sbin/batadv-vis ${vis_args} + fi + + if [ "$run_facters" = "1" ]; then + ( for file in $facters_dir/* ; do [ -x $file ] && $file ; done ) + if ! ( grep -q "for file in $facters_dir/* ; do " /etc/crontabs/root 2>/dev/null ) ; then + echo "*/5 * * * * ( for file in $facters_dir/* ; do [ -x $file ] && $file ; done )" >> /etc/crontabs/root + /etc/init.d/cron enable + /etc/init.d/cron restart + fi + fi +} + +stop() +{ + SERVICE_PID_FILE="$pid_file_alfred" + service_stop /usr/sbin/alfred + SERVICE_PID_FILE="$pid_file_vis" + [ -x /usr/sbin/batadv-vis ] && service_stop /usr/sbin/batadv-vis + sed "|for file in $facters_dir/* ; do |d" -i /etc/crontabs/root + /etc/init.d/cron restart +} diff --git a/alfred-devel/files/bat-hosts.lua b/alfred-devel/files/bat-hosts.lua new file mode 100644 index 0000000..8648caf --- /dev/null +++ b/alfred-devel/files/bat-hosts.lua @@ -0,0 +1,107 @@ +#!/usr/bin/lua + +local type_id = 64 -- bat-hosts + +function get_hostname() + local hostfile = io.open("/proc/sys/kernel/hostname", "r") + local ret_string = hostfile:read() + hostfile:close() + return ret_string +end + +function get_interfaces_names() + local ret = {} + + for name in io.popen("ls -1 /sys/class/net/"):lines() do + table.insert(ret, name) + end + + return ret +end + +function get_interface_address(name) + local addressfile = io.open("/sys/class/net/"..name.."/address", "r") + local ret_string = addressfile:read() + addressfile:close() + return ret_string +end + + +local function generate_bat_hosts() +-- get hostname and interface macs/names +-- then return a table containing valid bat-hosts lines + local n, i + local ifaces, ret = {}, {} + + local hostname = get_hostname() + + for n, i in ipairs(get_interfaces_names()) do + local address = get_interface_address(i) + if not ifaces[address] then ifaces[address] = i end + end + + for mac, iname in pairs(ifaces) do + if mac:match("^%x%x:%x%x:%x%x:%x%x:%x%x:%x%x$") and not mac:match("00:00:00:00:00:00") then + table.insert(ret, mac.." "..hostname.."_"..iname.."\n") + end + end + + return ret +end + +local function publish_bat_hosts() +-- pass a raw chunk of data to alfred + local fd = io.popen("alfred -s " .. type_id, "w") + if fd then + local ret = generate_bat_hosts() + if ret then + fd:write(table.concat(ret)) + end + fd:close() + end +end + +local function write_bat_hosts(rows) + local content = { "### /tmp/bat-hosts generated by alfred-bat-hosts\n", + "### /!\ This file is overwritten every 5 minutes /!\\n", + "### (To keep manual changes, replace /etc/bat-hosts symlink with a static file)\n" } + + -- merge the chunks from all nodes, de-escaping newlines + for _, row in ipairs(rows) do + local node, value = unpack(row) + table.insert(content, "# Node ".. node .. "\n") + table.insert(content, value:gsub("\x0a", "\n") .. "\n") + end + + -- write parsed content down to disk + local fd = io.open("/tmp/bat-hosts", "w") + if fd then + fd:write(table.concat(content)) + fd:close() + end + + -- try to make a symlink in /etc pointing to /tmp, + -- if it exists, ln will do nothing. + os.execute("ln -ns /tmp/bat-hosts /etc/bat-hosts 2>/dev/null") +end + +local function receive_bat_hosts() +-- read raw chunks from alfred, convert them to a nested table and call write_bat_hosts + local fd = io.popen("alfred -r " .. type_id) + --[[ this command returns something like + { "54:e6:fc:b9:cb:37", "00:11:22:33:44:55 ham_wlan0\x0a00:22:33:22:33:22 ham_eth0\x0a" }, + { "90:f6:52:bb:ec:57", "00:22:33:22:33:23 spam\x0a" }, + ]]-- + + if fd then + local output = fd:read("*a") + if output then + assert(loadstring("rows = {" .. output .. "}"))() + write_bat_hosts(rows) + end + fd:close() + end +end + +publish_bat_hosts() +receive_bat_hosts()