Putting on board - Wyliodrin/wyliodrin-app-server GitHub Wiki

#!/bin/bash

###################################################################################################
# To Enable SSH:
# Make a "ssh" file on the "/boot" partition.
###################################################################################################

###################################################################################################
# Raspberry Pi install script
#
# !!! BEFORE RUNNING THIS SCRIPT !!!
# Select 7 -> A1 Expand Filesystem.
# Select 5 Interfacing Options and then P4 SPI - Enable/Disable automatic loading.
# Select 5 Interfacing Options and then P5 I2C - Enable/Disable automatic loading.
# Select 5 Interfacing Options and then P6 Serial - Disable serial shell.
# Add "dtparam=i2c_arm=on" and "dtparam=spi=on" in /boot/config.txt (already added).
# Add "i2c-dev" (already added) and "spi-dev" in /etc/modules.
#
# Tested on 2017-04-10-raspbian-jessie-lite.
#
# Author: Razvan SERBAN (original script made by Razvan Madalin MATEI <[email protected]>)
# Date last modified: July 2017
###################################################################################################



###################################################################################################
# Sanity checks
###################################################################################################

# Test whether the script is run by root or not
if [ ! "$(whoami)" = "root" ]; then
  printf 'ERROR: This script must be run as root\n' 1>&2
  exit 1
fi

# Check whether raspi-config is installed or not
dpkg -s raspi-config > /dev/null 2>&1
if [ $? -ne 0 ]; then
  printf 'ERROR: raspi-config not installed\n' 1>&2
  exit 1
fi

# Check minimum space required
MIN_SIZE=$((700 * 1024))
df_result=($(df / | tail -n 1))
if [ ${df_result[3]} -lt $MIN_SIZE ]; then
  printf 'ERROR: At least 700MB of space required\n' 1>&2
  exit 1
fi



###################################################################################################
# Script variables
###################################################################################################

SANDBOX_PATH=/sandbox
WVERSION=v3.9
#LWVERSION is version 3
LWVERSION=master
WAPPSERVERVERSION=master



###################################################################################################
# Actual installation
###################################################################################################

# Install some stuff
apt-get update
apt-get install -y git gcc g++ gcc-4.7 g++-4.7 make pkg-config libexpat1-dev libssl-dev           \
  libhiredis-dev dh-autoreconf libfuse-dev libcurl4-gnutls-dev libevent-dev redis-server          \
  supervisor vim python-dev libi2c-dev python-pip libjansson-dev cmake mc mplayer arduino minicom \
  picocom bluez fuse libusb-dev libbluetooth-dev bluetooth joystick wpasupplicant                 \
  python-smbus curl libicu-dev mpg123 firmware-ralink firmware-realtek wireless-tools
apt-get clean

# Use gcc and g++ 4.7
update-alternatives --remove-all gcc
update-alternatives --remove-all g++
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 2             \
  --slave /usr/bin/g++ g++ /usr/bin/g++-4.7
update-alternatives --auto gcc

# Install some more stuff
pip install msgpack-python
pip install redis
pip install ino
pip install pyfirmata

# Create sandbox directory
mkdir -p $SANDBOX_PATH

# Install pybass
cd $SANDBOX_PATH
git clone https://github.com/Wyliodrin/pybass.git
cd pybass
python setup.py install
cp lib/hardfp/libbass* /usr/local/lib
ldconfig
cd $SANDBOX_PATH
rm -rf pybass

# Install BrickPi
cd $SANDBOX_PATH
git clone https://github.com/DexterInd/BrickPi.git
cd BrickPi/Software/BrickPi_Python
python setup.py install
cd $SANDBOX_PATH
rm -rf BrickPi

# Install libstrophe
cd $SANDBOX_PATH
git clone https://github.com/strophe/libstrophe.git
cd libstrophe
./bootstrap.sh
./configure --prefix=/usr
make
make install
cd $SANDBOX_PATH
rm -rf libstrophe

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
apt-get install -y nodejs


# Install wiringPi
cd $SANDBOX_PATH
git clone https://github.com/Wyliodrin/wiringPi.git
cd wiringPi
./build
cd $SANDBOX_PATH
rm -rf wiringPi

# Install pcre
cd $SANDBOX_PATH
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
tar -xzf pcre-8.39.tar.gz
rm -f pcre-8.39.tar.gz
cd pcre-8.39
./configure --prefix=/usr
make
make install
cd $SANDBOX_PATH
rm -rf pcre-8.39

# Install swig 3.0.12 (not lower than 3.0.5)
cd $SANDBOX_PATH
wget https://downloads.sourceforge.net/project/swig/swig/swig-3.0.12/swig-3.0.12.tar.gz
tar -xzf swig-3.0.12.tar.gz
rm -f swig-3.0.12.tar.gz
cd swig-3.0.12
./configure
make
make install
cd $SANDBOX_PATH
rm -rf swig-3.0.12

#node dependency for libwyliodrin
apt-get install -y node-gyp
apt-get install -y libuv0.10-dev

# Install libwyliodrin
cd $SANDBOX_PATH
git clone https://github.com/Wyliodrin/libwyliodrin.git
cd libwyliodrin
git checkout $LWVERSION
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr -DRASPBERRYPI=ON ..
make
make install
cd $SANDBOX_PATH
rm -rf libwyliodrin

# Run libwyliodrin scripts
install_social
update_streams

# Link wyliodrin module used in node-red
ln -s /usr/lib/node_modules /usr/lib/node

# Install wyliodrin-app-server
cd $SANDBOX_PATH
git clone https://github.com/Wyliodrin/wyliodrin-app-server.git
cd wyliodrin-app-server
git checkout $WAPPSERVERVERSION
npm install --unsafe-perm
mkdir -p /usr/wyliodrin/wyliodrin-app-server
cp -rf * /usr/wyliodrin/wyliodrin-app-server
ln -s /usr/wyliodrin/wyliodrin-app-server/source/startup.js /usr/wyliodrin/wyliodrin-app-server/startup.js
cd $SANDBOX_PATH
rm -rf wyliodrin-app-server

# Set boardtype to raspberry
mkdir -p /etc/wyliodrin
echo -n raspberrypi > /etc/wyliodrin/boardtype

# Create settings_raspberry.json
printf '{
  "config_file":  "/boot/wyliodrin.json",
  "home":         "/wyliodrin",
  "mount_file":   "/wyliodrin/projects/mnt",
  "build_file":   "/wyliodrin/projects/build",
  "shell":        "bash",
  "run":          "sudo -E make -f Makefile.raspberrypi run",
  "stop":         "sudo kill -9",
  "poweroff":     "sudo poweroff",
  "logout":       "/etc/wyliodrin/logs.out",
  "logerr":       "/etc/wyliodrin/logs.err",
  "hlogout":      "/etc/wyliodrin/hlogs.out",
  "hlogerr":      "/etc/wyliodrin/hlogs.err"
}\n' > /etc/wyliodrin/settings_raspberrypi.json

# Create home
mkdir /wyliodrin

# Create mount and build directories
mkdir -p /wyliodrin/projects/mnt
mkdir -p /wyliodrin/projects/build

# Startup script
printf '
[supervisord]
[program:wyliodrin-app-server]
directory=/usr/wyliodrin/wyliodrin-app-server
command=/usr/bin/node startup.js
user=pi
autostart=true
exitcodes=255
autorestart=unexpected
environment=HOME="/wyliodrin"
priority=40
' > /etc/supervisor/conf.d/wyliodrin-app-server.conf

# Wifi
cp /etc/network/interfaces /etc/network/interfaces.orig
printf 'auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet manual
wpa-roam /etc/wyliodrin/wireless.conf
iface default inet dhcp
' > /etc/network/interfaces

# Change owner of directories used by wyliodrin
chown -R pi:pi /wyliodrin
chown -R pi:pi /etc/wyliodrin

# Copy bashrc
cp /home/pi/.bashrc /wyliodrin

# Clean
apt-get clean
rm -rf $SANDBOX_PATH
rm -rf /home/pi/.npm
rm -rf /home/root/.npm

# Supervisor reload
supervisorctl reload

rm -rf $SANDBOX_PATH

# Reboot
reboot