From eb35ff8290613785ec97aca6d3026a918f155689 Mon Sep 17 00:00:00 2001 From: zen Date: Wed, 16 Mar 2022 16:36:17 -0700 Subject: [PATCH] increasing POSIX compliance --- ingredients/copper | 107 +++++++++++++++++++++------------------------ ingredients/gold | 2 +- ingredients/iron | 23 +++++----- recipes/ao.sh | 4 +- recipes/flask.sh | 12 ++--- recipes/init.sh | 33 ++++---------- 6 files changed, 83 insertions(+), 98 deletions(-) diff --git a/ingredients/copper b/ingredients/copper index b3fb4b8..aa34fe5 100644 --- a/ingredients/copper +++ b/ingredients/copper @@ -34,12 +34,11 @@ locate_torrc() { configure_tor() { locate_torrc - echo -e "Your existing torrc file has the following settings: " - echo "" + say "Your existing torrc file has the following settings: " + say "" cat $TORRCPATH | grep '^[^#]' - echo "" - echo -en "Would you like to reset it?: ${BLUE}(y/n)${RESET} " - read torrc_reset + say "" + ask_for torrc_reset "Would you like to reset it?: ${BLUE}(y/n)${RESET} " case $torrc_reset in "Y" | "y") cp resources/torrc-template . @@ -74,12 +73,12 @@ get_external_ip() { } initialize_nginx() { - echo -e "${BOLD}Installing and configuring NGINX${RESET}" - echo "" + say "${BOLD}Installing and configuring NGINX${RESET}" + say "" install_if_needed nginx # Making sure this version of NGINX supports sites-enabled - if [[ -z $(sudo cat /etc/nginx/nginx.conf | grep sites-enabled) ]]; then + if [ -z $(sudo cat /etc/nginx/nginx.conf | grep sites-enabled) ]; then sudo mkdir -p /etc/nginx/sites-available sudo mkdir -p /etc/nginx/sites-enabled sudo cp resources/nginx/base.nginx.conf /etc/nginx/nginx.conf @@ -94,28 +93,27 @@ make_site() { if [ -f resources/nginx/${SITE}.nginx.conf ]; then NGINX_SITE_LOCATION=/etc/nginx/sites-available/${SITE} if [ -f $NGINX_SITE_LOCATION ]; then - echo -e "You already have a site available for ${BLUE}${SITE}${RESET}, what would you like to do?" - echo -en "${BOLD}R${RESET}eset it, ${BOLD}A${RESET}ctivate it, or do ${BOLD}N${RESET}othing? (r/a/n): " - read whatdo + say "You already have a site available for ${BLUE}${SITE}${RESET}, what would you like to do?" + ask_for whatdo "${BOLD}R${RESET}eset it, ${BOLD}A${RESET}ctivate it, or do ${BOLD}N${RESET}othing? (r/a/n): " case $whatdo in "R" | "r") - echo "Resetting sites-available/${SITE}" + say "Resetting sites-available/${SITE}" sudo rm /etc/nginx/sites-available/${SITE} ;; "A" | "a") - echo "Activating sites-available/${SITE}" + say "Activating sites-available/${SITE}" if [ ! -e /etc/nginx/sites-enabled/${SITE} ]; then sudo ln -s /etc/nginx/sites-available/${SITE} /etc/nginx/sites-enabled/ fi ;; "N" | "n") - echo "Okay, we'll leave it be." + say "Okay, we'll leave it be." ;; *) - echo "Instructions unclear, accidentally an choice" + say "Instructions unclear, accidentally an choice" ;; esac - echo "" + say "" fi if [ ! -f $NGINX_SITE_LOCATION ]; then @@ -125,7 +123,7 @@ make_site() { KEY=$(echo $keyval | cut -d'=' -f 1) VAL=$(echo $keyval | cut -d'=' -f 2) - echo "Substituting $KEY for $VAL" + say "Substituting $KEY for $VAL" sudo sed -i "s#$KEY#$VAL#g" $NGINX_SITE_LOCATION done @@ -134,18 +132,17 @@ make_site() { fi fi else - echo "" - echo -e "${RED}Sorry${RESET}, ${SITE} isn't available as an nginx template" - echo "We have..." + say "" + say "${RED}Sorry${RESET}, ${SITE} isn't available as an nginx template" + say "We have..." echo `ls resources/nginx` fi } get_domain() { if [ ! -z $DOMAIN ]; then - echo -e "Your domain name is currently set to ${BLUE}${DOMAIN}${RESET}" - echo -ne "would you like to change it? ${BLUE}(y/n): ${RESET}" - read newdns + say "Your domain name is currently set to ${BLUE}${DOMAIN}${RESET}" + ask_for newdns "would you like to change it? ${BLUE}(y/n): ${RESET}" case $newdns in y | Y) forget "DOMAIN" @@ -155,33 +152,30 @@ get_domain() { fi if [ -z $DOMAIN ]; then - echo -en "Do you have a domain name pointing to this computer? ${BLUE}(y/n)${RESET}: " - read dns - echo "" + ask_for dns "Do you have a domain name pointing to this computer? ${BLUE}(y/n)${RESET}: " + say "" case $dns in y | Y) - echo "Good to hear! What is it?" + say "Good to hear! What is it?" OKAY=0 while [ $OKAY -eq 0 ]; do - echo -n "http://" - read DOMAIN - echo "" - echo -ne "is ${BLUE}http://${DOMAIN}${RESET} correct? ${BLUE}(y/n): ${RESET}" - read correct + ask_for DOMAIN "http://" + say "" + ask_for correct "is ${BLUE}http://${DOMAIN}${RESET} correct? ${BLUE}(y/n): ${RESET}" case $correct in y | Y) OKAY=1 ;; *) - echo "Okay, let's try again! What is your domain name?" + say "Okay, let's try again! What is your domain name?" ;; esac done - echo -e "${BLUE}${DOMAIN}${RESET}, got it." + say "${BLUE}${DOMAIN}${RESET}, got it." remember "DOMAIN=${DOMAIN}" ;; *) - echo "Okay, let's just leave it open for now." + say "Okay, let's just leave it open for now." ;; esac fi @@ -206,22 +200,23 @@ enable_ssl() { echo "We've already gone through the SSL enabling process! Skipping" else if [ ! -z $DOMAIN ]; then - read -p "Would you like to enable SSL via Certbot? (y/n): " -n1 ssl - echo "" + ask_for ssl "Would you like to enable SSL via Certbot? (y/n): " + say "" case $ssl in y | Y) - echo "Alright, let's get Certbot in here!" + say "Alright, let's get Certbot in here!" install_if_needed python3 certbot python3-certbot-nginx - echo -e "${BOLD}Take it away, Certbot${NC}" - SSL=$(sudo certbot --nginx) + say "${BOLD}Take it away, Certbot${RESET}" + sudo certbot --nginx + SSL=$? remember "SSL=$SSL" ;; *) - echo "Yea, SSL is like, totally whatever anyways..." + say "Yea, SSL is like, totally whatever anyways..." ;; esac else - echo "We can't configure SSL without a domain! Skipping" + say "We can't configure SSL without a domain! Skipping" fi fi @@ -238,7 +233,7 @@ enable_ssl() { } check_ports() { - echo -e "${BOLD}Checking Port Accessibility${RESET}" + say "${BOLD}Checking Port Accessibility${RESET}" if [ -z $DOMAIN ]; then ADDR=$EXTERNAL_IP @@ -246,38 +241,38 @@ check_ports() { ADDR=$DOMAIN fi - echo -e "Querying this computer's network from ${BLUE}${ADDR}${RESET}" + say "Querying this computer's network from ${BLUE}${ADDR}${RESET}" echo "" install_if_needed nmap nmap -Pn $EXTERNAL_IP > nmap.txt OPEN=1 if grep -qE "^80/.*(open|filtered)" nmap.txt; then - echo -e "I can see port ${GREEN}80${RESET}!" + say "I can see port ${GREEN}80${RESET}!" else - echo -e "Uh oh, port ${RED}80${RESET} isn't showing up..." + say "Uh oh, port ${RED}80${RESET} isn't showing up..." OPEN=0 fi if grep -qE "^443/.*(open|filtered)" nmap.txt; then - echo -e "I can see port ${GREEN}443${RESET} as well!" + say "I can see port ${GREEN}443${RESET} as well!" else - echo -e "Uh oh, port ${RED}443${RESET} isn't showing up..." + say "Uh oh, port ${RED}443${RESET} isn't showing up..." OPEN=0 fi rm nmap.txt - echo "" + say "" # TODO I changed default to src on the line below - impact? LOCAL_IP=$(ip route | grep src | grep -oP "(?<=src )[^ ]+") ROUTER_IP=$(route -n | grep ^0.0.0.0 | awk '{print $2}') - if [[ $OPEN -eq 0 ]]; then - echo -e "${RED}Port configuration needed.${RESET} Something (probably your wireless router) is blocking us from serving this page to the rest of the internet." - echo "Port forwarding is relatively simple, but as it stands it is beyond the scope of this script to be able to automate it." - echo -e "You'll probably need to look up the login information for your specific router and forward the red ports to the local IP of this computer (${BOLD}${LOCAL_IP}${RESET})." - echo -e "You can log into your router at this IP address: ${BOLD}${ROUTER_IP}${RESET}" - echo "That's all the help I can give you regarding port forwarding. Good luck!" - echo "" + if [ "$OPEN" -eq 0 ]; then + say "${RED}Port configuration needed.${RESET} Something (probably your wireless router) is blocking us from serving this page to the rest of the internet." + say "Port forwarding is relatively simple, but as it stands it is beyond the scope of this script to be able to automate it." + say "You'll probably need to look up the login information for your specific router and forward the red ports to the local IP of this computer (${BOLD}${LOCAL_IP}${RESET})." + say "You can log into your router at this IP address: ${BOLD}${ROUTER_IP}${RESET}" + say "That's all the help I can give you regarding port forwarding. Good luck!" + say "" fi } diff --git a/ingredients/gold b/ingredients/gold index 552f1cd..eb678a5 100644 --- a/ingredients/gold +++ b/ingredients/gold @@ -22,7 +22,7 @@ install_bitcoin() { fi # This still relies on package management though - install_if_needed boost + install_if_needed boost autoconf tar -xvf 🜍/bitcoin-22.0.tar.gz sleep 1 diff --git a/ingredients/iron b/ingredients/iron index 9813796..ab5da98 100644 --- a/ingredients/iron +++ b/ingredients/iron @@ -22,6 +22,10 @@ fi set_node_to() { if check_for nvm; then + if ! check_for node; then + nvm install v16.14.0 + fi + if [ ! $(node -v) = $1 ]; then nvm install $1 nvm alias default $1 @@ -33,8 +37,8 @@ set_node_to() { } install_nvm() { - echo -e "${BOLD}Installing Node Version Manager${RESET}" - if [ -n $NVM_DIR ]; then + say "${BOLD}Installing Node Version Manager${RESET}" + if [ -d "$NVM_DIR" ]; then echo "nvm already installed! skipping" else chmod +x scripts/nvm_install.sh @@ -51,18 +55,17 @@ build_service_from_template() { shift echo "" if [ -f resources/service-templates/${SERVICE} ]; then - echo "Creating $SERVICE.service..." + say "Creating $SERVICE.service..." SERVICE_FILE=/etc/systemd/system/${SERVICE}.service if [ -f "$SERVICE_FILE" ]; then - echo "Seems like you've already installed ${SERVICE} here!" - echo -en "Would you like to recreate it? ${BLUE}(y/n)${RESET} " - read reset + say "Seems like you've already installed ${SERVICE} here!" + ask_for reset "Would you like to recreate it? ${BLUE}(y/n)${RESET} " case $reset in "Y" | "y") sudo rm $SERVICE_FILE ;; "N" | "n") - echo "Okay, we'll leave it as is." + say "Okay, we'll leave it as is." ;; esac fi @@ -77,12 +80,12 @@ build_service_from_template() { KEY=$(echo $keyval | cut -d'=' -f 1) VAL=$(echo $keyval | cut -d'=' -f 2) - echo "Substituting $KEY for $VAL" + say "Substituting $KEY for $VAL" sudo sed -i "s#$KEY#$VAL#g" $SERVICE_FILE done fi else - echo "No service template available for $SERVICE" + say "No service template available for $SERVICE" fi } @@ -90,7 +93,7 @@ activate_service() { SERVICE=$1 SERVICE_FILE=/etc/systemd/system/${SERVICE}.service if [ -f "$SERVICE_FILE" ]; then - echo -e "Enabling and starting ${GREEN}${SERVICE}${RESET}" + say "Enabling and starting ${GREEN}${SERVICE}${RESET}" sudo systemctl enable ${SERVICE} sudo systemctl start ${SERVICE} fi diff --git a/recipes/ao.sh b/recipes/ao.sh index 199b535..6d8a38b 100755 --- a/recipes/ao.sh +++ b/recipes/ao.sh @@ -3,7 +3,7 @@ # Script for installing the base dependencies of AO and getting it running # Bare Metal Alchemist, 2022 -source ingredients/lead +. ingredients/lead source ingredients/copper source ingredients/iron source ingredients/gold @@ -38,6 +38,8 @@ if [ "$EUID" -eq 0 ]; then exit 1 fi + +# TODO: dependencies should not be frontloaded, they should be installed as needed echo -e "Making sure we've got the basics..." echo -e "(you'll probably need to input ${BLUE}your 'sudo' password${RESET} here)" case $DISTRO in diff --git a/recipes/flask.sh b/recipes/flask.sh index cf35357..bcc9156 100755 --- a/recipes/flask.sh +++ b/recipes/flask.sh @@ -3,7 +3,7 @@ # Script for installing a simple Flask server and deploying it # Bare Metal Alchemist, 2022 -source ingredients/lead +. ingredients/lead source ingredients/iron source ingredients/copper @@ -31,7 +31,7 @@ say "Well, as an alchemy-themed toolkit, I must say: ${GREEN}good decision :)${R say "" # Make sure this script isn't being run with sudo in front -if [ "$EUID" -eq 0 ]; then +if [ "$(id -u)" -eq 0 ]; then say "${RED}${BOLD}Woah there!${RESET} Seems you're running this script as a superuser." say "" say "That might cause some issues with permissions and whatnot. Run this script as your default user (without sudo) and I'll ask you when I need superuser permissions" @@ -40,11 +40,10 @@ if [ "$EUID" -eq 0 ]; then fi say "${ULINE}Making sure we've got the basics...${RESET}" -echo -e "(you'll probably need to input ${BLUE}your 'sudo' password${RESET} here)" +say "(you'll probably need to input ${BLUE}your 'sudo' password${RESET} here)" case $DISTRO in "debian") - say "HEY I HAVEN'T TESTED THIS BY THE WAY" - install_if_needed python python-pip + install_if_needed python python3-pip python-is-python3 python3-venv pip install --upgrade pip ;; "arch") @@ -105,7 +104,8 @@ say "" say "${BOLD}We've got everything!${RESET} I'm going to set you up with \ a basic Flask page now\n" -mkdir -p ${FLASK_DIR}/{templates,static} +mkdir -p ${FLASK_DIR}/templates +mkdir -p ${FLASK_DIR}/static cp resources/flask/app.py ${FLASK_DIR} cp resources/flask/demo.css ${FLASK_DIR}/static cp resources/flask/demo.html ${FLASK_DIR}/templates diff --git a/recipes/init.sh b/recipes/init.sh index 78c3a5d..85933e7 100755 --- a/recipes/init.sh +++ b/recipes/init.sh @@ -3,24 +3,7 @@ # This is a script to be run on a fresh installation of Raspbian in order to make it suitable (to me) for CLI development # ~ Zen, 2022 -source ingredients/lead - -echo "Updating the repositories..." -case $DISTRO in - "debian") - sudo apt update - sudo apt autoremove - sudo apt upgrade - ;; - "arch") - sudo pacman -Syu - ;; - "mac") - install - sudo brew update - ;; -esac -echo "" +. ingredients/lead echo "Making sure we've got the basics..." case $DISTRO in @@ -36,10 +19,12 @@ case $DISTRO in esac echo "" -echo "Getting tmux-powerline" -mkdir -p $HOME/.tmux -git clone https://github.com/erikw/tmux-powerline.git $HOME/.tmux/tmux-powerline -echo "" +if [ ! -d "$HOME/.tmux/tmux-powerline" ]; then + echo "Getting tmux-powerline" + mkdir -p $HOME/.tmux + git clone https://github.com/erikw/tmux-powerline.git $HOME/.tmux/tmux-powerline + echo "" +fi echo "Copying configuration files" mkdir -p $HOME/.vim/colors @@ -50,8 +35,8 @@ cp resources/tmux-powerline-theme.sh $HOME/.tmux/tmux-powerline/themes/default.s echo "" # TODO is this needed? can we install p10k on base zsh? -#echo "Installing Oh My Zsh for theming - this could take a moment" -#sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" +echo "Installing Oh My Zsh for theming - this could take a moment" +sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" cat resources/zshrc-extras >> $HOME/.zshrc echo ""