From 637ac3d9176becb2396cdd13857c18e71458f960 Mon Sep 17 00:00:00 2001 From: chiefwigms Date: Tue, 30 Jun 2020 23:19:22 -0600 Subject: [PATCH] Add update/restart/reboot/shutdown feature (experimental), fix brew session history, add cache install hooks in firstboot.sh --- app/__init__.py | 1 + app/main/config.py | 5 +++++ app/main/routes_frontend.py | 36 ++++++++++++++++++++++++++++++++++-- app/main/session_parser.py | 4 ++-- app/templates/navbar.html | 8 ++++++++ scripts/pi/_readme.txt | 5 +++++ scripts/pi/firstboot.sh | 8 ++++++++ server.py | 2 +- 8 files changed, 64 insertions(+), 5 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 12d4361..b64a251 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -36,6 +36,7 @@ def create_app(debug=False): app.config.update( SECRET_KEY='bosco', CORS_HEADERS='Content-Type', + BASE_PATH=BASE_PATH, RECIPES_PATH=BASE_PATH.joinpath('app/recipes'), SESSIONS_PATH=BASE_PATH.joinpath('app/sessions') ) diff --git a/app/main/config.py b/app/main/config.py index e824a25..cadf3f2 100644 --- a/app/main/config.py +++ b/app/main/config.py @@ -1,6 +1,11 @@ from flask import current_app +# base path +def base_path(): + return current_app.config['BASE_PATH'] + + # recipe paths def zymatic_recipe_path(): return current_app.config['RECIPES_PATH'].joinpath('zymatic') diff --git a/app/main/routes_frontend.py b/app/main/routes_frontend.py index f3fa044..dabb8eb 100644 --- a/app/main/routes_frontend.py +++ b/app/main/routes_frontend.py @@ -1,12 +1,16 @@ import json +import os import requests +import sys import uuid -from flask import render_template, request +from flask import render_template, request, redirect +from threading import Thread +from time import sleep from . import main from .recipe_parser import PicoBrewRecipe, PicoBrewRecipeImport, ZymaticRecipe, ZymaticRecipeImport, ZSeriesRecipe from .session_parser import load_ferm_session, get_ferm_graph_data, get_brew_graph_data, load_brew_session, active_brew_sessions, active_ferm_sessions -from .config import zymatic_recipe_path, zseries_recipe_path, pico_recipe_path, ferm_archive_sessions_path, brew_archive_sessions_path +from .config import base_path, zymatic_recipe_path, zseries_recipe_path, pico_recipe_path, ferm_archive_sessions_path, brew_archive_sessions_path # -------- Routes -------- @@ -16,6 +20,34 @@ def index(): ferm_sessions=load_active_ferm_sessions()) +@main.route('/restart_server') +def restart_server(): + # git pull & install any updated requirements + os.system('cd {0};git pull;pip3 install -r requirements.txt'.format(base_path())) + # TODO: Close file handles for open sessions? + + def restart(): + sleep(2) + os.execl(sys.executable, *([sys.executable]+sys.argv)) + thread = Thread(target=restart, daemon=True) + thread.start() + return redirect('/') + + +@main.route('/restart_system') +def restart_system(): + os.system('shutdown -r now') + # TODO: redirect to a page with alert of restart + return redirect('/') + + +@main.route('/shutdown_system') +def shutdown_system(): + os.system('shutdown -h now') + # TODO: redirect to a page with alert of shutdown + return redirect('/') + + @main.route('/brew_history') def brew_history(): return render_template('brew_history.html', sessions=load_brew_sessions()) diff --git a/app/main/session_parser.py b/app/main/session_parser.py index faf4240..0152089 100644 --- a/app/main/session_parser.py +++ b/app/main/session_parser.py @@ -42,8 +42,8 @@ def load_brew_session(file): 'data': json_data, 'graph': get_brew_graph_data(chart_id, name, step, json_data) } - if len(json_data) > 0: - session['recovery'] = json_data[-1]['recovery'] + if len(json_data) > 0 and 'recovery' in json_data[-1]: + session.update({'recovery': json_data[-1]['recovery']}) return (session) diff --git a/app/templates/navbar.html b/app/templates/navbar.html index ee3cac0..4fa280b 100644 --- a/app/templates/navbar.html +++ b/app/templates/navbar.html @@ -26,5 +26,13 @@ + \ No newline at end of file diff --git a/scripts/pi/_readme.txt b/scripts/pi/_readme.txt index 0b847e6..8b14a44 100644 --- a/scripts/pi/_readme.txt +++ b/scripts/pi/_readme.txt @@ -3,6 +3,11 @@ Download lastest Lite image from https://www.raspberrypi.org/downloads/raspberry Download firstboot.service from https://github.com/nmcclain/raspberian-firstboot (Remove ExecStartPost from firstboot.service - script will rename itself) Extract img from zip +Optional: + For offline-ish install (vanilla image on sd card): + apt-get -y install --print-uris | cut -d\' -f2 | grep http:// > debs2dl + wget -i debs2dl + dpkg -i *.deb mkdir mnt boot losetup -P /dev/loop0 diff --git a/scripts/pi/firstboot.sh b/scripts/pi/firstboot.sh index 4a15d88..1fa6a83 100644 --- a/scripts/pi/firstboot.sh +++ b/scripts/pi/firstboot.sh @@ -55,6 +55,10 @@ apt -y update #apt -y upgrade apt -y --autoremove purge ifupdown dhcpcd5 isc-dhcp-client isc-dhcp-common rsyslog avahi-daemon apt-mark hold ifupdown dhcpcd5 isc-dhcp-client isc-dhcp-common rsyslog raspberrypi-net-mods openresolv avahi-daemon libnss-mdns +if [ -d "/boot/offline/deb" ]; then + # Speed up install with pre-packaged debs + dpkg -i /boot/offline/deb/*.deb +fi apt -y install libnss-resolve hostapd dnsmasq dnsutils samba git python3 python3-pip nginx openssh-server # Install Picobrew server @@ -63,6 +67,10 @@ cd / git clone https://github.com/chiefwigms/picobrew_pico.git cd /picobrew_pico git update-index --assume-unchanged config.yaml +if [ -d "/boot/offline/pip" ]; then + # Speed up install with pre-packaged wheels + pip3 install /boot/offline/pip/*.whl +fi pip3 install -r requirements.txt cd / diff --git a/server.py b/server.py index 6d3181e..be1aee0 100644 --- a/server.py +++ b/server.py @@ -16,4 +16,4 @@ app = create_app(debug=True) if __name__ == '__main__': - socketio.run(app, host=HOST, port=PORT) + socketio.run(app, host=HOST, port=PORT, use_reloader=False)