From c652d5647ca0648775aed9ba9c7b97d337db5796 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 19 Dec 2018 22:55:23 +0100 Subject: [PATCH] Added platyvenv script to manage platypush instances from python virtual environments --- bin/platyvenv | 236 ++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 237 insertions(+) create mode 100755 bin/platyvenv diff --git a/bin/platyvenv b/bin/platyvenv new file mode 100755 index 000000000..14fea0c13 --- /dev/null +++ b/bin/platyvenv @@ -0,0 +1,236 @@ +#!/bin/bash + +############################################################################## +# This script allows you to easily manage Platypush instances through Python # +# virtual environment. You can build environments from a config.yaml file # +# and automatically managed the required dependencies, as well as start, # +# stop and remove them # +# # +# @author: Fabio Manganiello # +# @licence: MIT # +############################################################################## + + +workdir=$HOME/.local/share/platypush/venv + +function build { + cfgfile= + + while getopts ':c:' opt; do + case $opt in + c) + cfgfile=$OPTARG;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1;; + :) + echo "Option -$OPTARG requires the path to a Platypush configuration file" >&2 + exit 1;; + esac + done + + if [ -z "$cfgfile" ]; then + echo "Usage: $0 build -c " >&2 + exit 1 + fi + + echo "Parsing configuration file" + deps=() + + while read -r line; do + echo $line | egrep '``pip install .+?``' > /dev/null 2>&1 + if (( $? != 0 )); then + continue + fi + + dep=$(echo $line | sed -r -e 's/.*``pip install (.+?)``.*/\1/') + deps+=("$dep") + done <<< $(python <" >&2 + exit 1 + fi + + env=$1 + envdir=$workdir/$env + logsdir=$envdir/var/log/platypush + rundir=$envdir/var/run + pidfile=$rundir/platypush.pid + cfgfile=$envdir/etc/platypush/config.yaml + + if [ ! -d "$envdir" ]; then + echo "No such directory: $envdir" >&2 + exit 1 + fi + + mkdir -p $logsdir + mkdir -p $rundir + + if [[ -f "$pidfile" ]]; then + pid=`cat "$pidfile"` + if ps -p $pid | grep platypush; then + echo "Another instance (PID $pid) is running, please stop that instance first" + exit 1 + fi + + echo "A PID file was found but the process doesn't seem to be running, starting anyway" + rm -f "$pidfile" + fi + + python3 -m venv $envdir + cd $envdir + source bin/activate + bin/platypush -c "$cfgfile" -P "$pidfile" > $logsdir/stdout.log 2> $logsdir/stderr.log & + start_time=`date +'%s'` + timeout=30 + + while :; do + [[ -f "$pidfile" ]] && break + now=`date +'%s'` + let elapsed=$now-$start_time + if (( $elapsed >= $timeout )); then + echo "Platypush instance '$env' didn't start within $timeout seconds" >&2 + exit 1 + fi + + echo -n '.' + sleep 1 + done + + pid=`cat "$pidfile"` + echo + echo "Platypush environment $env started with PID $pid, logs dir: $logsdir" +} + +function stop { + if [ -z "$1" ]; then + echo "Usage: $0 stop " >&2 + exit 1 + fi + + env=$1 + envdir=$workdir/$env + rundir=$envdir/var/run + pidfile=$rundir/platypush.pid + + if [ ! -d "$envdir" ]; then + echo "No such directory: $envdir" >&2 + exit 1 + fi + + if [[ ! -f "$pidfile" ]]; then + echo "No pidfile found for instance "$env"" + exit 1 + fi + + pid=`cat "$pidfile"` + pids="$pid `ps --no-headers -o pid= --ppid $pid`" + kill -9 $pids + rm -f "$pidfile" + echo "Instance '$env' with PID $pid stopped" +} + +function rme { + if [ -z "$1" ]; then + echo "Usage: $0 rm " >&2 + exit 1 + fi + + envdir=$workdir/$1 + rundir=$envdir/var/run + pidfile=$rundir/platypush.pid + + if [ ! -d "$envdir" ]; then + echo "No such directory: $envdir" >&2 + exit 1 + fi + + if [[ -f "$pidfile" ]]; then + if ps -p `cat "$pidfile"` | grep platypush; then + echo "Another instance (PID $pidfile) is running, please stop that instance first" + exit 1 + fi + + echo "A PID file was found but the process doesn't seem to be running, removing anyway" + fi + + rm -rf "$envdir" + echo "$envdir removed" +} + +function usage { + echo "Usage: $0 [options]" >&2 + exit 1 +} + +if (( $# < 1 )); then + usage +fi + +action=$1 +shift +mkdir -p $workdir + +case $action in + 'build') build $*;; + 'start') start $*;; + 'stop') stop $*;; + 'rm') rme $*;; + *) usage;; +esac + diff --git a/setup.py b/setup.py index 1bde55be5..5053cced8 100755 --- a/setup.py +++ b/setup.py @@ -49,6 +49,7 @@ setup( 'platydock=platypush.platydock:main', ], }, + scripts = ['bin/platyvenv'], # data_files = [ # ('/etc/platypush', ['platypush/config.example.yaml']) # ],