Last active
December 16, 2016 17:02
-
-
Save nosix/44ef14e1eaf26da9c12344a934905954 to your computer and use it in GitHub Desktop.
bash command to deploy and run python on remote Raspberry Pi.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CMD=`basename $0` | |
usage() { | |
echo "Usage: $CMD [-r script] [-d dst] [-s src]" 1>&2 | |
} | |
COMMAND= | |
DST= | |
SRC= | |
# Process options | |
while getopts r:d:s: OPT | |
do | |
case $OPT in | |
"r" ) SCRIPT=$OPTARG ;; | |
"d" ) DST=$OPTARG ;; | |
"s" ) SRC=$OPTARG ;; | |
* ) usage; exit 1 ;; | |
esac | |
done | |
# Drop options | |
shift `expr $OPTIND - 1` | |
# Check arguments | |
if [ -z "$SCRIPT" ] && [ -z "$SRC" ] ; then | |
usage; exit 1 | |
fi | |
: ${PI_USER:='pi'} | |
: ${PI_HOST:='raspberrypi.local'} | |
: ${PI_HOME:='~/pyhome'} | |
: ${PI_ENV:='~/pyenv/python3'} | |
if [[ "$DST" =~ ^/ ]] || [ -z "$DST" ] ; then | |
dst="$DST" | |
else | |
dst="/$DST" | |
fi | |
SCP_RESULT=0 | |
if [ -n "$SRC" ]; then | |
scp -r $SRC $PI_USER@$PI_HOST:$PI_HOME$dst | |
SCP_RESULT=$? | |
fi | |
if [ $SCP_RESULT -eq 0 ] && [ -n "$SCRIPT" ] ; then | |
ssh -t -t $PI_USER@$PI_HOST \ | |
"source $PI_ENV/bin/activate; cd $PI_HOME; python $SCRIPT" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment