From cc659d0837583bbf87e3ee10b3ad00b24798c08b Mon Sep 17 00:00:00 2001 From: root Date: Wed, 21 Dec 2022 19:29:04 -0500 Subject: [PATCH] Dynamically extract php_version & python_version --- iiab-install | 9 ++-- roles/0-init/tasks/main.yml | 11 +++-- roles/jupyterhub/tasks/install.yml | 2 +- roles/lokole/tasks/install.yml | 6 +-- roles/lokole/tasks/setup.yml | 4 +- roles/lokole/templates/lokole-nginx.conf.j2 | 4 +- runrole | 55 ++++++++++++++------- scripts/local_facts.fact | 39 +++++++++------ vars/debian-11.yml | 8 +-- vars/debian-12.yml | 8 +-- vars/linuxmint-20.yml | 6 +-- vars/linuxmint-21.yml | 6 +-- vars/raspbian-11.yml | 8 +-- vars/ubuntu-2004.yml.unused | 6 +-- vars/ubuntu-2204.yml | 6 +-- vars/ubuntu-2210.yml | 6 +-- vars/ubuntu-2304.yml | 6 +-- 17 files changed, 113 insertions(+), 77 deletions(-) diff --git a/iiab-install b/iiab-install index 08dc7eed7..532cbf81d 100755 --- a/iiab-install +++ b/iiab-install @@ -65,7 +65,7 @@ if [ ! -f /etc/iiab/local_vars.yml ]; then echo -e "████████████████████████████████████████████████████████████████████████████████\n" >&2 fi - echo -e "\nEXITING: /opt/iiab/iiab/iiab-install REQUIRES /etc/iiab/local_vars.yml\n" >&2 + echo -e "\n\e[1mEXITING: /opt/iiab/iiab/iiab-install REQUIRES /etc/iiab/local_vars.yml\e[0m\n" >&2 echo -e "(1) See http://FAQ.IIAB.IO -> What is local_vars.yml and how do I customize it?" >&2 echo -e "(2) SMALL/MEDIUM/LARGE samples are included in /opt/iiab/iiab/vars" >&2 @@ -124,7 +124,7 @@ CURR_ANSIBLE_VER=0 #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" +if [[ $(command -v ansible) ]]; then # "command -v" is POSIX compliant; it catches built-in commands like "cd" CURR_ANSIBLE_VER=$(ansible --version | head -1 | cut -f 2- -d " " | sed 's/.* \([^ ]*\)\].*/\1/') # Above works with 'ansible [core 2.11.0rc2]' -- these old ways do not: #CURR_ANSIBLE_VER=$(ansible --version | head -1 | awk '{print $2}') @@ -179,14 +179,15 @@ fi # /etc/iiab/iiab_state.yml is mandatory and must be created here. Background: # Allow iiab-install to read IIAB_STATE_FILE to not repeat installs of previous # roles that already completed within the stage. -if [ ! -f $IIAB_STATE_FILE ]; then - #touch $IIAB_STATE_FILE +if [ ! -f $IIAB_STATE_FILE ]; then # touch $IIAB_STATE_FILE + echo -e "\nCreating... $IIAB_STATE_FILE" cat > $IIAB_STATE_FILE << EOF # DO *NOT* MANUALLY EDIT THIS, THANKS! # IIAB does NOT currently support uninstalling apps/services. EOF fi + echo -e "\nTRY TO RERUN './iiab-install' IF IT FAILS DUE TO CONNECTIVITY ISSUES ETC!\n" echo -e "\e[1mRunning local Ansible playbooks...\n...Stage 0 will now run\n...followed by Stages $(($STAGE + 1))-9\n...and then the Network Role.\e[0m\n" diff --git a/roles/0-init/tasks/main.yml b/roles/0-init/tasks/main.yml index 27de31b64..7a1532898 100644 --- a/roles/0-init/tasks/main.yml +++ b/roles/0-init/tasks/main.yml @@ -7,13 +7,14 @@ # Higher-level purpose explained at the bottom of: # https://github.com/iiab/iiab/blob/master/vars/default_vars.yml -- name: "Ansible just ran /etc/ansible/facts.d/local_facts.fact to set 14 vars -- here we extract 5 of those -- iiab_stage: {{ ansible_local.local_facts.stage }}, rpi_model: {{ ansible_local.local_facts.rpi_model }}, devicetree_model: {{ ansible_local.local_facts.devicetree_model }}, os_ver: {{ ansible_local.local_facts.os_ver }}, python_ver: {{ ansible_local.local_facts.python_ver }}" +- name: "Ansible just ran /etc/ansible/facts.d/local_facts.fact to set 15 vars -- here we extract 6 of those -- iiab_stage: {{ ansible_local.local_facts.stage }}, rpi_model: {{ ansible_local.local_facts.rpi_model }}, devicetree_model: {{ ansible_local.local_facts.devicetree_model }}, os_ver: {{ ansible_local.local_facts.os_ver }}, python_version: {{ ansible_local.local_facts.python_version }}, php_version: {{ ansible_local.local_facts.php_version }}" set_fact: iiab_stage: "{{ ansible_local.local_facts.stage }}" rpi_model: "{{ ansible_local.local_facts.rpi_model }}" devicetree_model: "{{ ansible_local.local_facts.devicetree_model }}" os_ver: "{{ ansible_local.local_facts.os_ver }}" - python_ver: "{{ ansible_local.local_facts.python_ver }}" + python_version: "{{ ansible_local.local_facts.python_version }}" + php_version: "{{ ansible_local.local_facts.php_version }}" # Initialize /etc/iiab/iiab.ini writing the 'location' and 'version' sections # once and only once, to preserve the install date and git hash. @@ -96,8 +97,10 @@ value: "{{ devicetree_model }}" - option: os_ver value: "{{ os_ver }}" - - option: python_ver - value: "{{ python_ver }}" + - option: python_version + value: "{{ python_version }}" + - option: php_version + value: "{{ php_version }}" - option: first_run value: "{{ first_run }}" - option: local_tz # e.g. 'EDT' (summer) or 'EST' (winter) after Ansible interprets symlink /etc/localtime -- or 'UTC' if /etc/localtime doesn't exist diff --git a/roles/jupyterhub/tasks/install.yml b/roles/jupyterhub/tasks/install.yml index d4ce6c0eb..68f909ea7 100644 --- a/roles/jupyterhub/tasks/install.yml +++ b/roles/jupyterhub/tasks/install.yml @@ -72,7 +72,7 @@ dest: /etc/systemd/system/jupyterhub.service # 2022-07-07: No longer needed, thx to upstream fixes -# - name: Install {{ jupyterhub_venv }}/bin/getsite.py from template, to fetch site_packages path, e.g. {{ jupyterhub_venv }}/lib/python{{ python_ver }}/site-packages +# - name: Install {{ jupyterhub_venv }}/bin/getsite.py from template, to fetch site_packages path, e.g. {{ jupyterhub_venv }}/lib/python{{ python_version }}/site-packages # template: # src: getsite.py.j2 # dest: "{{ jupyterhub_venv }}/bin/getsite.py" diff --git a/roles/lokole/tasks/install.yml b/roles/lokole/tasks/install.yml index 82b1c9cf4..a1ea1d357 100644 --- a/roles/lokole/tasks/install.yml +++ b/roles/lokole/tasks/install.yml @@ -55,7 +55,6 @@ extra_args: --no-cache-dir # To avoid caching issues e.g. soon after new releases hit https://pypi.org/project/opwen-email-client/ when: lokole_commit is undefined and lokole_version is undefined -# 2022-12-21: Dynamic callout to Python moved to scripts/local_facts.fact (Python 3 is now a mandatory IIAB pre-req) # - name: Compile translations # shell: | # python_version=$(python3 -c 'from sys import version_info; print("%s.%s" % (version_info.major, version_info.minor));';) @@ -63,11 +62,12 @@ # with_items: # - "{{ lokole_venv }}/lib/python${python_version}/site-packages/opwen_email_client/webapp" -- name: Compile translations for Python {{ python_ver }} +# 2022-12-21: python_version determined by scripts/local_facts.fact -- to match templates/lokole-nginx.conf.j2 +- name: Compile translations for Python {{ python_version }} shell: | {{ lokole_venv }}/bin/pybabel compile -d {{ item }}/translations with_items: - - "{{ lokole_venv }}/lib/python{{ python_ver }}/site-packages/opwen_email_client/webapp" + - "{{ lokole_venv }}/lib/python{{ python_version }}/site-packages/opwen_email_client/webapp" - name: Create system {{ lokole_user }} user ansible.builtin.user: diff --git a/roles/lokole/tasks/setup.yml b/roles/lokole/tasks/setup.yml index a8542f90b..58a5ec716 100644 --- a/roles/lokole/tasks/setup.yml +++ b/roles/lokole/tasks/setup.yml @@ -3,11 +3,11 @@ name: supervisor state: started +# 2022-12-21: python_version determined by scripts/local_facts.fact -- to match templates/lokole-nginx.conf.j2 - name: Create Lokole admin user with password, for http://box{{ lokole_url }} # http://box/lokole shell: | while read envvar; do export "$envvar"; done < {{ lokole_run_dir }}/settings.env - python_version=$(python3 -c 'from sys import version_info; print("%s.%s" % (version_info.major, version_info.minor));';) - cd {{ lokole_venv }}/lib/python${python_version}/site-packages/ + cd {{ lokole_venv }}/lib/python{{ python_version }}/site-packages/ export FLASK_APP="opwen_email_client.webapp:app" {{ lokole_venv }}/bin/flask manage createadmin --name='{{ lokole_admin_user }}' --password='{{ lokole_admin_password }}' diff --git a/roles/lokole/templates/lokole-nginx.conf.j2 b/roles/lokole/templates/lokole-nginx.conf.j2 index a2084a160..002961fcf 100644 --- a/roles/lokole/templates/lokole-nginx.conf.j2 +++ b/roles/lokole/templates/lokole-nginx.conf.j2 @@ -1,9 +1,9 @@ location = {{ lokole_url }}/favicon.ico { - alias {{ lokole_venv }}/lib/python{{ python_ver }}/site-packages/opwen_email_client/webapp/static/favicon.ico; + alias {{ lokole_venv }}/lib/python{{ python_version }}/site-packages/opwen_email_client/webapp/static/favicon.ico; } location ~ ^{{ lokole_url }}/static/(.*)$ { - alias {{ lokole_venv }}/lib/python{{ python_ver }}/site-packages/opwen_email_client/webapp/static/$1; + alias {{ lokole_venv }}/lib/python{{ python_version }}/site-packages/opwen_email_client/webapp/static/$1; } location {{ lokole_url }}/ { diff --git a/runrole b/runrole index 644665d2f..ad1387e2c 100755 --- a/runrole +++ b/runrole @@ -15,7 +15,7 @@ PLAYBOOK=run-one-role.yml if [ ! -f $PLAYBOOK ]; then echo "Exiting: IIAB Playbook not found." - echo "Please run this in /opt/iiab/iiab (top level of the git repo)." + echo "Please run this in /opt/iiab/iiab (top level of git repo)." exit 1 fi @@ -28,28 +28,48 @@ if [ $# -eq 0 ] || [ "$2" == "--reinstall" ] || [ "$3" == "--reinstall" ]; then exit 0 fi -# 2020-08-05: yes /etc/iiab/iiab_state.yml is necessary, but we DON'T -# want to encourage sloppy operators to delete/touch this file. -# -# (The iiab_state.yml file should always be created by ./iiab-install, -# for IIAB's Ansible roles that then auto-populate this file.) -# -# FYI ./iiab-network and ./iiab-configure likewise warn operators (IN RED!) -# if they try to run without the existence of /etc/iiab/iiab_state.yml : -# -# ERROR! vars file /etc/iiab/iiab_state.yml was not found -# -# Needed for Stages 1-3 if not installed yet -#if [ ! -f $IIAB_STATE_FILE ]; then -# touch $IIAB_STATE_FILE -#fi - if [ "$1" == "--reinstall" ]; then ARGS="$ARGS\"reinstall\":True," # Needs boolean not string so use JSON list REINSTALL=true shift fi + +# 4 snippets to guide -> bootstrap -> accelerate role debugging on bare OS's: + +mkdir -p /etc/iiab # -p avoids errors, effectively like '|| true' +if [ ! -f /etc/iiab/local_vars.yml ]; then + echo -e "\n\e[1mEXITING: /opt/iiab/iiab/iiab-install REQUIRES /etc/iiab/local_vars.yml\e[0m\n" >&2 + + echo -e "(1) See http://FAQ.IIAB.IO -> What is local_vars.yml and how do I customize it?" >&2 + echo -e "(2) SMALL/MEDIUM/LARGE 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 + + echo -e ' https://download.iiab.io\n' >&2 + + exit 1 +fi + +# In comparison, ./iiab-network and ./iiab-configure warn operators (IN RED) +# if run without the existence of /etc/iiab/iiab_state.yml +if [ ! -f $IIAB_STATE_FILE ]; then # touch $IIAB_STATE_FILE + echo -e "\n\e[1mCreating... $IIAB_STATE_FILE\e[0m" + cat > $IIAB_STATE_FILE << EOF +# DO *NOT* MANUALLY EDIT THIS, THANKS! +# IIAB does NOT currently support uninstalling apps/services. + +EOF +fi + +if ! [[ $(command -v ansible) ]]; then + echo -e "\n\e[1mPlease install Ansible, by running:\n\nsudo /opt/iiab/iiab/scripts/ansible\e[0m\n" + exit 1 +fi + +mkdir -p /etc/ansible/facts.d +cp scripts/local_facts.fact /etc/ansible/facts.d/local_facts.fact + + ROLE_VAR=$1 # Ansible role name & var name sometimes differ :/ if [ $1 == "calibre-web" ]; then ROLE_VAR=calibreweb @@ -105,6 +125,7 @@ if ! $INSTALL; then ARGS="$ARGS\"${ROLE_VAR}_install\":True," fi + if [ $# -eq 2 ]; then export ANSIBLE_LOG_PATH="$2" else diff --git a/scripts/local_facts.fact b/scripts/local_facts.fact index 236e08619..b78e21e43 100755 --- a/scripts/local_facts.fact +++ b/scripts/local_facts.fact @@ -13,14 +13,15 @@ STAGE=0 OS="none" VERSION_ID="none" # Temp var, for: OS_VER="$OS-$VERSION_ID" OS_VER="none" -PYTHON_VER="none" -IIAB_REMOTE_URL="none" IIAB_BRANCH="none" -IIAB_COMMIT="none" +IIAB_REMOTE_URL="none" IIAB_RECENT_TAG="none" +IIAB_COMMIT="none" RPI_MODEL="none" DEVICETREE_MODEL="none" ANSIBLE_VERSION="none" +PYTHON_VERSION="none" +PHP_VERSION="none" DHCPCD="none" # The last 3 conditioned on string output not RC. SEE BELOW. NETWORK_MANAGER="none" SYSTEMD_NETWORKD="none" @@ -90,6 +91,7 @@ case $OS_VER in ;; esac + # These next 4 help indicate what version of IIAB tmp=$(git rev-parse --abbrev-ref HEAD) && IIAB_BRANCH=$tmp @@ -102,11 +104,12 @@ tmp=$(git config branch.$IIAB_BRANCH.remote) && { fi } +tmp=$(git describe --tags --abbrev=0) && + IIAB_RECENT_TAG=$tmp + tmp=$(git rev-parse --verify HEAD) && IIAB_COMMIT=$tmp -tmp=$(git describe --tags --abbrev=0) && - IIAB_RECENT_TAG=$tmp grep -iq raspberry /proc/device-tree/model && RPI_MODEL=$(grep -ai raspberry /proc/device-tree/model | tr -d '\0') @@ -119,6 +122,7 @@ grep -iq raspberry /proc/device-tree/model && tmp=$(tr -d '\0' < /proc/device-tree/model) && DEVICETREE_MODEL=$tmp + tmp=$(ansible --version) && ANSIBLE_VERSION=$(echo "$tmp" | head -1 | cut -f 2- -d " " | sed 's/.* \([^ ]*\)\].*/\1/') # Above works with 'ansible [core 2.11.0rc2]' -- these old ways do not: @@ -126,12 +130,18 @@ tmp=$(ansible --version) && #ANSIBLE_VERSION=$(echo "$tmp" | head -1 | sed -e 's/.* //') if tmp=$(python3 -c 'from sys import version_info; print("%s.%s" % (version_info.major, version_info.minor));'); then - PYTHON_VER=$tmp + PYTHON_VERSION=$tmp 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 +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 + # 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 @@ -157,17 +167,18 @@ tmp=$(systemctl is-enabled systemd-networkd) # https://en.wikipedia.org/wiki/Here_document cat <