1
0
Fork 0
mirror of https://github.com/iiab/iiab.git synced 2025-02-12 19:22:24 +00:00
iiab/scripts/local_facts.fact

138 lines
4.6 KiB
Bash
Executable file

#!/bin/bash
# Higher-level purpose explained at the bottom of:
# https://github.com/iiab/iiab/blob/master/vars/default_vars.yml
# 2020-10-27: Most of the 10 variables require a command[*] to be run to
# 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
# If statements then use that RC to force the var to these default values...
STAGE=0
OS="none"
VERSION_ID="none" # This var's combined with the above, before being output
IIAB_BRANCH="none"
IIAB_COMMIT="none"
#XO_MODEL="none"
RPI_MODEL="none"
ANSIBLE_VERSION="none"
DHCPCD="none" # The last 3 conditioned on string output not RC. SEE BELOW.
NETWORK_MANAGER="none"
SYSTEMD_NETWORKD="none"
# STAGE variable is for ./iiab-install which runs Ansible with iiab-stages.yml
# - fresh installs start at STAGE 0
# - interrupted installs record the last completed STAGE (1-9)
#
# 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.
# /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
OS=$(echo "$tmp" | cut -d= -f2)
OS=${OS//\"/} # Remove all '"'
fi
if [ -f /etc/rpi-issue ]; then
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
if tmp=$(grep ^VERSION_ID= /etc/os-release); then
VERSION_ID=$(echo "$tmp" | cut -d= -f2)
VERSION_ID=${VERSION_ID//\"/} # Remove all '"'
VERSION_ID=${VERSION_ID%%.*} # Remove all '.' & stuff to the right of em
fi
OS_VER=$OS-$VERSION_ID
# Previously supported Linux distributions / versions:
#"fedora-18" | \
#"fedora-22" | \
#"debian-8" | \
#"debian-9" | \
#"debian-10" | \
#"ubuntu-16" | \
#"ubuntu-17" | \
#"ubuntu-18" | \
#"ubuntu-19" | \
#"centos-7" | \
#"raspbian-8" | \
#"raspbian-9" | \
# 2021-06-19: Ubuntu 21.10 (Impish Indri) not yet supported but this
# unreleased OS can help testing. For now this means MANUALLY changing
# php_version: 7.4 to 8.0 in /opt/iiab/iiab/vars/ubuntu-21.yml
# 2020-07-31: Debian 12 (Bookworm) not yet supported but adding this line
# to its /etc/os-release can help testing this upcoming OS: VERSION_ID="12"
case $OS_VER in
"debian-11" | \
"debian-12" | \
"ubuntu-20" | \
"ubuntu-21" | \
"linuxmint-20" | \
"raspbian-10" | \
"raspbian-11")
;;
*) OS_VER="OS Not Supported -- Plz Read: https://github.com/iiab/iiab/wiki/IIAB-Platforms"
;;
esac
# These next 2 help indicate what version of IIAB
tmp=$(git rev-parse --abbrev-ref HEAD) &&
IIAB_BRANCH=$tmp
tmp=$(git rev-parse --verify HEAD) &&
IIAB_COMMIT=$tmp
#tmp=$(cat /proc/device-tree/mfg-data/MN) &&
# XO_MODEL=$tmp
tmp=$(cat /proc/device-tree/model) &&
RPI_MODEL=$tmp
tmp=$(ansible --version) &&
ANSIBLE_VERSION=$(echo "$tmp" | head -1 | cut -f 2- -d " ")
# 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/.* //')
# 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
tmp=$(systemctl is-enabled dhcpcd)
[[ $tmp != "" ]] &&
DHCPCD=$tmp
#[[ $tmp ]] && DHCPCD=$tmp # Short Ain't Sweet (less understandable)
# is_redhat uses "NetworkManager". Debian 7 & Ubuntu 14.10 required
# "network-manager" (prior to 2015/systemd). Ubuntu 20.10 dropped the
# legacy symlink from "network-manager.service" to "NetworkManager.service"
tmp=$(systemctl is-enabled NetworkManager)
[[ $tmp != "" ]] &&
NETWORK_MANAGER=$tmp
tmp=$(systemctl is-enabled systemd-networkd)
[[ $tmp != "" ]] &&
SYSTEMD_NETWORKD=$tmp
cat <<EOF
{"stage" : "$STAGE",
"dhcpcd" : "$DHCPCD",
"network_manager" : "$NETWORK_MANAGER",
"systemd_networkd" : "$SYSTEMD_NETWORKD",
"iiab_branch" : "$IIAB_BRANCH",
"iiab_commit" : "$IIAB_COMMIT",
"rpi_model" : "$RPI_MODEL",
"ansible_version" : "$ANSIBLE_VERSION",
"os" : "$OS",
"os_ver" : "$OS_VER"}
EOF
#"xo_model" : "$XO_MODEL",