Selaa lähdekoodia

add ffho-autoupdater
based on gluon-autoupdater, but has the ability to replace pattern in the url
%v - OpenWRT-Version
%n - The Target
%S - same as %n
%GS - site_code
%GV - gluon-version
%GR - release

Karsten Böddeker 8 vuotta sitten
vanhempi
commit
72fcb3a0fb

+ 41 - 0
ffho/ffho-autoupdater/Makefile

@@ -0,0 +1,41 @@
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=ffho-autoupdater
+PKG_VERSION:=1
+PKG_RELEASE:=$(GLUON_VERSION).$(GLUON_SITE_CODE)-$(GLUON_RELEASE).$(GLUON_CONFIG_VERSION)
+
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
+PKG_BUILD_DEPENDS := respondd
+
+include $(GLUONDIR)/include/package.mk
+
+define Package/ffho-autoupdater
+  SECTION:=ffho
+  CATEGORY:=Gluon
+  DEPENDS:=+gluon-core +libgluonutil +micrond +autoupdater
+  TITLE:=Automatically update firmware
+endef
+
+define Build/Prepare
+	mkdir -p $(PKG_BUILD_DIR)
+	$(CP) ./src/* $(PKG_BUILD_DIR)/
+endef
+
+define Package/ffho-autoupdater/install
+	$(CP) ./files/* $(1)/
+
+	$(INSTALL_DIR) $(1)/lib/gluon/respondd
+	$(CP) $(PKG_BUILD_DIR)/respondd.so $(1)/lib/gluon/respondd/autoupdater.so
+
+	if [ '$(GLUON_BRANCH)' ]; then \
+		$(INSTALL_DIR) $(1)/lib/gluon/autoupdater; \
+		echo '$(GLUON_BRANCH)' > $(1)/lib/gluon/autoupdater/default_branch; \
+	fi
+endef
+
+define Package/ffho-autoupdater/postinst
+#!/bin/sh
+$(call GluonCheckSite,check_site.lua)
+endef
+
+$(eval $(call BuildPackage,ffho-autoupdater))

+ 14 - 0
ffho/ffho-autoupdater/check_site.lua

@@ -0,0 +1,14 @@
+need_string 'autoupdater.branch'
+
+local function check_branch(k, _)
+   assert_uci_name(k)
+
+   local prefix = string.format('autoupdater.branches[%q].', k)
+
+   need_string(prefix .. 'name')
+   need_string_array(prefix .. 'mirrors')
+   need_number(prefix .. 'good_signatures')
+   need_string_array(prefix .. 'pubkeys')
+end
+
+need_table('autoupdater.branches', check_branch)

+ 16 - 0
ffho/ffho-autoupdater/files/lib/gluon/autoupdater/lib.sh

@@ -0,0 +1,16 @@
+# Library to be sourced by download.d/abort.d scripts
+
+
+stop() {
+        if [ -x /etc/init.d/$1 ]; then
+                echo "Stopping $1..."
+                /etc/init.d/$1 stop
+        fi
+}
+
+start_enabled() {
+        if [ -x /etc/init.d/$1 ] && /etc/init.d/$1 enabled; then
+                echo "Starting $1..."
+                /etc/init.d/$1 start
+        fi
+}

+ 79 - 0
ffho/ffho-autoupdater/files/lib/gluon/upgrade/500-autoupdater

@@ -0,0 +1,79 @@
+#!/usr/bin/lua
+
+local fs = require 'nixio.fs'
+local site = require 'gluon.site_config'
+local uci = require 'luci.model.uci'
+local util = require 'luci.util'
+
+local c = uci.cursor()
+
+
+local subst = {}
+
+subst['%%v'] = util.trim(fs.readfile('/etc/openwrt_version'))
+subst['%%n'], subst['%%S'] = util.exec('. /etc/openwrt_release; echo $DISTRIB_CODENAME; echo $DISTRIB_TARGET'):match('([^\n]*)\n([^\n]*)')
+subst['%%GS'] = site.site_code
+subst['%%GV'] = util.trim(fs.readfile('/lib/gluon/gluon-version'))
+subst['%%GR'] = util.trim(fs.readfile('/lib/gluon/release'))
+
+function replace_patterns(url)
+	for k, v in pairs(subst) do
+		url = url:gsub(k, v)
+	end
+
+	return url
+end
+
+for name, config in pairs(site.autoupdater.branches) do
+	c:delete('autoupdater', name)
+	mirrors = {}
+	for i, v in ipairs(config.mirrors) do
+		mirrors[i]=replace_patterns(v)
+	end
+	c:section('autoupdater', 'branch', name,
+		  {
+			  name = config.name,
+			  mirror = mirrors,
+			  good_signatures = config.good_signatures,
+			  pubkey = config.pubkeys,
+		  }
+	)
+end
+
+if not c:get('autoupdater', 'settings') then
+	local enabled = 0
+	local branch = site.autoupdater.branch
+
+	local f = io.open('/lib/gluon/autoupdater/default_branch')
+	if f then
+		enabled = 1
+		branch = f:read('*line')
+		f:close()
+	end
+
+	c:section('autoupdater', 'autoupdater', 'settings',
+		  {
+			  enabled = enabled,
+			  branch = branch,
+		  }
+	)
+end
+
+c:set('autoupdater', 'settings', 'version_file', '/lib/gluon/release')
+
+c:save('autoupdater')
+c:commit('autoupdater')
+
+
+local autoupdater_util = require 'autoupdater.util'
+autoupdater_util.randomseed()
+
+
+-- Perform updates at a random time between 04:00 and 05:00, and once an hour
+-- a fallback update (used after the regular updates haven't
+local minute = math.random(0, 59)
+
+local f = io.open('/usr/lib/micron.d/autoupdater', 'w')
+f:write(string.format('%i 4 * * * /usr/sbin/autoupdater\n', minute))
+f:write(string.format('%i 0-3,5-23 * * * /usr/sbin/autoupdater --fallback\n', minute))
+f:close()

+ 9 - 0
ffho/ffho-autoupdater/files/usr/lib/autoupdater/abort.d/90gluon-autoupdater

@@ -0,0 +1,9 @@
+#!/bin/sh
+
+. /lib/gluon/autoupdater/lib.sh
+
+
+start_enabled cron
+start_enabled haveged
+start_enabled micrond
+start_enabled sysntpd

+ 9 - 0
ffho/ffho-autoupdater/files/usr/lib/autoupdater/download.d/10gluon-autoupdater

@@ -0,0 +1,9 @@
+#!/bin/sh
+
+. /lib/gluon/autoupdater/lib.sh
+
+
+stop cron
+stop haveged
+stop micrond
+stop sysntpd

+ 6 - 0
ffho/ffho-autoupdater/src/Makefile

@@ -0,0 +1,6 @@
+all: respondd.so
+
+CFLAGS += -Wall
+
+respondd.so: respondd.c
+	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -o $@ $^ $(LDLIBS) -lgluonutil -luci

+ 79 - 0
ffho/ffho-autoupdater/src/respondd.c

@@ -0,0 +1,79 @@
+/*
+  Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
+  All rights reserved.
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions are met:
+
+    1. Redistributions of source code must retain the above copyright notice,
+       this list of conditions and the following disclaimer.
+    2. Redistributions in binary form must reproduce the above copyright notice,
+       this list of conditions and the following disclaimer in the documentation
+       and/or other materials provided with the distribution.
+
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#include <respondd.h>
+
+#include <json-c/json.h>
+#include <libgluonutil.h>
+
+#include <uci.h>
+
+#include <string.h>
+
+
+static struct json_object * get_autoupdater(void) {
+	struct uci_context *ctx = uci_alloc_context();
+	ctx->flags &= ~UCI_FLAG_STRICT;
+
+	struct uci_package *p;
+	if (uci_load(ctx, "autoupdater", &p))
+		goto error;
+
+	struct uci_section *s = uci_lookup_section(ctx, p, "settings");
+	if (!s)
+		goto error;
+
+	struct json_object *ret = json_object_new_object();
+
+	json_object_object_add(ret, "branch", gluonutil_wrap_string(uci_lookup_option_string(ctx, s, "branch")));
+
+	const char *enabled = uci_lookup_option_string(ctx, s, "enabled");
+	json_object_object_add(ret, "enabled", json_object_new_boolean(enabled && !strcmp(enabled, "1")));
+
+	uci_free_context(ctx);
+
+	return ret;
+
+ error:
+	uci_free_context(ctx);
+	return NULL;
+}
+
+static struct json_object * respondd_provider_nodeinfo(void) {
+	struct json_object *ret = json_object_new_object();
+
+	struct json_object *software = json_object_new_object();
+	json_object_object_add(software, "autoupdater", get_autoupdater());
+	json_object_object_add(ret, "software", software);
+
+	return ret;
+}
+
+
+const struct respondd_provider_info respondd_providers[] = {
+	{"nodeinfo", respondd_provider_nodeinfo},
+	{}
+};