Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1-line-installer: facilitate testing w/ instant install of CLI-specified PR's e.g. 'sudo iiab 361 2607' #158

Merged
merged 24 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
503db57
Merge pull request #28 from iiab/master
holta Oct 31, 2020
5197884
1-line install: facilitate testing w/ ad hoc PR's e.g. sudo iiab 2607
holta Oct 31, 2020
05382c1
Allow for: 'curl d.iiab.io/install.txt | sudo bash -s 2604 2607'
holta Nov 1, 2020
ca37f64
1-line-installer: basic code cleanup
holta Nov 1, 2020
8f37fb6
2 PR POTLUCK test branches might be used in future?
holta Nov 2, 2020
2e7e722
Did PR's change yr local_vars.yml ? If likely, request nano editing
holta Nov 2, 2020
631dbd9
Update install.txt
holta Nov 2, 2020
e9e60f2
Remove "--depth 1" so more PR's auto-merge, set stub email address fo…
holta Nov 2, 2020
183efde
Update install.txt
holta Nov 2, 2020
f85f84a
apt install only when nec, to avoid UX clutter/delays
holta Nov 2, 2020
d2d54a8
Line breaks for code readability
holta Nov 2, 2020
8aaa524
Update install.txt
holta Nov 2, 2020
a015a35
Explain git email address, required by git pull
holta Nov 2, 2020
1f7ab26
Update install.txt
holta Nov 2, 2020
50aa857
Indent conditionals for readability
holta Nov 2, 2020
85dac9f
Update install.txt
holta Nov 2, 2020
85a62e3
Cleaner UX: hide "mv: cannot stat '/usr/sbin/iiab': No such file or d…
holta Nov 2, 2020
a800362
Safer CLI params (PR #s, to pass on to /usr/sbin/iiab)
holta Nov 2, 2020
fe494c7
/usr/sbin/iiab param handling: "$@" array, safer than $* string
holta Nov 2, 2020
b0a3e63
/etc/iiab/pr-queue so 'sudo iiab' applies PR's after reboots/nano/cra…
holta Nov 2, 2020
ac39644
Update install.txt
holta Nov 2, 2020
1aac0d4
Rename /etc/iiab/pr-list to /etc/iiab/pr-list-pulled
holta Nov 2, 2020
6ee8957
Explain /etc/iiab/pr-queue
holta Nov 2, 2020
8426669
Update install.txt
holta Nov 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 141 additions & 39 deletions iiab
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@

set -e # Exit on error (avoids snowballing)
export DEBIAN_FRONTEND=noninteractive # Bypass (most!) interactive questions
BASEDIR=/opt/iiab
CONFDIR=/etc/iiab
FLAGDIR=$CONFDIR/install-flags
FLAGDIR=/etc/iiab/install-flags
APTPATH=/usr/bin # Avoids problematic /usr/local/bin/apt on Linux Mint

[ $(id -un) = root ] || { echo "Please run 'sudo iiab'" ; exit 1 ; }

mkdir -p $FLAGDIR # Whih also creates /etc/iiab
# Save CLI params (PR #s) for section I. so 'sudo iiab' continues after reboots
[ $# != 0 ] && printf '%s\n' "$@" >> /etc/iiab/pr-queue

# A. Subroutine for B. and D. Returns true (0) if username ($1) exists with password ($2)
command -v grep &> /dev/null || $APTPATH/apt -y install grep # 3 MORE BELOW!
command -v grep &> /dev/null ||
$APTPATH/apt -y install grep # AVOID UX CLUTTER+DELAYS: 4 MORE BELOW!
check_user_pwd() {
# $meth (hashing method) is typically '6' which implies 5000 rounds
# of SHA-512 per /etc/login.defs -> /etc/pam.d/common-password
Expand All @@ -65,7 +68,7 @@ check_user_pwd() {
}

# B. Ask for password change if pi/raspberry default remains
if check_user_pwd "pi" "raspberry" ; then
if check_user_pwd "pi" "raspberry"; then
echo -e "\n\nRaspberry Pi's are COMPROMISED often if the default password is not changed!\n"

echo -n "What password do you want for GNU/Linux user 'pi' ? "
Expand Down Expand Up @@ -94,7 +97,7 @@ if ! grep -q '^iiab_admin_user_install:\s\+[fF]alse\b' /etc/iiab/local_vars.yml
fi

# D. If 'iiab-admin' (or equivalent) use the default password, prompt for change
if check_user_pwd "$IIAB_ADMIN_USER" "$IIAB_ADMIN_PUBLISHED_PWD" ; then
if check_user_pwd "$IIAB_ADMIN_USER" "$IIAB_ADMIN_PUBLISHED_PWD"; then
echo -e "\n\nUser '$IIAB_ADMIN_USER' retains default password '$IIAB_ADMIN_PUBLISHED_PWD' per http://FAQ.IIAB.IO\n"

echo -e "This is for login to Internet-in-a-Box's Admin Console (http://box.lan/admin)"
Expand All @@ -105,28 +108,29 @@ if check_user_pwd "$IIAB_ADMIN_USER" "$IIAB_ADMIN_PUBLISHED_PWD" ; then
echo "$IIAB_ADMIN_USER":"$ans" | chpasswd || true # Overrides 'set -e'
fi

mkdir -p $FLAGDIR
if [ -f $FLAGDIR/iiab-complete ]; then
echo -e "\n\nIIAB INSTALLATION (/usr/sbin/iiab) IS ALREADY COMPLETE -- per existence of:"
echo -e "$FLAGDIR/iiab-complete -- nothing to do.\n"
exit 0
fi

# E. Position & customize $CONFDIR/local_vars.yml
command -v wget &> /dev/null || $APTPATH/apt -y install wget # AS ABOVE (grep)
command -v nano &> /dev/null || $APTPATH/apt -y install nano # AND BELOW (git)
cd $CONFDIR
if [ -f local_vars.yml ]; then
# E. Position & customize /etc/iiab/local_vars.yml
command -v wget &> /dev/null ||
$APTPATH/apt -y install wget # AS ABOVE (grep)
command -v nano &> /dev/null ||
$APTPATH/apt -y install nano # BELOW (git, jq)
local_vars_src_file=local_vars_big.yml # Testing default, for section I. (PR's can change local_vars.yml templates)
if [ -f /etc/iiab/local_vars.yml ]; then

# FUTURE: Test if their local_vars.yml is sufficiently version-compatible !

echo -e "\n\n EXISTING $CONFDIR/local_vars.yml is being used to install Internet-in-a-Box\n"
echo -e "\n\n EXISTING /etc/iiab/local_vars.yml is being used to install Internet-in-a-Box\n"

echo -e " 🚂 🚃 🚄 🚅 🚆 🚇 🚈 🚉 🚊 🚋 🚌 🚍 🚎 🚏 🚐 🚑 🚒 🚚 🚛 🚜 🚞 🚟 🚠 🚡 🚲\n"

echo -e " Google 'local_vars.yml' to learn more!"
else
echo -e "\n\nInstalling Internet-in-a-Box requires $CONFDIR/local_vars.yml\n"
echo -e "\n\nInstalling Internet-in-a-Box requires /etc/iiab/local_vars.yml\n"

echo -e "Do you want (1) 🚵 MIN-sized (2) 🚢🚣 MEDIUM-sized or (3) 🚂🚃🚃 BIG-sized?"
echo -e "Or (m) for 🚑 MEDICAL? YOU NEED 2 GB RAM OR HIGHER, IF YOU CHOOSE (3)\n"
Expand All @@ -142,23 +146,28 @@ else
echo
case $local_vars_size in
1)
wget -O local_vars.yml https://github.com/iiab/iiab/raw/master/vars/local_vars_min.yml
local_vars_src_file=local_vars_min.yml
;;
3)
wget -O local_vars.yml https://github.com/iiab/iiab/raw/master/vars/local_vars_big.yml
local_vars_src_file=local_vars_big.yml
;;
m)
wget -O local_vars.yml https://github.com/iiab/iiab/raw/master/vars/local_vars_medical.yml
local_vars_src_file=local_vars_medical.yml
;;
*)
wget -O local_vars.yml https://github.com/iiab/iiab/raw/master/vars/local_vars_medium.yml
local_vars_src_file=local_vars_medium.yml
;;
esac
if [ -d /opt/iiab/iiab/vars ]; then
cp /opt/iiab/iiab/vars/$local_vars_src_file /etc/iiab/local_vars.yml
else
wget -O /etc/iiab/local_vars.yml https://github.com/iiab/iiab/raw/master/vars/$local_vars_src_file
fi

echo -en "\nEdit $CONFDIR/local_vars.yml to customize your Internet-in-a-Box? [Y/n] "
echo -en "\nEdit /etc/iiab/local_vars.yml to customize your Internet-in-a-Box? [Y/n] "
read ans < /dev/tty
if [ "$ans" != "n" ] && [ "$ans" != "N" ]; then
echo -e "\n1) PLEASE RUN: sudo nano $CONFDIR/local_vars.yml\n"
echo -e "\n1) PLEASE RUN: sudo nano /etc/iiab/local_vars.yml\n"

echo -e "2) After you're done editing, RUN 'sudo iiab' TO CONTINUE!\n"
exit 0
Expand All @@ -178,7 +187,7 @@ echo -e "\n\n'apt update' is checking for OS updates...\n"
#apt -y update || true # Overrides 'set -e'
#echo -e "\nNOW THE REAL 'apt update' WILL RUN...\n"
$APTPATH/apt -qq update > /tmp/apt.stdout 2> /tmp/apt.stderr || true # Overrides 'set -e'
if [ $(wc -c < /tmp/apt.stderr) -gt 82 ]; then # apt.stderr typically contains exactly 82 characters when there are no errors, no matter the primary locale, i.e. 3-line file "\nWARNING: apt does not have a stable CLI interface. Use with caution in scripts.\n\n" ...OR... in other cases more than 82, e.g. many lines of errors when apt is busy/locked/offline/etc
if (( $(wc -c < /tmp/apt.stderr) > 82 )); then # apt.stderr typically contains exactly 82 characters when there are no errors, no matter the primary locale, i.e. 3-line file "\nWARNING: apt does not have a stable CLI interface. Use with caution in scripts.\n\n" ...OR... in other cases more than 82, e.g. many lines of errors when apt is busy/locked/offline/etc
echo -e "'apt update' FAILED. VERIFY YOU'RE ONLINE and resolve all errors below:\n"
cat /tmp/apt.stderr
exit 1
Expand All @@ -198,44 +207,137 @@ cat /tmp/apt.stdout # "All packages are up to date.\n"
echo -ne "\nHit [ENTER] to confirm you'll TRY TO RERUN 'sudo iiab' IF THERE IS A PROBLEM: "
read ans < /dev/tty

######################### INTERACTIVE STUFF IS ABOVE #########################
####################### MAIN INTERACTIVE STUFF IS ABOVE #######################

# G. If RPi, lower reserve disk space from ~5% to 2%
# G. If microSD, lower reserve disk space from ~5% to 2%
#if [ -f /proc/device-tree/model ] && grep -qi raspberry /proc/device-tree/model; then
if [ -e /dev/mmcblk0p2 ]; then
echo -e "\n\nFound microSD card /dev/mmcblk0p2: Lower its reserve disk space from ~5% to 2%\n"
tune2fs -m 2 /dev/mmcblk0p2
fi

# H. Clone 3 IIAB repos
echo -e "\n\nDOWNLOAD (CLONE) IIAB'S 3 KEY REPOS INTO $BASEDIR ...\n"
$APTPATH/apt -y install git # grep, wget, nano ABOVE -- AVOID UX CLUTTER+DELAYS
mkdir -p $BASEDIR
cd $BASEDIR
echo -e "\n\nDOWNLOAD (CLONE) IIAB'S 3 KEY REPOS INTO /opt/iiab ..."
command -v git &> /dev/null ||
$APTPATH/apt -y install git # AVOID UX CLUTTER+DELAYS
mkdir -p /opt/iiab
cd /opt/iiab
echo
if [ -d iiab ]; then
echo -e "REPO EXISTS? Consider 'cd /opt/iiab/iiab; git pull'"
else
git clone https://github.com/iiab/iiab --depth 1
git clone https://github.com/iiab/iiab
fi
echo
if [ -d iiab-admin-console ]; then
echo -e "REPO EXISTS? Consider 'cd /opt/iiab/iiab-admin-console; git pull'"
else
git clone https://github.com/iiab/iiab-admin-console --depth 1
git clone https://github.com/iiab/iiab-admin-console
fi
echo
if [ -d iiab-factory ]; then
echo -e "REPO EXISTS? Consider 'cd /opt/iiab/iiab-factory; git pull'"
else
git clone https://github.com/iiab/iiab-factory --depth 1
git clone https://github.com/iiab/iiab-factory
fi

# I. Install optional PR's
echo -e "\n\nINSTALL PR's FROM iiab/iiab OR iiab/iiab-admin-console -- USAGE EXAMPLES:\n"
echo -e " sudo iiab 361 2604 2607"
echo -e " curl d.iiab.io/install.txt | sudo bash -s 361 2604 2607"
command -v jq &> /dev/null ||
$APTPATH/apt -y install jq # sed to avoid delays?
if [ $# != 0 ]; then # Stub email for 'git pull' if one isn't already set
git config --get user.email 1> /dev/null ||
git config --global user.email "[email protected]"
echo -e "\nYour git email is set to: $(git config --get user.email)"
echo -e 'Change this by running: sudo git config --global user.email "[email protected]"'
fi

cp /opt/iiab/iiab/vars/$local_vars_src_file /tmp
# Similar to 'for pr in "$@"' but survives reboots:
while [ -s /etc/iiab/pr-queue ] # Test that file exists and is non-empty
do
pr=$(head -1 /etc/iiab/pr-queue) # Try the next PR, from top of queue
if ! [[ "$pr" =~ ^[1-9][0-9]*$ ]]; then
echo -e "\n\e[1mInvalid PR number:\e[0m $pr\n"
echo -n "Delete it from top of /etc/iiab/pr-queue and continue? [Y/n] "
read ans < /dev/tty
[ "$ans" = "n" ] || [ "$and" = "N" ] &&
exit 1
sed -i '1d' /etc/iiab/pr-queue # Delete 1st line
continue
fi

if (( pr >= 1561 )); then # iiab/iiab's lowest PR as of October 2020
repo=iiab
else
repo=iiab-admin-console
fi

http_code=$(curl -sH "Accept: application/vnd.github.v3" https://api.github.com/repos/iiab/$repo/pulls/$pr -w "%{http_code}" -o /tmp/iiab-pr.json)
if [ "$http_code" != 200 ] ; then
echo -e "\n\e[41mCOULD NOT FIND PR #$pr (https://github.com/iiab/$repo/pull/$pr)\e[0m\n"
echo -en "\e[41mHTTP ERROR: $http_code\e[0m\e[1m "
[ "$http_code" = 403 ] &&
echo -en "(DID YOU REQUEST 60+ PR's IN ONE HOUR?)"
[ "$http_code" = 404 ] &&
echo -en "(IS #$pr AN ISSUE, RATHER THAN A PR?)"
echo -e "\n"

cat /tmp/iiab-pr.json # GitHub sometimes includes an English explanation of the error, and URL, e.g. https://developer.github.com/v3/#rate-limiting
echo
curl -IsH "Accept: application/vnd.github.v3" https://api.github.com/repos/iiab/$repo/pulls/$pr # HTTP headers typically reveal a lot more!

echo -n "Delete it from top of /etc/iiab/pr-queue and continue? [Y/n] "
read ans < /dev/tty
[ "$ans" = "n" ] || [ "$and" = "N" ] &&
exit 1
sed -i '1d' /etc/iiab/pr-queue # Delete 1st line
continue
fi

account=$(jq -r '.head.label' /tmp/iiab-pr.json | cut -d: -f1)
branch=$(jq -r '.head.label' /tmp/iiab-pr.json | cut -d: -f2)

echo -e "\nPR #$pr: cd /opt/iiab/$repo"
cd /opt/iiab/$repo
#git checkout -b POTLUCK master || true # If test branch name/norm in future...for both repos?
echo -e "git pull --no-edit https://github.com/$account/$repo $branch" # Or $repo.git
git pull --no-edit https://github.com/$account/$repo $branch || {
echo -e "\n\e[1mgit exit/error code:\e[0m $?\n" # if hides err code
echo -n "Delete it from top of /etc/iiab/pr-queue and continue? [Y/n] "
read ans < /dev/tty
[ "$ans" = "n" ] || [ "$and" = "N" ] &&
exit 1
sed -i '1d' /etc/iiab/pr-queue # Delete 1st line
continue
}

sed -i '1d' /etc/iiab/pr-queue # Delete 1st line
echo "$pr $(date '+%F %T %Z')" >> /etc/iiab/pr-list-pulled # For iiab-diagnostics
done

if ! cmp -s /tmp/$local_vars_src_file /opt/iiab/iiab/vars/$local_vars_src_file; then
mv /etc/iiab/local_vars.yml /etc/iiab/local_vars.yml.old
cp /opt/iiab/iiab/vars/$local_vars_src_file /etc/iiab/local_vars.yml

echo -e "\n\e[1mWARNING: /opt/iiab/iiab/vars/$local_vars_src_file HAS CHANGED!\n"
echo -e "YOUR /etc/iiab/local_vars.yml WAS MOVED TO /etc/iiab/local_vars.yml.old\e[0m\n"
echo -n "Edit the NEW /etc/iiab/local_vars.yml to customize Internet-in-a-Box? [Y/n] "
read ans < /dev/tty
if [ "$ans" != "n" ] && [ "$ans" != "N" ]; then
echo -e "\n1) PLEASE RUN: sudo nano /etc/iiab/local_vars.yml\n"
echo -e "2) After you're done editing, RUN 'sudo iiab' TO CONTINUE!\n"
exit 0
fi
fi

# I. Install Ansible + 2 IIAB repos
# J. Install Ansible + 2 IIAB repos
echo -e "\n\nINSTALL ANSIBLE + CORE IIAB SOFTWARE + ADMIN CONSOLE / CONTENT PACK MENUS...\n"

echo -e "Install Ansible..."
cd $BASEDIR/iiab/scripts/
cd /opt/iiab/iiab/scripts/
./ansible

echo -e "\n┌──────────────────────────────────────────────────────────────────────────────┐"
Expand All @@ -247,8 +349,8 @@ echo -e "│
echo -e "│ child script ./iiab-install -- both avoid repeating any of the 9 stages. │"
echo -e "│ │"
echo -e "└──────────────────────────────────────────────────────────────────────────────┘"
cd $BASEDIR/iiab/
./iiab-install $@
cd /opt/iiab/iiab/
./iiab-install # "$@"

if [ -f $FLAGDIR/iiab-admin-console-complete ]; then
echo -e "ADMIN CONSOLE INSTALLATION IS ALREADY COMPLETE -- per existence of:"
Expand All @@ -260,7 +362,7 @@ else
echo -e "Install Admin Console... (also runs iiab-get-kiwix-cat to d/l Kiwix catalog,"
echo -e "and installs Dynamic Menuing for /library/www/html/home/index.html)\n"

cd $BASEDIR/iiab-admin-console
cd /opt/iiab/iiab-admin-console
./install
touch $FLAGDIR/iiab-admin-console-complete
fi
Expand All @@ -279,7 +381,7 @@ else
systemctl start iiab-cmdsrv || true # Overrides 'set -e'
fi

# J. KA Lite prep
# K. KA Lite prep
if [ -d /library/ka-lite ]; then
echo -e "\n\nKA LITE REQUIRES 2 THINGS...\n"

Expand Down Expand Up @@ -319,15 +421,15 @@ fi
# kalite manage retrievecontentpack download en
# OLD WAY ABOVE - fails w/ sev ISPs per https://github.com/iiab/iiab/issues/871

# K. Start BitTorrent downloads, e.g. if $CONFDIR/local_vars.yml requested any
if (systemctl -q is-active transmission-daemon) then
# L. Start BitTorrent downloads, e.g. if /etc/iiab/local_vars.yml requested any
if systemctl -q is-active transmission-daemon; then
echo -e "\n\nSTARTING BITTORRENT DOWNLOAD(S) for KA Lite...Please Monitor: http://box:9091\n"
transmission-remote -n Admin:changeme -t all --start
fi

touch $FLAGDIR/iiab-complete

# L. Educate Implementers prior to rebooting!
# M. Educate Implementers prior to rebooting!
echo -e "\n\n ┌───────────────────────────────────────────────────────────┐"
echo -e " │ │"
echo -e " │ INTERNET-IN-A-BOX (IIAB) SOFTWARE INSTALL IS COMPLETE │"
Expand Down
6 changes: 4 additions & 2 deletions install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ set -e # Exit on error (avoids snowballing)
export DEBIAN_FRONTEND=noninteractive # Bypass (most!) interactive questions

# Save script to /usr/sbin/iiab (easy resume/continue mnemonic 'sudo iiab')
mv /usr/sbin/iiab /usr/sbin/iiab.old || true # Overrides 'set -e'
mv /usr/sbin/iiab /usr/sbin/iiab.old 2> /dev/null || true # Overrides 'set -e'
curl https://raw.githubusercontent.com/iiab/iiab-factory/master/iiab > /usr/sbin/iiab
chmod 0744 /usr/sbin/iiab

# Run install script!
/usr/sbin/iiab
/usr/sbin/iiab "$@" # Pass on all CLI params (PR #s) for easy community testing
# allowing for e.g.: curl d.iiab.io/install.txt | sudo bash -s 361 2604 2607
# As explained in /usr/sbin/iiab Lines 244-319 ("I. Install optional PR's").