From b410fa83bf096d006d340a6caddc2ecfbd3f4aba Mon Sep 17 00:00:00 2001 From: Zen Date: Sun, 27 Feb 2022 10:16:41 -0600 Subject: [PATCH] modularized service templating --- ingredients/copper | 155 ++++++++++++------ recipes/ao.sh | 112 ++++--------- .../ao} | 0 .../bitcoin} | 0 .../lightning} | 0 .../tor} | 0 6 files changed, 131 insertions(+), 136 deletions(-) rename resources/{ao-service-template => service-templates/ao} (100%) rename resources/{bitcoin-service-template => service-templates/bitcoin} (100%) rename resources/{lightning-service-template => service-templates/lightning} (100%) rename resources/{tor-service-template => service-templates/tor} (100%) diff --git a/ingredients/copper b/ingredients/copper index 3ab2071..a7507c6 100644 --- a/ingredients/copper +++ b/ingredients/copper @@ -1,9 +1,9 @@ #!/bin/sh # Bare Metal Alchemist, 2022 -############################################# -# Copper - ♀ # -############################################# +############################################# +# Copper - ♀ # +############################################# # Copper wires form the backbone of electrical systems worldwide # and much of the internet as a whole. @@ -21,7 +21,7 @@ locate_torrc() { TORRCPATH="${HOME}/.tor/torrc" else echo -e "${RED}Uh oh...${RESET} I couldn't figure out\ - where your torrc file is. That might cause some issues" + where your torrc file is. That might cause some issues" exit 1 fi fi @@ -41,10 +41,10 @@ configure_tor() { read torrc_reset case $torrc_reset in "Y" | "y") - cp resources/torrc-template . - sudo sed -i "s#USER#${USER}#g" torrc-template - sudo sed -i "s#HOME#${HOME}#g" torrc-template - sudo mv torrc-template $TORRCPATH + cp resources/torrc-template . + sudo sed -i "s#USER#${USER}#g" torrc-template + sudo sed -i "s#HOME#${HOME}#g" torrc-template + sudo mv torrc-template $TORRCPATH echo -e "${GREEN}Torrc file reset!${RESET}" ;; '*') @@ -73,55 +73,104 @@ get_external_ip() { } get_domain_name() { - read -p "Do you have a domain name pointing to this computer? (y/n): " dns - echo "" - case $dns in - y | Y) - echo "Good to hear! What is it?" - read -p "http://" domain - ;; - *) - echo "Okay, let's just leave it open for now." - domain=$(dig @resolver4.opendns.com myip.opendns.com +short) - anywhere=1 - echo "Try accessing this AO from either localhost, 127.0.0.1, or ${domain}" - ;; - esac - - if [ "$anywhere" -eq 1 ]; then - ACCESS_POINT=http://localhost - else - ACCESS_POINT=https://$domain - fi + read -p "Do you have a domain name pointing to this computer? (y/n): " dns + echo "" + case $dns in + y | Y) + echo "Good to hear! What is it?" + read -p "http://" domain + ;; + *) + echo "Okay, let's just leave it open for now." + domain=$(dig @resolver4.opendns.com myip.opendns.com +short) + anywhere=1 + echo "Try accessing this AO from either localhost, 127.0.0.1, or ${domain}" + ;; + esac + + if [ "$anywhere" -eq 1 ]; then + ACCESS_POINT=http://localhost + else + ACCESS_POINT=https://$domain + fi +} + +build_service_from_template() { + SERVICE=$1 + shift + echo "" + if [ -f resources/service-templates/${SERVICE} ]; then + echo "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 -n "Would you like to recreate it? ${BLUE}(y/n)${RESET} " + read reset + case reset in + "Y" | "y") + sudo rm $SERVICE_FILE + ;; + "N" | "n") + echo "Okay, we'll leave it as is." + ;; + esac + fi + + if [ ! -f "$SERVICE_FILE" ]; then + sudo cp resources/service-templates/${SERVICE} $SERVICE_FILE + + # Common template values + sudo sed -i "s#USER#${USER}#g" $SERVICE_FILE + sudo sed -i "s#HOME#${HOME}#g" $SERVICE_FILE + for keyval; do + KEY=$(echo $keyval | cut -d'=' -f 1) + VAL=$(echo $keyval | cut -d'=' -f 2) + + echo "Substituting $KEY for $VAL" + sudo sed -i "s#$KEY#$VAL#g" $SERVICE_FILE + done + fi + else + echo "No service template available for $SERVICE" + fi +} + +activate_service() { + SERVICE_FILE=/etc/systemd/system/${SERVICE}.service + if [ -f "$SERVICE_FILE" ]; then + echo -e "Enabling and starting ${GREEN}${SERVICE}${RESET}" + sudo systemctl enable ${SERVICE} + sudo systemctl start ${SERVICE} + fi } check_ports() { - install_if_needed nmap - nmap -Pn $domain > nmap.txt - OPEN=1 - if grep -qE "^80/.*(open|filtered)" nmap.txt; then - echo -e "I can see port ${GREEN}80${RESET}!" - else - echo -e "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!" - else - echo -e "Uh oh, port ${RED}443${RESET} isn't showing up..." - OPEN=0 - fi - rm nmap.txt - echo "" - 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." + install_if_needed nmap + nmap -Pn $domain > nmap.txt + OPEN=1 + if grep -qE "^80/.*(open|filtered)" nmap.txt; then + echo -e "I can see port ${GREEN}80${RESET}!" + else + echo -e "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!" + else + echo -e "Uh oh, port ${RED}443${RESET} isn't showing up..." + OPEN=0 + fi + rm nmap.txt + echo "" + 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}$(ip route | grep default | grep -oP "(?<=src )[^ ]+")${RESET})." - echo -e "You can log into your router at this IP address: ${BOLD}$(route -n | grep ^0.0.0.0 | awk '{print $2}')${RESET}" - echo "That's all the help I can give you regarding port forwarding. Good luck!" - echo "" - fi + echo -e "You can log into your router at this IP address: ${BOLD}$(route -n | grep ^0.0.0.0 | awk '{print $2}')${RESET}" + echo "That's all the help I can give you regarding port forwarding. Good luck!" + echo "" + fi } diff --git a/recipes/ao.sh b/recipes/ao.sh index c4c0361..9db2005 100755 --- a/recipes/ao.sh +++ b/recipes/ao.sh @@ -53,7 +53,7 @@ case $DISTRO in fi install_if_needed wget python gmp sqlite3 autoconf-archive pkgconf libev \ - python-mako python-pip net-tools zlib libsodium gettext dnsutils nginx + python-mako python-pip net-tools zlib libsodium gettext nginx ;; "mac") # install_if_needed better-computer @@ -257,25 +257,29 @@ esac echo "Excellent! We've configured $AO_NGINX_CONF to serve your AO from $domain" echo "" - read -p "Would you like to enable SSL via Certbot? (y/n): " -n1 ssl - echo "" - case $ssl in - y | Y) - echo "Alright, let's get Certbot in here!" - install_if_needed python3 certbot python3-certbot-nginx - echo -e "${BOLD}Take it away, Certbot${NC}" - sudo certbot --nginx - ;; - *) - echo "Yea, SSL is lame anyways..." - ;; - esac + if [ -z $anywhere ]; then + read -p "Would you like to enable SSL via Certbot? (y/n): " -n1 ssl + echo "" + case $ssl in + y | Y) + echo "Alright, let's get Certbot in here!" + install_if_needed python3 certbot python3-certbot-nginx + echo -e "${BOLD}Take it away, Certbot${NC}" + sudo certbot --nginx + ;; + *) + echo "Yea, SSL is lame anyways..." + ;; + esac + fi echo "" # ------------------- Step 7 - Systemd Setup ------------------- READY='' -echo -e "\n${BOLD}Alright, almost there!${RESET} Now we just need to set up the system daemons for Tor, Bitcoin, Lightning, and the AO so that everything opens on startup." +echo -e "\n${BOLD}Alright, almost there!${RESET} Now we just need to \ + set up the system daemons for Tor, Bitcoin, Lightning, and the AO\ + so that everything opens on startup." while [[ -z $READY ]]; do echo -en "${BLUE}You ready? (y/n):${RESET} " read -n1 ao_select @@ -293,21 +297,7 @@ while [[ -z $READY ]]; do esac done -echo "" -echo "Creating tor.service..." -TOR_SERVICE=/etc/systemd/system/tor.service -if [ -f "$TOR_SERVICE" ]; then - echo "Seems like you've already got tor here!" -else - sudo cp resources/tor-service-template $TOR_SERVICE - - # Making sure all values have been de-templated - sudo sed -i "s#USER#${USER}#g" $TOR_SERVICE - sudo sed -i "s#HOME#${HOME}#g" $TOR_SERVICE - sudo sed -i "s#TORRCPATH#${TORRCPATH}#g" $TOR_SERVICE - sudo sed -i "s#TORPATH#$(which tor)#g" $TOR_SERVICE -fi - +build_service_from_template tor "TORRCPATH=$TORRCPATH" "TORPATH=`which tor`" # Creating the .tor directory sudo mkdir -p $HOME/.tor @@ -315,67 +305,22 @@ sudo chown tor $HOME/.tor sudo chgrp $USER $HOME/.tor sudo chmod 770 $HOME/.tor -echo "Enabling and starting Tor" -sudo systemctl enable tor -sudo systemctl start tor +activate_service tor echo "" -echo "Creating bitcoin.service..." -BTC_SERVICE=/etc/systemd/system/bitcoin.service -if [ -f "$BTC_SERVICE" ]; then - echo -e "Seems like you've already have a bitcoin service!" -else - sudo cp resources/bitcoin-service-template $BTC_SERVICE - - # Making sure all values have been de-templated - sudo sed -i "s#USER#${USER}#g" $BTC_SERVICE - sudo sed -i "s#HOME#${HOME}#g" $BTC_SERVICE - sudo sed -i "s#BITCOIND#$(which bitcoind)#g" $BTC_SERVICE -fi -echo -e "Enabling and starting ${GREEN}Bitcoin${RESET}" -sudo systemctl enable bitcoin -sudo systemctl start bitcoin +build_service_from_template bitcoin "BITCOIND=`which bitcoind`" +activate_service bitcoin echo "" -echo "Creating lightning.service..." -LN_SERVICE=/etc/systemd/system/lightning.service -if [ -f "$LN_SERVICE" ]; then - echo -e "Seems like you've already have a lightning service!" -else - sudo cp resources/lightning-service-template $LN_SERVICE - - # Making sure all values have been de-templated - sudo sed -i "s#USER#${USER}#g" $LN_SERVICE - sudo sed -i "s#HOME#${HOME}#g" $LN_SERVICE - sudo sed -i "s#LIGHTNINGD#$(which lightningd)#g" $LN_SERVICE -fi -echo -e "Enabling and starting ${GREEN}lightning${RESET} " -sudo systemctl enable lightning -sudo systemctl start lightning +build_service_from_template lightningd "LIGHTNINGD=`which lightningd`" +activate_service lightningd echo "" -echo "Creating ao.service..." -AO_SERVICE=/etc/systemd/system/ao.service -if [ -f "$AO_SERVICE" ]; then - echo "Seems like you've already added one of these!" -else - sudo cp resources/ao-service-template $AO_SERVICE - - # Making sure all values have been de-templated - sudo sed -i "s#USER#${USER}#g" $AO_SERVICE - sudo sed -i "s#HOME#${HOME}#g" $AO_SERVICE - sudo sed -i "s#NODE#$(which node)#g" $AO_SERVICE - sudo sed -i "s#AO#${AO}#g" $AO_SERVICE - sudo sed -i "s#NODE_PARAMS#${NODE_PARAMS}#g" $AO_SERVICE -fi -echo -e "Enabling and starting the ${GREEN}AO${RESET}'s backend" -sudo systemctl enable ao -sudo systemctl start ao +build_service_from_template ao "NODE=`which node`" "AO=$AO" "NODE_PARAMS=$NODE_PARAMS" +activate_service ao echo "" -echo -e "Enabling and starting ${GREEN}NGINX${RESET} as the webserver" -sudo systemctl enable nginx -sudo systemctl start nginx +activate_service nginx # ------------------- Step 8 - Port Testing ------------------- @@ -384,6 +329,7 @@ echo -e "${BOLD}One more thing!${RESET} We need to make sure that your ports are check_ports # ------------------- Step 9 - Health Check ------------------- + echo '*********************************************************' echo -e "* ${BOLD}Version Information${RESET} *" echo '*********************************************************' diff --git a/resources/ao-service-template b/resources/service-templates/ao similarity index 100% rename from resources/ao-service-template rename to resources/service-templates/ao diff --git a/resources/bitcoin-service-template b/resources/service-templates/bitcoin similarity index 100% rename from resources/bitcoin-service-template rename to resources/service-templates/bitcoin diff --git a/resources/lightning-service-template b/resources/service-templates/lightning similarity index 100% rename from resources/lightning-service-template rename to resources/service-templates/lightning diff --git a/resources/tor-service-template b/resources/service-templates/tor similarity index 100% rename from resources/tor-service-template rename to resources/service-templates/tor