Last active
August 29, 2015 13:57
-
-
Save mcos/9646711 to your computer and use it in GitHub Desktop.
Script to download and install the https://github.com/robinpowered/bifrost package onto a 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/sh | |
# Copyright 2014 Robin Powered Inc. | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# Directories we want to work with | |
TMP_DIR="/tmp" | |
OPT_DIR="/opt" | |
NODE_DIR="${OPT_DIR}/node" | |
NODE_BIN_DIR="${NODE_DIR}/bin" | |
USR_LOCAL_BIN_DIR="/usr/local/bin" | |
PI_USER="pi" | |
PI_USER_DIR="/home/pi" | |
RBN_PI="rbn-pi" | |
RBN_PI_USER_DIR="${PI_USER_DIR}/${RBN_PI}" | |
# %N Command doesn't work with date on MacOS, but this script is to be run on | |
# a raspberry pi, so we'll overlook that now. | |
NOW_MILLIS=$(date +"%s.%3N") | |
# Supported version of node for pi is 0.10.24 | |
# Do we want to take a specific version from a passed argument at some stage? | |
NODE="node" | |
NPM="npm" | |
COFFEE="coffee-script" | |
FOREVER="forever" | |
NODE_GYP="node-gyp" | |
NODE_VERSION="0.10.24" | |
NODE_PKG_NAME="node-v${NODE_VERSION}-linux-arm-pi" | |
NODE_TAR_FILE="${NODE_PKG_NAME}.tar.gz" | |
NODE_URL="http://nodejs.org/dist/v${NODE_VERSION}/${NODE_TAR_FILE}" | |
stop_processes() { | |
WHICH_FOREVER=`which ${FOREVER} 2>&1` | |
if [ $? -eq 0 ] && [ -x ${WHICH_FOREVER} ]; then | |
echo "Stopping forever processes" | |
forever stopall | |
fi | |
} | |
install_apt_packages() { | |
echo "Updating Aptitude..." | |
apt-get -y update | |
apt-get -y upgrade | |
# Install git and wget | |
echo "Installing git, wget, libusb-dev, libusb-1.0-0.dev, libbluetooth-dev" | |
apt-get install -y git wget libusb-dev libusb-1.0-0.dev libbluetooth-dev bluez | |
} | |
install_node() { | |
if [ -d ${NODE_DIR} ]; then | |
echo "Moving the old node directory to ${NODE_DIR}.${NOW_MILLIS}" | |
mv "${NODE_DIR}" "${NODE_DIR}.${NOW_MILLIS}" | |
fi | |
# Remove old version of tarfile | |
if [ -e "${TMP_DIR}/${NODE_TAR_FILE}" ]; then | |
echo "Found an old version of the tar file at ${TMP_DIR}/${NODE_TAR_FILE}, \ | |
moving it to ${TMP_DIR}/${NODE_TAR_FILE}.${NOW_MILLIS}" | |
mv "${TMP_DIR}/${NODE_TAR_FILE}" "${TMP_DIR}/${NODE_TAR_FILE}.${NOW_MILLIS}" | |
fi | |
# Remove old version of node directory | |
if [ -d ${TMP_DIR}/${NODE_PKG_NAME} ]; then | |
echo "Found an old version of the node folder at ${TMP_DIR}/${NODE_PKG_NAME}, \ | |
moving it to ${TMP_DIR}/${NODE_PKG_NAME}.${NOW_MILLIS}" | |
mv "${TMP_DIR}/${NODE_PKG_NAME}" "${TMP_DIR}/${NODE_PKG_NAME}.${NOW_MILLIS}" | |
fi | |
# Install node | |
cd "$TMP_DIR" \ | |
&& wget "$NODE_URL" | |
echo "Un-tarring the package into $TMP_DIR" | |
tar xzf "${TMP_DIR}/${NODE_TAR_FILE}" | |
echo "Copying from ${TMP_DIR}/${NODE_PKG_NAME}/ to ${OPT_DIR}" | |
cp -r "${TMP_DIR}/${NODE_PKG_NAME}/" ${OPT_DIR} | |
OPT_NODE_PKG_DIR="${OPT_DIR}/${NODE_PKG_NAME}" | |
echo "Moving from ${OPT_NODE_PKG_DIR} to ${NODE_DIR}" | |
mv ${OPT_NODE_PKG_DIR} ${NODE_DIR} | |
# Instead of changing /etc/profile to add node to the path, do this | |
NODE_BINS="${NODE} ${NPM}" | |
for nb in ${NODE_BINS} | |
do | |
if [ -h "${USR_LOCAL_BIN_DIR}/${nb}" ]; then | |
rm "${USR_LOCAL_BIN_DIR}/${nb}" | |
fi | |
echo "Symlinking the ${nb} binary into ${USR_LOCAL_BIN_DIR}" | |
ln -s "${NODE_BIN_DIR}/${nb}" "${USR_LOCAL_BIN_DIR}/${nb}" | |
done | |
} | |
get_node() { | |
# Check to see if the node command exists, if it does - ask the user to override it | |
WHICH_NODE=`which ${NODE} 2>&1` | |
if [ $? -eq 0 ] && [ -x ${WHICH_NODE} ]; then | |
echo "node.js already exists on this system, skipping the install" | |
else | |
echo "Can't detect node.js on this system, so we'll go ahead and install it..." | |
install_node | |
fi | |
} | |
install_node_modules() { | |
NODE_PACKAGES="${COFFEE} ${FOREVER} ${NODE_GYP}" | |
echo "Installing node modules: ${NODE_PACKAGES}" | |
for np in ${NODE_PACKAGES} | |
do | |
$NPM install -g $np | |
done | |
} | |
get_rbn_pi() { | |
# Clone the rbn-pi repository | |
# Unfortunately, this is how we're going to do things for now. | |
echo "Cloning the ${RBN_PI} git repo" | |
TMP_RBN_PI_DIR="${TMP_DIR}/${RBN_PI}" | |
RBN_PI_REPO="https://github.com/robinpowered/bifrost.git" | |
cd ${TMP_DIR} && git clone ${RBN_PI_REPO} ${RBN_PI} | |
if [ -d ${RBN_PI_USER_DIR} ]; then | |
echo "Found an old version of the ${RBN_PI} folder to ${RBN_PI_USER_DIR}.${NOW_MILLIS}" | |
mv ${RBN_PI_USER_DIR} "${RBN_PI_USER_DIR}.${NOW_MILLIS}" | |
fi | |
mv ${TMP_RBN_PI_DIR} ${RBN_PI_USER_DIR} | |
chown -R ${PI_USER}:${PI_USER} ${RBN_PI_USER_DIR} | |
} | |
start_processes() { | |
echo "Running an ${NPM} install in the ${RBN_PI_USER_DIR} directory" | |
cd ${RBN_PI_USER_DIR} && ${NPM} install --production | |
echo "Starting this process using ${FOREVER}" | |
FOREVER_START_PI="${RBN_PI_USER_DIR}/start.sh" | |
chmod 755 ${FOREVER_START_PI} | |
sh $FOREVER_START_PI | |
# Save the forever start command in cron.d | |
CRON="cron" | |
CRON_DIR="/etc/cron.d" | |
RBN_PI_CRON_DIR="${RBN_PI_USER_DIR}/cron" | |
RBN_PI_CRON_FILENAME="rbn-pi-reboot" | |
RBN_PI_CRON_PATH="${RBN_PI_CRON_DIR}/${RBN_PI_CRON_FILENAME}" | |
cp ${RBN_PI_CRON_PATH} ${CRON_DIR} | |
# Restart the cron service, to make sure that the changes in this file are picked up | |
service ${CRON} restart | |
} | |
# ================================= | |
# Main Program Flow | |
# ================================= | |
stop_processes | |
install_apt_packages | |
get_node | |
install_node_modules | |
get_rbn_pi | |
start_processes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment