From bec2d22cf8b0807193184443ec8da1a5e455a78a Mon Sep 17 00:00:00 2001 From: billz Date: Wed, 16 Dec 2020 10:01:11 +0000 Subject: [PATCH 1/6] Tidy install loader, usage notes readability + examples --- installers/raspbian.sh | 179 ++++++++++++++++++++++------------------- 1 file changed, 95 insertions(+), 84 deletions(-) diff --git a/installers/raspbian.sh b/installers/raspbian.sh index 9118b022..24d8f665 100755 --- a/installers/raspbian.sh +++ b/installers/raspbian.sh @@ -2,29 +2,24 @@ # # RaspAP Quick Installer # Author: @billz +# Author URI: https://github.com/billz/ # License: GNU General Public License v3.0 +# License URI: https://github.com/billz/raspap-webgui/blob/master/LICENSE +# +# Usage: raspbian.sh options # # Installs an instance of RaspAP. # -# Available options: -# -y, --yes, --assume-yes -# Assume "yes" as answer to all prompts and run non-interactively -# -c, --cert, --certficate -# Installs mkcert and generates an SSL certificate for lighttpd -# -o, --openvpn -# Used with -y, --yes, sets OpenVPN install option (0=no install) -# -a, --adblock -# Used with -y, --yes, sets Adblock install option (0=no install) -# -r, --repo, --repository -# Overrides the default GitHub repo (billz/raspap-webgui) -# -b, --branch -# Overrides the default git branch (master) -# -h, --help -# Outputs usage notes and exits -# -u, --upgrade -# Upgrades an existing installation to the latest release version -# -v, --version -# Outputs release info and exits +# OPTIONS: +# -y, --yes, --assume-yes Assume "yes" as answer to all prompts and run non-interactively +# -c, --cert, --certficate Installs mkcert and generates an SSL certificate for lighttpd +# -o, --openvpn Used with -y, --yes, sets OpenVPN install option (0=no install) +# -a, --adblock Used with -y, --yes, sets Adblock install option (0=no install) +# -r, --repo, --repository Overrides the default GitHub repo (billz/raspap-webgui) +# -b, --branch Overrides the default git branch (master) +# -h, --help Outputs usage notes and exits +# -u, --upgrade Upgrades an existing installation to the latest release version +# -v, --version Outputs release info and exits # # Depending on options passed to the installer, ONE of the following # additional shell scripts will be downloaded and sourced: @@ -36,54 +31,19 @@ # You are not obligated to bundle the LICENSE file with your RaspAP projects as long # as you leave these references intact in the header comments of your source files. -# Fetch latest release from GitHub API -repo="billz/raspap-webgui" #override with -r, --repo option -readonly RASPAP_LATEST=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' ) -branch="$RASPAP_LATEST" #override with -b, --branch option +set -eo pipefail +readonly PROGNAME=$(basename $0) -function _setup_colors() { - ANSI_RED="\033[0;31m" - ANSI_GREEN="\033[0;32m" - ANSI_YELLOW="\033[0;33m" - ANSI_RASPBERRY="\033[0;35m" - ANSI_ERROR="\033[1;37;41m" - ANSI_RESET="\033[m" -} +function _main() { + # set defaults + repo="billz/raspap-webgui" # override with -r, --repo option + branch="$RASPAP_LATEST" # override with -b, --branch option -function _log_output() { - readonly LOGFILE_PATH="/tmp" - exec > >(tee -i $LOGFILE_PATH/raspap_install.log) - exec 2>&1 -} - -function _usage() { - cat << EOF -Usage: $(basename "$0") [OPTION] - -Installs an instance of RaspAP. - -Available options: --y, --yes, --assume-yes - Assumes "yes" as an answer to all prompts --c, --cert, --certificate - Installs an SSL certificate for lighttpd --o, --openvpn - Used with -y, --yes, sets OpenVPN install option (0=no install) --a, --adblock - Used with -y, --yes, sets Adblock install option (0=no install) --r, --repo, --repository - Overrides the default GitHub repo (billz/raspap-webgui) --b, --branch - Overrides the default git branch (latest release) --h, --help - Outputs usage notes and exits --u, --upgrade - Upgrades an existing installation to the latest release version --v, --version - Outputs release info and exits - -EOF - exit + _parse_params "$@" + _get_release + _setup_colors + _log_output + _load_installer } function _parse_params() { @@ -94,7 +54,7 @@ function _parse_params() { adblock_option=1 while :; do - case ${1} in + case "${1-}" in -y|--yes|--assume-yes) assume_yes=1 apt_option="-y" @@ -140,16 +100,57 @@ function _parse_params() { done } -function _version() { - echo -e "RaspAP v${RASPAP_LATEST} - Simple AP setup & WiFi management for Debian-based devices" +function _setup_colors() { + ANSI_RED="\033[0;31m" + ANSI_GREEN="\033[0;32m" + ANSI_YELLOW="\033[0;33m" + ANSI_RASPBERRY="\033[0;35m" + ANSI_ERROR="\033[1;37;41m" + ANSI_RESET="\033[m" +} + +function _log_output() { + readonly LOGFILE_PATH="/tmp" + exec > >(tee -i $LOGFILE_PATH/raspap_install.log) + exec 2>&1 +} + +function _usage() { + cat << EOF +Usage: $PROGNAME options + +Installs an instance of RaspAP. + +OPTIONS: +-y, --yes, --assume-yes Assumes "yes" as an answer to all prompts +-c, --cert, --certificate Installs an SSL certificate for lighttpd +-o, --openvpn Used with -y, --yes, sets OpenVPN install option (0=no install) +-a, --adblock Used with -y, --yes, sets Adblock install option (0=no install) +-r, --repo, --repository Overrides the default GitHub repo (billz/raspap-webgui) +-b, --branch Overrides the default git branch (latest release) +-u, --upgrade Upgrades an existing installation to the latest release version +-v, --version Outputs release info and exits +-h, --help Outputs usage notes and exits + +Examples: + Run locally specifying GitHub repo and branch: + $PROGNAME --repo foo/bar --branch my/branch + + Run locally requesting release info: + $PROGNAME --version + + Invoke installer remotely, run non-interactively with option flags: + curl -sL https://install.raspap.com | bash -s -- --yes --openvpn 1 --adblock 0 + +EOF exit } -_parse_params "$@" -_setup_colors -_log_output - -UPDATE_URL="https://raw.githubusercontent.com/$repo/$branch/" +function _version() { + _get_release + echo -e "RaspAP v${RASPAP_LATEST} - Simple AP setup & WiFi management for Debian-based devices" + exit +} # Outputs a welcome message function _display_welcome() { @@ -166,6 +167,11 @@ function _display_welcome() { echo -e "The Quick Installer will guide you through a few easy steps${ANSI_RESET}\n\n" } +# Fetch latest release from GitHub API +function _get_release() { + readonly RASPAP_LATEST=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' ) +} + # Outputs a RaspAP Install log line function _install_log() { echo -e "${ANSI_GREEN}RaspAP Install: $1${ANSI_RESET}" @@ -197,15 +203,20 @@ function _update_system_packages() { } # Fetch required installer functions -if [ "${install_cert:-}" = 1 ]; then - source="mkcert" - wget -q ${UPDATE_URL}installers/${source}.sh -O /tmp/raspap_${source}.sh - source /tmp/raspap_${source}.sh && rm -f /tmp/raspap_${source}.sh - _install_certificate || _install_status 1 "Unable to install certificate" -else - source="common" - wget -q ${UPDATE_URL}installers/${source}.sh -O /tmp/raspap_${source}.sh - source /tmp/raspap_${source}.sh && rm -f /tmp/raspap_${source}.sh - _install_raspap || _install_status 1 "Unable to install RaspAP" -fi +function _load_installer() { + UPDATE_URL="https://raw.githubusercontent.com/$repo/$branch/" + if [ "${install_cert:-}" = 1 ]; then + source="mkcert" + wget -q ${UPDATE_URL}installers/${source}.sh -O /tmp/raspap_${source}.sh + source /tmp/raspap_${source}.sh && rm -f /tmp/raspap_${source}.sh + _install_certificate || _install_status 1 "Unable to install certificate" + else + source="common" + wget -q ${UPDATE_URL}installers/${source}.sh -O /tmp/raspap_${source}.sh + source /tmp/raspap_${source}.sh && rm -f /tmp/raspap_${source}.sh + _install_raspap || _install_status 1 "Unable to install RaspAP" + fi +} + +_main "$@" From 3771ef5b6a789df9b43943b960bbf5d4061b80a2 Mon Sep 17 00:00:00 2001 From: billz Date: Wed, 16 Dec 2020 10:11:50 +0000 Subject: [PATCH 2/6] Minor: installer usage notes --- installers/raspbian.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/installers/raspbian.sh b/installers/raspbian.sh index 24d8f665..8f1b262b 100755 --- a/installers/raspbian.sh +++ b/installers/raspbian.sh @@ -32,7 +32,6 @@ # as you leave these references intact in the header comments of your source files. set -eo pipefail -readonly PROGNAME=$(basename $0) function _main() { # set defaults @@ -117,7 +116,7 @@ function _log_output() { function _usage() { cat << EOF -Usage: $PROGNAME options +Usage: raspbian.sh options Installs an instance of RaspAP. @@ -134,10 +133,10 @@ OPTIONS: Examples: Run locally specifying GitHub repo and branch: - $PROGNAME --repo foo/bar --branch my/branch + raspbian.sh --repo foo/bar --branch my/branch Run locally requesting release info: - $PROGNAME --version + raspbian.sh --version Invoke installer remotely, run non-interactively with option flags: curl -sL https://install.raspap.com | bash -s -- --yes --openvpn 1 --adblock 0 From 237d56109080617416ab9a18438052a634c68750 Mon Sep 17 00:00:00 2001 From: billz Date: Wed, 16 Dec 2020 10:44:30 +0000 Subject: [PATCH 3/6] Define branch after get_release --- installers/raspbian.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/installers/raspbian.sh b/installers/raspbian.sh index 8f1b262b..b7948392 100755 --- a/installers/raspbian.sh +++ b/installers/raspbian.sh @@ -36,10 +36,9 @@ set -eo pipefail function _main() { # set defaults repo="billz/raspap-webgui" # override with -r, --repo option - branch="$RASPAP_LATEST" # override with -b, --branch option - - _parse_params "$@" _get_release + branch="$RASPAP_LATEST" # override with -b, --branch option + _parse_params "$@" _setup_colors _log_output _load_installer From 64ee7b4f3d4d83f9dc0d105ac38a434449dd12c6 Mon Sep 17 00:00:00 2001 From: billz Date: Wed, 16 Dec 2020 10:47:45 +0000 Subject: [PATCH 4/6] Fix: Update raspap_latest to non-readonly --- installers/raspbian.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/installers/raspbian.sh b/installers/raspbian.sh index b7948392..9e6c505f 100755 --- a/installers/raspbian.sh +++ b/installers/raspbian.sh @@ -167,7 +167,7 @@ function _display_welcome() { # Fetch latest release from GitHub API function _get_release() { - readonly RASPAP_LATEST=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' ) + RASPAP_LATEST=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' ) } # Outputs a RaspAP Install log line From fa8c6548a58f95e2dbc2d67562cd5d9847edf262 Mon Sep 17 00:00:00 2001 From: billz Date: Wed, 16 Dec 2020 11:03:30 +0000 Subject: [PATCH 5/6] update get_release --- installers/raspbian.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/installers/raspbian.sh b/installers/raspbian.sh index 9e6c505f..0611f14e 100755 --- a/installers/raspbian.sh +++ b/installers/raspbian.sh @@ -37,7 +37,7 @@ function _main() { # set defaults repo="billz/raspap-webgui" # override with -r, --repo option _get_release - branch="$RASPAP_LATEST" # override with -b, --branch option + branch=$RASPAP_LATEST # override with -b, --branch option _parse_params "$@" _setup_colors _log_output @@ -167,7 +167,7 @@ function _display_welcome() { # Fetch latest release from GitHub API function _get_release() { - RASPAP_LATEST=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' ) + readonly RASPAP_LATEST=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | grep -Po '"tag_name": "\K.*?(?=")' ) } # Outputs a RaspAP Install log line From 87e2d812a79b52ad2969569054fb8c1893f33a8b Mon Sep 17 00:00:00 2001 From: billz Date: Wed, 16 Dec 2020 11:35:15 +0000 Subject: [PATCH 6/6] Tidy installer + common functions --- installers/common.sh | 46 ++++++++++++++++++++++-------------------- installers/raspbian.sh | 11 ++++++++-- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/installers/common.sh b/installers/common.sh index ce5adcb1..77392501 100755 --- a/installers/common.sh +++ b/installers/common.sh @@ -2,8 +2,10 @@ # # RaspAP installation functions # Author: @billz +# Author URI: https://github.com/billz/ # License: GNU General Public License v3.0 -# +# License URI: https://github.com/billz/raspap-webgui/blob/master/LICENSE + # You are not obligated to bundle the LICENSE file with your RaspAP projects as long # as you leave these references intact in the header comments of your source files. @@ -27,6 +29,27 @@ webroot_dir="/var/www/html" git_source_url="https://github.com/$repo" # $repo from install.raspap.com # NOTE: all the below functions are overloadable for system-specific installs +function _install_raspap() { + _display_welcome + _config_installation + _update_system_packages + _install_dependencies + _enable_php_lighttpd + _create_raspap_directories + _optimize_php + _check_for_old_configs + _download_latest_files + _change_file_ownership + _create_hostapd_scripts + _create_lighttpd_scripts + _move_config_file + _default_configuration + _configure_networking + _prompt_install_adblock + _prompt_install_openvpn + _patch_system_files + _install_complete +} # Prompts user to set installation options function _config_installation() { @@ -550,24 +573,3 @@ function _install_complete() { fi } -function _install_raspap() { - _display_welcome - _config_installation - _update_system_packages - _install_dependencies - _enable_php_lighttpd - _create_raspap_directories - _optimize_php - _check_for_old_configs - _download_latest_files - _change_file_ownership - _create_hostapd_scripts - _create_lighttpd_scripts - _move_config_file - _default_configuration - _configure_networking - _prompt_install_adblock - _prompt_install_openvpn - _patch_system_files - _install_complete -} diff --git a/installers/raspbian.sh b/installers/raspbian.sh index 0611f14e..0cdf4514 100755 --- a/installers/raspbian.sh +++ b/installers/raspbian.sh @@ -36,8 +36,7 @@ set -eo pipefail function _main() { # set defaults repo="billz/raspap-webgui" # override with -r, --repo option - _get_release - branch=$RASPAP_LATEST # override with -b, --branch option + _parse_params "$@" _setup_colors _log_output @@ -202,6 +201,14 @@ function _update_system_packages() { # Fetch required installer functions function _load_installer() { + # fetch latest release tag + _get_release + + # assign default branch if not defined with -b, --branch option + if [ -z ${branch} ]; then + branch=$RASPAP_LATEST + fi + UPDATE_URL="https://raw.githubusercontent.com/$repo/$branch/" if [ "${install_cert:-}" = 1 ]; then source="mkcert"