Skip to content

Commit

Permalink
Add update/restart/reboot/shutdown feature (experimental), fix brew s…
Browse files Browse the repository at this point in the history
…ession history, add cache install hooks in firstboot.sh
  • Loading branch information
chiefwigms committed Jul 1, 2020
1 parent bf27f48 commit 637ac3d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
Expand Down
5 changes: 5 additions & 0 deletions app/main/config.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
36 changes: 34 additions & 2 deletions app/main/routes_frontend.py
Original file line number Diff line number Diff line change
@@ -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 --------
Expand All @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions app/main/session_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
8 changes: 8 additions & 0 deletions app/templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@
</li>
<li class="nav-item"><a class="nav-link" href="/brew_history">Brew History</a></li>
<li class="nav-item"><a class="nav-link" href="/ferm_history">Ferm History</a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">System</a>
<div class="dropdown-menu dropdown-menu-right navbar-dark bg-dark text-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item nav-link bg-dark" href="/restart_server">Update/Restart Server</a>
<a class="dropdown-item nav-link bg-dark" href="/restart_system">Restart System (Pi)</a>
<a class="dropdown-item nav-link bg-dark" href="/shutdown_system">Shutdown System (Pi)</a>
</div>
</li>
</ul>
</nav>
5 changes: 5 additions & 0 deletions scripts/pi/_readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <packages> | cut -d\' -f2 | grep http:// > debs2dl
wget -i debs2dl
dpkg -i *.deb

mkdir mnt boot
losetup -P /dev/loop0 <image>
Expand Down
8 changes: 8 additions & 0 deletions scripts/pi/firstboot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 /

Expand Down
2 changes: 1 addition & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 637ac3d

Please sign in to comment.