treehouses/builder

View on GitHub
autorunonce

Summary

Maintainability
Test Coverage
#!/bin/bash

# ----- BASIC CONFIGURATIONS: -----

# Sets hostname to a desirable name
# (default: treehouses)
hostname='treehouses'

# If "button", bluetooth is enabled unless GPIO 18 is pulled to ground
# If "normal", bluetooth is always on.
# If "off", bluetooth is always off
# (Default: button)
bluetooth="button"

# If true, this allows trusted treehouses team members to log on to the system without a password
# It will search for key pairs or authorized_key files directly in /boot folder to incorporate in the ssh server
# (default: true)
treehousesteamsshkeys="true"
addauthorizedkey=""


# ----- INTERNET CONFIGURATIONS: -----

# Options for connectiontype:
#   "ethernet" connects to ethernet only
#   "wifi" connects to an existing router
#   "apinternet" turns the raspberry pi into a hotspot with internet connection
#   "aplocal" turns the raspberry pi into a hotspot without internet connection
#   "bridge" connects to an existing router, creates a raspberry pi hotspot and bridges these two connections.
connectiontype="apinternet"

# Change this value to true if you are using a hidden network
# (default: false)
hidden="false"

# wifiname: Insert the name (SSID) of your wifi router
# wifipassword: Insert the password of your wifi router. Leave field empty for open networks
# (Edit if "connectiontype" is "wifi" or "bridge")
wifiname=''
wifipassword=''

# for Enterprise only, please leave it blank if you are not using WPA2-Enterprise
identity=''

# hotspotname: Change to the name you want your hotspot to have, raspberry pi will be a hotspot with this name as SSID
# hotspotpassword:
#   Insert the password you want your Raspberry Pi hotspot to have. Can be blank for no password (open network)
#   Passwords must be 8 characters or more. If your password has less, it will be configured as open network
# (Edit if "connectiontype" is "apinternet", "aplocal" or "bridge")
hotspotname='treehouses'
hotspotpassword=''


# ----- OTHER CONFIGURATION: -----

# startvnc: If true, it starts vnc and gui (default: false)
startvnc="false"

# starttor: If true, it starts a tor hidden service (default: false)
# starttornotice: If true, publishes these addresses to treehouses gitter or "tornoticeurl" (default: false)
starttor="false"
starttornotice="false"
tornoticeapiurl=""

# startplanet: If true, this starts the docker service (default: true)
# updateplanet: If true, this updates the planet to the latest revision (default: false)
startplanet="true"
updateplanet="false"

# enablecoralboard: If true, this enables the use of the Coral Enviro board. (default: false)
# startcoralenvirodemo: If true, this starts the demo on Coral Enviro board. (default: false)
enablecoralenviroboard="false"
startcoralenvirodemo="false"


# No need to edit below this line
#----------------------------------------------------------------------------------------------------#

# sets the desired hostname for your raspberry pi
treehouses rename $hostname

# expands the filesystem
treehouses expandfs

# configures the wifi antenna of the raspberry pi
if [ ! $(treehouses detect wifi) ]; then connectiontype="ethernet"; fi; # default network when no wifi
if [ ${#hotspotpassword} -lt 8 ]; then hotspotpassword=''; fi; # no password if the password is < 8 characters long
case $connectiontype in
  "ethernet")
    ;;
  "wifi")
    if [ "$hidden" == "false" ]; then
      if [ -z $identity ]; then
        treehouses wifi $wifiname $wifipassword
      else
        treehouses wifi $wifiname $wifipassword $identity
      fi
    else
      if [ -z $identity ]; then
        treehouses wifihidden $wifiname $wifipassword
      else
        treehouses wifihidden $wifiname $wifipassword $identity
      fi
    fi;;
  "apinternet")
    if [ "$hidden" == "false" ]; then
      treehouses ap internet $hotspotname $hotspotpassword
    else
      treehouses aphidden internet $hotspotname $hotspotpassword
    fi;;
  "aplocal")
    if [ "$hidden" == "false" ]; then
      treehouses ap local $hotspotname $hotspotpassword
    else
      treehouses aphidden local $hotspotname $hotspotpassword
    fi;;
  "bridge")
    treehouses bridge $wifiname $hotspotname $wifipassword $hotspotpassword;;
  *)
    echo "Unsupported configuration. Switching to default ap internet config";
    treehouses ap internet treehouses;;
esac

# disables button for bluetooth if Coral Env board is present, or constant bluetooth is selected
if [ $bluetooth != "off" ] && [ $(treehouses detect bluetooth) ]; then
  if [ $bluetooth = "button" ] && [ $enablecoralenviroboard = "false" ]; then
    treehouses button bluetooth
  else 
    treehouses bluetooth on
  fi
fi

# starts vnc and gui services
if [ $startvnc = "true" ]; then
  treehouses vnc on
fi

# starts tor service
if [ $starttor = "true" ]; then
  treehouses tor add 22
  if [ $startplanet = "true" ]; then
    treehouses tor add 80
    treehouses tor add 2200
  fi
  sleep 10
  sync
  if [ $starttornotice = "true" ]; then
    if [ ${#tornoticeapiurl} -ge 0 ]; then
      treehouses tor notice delete
      treehouses tor notice add $tornoticeapiurl
    fi
    treehouses tor notice on
  fi
  sync
  sleep 10
fi

# updates planet if there is an internet connection
if [ $updateplanet = "true" ] && [ "$(treehouses internet)" ]; then
  docker pull treehouses/planet:latest
  docker pull treehouses/planet:db-init
  docker tag treehouses/planet:latest treehouses/planet:local
  docker tag treehouses/planet:db-init treehouses/planet:db-init-local
fi

# removes treehouses team members sshkeys and places keys to .ssh folder
if [ $treehousesteamsshkeys = "false" ]; then
  treehouses sshkey deleteall
fi
if [[ ! -z $addauthorizedkey ]]; then
  treehouses sshkey add $addauthorizedkey
fi

# starts planet
if [ $startplanet = "true" ]; then
  {    
    echo "#!/bin/sh"
    echo
    echo "if [ \"$(treehouses container)\" = \"balena\" ]; then"
    echo "  export DOCKER_HOST=localhost:2375"
    echo "fi"
    echo "sleep 1"
    echo
    echo "planet_autorun=true"
    echo
    echo "if [ \"\$planet_autorun\" = true ]; then"
    echo "  treehouses services planet up"
    echo "fi"
  } > /boot/autorun

  mv /srv/tenalp /srv/planet
  sync;sync;sync
  treehouses services planet up
fi

# starts coral environmental board demo
if [ "$startcoralenvirodemo" = "true" ] && [ -e /sys/bus/iio/devices/iio:device0 ]; then
  treehouses coralenv demo-always-on
fi

# boots to console if RPiZero or RPi3A+ (not enough RAM?)
case "$(treehouses detect rpi)" in
  RPIZW|RPI3A+)
    treehouses bootoption console;;
esac

if [ "$(treehouses rebootneeded)" = "true" ]; then
  reboot
fi