2017-10-30 18:05:19 +00:00
#!/bin/bash -e
2017-11-10 10:09:07 +00:00
# Running from a git repo
2017-10-30 18:05:19 +00:00
# Add cmdline options for passing to ansible
2017-11-10 10:09:07 +00:00
# Todo add proper shift to gobble up --debug --reinstall
2017-11-10 08:06:43 +00:00
2019-10-25 01:05:47 +00:00
PLAYBOOK=iiab-stages.yml
INVENTORY=ansible_hosts
IIAB_STATE_FILE=/etc/iiab/iiab_state.yml
2017-11-10 01:59:17 +00:00
ARGS=""
2017-11-05 04:33:15 +00:00
CWD=`pwd`
OS=`grep ^ID= /etc/*release|cut -d= -f2`
OS=${OS//\"/}
2019-10-31 22:04:58 +00:00
MIN_RPI_KERN=4.19.79 # Can be further updated if necessary, when Raspbian's Oct 2019 kernels are more officially fixed such that running 'rpi-update' will no longer be nec soon, see: https://github.com/iiab/iiab/issues/1993
2019-10-31 21:33:43 +00:00
MIN_ANSIBLE_VER=2.8.7 # Ansible 2.8.3 and 2.8.6 have serious bugs, preventing their use with IIAB.
2017-11-08 07:53:54 +00:00
2018-07-23 21:49:34 +00:00
if [ ! -f /etc/iiab/local_vars.yml ]; then
2018-07-24 19:28:34 +00:00
2018-07-24 19:20:34 +00:00
if [ -f /opt/iiab/iiab/vars/local_vars.yml ]; then
2018-07-24 19:28:34 +00:00
echo -e "\nACTION NEEDED: YOUR /opt/iiab/iiab/vars/local_vars.yml IS NO LONGER SUPPORTED.\n" >&2
2018-09-29 02:55:04 +00:00
2018-07-24 20:16:08 +00:00
echo -e "███████████████████ TO MOVE IT TO THE CORRECT LOCATION, RUN: ███████████████████" >&2
2018-07-24 20:17:29 +00:00
echo -e "██ ██" >&2
2018-07-24 20:16:08 +00:00
echo -e "██ mv /opt/iiab/iiab/vars/local_vars.yml /etc/iiab/local_vars.yml ██" >&2
2018-07-24 20:17:29 +00:00
echo -e "██ ██" >&2
2018-07-24 20:16:08 +00:00
echo -e "████████████████████████████████████████████████████████████████████████████████\n" >&2
2018-07-24 19:20:34 +00:00
fi
2018-07-24 19:43:38 +00:00
echo -e "\nEXITING: /opt/iiab/iiab/iiab-install REQUIRES /etc/iiab/local_vars.yml\n" >&2
2018-09-29 02:55:04 +00:00
2018-09-28 13:10:22 +00:00
echo -e "(1) Please read http://wiki.laptop.org/go/IIAB/local_vars.yml to learn more" >&2
2018-07-24 19:43:38 +00:00
echo -e "(2) MIN/MEDIUM/BIG samples are included in /opt/iiab/iiab/vars" >&2
echo -e "(3) NO TIME FOR DETAILS? RUN INTERNET-IN-A-BOX'S FRIENDLY 1-LINE INSTALLER:\n" >&2
2018-09-29 02:55:04 +00:00
2019-10-25 01:44:07 +00:00
echo -e ' http://download.iiab.io\n' >&2
2018-07-24 19:43:38 +00:00
2018-07-23 21:49:34 +00:00
exit 1
fi
2018-07-24 19:28:34 +00:00
# FUTURE: Test if their local_vars.yml is sufficiently version-compatible !
2018-07-23 21:51:55 +00:00
echo -e "\n\n./iiab-install $* BEGUN IN $CWD\n"
2017-11-19 03:29:45 +00:00
if [ ! -f /etc/ansible/facts.d/local_facts.fact ]; then
mkdir -p /etc/ansible/facts.d
fi
cp ./scripts/local_facts.fact /etc/ansible/facts.d/local_facts.fact
echo "Placed /etc/ansible/facts.d/local_facts.fact into position."
2017-11-05 04:41:41 +00:00
if [ ! -f $PLAYBOOK ]; then
2019-10-25 01:44:07 +00:00
echo "EXITING: IIAB Playbook ""$PLAYBOOK"" not found."
echo "Please run './iiab-install' from /opt/iiab/iiab (top level of git repo)."
2017-11-05 04:41:41 +00:00
exit 1
fi
2017-11-19 03:29:45 +00:00
if [ "$1" != "--debug" ] && [ "$1" != "--reinstall" ] && [ "$1" != "" ]; then
echo "Use './iiab-install' for regular installs, or to continue an install."
2019-10-25 01:05:47 +00:00
echo "Use './iiab-install --reinstall' to force running all Stages 0-9, followed by the Network Role."
echo "Use './iiab-install --debug' to run Stage 0, followed by Stages 3-9, followed by the Network Role."
echo "Use './iiab-configure' to run Stage 0, followed by Stages 4-9."
echo "Use './runrole' to run Stage 0, followed by a single Stage or Role."
echo "Use './iiab-network' to run Stage 0, followed by the Network Role."
2017-11-19 03:29:45 +00:00
exit 1
fi
2017-11-10 02:19:38 +00:00
# Subroutine compares software version numbers. Generates rare false positives
# like "1.0 > 1" and "2.4.0 > 2.4". Avoid risks by structuring conditionals w/
2017-11-10 01:59:17 +00:00
# a consistent # of decimal points e.g. "if version_gt w.x.y.z a.b.c.d; then"
2018-10-26 21:16:06 +00:00
version_gt() { [ "$(printf '%s\n' "$@" | sort -V | head -1)" != "$1" ]; }
2017-11-10 01:59:17 +00:00
# Verify that Raspbian is running a recent enough kernel. As Raspbian
# updates on 4.9.41-v7+ broke bridging, WiFi AP & OpenVPN in Oct/Nov 2017.
CURR_KERN=`uname -r`
2019-11-14 17:01:11 +00:00
echo "Found Kernel ""$CURR_KERN"
2017-12-13 09:45:04 +00:00
if [ "$OS" == "raspbian" ] && version_gt $MIN_RPI_KERN $CURR_KERN ; then
2019-11-14 17:01:11 +00:00
echo -e "\nEXITING: Kernel ""$MIN_RPI_KERN"" or higher required with Raspbian."
2018-09-29 02:55:04 +00:00
echo -e "PLEASE RUN 'apt update' then 'apt install raspberrypi-kernel' then reboot."
2019-11-14 17:09:24 +00:00
echo -e "THEN IF NEC run 'rpi-update' to install a more recent kernel, then reboot."
2018-09-29 02:55:04 +00:00
echo -e "IIAB INSTALL INSTRUCTIONS: https://github.com/iiab/iiab/wiki/IIAB-Installation"
2017-11-10 01:59:17 +00:00
exit 1
fi
2017-11-10 02:19:38 +00:00
# Verify that a recent enough version of Ansible is installed. See #449. The
2017-11-10 01:59:17 +00:00
# "include:" command was inconsistently implemented prior to Ansible 2.4.x.x
2017-11-10 02:30:48 +00:00
CURR_ANSIBLE_VER=0
2017-12-13 11:39:29 +00:00
#if [ $(grep ubuntu /etc/apt/sources.list) ]; then # FAILS when multiple lines returned, due to single square brackets
#if grep -q ubuntu /etc/apt/sources.list ; then # Works: bypasses need for "> /dev/null" thanks to "grep -q" (quiet)
#if command -v ansible > /dev/null ; then # Works But Wordy!
#if [[ $(command -v ansible) ]]; then # Also Works! $(...) nests more easily than backticks
#if [[ `which ansible` ]]; then # "which" misses built-in commands like cd, and is RISKY per https://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
#if [[ `type -P ansible` ]]; then # "type -P" isn't POSIX compliant; it misses built-in commands like "cd"
if [[ `command -v ansible` ]]; then # "command -v" is POSIX compliant; it catches built-in commands like "cd"
2017-12-13 09:45:04 +00:00
#CURR_ANSIBLE_VER=`ansible --version | head -1 | sed -e 's/.* //'`
#CURR_ANSIBLE_VER=`ansible --version | head -1 | cut -f 2 -d " "`
2018-09-11 18:02:17 +00:00
CURR_ANSIBLE_VER=`ansible --version | head -1 | awk '{print $2}'` # to match scripts/ansible
2019-11-14 17:01:11 +00:00
echo "Found Ansible ""$CURR_ANSIBLE_VER"
2017-11-10 01:59:17 +00:00
fi
2017-12-13 09:45:04 +00:00
if version_gt $MIN_ANSIBLE_VER $CURR_ANSIBLE_VER ; then
2019-11-14 17:01:11 +00:00
echo -e "\nEXITING: Ansible ""$MIN_ANSIBLE_VER"" or higher required.\n"
2018-09-29 02:55:04 +00:00
echo -e "REMOVE PRIOR VERSIONS using 'apt purge ansible' and/or 'pip uninstall ansible'."
echo -e "THEN RUN 'scripts/ansible' to install the latest Ansible from PPA or RPM."
echo -e "'ansible --version' and 'apt -a list ansible' can also be very useful.\n"
echo -e "IIAB INSTALL INSTRUCTIONS: https://github.com/iiab/iiab/wiki/IIAB-Installation"
2017-11-10 01:59:17 +00:00
exit 1
2017-11-05 04:33:15 +00:00
fi
2017-11-10 01:59:17 +00:00
# Stage 0 will always be run. From there on up to Stage 9 we keep a counter
# (in /etc/iiab/iiab.env) of the highest completed Stage. Avoid repetition!
STAGE=0
2017-11-10 22:18:05 +00:00
if [ -f /etc/iiab/iiab.env ]; then
2017-12-13 09:45:04 +00:00
if grep -q STAGE= /etc/iiab/iiab.env ; then
2017-11-10 10:09:07 +00:00
source /etc/iiab/iiab.env
2019-11-14 17:01:11 +00:00
echo "Extracted STAGE=""$STAGE"" (counter) from /etc/iiab/iiab.env"
2017-11-10 10:09:07 +00:00
if ! [ "$STAGE" -eq "$STAGE" ] 2> /dev/null; then
2019-11-14 17:01:11 +00:00
echo -e "\nEXITING: STAGE (counter) value == ""$STAGE"" is non-integer in /etc/iiab/iiab.env"
2017-11-10 10:09:07 +00:00
exit 1
2017-11-25 16:11:57 +00:00
elif [ "$STAGE" -lt 0 ] || [ "$STAGE" -gt 9 ]; then
2019-11-14 17:01:11 +00:00
echo -e "\nEXITING: STAGE (counter) value == ""$STAGE"" is out-of-range in /etc/iiab/iiab.env"
2017-11-10 10:09:07 +00:00
exit 1
fi
fi
2019-10-25 01:05:47 +00:00
if [ "$1" == "--reinstall" ]; then
2017-11-20 20:06:35 +00:00
STAGE=0
2019-11-14 17:01:11 +00:00
ARGS="$ARGS"" --extra-vars reinstall=True"
2017-11-20 20:06:35 +00:00
sed -i 's/^STAGE=.*/STAGE=0/' /etc/iiab/iiab.env
echo "Wrote STAGE=0 (counter) to /etc/iiab/iiab.env"
2017-11-10 08:06:43 +00:00
elif [ "$STAGE" -ge 2 ] && [ "$1" == "--debug" ]; then
STAGE=2
sed -i 's/^STAGE=.*/STAGE=2/' /etc/iiab/iiab.env
echo "Wrote STAGE=2 (counter) to /etc/iiab/iiab.env"
elif [ "$STAGE" -eq 9 ]; then
echo -e "\nEXITING: STAGE (counter) in /etc/iiab/iiab.env shows Stage 9 Is Already Done."
2019-10-25 01:05:47 +00:00
echo -e "Use './iiab-install --reinstall' to force running all Stages 0-9, followed by the Network Role."
echo -e "Use './iiab-install --debug' to run Stage 0, followed by Stages 3-9, followed by the Network Role."
echo -e "Use './iiab-configure' to run Stage 0, followed by Stages 4-9."
echo -e "Use './runrole' to run Stage 0, followed by a single Stage or Role."
echo -e "Use './iiab-network' to run Stage 0, followed by the Network Role.\n\n"
exit 0 # Allows rerunning http://download.iiab.io/install.txt
2017-10-30 18:05:19 +00:00
fi
fi
2017-11-10 08:06:43 +00:00
if [ "$STAGE" -lt 2 ] && [ "$1" == "--debug" ]; then
echo -e "\n'--debug' *ignored* as STAGE (counter) < 2."
fi
2017-10-30 18:05:19 +00:00
2019-10-25 01:05:47 +00:00
# TEMPORARY: Catch images up to current code to benefit from pre-installed apps
2019-10-25 17:13:57 +00:00
# Workaround for (web-published) images; will go away later
# Assumes /etc/iiab/iiab_state.yml is not created until (prior run of) Stage 4 but
# config_vars2.yml is present with the stage counter altered by pi-gen to be 2.
2019-10-24 16:15:25 +00:00
if [ -f /etc/iiab/config_vars2.yml ]; then
2019-10-25 01:05:47 +00:00
mv /etc/iiab/config_vars2.yml $IIAB_STATE_FILE
2019-10-24 16:15:25 +00:00
2019-10-25 17:13:57 +00:00
# Fix up prior values in state file
# mongo role improved post image creation
2019-10-25 01:05:47 +00:00
if grep -q sugar $IIAB_STATE_FILE; && ! grep -q mongodb $IIAB_STATE_FILE; then
echo "mongodb_installed: True" >> $IIAB_STATE_FILE
2019-10-15 04:28:15 +00:00
fi
2019-10-25 17:13:57 +00:00
# another change to accout for
sed -i -e 's/pan_bluetooth/bluetooth/' $IIAB_STATE_FILE
2019-10-15 04:28:15 +00:00
if [ "$STAGE" -eq 2 ]; then
2019-10-25 01:05:47 +00:00
echo -e "\nCompleting Stage 3 from IIAB image (starts systemd service iiab-setup-db to run the 'mysql' role)."
2019-10-15 04:28:15 +00:00
systemctl start iiab-setup-db
fi
2019-10-25 01:05:47 +00:00
PLAYBOOK="iiab-from-console.yml" # Stage 4-9 then Network Role
ARGS="" # Removes '--extra-vars reinstall=True' if --reinstall, BUT WHY?
2019-10-25 17:13:57 +00:00
# the same as --reinstall execpt stage 3 is not run as there are no other functional
# changes in stage 3 to account for post image creation once the above is run.
# reinstall=True would force kiwix to re-download and re-install in commit
# ce2ec3b0cad76449caf3299003b5d297a3164181
## End image catch up
2019-10-15 04:28:15 +00:00
fi
2018-09-29 02:55:04 +00:00
echo -e "\nTRY TO RERUN './iiab-install' IF IT FAILS DUE TO CONNECTIVITY ISSUES ETC!\n"
2019-10-25 01:05:47 +00:00
echo -e "Running local Ansible playbooks...\n...Stage 0 will now run\n...followed by Stages $(($STAGE + 1))-9\n...and then the Network Role.\n"
2017-11-10 01:59:17 +00:00
2019-11-14 17:01:11 +00:00
export ANSIBLE_LOG_PATH="$CWD""/iiab-install.log"
2018-07-23 21:49:34 +00:00
2019-10-31 02:38:06 +00:00
ansible -m setup -i $INVENTORY localhost --connection=local | grep python
2019-10-31 20:00:57 +00:00
ansible -m setup -i $INVENTORY localhost --connection=local >> /dev/null # So vars are recorded in /opt/iiab/iiab/iiab-install.log
2019-10-31 02:38:06 +00:00
ansible-playbook -i $INVENTORY $PLAYBOOK ${ARGS} --connection=local
2019-10-31 20:00:57 +00:00
2018-07-20 23:17:15 +00:00
echo -e "./iiab-install $* COMPLETED IN $CWD\n\n"