2017-05-27 18:09:50 +00:00
#!/bin/bash
2018-04-28 16:27:32 +00:00
2021-07-30 08:01:12 +00:00
# Higher-level purpose explained at the bottom of:
# https://github.com/iiab/iiab/blob/master/vars/default_vars.yml
2022-12-22 03:11:17 +00:00
# 2020-10-27: Most of the [15] variables require a command[*] to be run to
2020-10-19 19:10:27 +00:00
# establish the var's value. WE DISPLAY ALL ERRORS / DIAGNOSTICS AND CONTINUE.
#
# [*] DOESN'T MATTER WHAT COMMAND: so long as it fails with Return Code != 0
2022-12-22 03:11:17 +00:00
# If RC == 0, var is forced from its default value here: (using cmd's output)
2020-10-19 19:10:27 +00:00
STAGE=0
OS="none"
2022-12-22 03:11:17 +00:00
VERSION_ID="none" # Just a temp var, for: OS_VER="$OS-$VERSION_ID"
2022-12-21 18:34:30 +00:00
OS_VER="none"
2020-10-19 19:10:27 +00:00
IIAB_BRANCH="none"
2022-12-22 00:29:04 +00:00
IIAB_REMOTE_URL="none"
2022-06-25 23:53:58 +00:00
IIAB_RECENT_TAG="none"
2022-12-22 00:29:04 +00:00
IIAB_COMMIT="none"
2020-10-19 19:10:27 +00:00
RPI_MODEL="none"
2021-11-27 15:36:20 +00:00
DEVICETREE_MODEL="none"
2020-10-19 19:10:27 +00:00
ANSIBLE_VERSION="none"
2022-12-22 00:29:04 +00:00
PYTHON_VERSION="none"
PHP_VERSION="none"
2020-10-19 19:10:27 +00:00
DHCPCD="none" # The last 3 conditioned on string output not RC. SEE BELOW.
NETWORK_MANAGER="none"
SYSTEMD_NETWORKD="none"
2020-10-19 16:45:36 +00:00
2020-10-19 19:49:43 +00:00
# STAGE variable is for ./iiab-install which runs Ansible with iiab-stages.yml
2020-10-19 16:45:36 +00:00
# - fresh installs start at STAGE 0
# - interrupted installs record the last completed STAGE (1-9)
#
2020-10-19 19:49:43 +00:00
# We initialize it to '0' (zero) to cover the following 2 possibs: (1) iiab.env
# doesn't exist, or (2) iiab.env exists but fails to set STAGE=<something>
source /etc/iiab/iiab.env || true # STAGE var auto-set, so no "if" required.
2018-04-28 16:27:32 +00:00
2020-10-27 18:03:55 +00:00
# /etc/lsb-release could also be grep'd. But /etc/upstream-release/lsb-release
# on Linux Mint 20 caused grep of /etc/*elease to fail (on directory not file)
if tmp=$(grep ^ID= /etc/os-release); then
2020-10-27 21:17:40 +00:00
OS=$(echo "$tmp" | cut -d= -f2)
2020-10-19 19:10:27 +00:00
OS=${OS//\"/} # Remove all '"'
fi
2020-05-29 16:59:28 +00:00
if [ -f /etc/rpi-issue ]; then
2020-10-27 20:57:56 +00:00
OS="raspbian" # For 64-bit Raspberry Pi OS which contains "ID=debian" as
fi # of 2020: https://github.com/raspberrypi/Raspberry-Pi-OS-64bit/issues/6
2020-10-19 19:10:27 +00:00
2020-10-27 14:39:34 +00:00
if tmp=$(grep ^VERSION_ID= /etc/os-release); then
2020-10-27 21:17:40 +00:00
VERSION_ID=$(echo "$tmp" | cut -d= -f2)
2021-09-27 18:15:06 +00:00
# https://gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
VERSION_ID=${VERSION_ID//\"/} # Remove all '"'
[[ $OS == "ubuntu" ]] && # e.g. '22.04' -> '2204'
VERSION_ID=${VERSION_ID//\./} # Remove all '.'
[[ $OS == "linuxmint" ]] && # e.g. '20.2' -> '20'
VERSION_ID=${VERSION_ID%%.*} # Remove all '.' & stuff to the right
2020-10-19 19:10:27 +00:00
fi
2021-09-27 18:15:06 +00:00
OS_VER="$OS-$VERSION_ID"
2017-05-27 18:09:50 +00:00
2020-09-02 18:39:19 +00:00
# Previously supported Linux distributions / versions:
2020-09-02 02:42:11 +00:00
#"fedora-18" | \
#"fedora-22" | \
2022-12-22 02:12:02 +00:00
#"centos-7" | \
2020-09-02 02:42:11 +00:00
#"debian-8" | \
#"debian-9" | \
2021-07-31 12:58:10 +00:00
#"debian-10" | \
2020-09-02 02:42:11 +00:00
#"ubuntu-16" | \
#"ubuntu-17" | \
#"ubuntu-18" | \
#"ubuntu-19" | \
2022-12-21 18:34:30 +00:00
#"ubuntu-2004" | \
2021-12-30 23:04:23 +00:00
#"ubuntu-2104" | \
2022-07-14 13:21:16 +00:00
#"ubuntu-2110" | \
2022-12-22 02:12:02 +00:00
#"linuxmint-20" | \
2020-09-02 02:42:11 +00:00
#"raspbian-8" | \
#"raspbian-9" | \
2021-11-23 16:09:23 +00:00
#"raspbian-10" | \
2020-09-02 18:39:19 +00:00
2021-09-27 22:04:40 +00:00
# 2021-09-27: With Debian 12 (Bookworm) pre-releases, please manually add
2021-09-27 18:15:06 +00:00
# this line to its /etc/os-release before installing IIAB: VERSION_ID="12"
2021-07-31 12:58:10 +00:00
2020-09-02 18:39:19 +00:00
case $OS_VER in
2020-10-21 16:54:52 +00:00
"debian-11" | \
2021-07-31 12:58:10 +00:00
"debian-12" | \
2021-09-27 18:15:06 +00:00
"ubuntu-2204" | \
2022-07-14 13:21:16 +00:00
"ubuntu-2210" | \
2022-10-28 17:55:43 +00:00
"ubuntu-2304" | \
2023-04-06 12:14:48 +00:00
"ubuntu-2310" | \
2021-09-27 12:43:23 +00:00
"linuxmint-21" | \
2023-04-04 21:22:25 +00:00
"raspbian-11" | \
"raspbian-12")
2021-09-27 19:14:09 +00:00
;;
2021-11-12 04:17:05 +00:00
*) echo -e "\n\e[41;1mOS '$OS_VER' IS NOT SUPPORTED. Please read:\e[0m\n\n\e[1mhttps://github.com/iiab/iiab/wiki/IIAB-Platforms\e[0m\n" ; exit 1 # Used by /opt/iiab/iiab/iiab-install
2021-09-27 19:14:09 +00:00
;;
2017-07-04 20:07:47 +00:00
esac
2020-08-20 16:51:17 +00:00
2022-12-22 00:29:04 +00:00
2022-06-29 16:50:11 +00:00
# These next 4 help indicate what version of IIAB
2020-10-19 19:10:27 +00:00
tmp=$(git rev-parse --abbrev-ref HEAD) &&
IIAB_BRANCH=$tmp
2020-10-19 16:45:36 +00:00
2022-07-06 06:39:29 +00:00
tmp=$(git config branch.$IIAB_BRANCH.remote) && {
if [[ $tmp =~ ^"https://" ]]; then
IIAB_REMOTE_URL=$tmp
else
IIAB_REMOTE_URL=$(git config remote.$tmp.url)
fi
}
2022-06-29 16:50:11 +00:00
2022-12-22 00:29:04 +00:00
tmp=$(git describe --tags --abbrev=0) &&
IIAB_RECENT_TAG=$tmp
2020-10-19 19:10:27 +00:00
tmp=$(git rev-parse --verify HEAD) &&
IIAB_COMMIT=$tmp
2017-05-27 18:09:50 +00:00
2022-01-01 02:50:51 +00:00
grep -iq raspberry /proc/device-tree/model &&
RPI_MODEL=$(grep -ai raspberry /proc/device-tree/model | tr -d '\0')
2020-03-04 23:02:01 +00:00
2021-11-27 15:36:20 +00:00
# /proc/device-tree/model e.g. 'Parallels ARM Virtual Machine' identical to...
# /sys/firmware/devicetree/base/model (also true on RPi hardware!)
2021-12-28 00:58:42 +00:00
# tr -d '\0' ...strips out its null byte, for cleaner output (PR #3086)
2021-12-26 22:14:43 +00:00
tmp=$(tr -d '\0' < /proc/device-tree/model) &&
2021-11-27 15:36:20 +00:00
DEVICETREE_MODEL=$tmp
2022-12-22 00:29:04 +00:00
2020-10-19 19:10:27 +00:00
tmp=$(ansible --version) &&
2021-11-12 04:17:05 +00:00
ANSIBLE_VERSION=$(echo "$tmp" | head -1 | cut -f 2- -d " " | sed 's/.* \([^ ]*\)\].*/\1/')
2021-04-13 16:14:27 +00:00
# Above works with 'ansible [core 2.11.0rc2]' -- these old ways do not:
#ANSIBLE_VERSION=$(echo "$tmp" | head -1 | awk '{print $2}')
#ANSIBLE_VERSION=$(echo "$tmp" | head -1 | sed -e 's/.* //')
2020-10-19 19:10:27 +00:00
2022-12-21 18:34:30 +00:00
if tmp=$(python3 -c 'from sys import version_info; print("%s.%s" % (version_info.major, version_info.minor));'); then
2022-12-22 00:29:04 +00:00
PYTHON_VERSION=$tmp
2022-12-21 18:34:30 +00:00
else
echo -e "\e[1m\nPython 3 is REQUIRED for Internet-in-a-Box. You might want to run:\n\nsudo apt install python3\n\e[0m"
exit 1
fi
2022-12-22 00:29:04 +00:00
tmp=$(apt list php) &&
PHP_VERSION=$(echo $tmp | grep -Po '[0-9]+\.[0-9]+' | head -1)
# Extracts the first (topmost, leftmost) MAJOR.MINOR, even if not yet installed
# Safer than: echo $tmp | grep php | head -1 | sed 's/.*://; s/[^0-9.].*//')
# https://stackoverflow.com/questions/16675179/how-to-use-sed-to-extract-substring/16675391#16675391
2020-10-19 19:29:46 +00:00
2021-01-30 00:03:28 +00:00
# THE LAST 3 BELOW ARE DIFFERENT as "systemctl is-enabled" unhelpfully returns
# the same error code (i.e. 1) REGARDLESS whether the service is (A) disabled
# or (B) doesn't exist. SO WE TEST THE STRING OUTPUT INSTEAD OF THE RETURN CODE
2020-10-19 19:10:27 +00:00
tmp=$(systemctl is-enabled dhcpcd)
2020-10-19 19:29:46 +00:00
[[ $tmp != "" ]] &&
DHCPCD=$tmp
#[[ $tmp ]] && DHCPCD=$tmp # Short Ain't Sweet (less understandable)
2017-11-19 03:08:11 +00:00
2020-10-27 11:53:28 +00:00
# is_redhat uses "NetworkManager". Debian 7 & Ubuntu 14.10 required
# "network-manager" (prior to 2015/systemd). Ubuntu 20.10 dropped the
2020-10-27 14:52:11 +00:00
# legacy symlink from "network-manager.service" to "NetworkManager.service"
2020-10-27 11:53:28 +00:00
tmp=$(systemctl is-enabled NetworkManager)
2020-10-19 19:29:46 +00:00
[[ $tmp != "" ]] &&
NETWORK_MANAGER=$tmp
2020-10-19 16:45:36 +00:00
2020-10-19 19:10:27 +00:00
tmp=$(systemctl is-enabled systemd-networkd)
2020-10-19 19:29:46 +00:00
[[ $tmp != "" ]] &&
SYSTEMD_NETWORKD=$tmp
2020-10-19 16:45:36 +00:00
2017-11-23 02:38:46 +00:00
2021-09-28 03:31:18 +00:00
# https://en.wikipedia.org/wiki/Here_document
2017-05-27 18:09:50 +00:00
cat <<EOF
2020-10-27 13:27:21 +00:00
{"stage" : "$STAGE",
2022-12-22 00:29:04 +00:00
"os" : "$OS",
"os_ver" : "$OS_VER",
2020-10-19 19:10:27 +00:00
"iiab_branch" : "$IIAB_BRANCH",
2022-12-22 00:29:04 +00:00
"iiab_remote_url" : "$IIAB_REMOTE_URL",
2022-06-25 23:53:58 +00:00
"iiab_recent_tag" : "$IIAB_RECENT_TAG",
2022-12-22 00:29:04 +00:00
"iiab_commit" : "$IIAB_COMMIT",
2020-10-19 19:10:27 +00:00
"rpi_model" : "$RPI_MODEL",
2021-11-27 15:36:20 +00:00
"devicetree_model" : "$DEVICETREE_MODEL",
2017-11-10 19:01:22 +00:00
"ansible_version" : "$ANSIBLE_VERSION",
2022-12-22 00:29:04 +00:00
"python_version" : "$PYTHON_VERSION",
"php_version" : "$PHP_VERSION",
"dhcpcd" : "$DHCPCD",
"network_manager" : "$NETWORK_MANAGER",
"systemd_networkd" : "$SYSTEMD_NETWORKD"}
2017-05-27 18:09:50 +00:00
EOF