Created
April 19, 2014 15:27
-
-
Save jedahan/11087620 to your computer and use it in GitHub Desktop.
scripts to setup a raspberry pi
This file contains 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
#!/usr/bin/env bash | |
### PedalPower Bootstrap | |
# This script will install the necessary dependencies, code and init scripts to run a pedalpower server node. | |
# It should only be run on a clean raspbian image, and only need to run if your config changes | |
### Configuration | |
# Set the hostname and static ip here | |
# Setup logfile | |
name='pedalpower-server' | |
export ip='10.0.0.10' | |
# Quit on error | |
set -e | |
# Our banner | |
echo | |
echo "Pedal Power Footstrap" | |
echo | |
# Expand to the entire sd card if we don't have at least 2 gigs free | |
size=`df -l | grep rootfs | awk '{ print $2 }'` | |
[[ $(($size - 1804128)) -lt 1804128 ]] && sudo raspi-config --expand-rootfs && sudo reboot | |
# Display colorized error output | |
function _error() { | |
COLOR='\033[00;31m' # red | |
RESET='\033[00;00m' # white | |
echo -e "${COLOR}[PedalPower ERROR] ${@}${RESET}" | |
exit 1 | |
} | |
# Display colorized warning output | |
function _info() { | |
COLOR='\033[00;32m' # green | |
RESET='\033[00;00m' # white | |
echo -e "${COLOR}[PedalPower INFO] ${@}${RESET}" | |
} | |
### Check that we have a network connection | |
_info "Checking for a network connection" | |
ping -c 5 google.com || _error "can't ping google" | |
### Permissions | |
# We ask for the administrator password upfront, and update a timestamp until | |
# the script is finished | |
sudo -v | |
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null & | |
### Install software dependencies | |
_info "Installing software dependencies" | |
_info "Updating software list" | |
sudo aptitude update | |
_info "Upgrading system software" | |
sudo aptitude upgrade -y | |
#_info "Installing package dependencies" | |
#[[ -f `which redis` ]] || sudo aptitude install -y redis | |
if [[ ! -f `which npm` ]]; then | |
version="0.10.22" | |
_info "Installing node.js ${version}" | |
pushd /usr/local | |
sudo wget http://nodejs.org/dist/v${version}/node-v${version}-linux-arm-pi.tar.gz | |
sudo tar xzf node-v${version}-linux-arm-pi.tar.gz --strip=1 | |
popd | |
fi | |
_info "Installing libraries" | |
npm install | |
### Init scripts | |
# Generate the install initscripts using foreman | |
_info "Installing initscript" | |
initscript=`dirname $PWD`/venue-server/raspberrypi/venueserver | |
sudo cp $initscript /etc/init.d/ || _error "failed to copy initscript" | |
_info "Enabling initscript" | |
sudo update-rc.d venueserver defaults || _error "failed to enable initscript" | |
### Network config | |
# Each server node should have the same name and resolve to the same ip | |
# This is hardcoded in the software and this config script so that if a Pi | |
# fails, you can just pop in a new one and not miss a beat | |
# We do not error out on networking stuff because they produce errors | |
# on failed hostname lookup. That is to say, the ip and routing | |
# can be set fine and the ifconfig and route utilities still complain | |
# if the hostname is not in the router's lookup table. No big deal. | |
_info "Setting up networking" | |
_info "Setting hostname to ${name}" | |
oldname=`hostname` | |
export gateway='10.0.0.1' | |
export netmask='255.255.255.0' | |
[[ `hostname` -ne ${name} ]] || sudo hostname ${name} | |
[[ -e /etc/hostname ]] || sudo touch /etc/hostname | |
grep -q ${name} /etc/hostname || sudo sed -ie "1s/.*/${name}/" /etc/hostname | |
grep -q ${name} /etc/hosts || sudo sed -ie "s/${oldname}/${name}/" /etc/hosts | |
_info "Setting ip to ${ip}, gateway to ${gateway}, netmask to ${netmask}" | |
sudo ifconfig eth0 ${ip} netmask ${netmask} | |
sudo route add default gw ${gateway} | |
# Make it permanent | |
sudo sed -ie '/eth0/d' /etc/network/interfaces | |
sudo tee -a /etc/network/interfaces << EOF | |
auto eth0 | |
iface eth0 inet static | |
address ${ip} | |
netmask ${netmask} | |
gateway ${gateway} | |
EOF | |
_info "done!" |
This file contains 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
#!/usr/bin/env bash | |
# Derived from Ray Viljoen's Raspberry PI Install Script | |
# Much love to Ray | |
# Quit on error | |
set -e | |
_piline="------------------------------------------------------------------------------------" | |
# Display colorized information output | |
function _info() { | |
COLOR='\033[00;32m' # green | |
RESET='\033[00;00m' # white | |
echo -e "${COLOR}${@}${RESET}" | |
} | |
# Display colorized warning output | |
function _warn() { | |
COLOR='\033[00;31m' # red | |
RESET='\033[00;00m' # white | |
echo -e "${COLOR}${@}${RESET}" | |
} | |
_info ' | |
__________.___ .___ __ .__ .__ | |
\______ \ | | | ____ _______/ |______ | | | | ___________ | |
| ___/ | | |/ \ / ___/\ __\__ \ | | | | _/ __ \_ __ \ | |
| | | | | | | \\___ \ | | / __ \| |_| |_\ ___/| | \/ | |
|____| |___| |___|___| /____ > |__| (____ /____/____/\___ >__| | |
\/ \/ \/ \/ | |
' | |
# Set $1 as image path | |
DISTRO="$1" | |
# Check image path is set else exit | |
if [ -z "$DISTRO" ]; then | |
_warn $_piline | |
_warn "ERR: No image path or url supplied" | |
_warn $_piline | |
exit 0 | |
fi | |
# Selected disk | |
_udisk="" | |
function promptDisk() { | |
# ================================================ | |
# Check connected disks and save paths to array | |
# ================================================ | |
# Counter | |
i=0 | |
echo $_piline | |
# Loop over df -lh | |
while read line; do | |
# Print with local mount only and add counter to select disk | |
if [ "$i" -gt 0 ]; then | |
echo "$i) $line" | |
# Asign first work (path) to array with corresponding counter | |
_mount[i]=$( echo $line | awk '{print $1;}') | |
else | |
_info " $line" | |
echo $_piline | |
fi | |
# Increment counter | |
((i++)) | |
done <<< "$(df -h)" | |
echo -e "$_piline\n" | |
_opts='' | |
for i in "${!_mount[@]}"; do | |
[ -z "$_opts" ] && _opts="${_opts}${i}" || _opts="${_opts}, ${i}" | |
done | |
# Ask user to select mounted disk | |
echo "Select the disk to use by enetering the disk number." | |
_warn "*** MAKE SURE YOU SELECT THE CORRECT DISK ***" | |
_warn "*** Refer to the Readme if uncertain ***" | |
echo -n -e "\nUse disk [ $_opts ] #" | |
read ans | |
# Set selected disk | |
_udisk=${_mount[$ans]} | |
# Test if valid disk selected | |
if [ -z "$_udisk" ]; then | |
_warn "\n ======= Invalid selection ======= \n" | |
promptDisk | |
fi | |
} | |
# Run prompt | |
promptDisk | |
# =========================================================== | |
# Past this point a valid disk has been selected, so proceed. | |
# =========================================================== | |
# Format disk name to raw format | |
_rawdisk=$( echo $_udisk | awk 'sub("..$", "")' | sed 's/disk/rdisk/') | |
# Unmount Disk | |
echo "Unmounting Disk" | |
diskutil unmount $_udisk | |
# If DISTRO is a url, then download and find the .img file | |
url_regex='https?://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' | |
if [[ $DISTRO =~ $url_regex ]]; then | |
tmpdir=`mktemp -d -t pedal` | |
pushd $tmpdir | |
curl -ORL $DISTRO | |
DISTRO=`ls $PWD/*` | |
popd | |
fi | |
# If the file is a zip, unzip it | |
zip_regex='.*zip$' | |
if [[ ${DISTRO} =~ $zip_regex ]]; then | |
tmpdir=`mktemp -d -t pedal` | |
pushd $tmpdir | |
unzip $DISTRO | |
DISTRO=`ls $PWD/*` | |
popd | |
fi | |
echo "Writing image" | |
echo "Ctrl+T to see progress..." | |
sudo dd bs=1m if=${DISTRO} of=${_rawdisk} | |
# Eject disk | |
echo "Ejecting Disk" | |
diskutil eject ${_rawdisk} | |
_info "All Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, if I remember correctly you grabbed these from a private repo — any problem with sharing them?