From a73b888e1da048a2ac47f6c265c5cf31ef92fe94 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Sat, 30 Jan 2021 20:33:19 +0100 Subject: [PATCH] Initial version of the automatic addon updater --- addons_updater/Dockerfile | 16 ++++++ addons_updater/README.md | 42 ++++++++++++++ addons_updater/build.json | 8 +++ addons_updater/config.json | 31 ++++++++++ addons_updater/rootfs/etc/cont-init.d/run | 70 +++++++++++++++++++++++ 5 files changed, 167 insertions(+) create mode 100644 addons_updater/Dockerfile create mode 100644 addons_updater/README.md create mode 100644 addons_updater/build.json create mode 100644 addons_updater/config.json create mode 100644 addons_updater/rootfs/etc/cont-init.d/run diff --git a/addons_updater/Dockerfile b/addons_updater/Dockerfile new file mode 100644 index 000000000..7c4dfd7a5 --- /dev/null +++ b/addons_updater/Dockerfile @@ -0,0 +1,16 @@ +ARG BUILD_FROM +FROM $BUILD_FROM + +VOLUME [ "/data" ] + +#Install last version https://github.com/dvershinin/lastversion +RUN \ + apk add --no-cache git \ + && pip install --upgrade pip \ + && pip install lastversion + +# Copy root filesystem +COPY rootfs / + +# Set shell +SHELL ["/bin/bash", "-o", "pipefail", "-c"] diff --git a/addons_updater/README.md b/addons_updater/README.md new file mode 100644 index 000000000..f108076c8 --- /dev/null +++ b/addons_updater/README.md @@ -0,0 +1,42 @@ +# Home assistant add-on: addon updated +![Supports aarch64 Architecture][aarch64-shield] ![Supports amd64 Architecture][amd64-shield] ![Supports armhf Architecture][armhf-shield] ![Supports armv7 Architecture][armv7-shield] + +## About + +This script allows to automatically update addons based on upstream new releases + +## Installation + +The installation of this add-on is pretty straightforward and not different in +comparison to installing any other Hass.io add-on. + +1. [Add my Hass.io add-ons repository][repository] to your Hass.io instance. +1. Install this add-on. +1. Click the `Save` button to store your configuration. +1. Make sure that the two ports are open on your router +1. Start the add-on. +1. Check the logs of the add-on to see if everything went well. +1. Carefully configure the add-on to your preferences, see the official documentation for for that. + +## Configuration + +No webUI. Set everything through configuration. + +''' + - slug: the slug name from your repo + current: the initial version of your addon + beta: true/false ; should it look only for releases or prereleases ok + repository: 'name/repo' coming from github + upstream: name/repo, example is 'linuxserver/docker-emby' + - gituser: your github username + - gituser: your github email + - gitpass: add your github password here, or a specific key if you have two factor identification enabled + - frequency: placeholder for automatic update. Not used. +''' + + +[repository]: https://github.com/alexbelgium/hassio-addons +[aarch64-shield]: https://img.shields.io/badge/aarch64-yes-green.svg +[amd64-shield]: https://img.shields.io/badge/amd64-yes-green.svg +[armhf-shield]: https://img.shields.io/badge/armhf-yes-green.svg +[armv7-shield]: https://img.shields.io/badge/armv7-yes-green.svg \ No newline at end of file diff --git a/addons_updater/build.json b/addons_updater/build.json new file mode 100644 index 000000000..5e7e47c86 --- /dev/null +++ b/addons_updater/build.json @@ -0,0 +1,8 @@ +{ + "build_from": { + "aarch64": "homeassistant/aarch64-base-python:3.9-alpine3.13", + "armhf": "homeassistant/armhf-base-python:3.9-alpine3.13", + "armv7": "homeassistant/armv7-base-python:3.9-alpine3.13", + "amd64": "homeassistant/amd64-base-python:3.9-alpine3.13" + } +} diff --git a/addons_updater/config.json b/addons_updater/config.json new file mode 100644 index 000000000..3bb923e88 --- /dev/null +++ b/addons_updater/config.json @@ -0,0 +1,31 @@ +{ + "name": "Addons_Updater", + "version": "1.0", + "slug": "updater", + "description": "Automatic addons update upon upstream new release", + "url": "https://github.com/alexbelgium/hassio-addons/tree/master/updater", + "arch": [ + "aarch64", + "amd64", + "armv7", + "armhf" + ], + "ports": { + }, + "ports_description": { + }, + "environment": { + }, + "options": { + "addon": [{ "slug": "addon slug", "current": "1.1.1.1", "beta": false, "repository": "https://github.com/user/repo", "upstream": "github_user/repo_name" }], + "gituser": "gituser", + "gitpass": "gitpassword", + "frequency": 7 + }, + "schema": { + "addon": [{ "slug": "str", "current": "str", "beta": "bool" , "repository": "str", "upstream": "str"}], + "gituser": "str", + "gitpass": "str", + "frequency": "int" + } +} diff --git a/addons_updater/rootfs/etc/cont-init.d/run b/addons_updater/rootfs/etc/cont-init.d/run new file mode 100644 index 000000000..f811fda09 --- /dev/null +++ b/addons_updater/rootfs/etc/cont-init.d/run @@ -0,0 +1,70 @@ +#!/usr/bin/env bashio + +bashio::log.info "Checking status of referenced repositoriess..." + +#Defining github values +bashio::log.info "... github authentification" +GITUSER=$(bashio::config 'gituser') +GITPASS=$(bashio::config 'gitpass') +GITMAIL=$(bashio::config 'gitemail') +git config --system http.sslVerify false +git config --global credential.helper 'cache --timeout 7200' +git config --global user.name ${GITUSER} +git config --global user.password ${GITPASS} +git config --global user.email ${GITMAIL} + +bashio::log.info "... parse addons" +for addons in $(bashio::config "addon|keys"); do + SLUG=$(bashio::config "addon[${addons}].slug") + REPOSITORY=$(bashio::config "addon[${addons}].repository") + UPSTREAM=$(bashio::config "addon[${addons}].upstream") + CURRENT=$(bashio::config "addon[${addons}].current") + BETA=$(bashio::config "addon[${addons}].beta") + BASENAME=$(basename $REPOSITORY) + bashio::log.info "... $SLUG : check started" + + git remote set-url origin https://${GITUSER}:${GITPASS}@github.com/${REPOSITORY} + + #If beta flag, select beta version + if [ ${BETA} = true ]; then + bashio::log.info "... $SLUG : beta is on" + LASTVERSION=$(lastversion --pre $UPSTREAM) + else + bashio::log.info "... $SLUG : beta is off" + LASTVERSION=$(lastversion $UPSTREAM) + fi + + if [ ${CURRENT} != ${LASTVERSION} ]; then + bashio::log.info "... $SLUG : update from $CURRENT to $LASTVERSION" + + #Update local version + bashio::log.info "... $SLUG : cloning base repo" + cd / + git clone https://github.com/${REPOSITORY} || cd /${BASENAME} && git fetch --all && git reset --hard origin/master + + #Define the folder addon + bashio::log.info "... $SLUG : checking exists in repo" + cd /${BASENAME}/${SLUG} || bashio::log.error "$SLUG addon not found in this repository. Exiting." exit + + #Change all instances of version + bashio::log.info "... $SLUG : updating files" + + files=$(grep -rl '"'${CURRENT}'"' /hassio-addons/emby) && echo $files | xargs sed -i 's/"'${CURRENT}'"/"'${LASTVERSION}'"/g' + git commit -m "Bot update" $files + + #Git commit and push + bashio::log.info "... $SLUG : push to master" + git remote set-url origin https://${GITUSER}:${GITPASS}@github.com/${REPOSITORY} + git push + + #Update the current flag + bashio::log.info "... $SLUG : updating current flag" + sed -i "s/${CURRENT}/${LASTVERSION}/g" /data/options.json + + #Log + bashio::log.info "... $SLUG : updated and published" + + else + bashio::log.info "Addon $SLUG is already up-to-date." + fi +done \ No newline at end of file