hoekbrwr

Script for installing all nodes with for loop.

Feb 7th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.54 KB | None | 0 0
  1. ## ROUTINES
  2. ## Here at the beginning, a load of useful routines - see further down
  3.  
  4. # Get time as a UNIX timestamp (seconds elapsed since Jan 1, 1970 0:00 UTC)
  5. startTime="$(date +%s)"
  6. columns=$(tput cols)
  7. user_response=""
  8.  
  9. # High Intensity
  10. IGreen='\e[0;92m'       # Green
  11. IYellow='\e[0;93m'      # Yellow
  12. IBlue='\e[0;94m'        # Blue
  13. ICyan='\e[0;96m'        # Cyan
  14. IWhite='\e[0;97m'       # White
  15.  
  16. # Bold High Intensity
  17. BIRed='\e[1;91m'        # Red
  18. BIGreen='\e[1;92m'      # Green
  19. BIYellow='\e[1;93m'     # Yellow
  20. BIPurple='\e[1;95m'     # Purple
  21. BIMagenta='\e[1;95m'    # Purple
  22. BICyan='\e[1;96m'       # Cyan
  23. BIWhite='\e[1;97m'      # White
  24.  
  25. skip=0
  26. other=0
  27.  
  28. clean_stdin()
  29. {
  30.     while read -r -t 0; do
  31.         read -n 256 -r -s
  32.     done
  33. }
  34.  
  35. # Permanent loop until both passwords are the same..
  36. function user_input {
  37.     local VARIABLE_NAME=${1}
  38.     local VARIABLE_NAME_1="A"
  39.     local VARIABLE_NAME_2="B"
  40.     while true; do
  41.         printf "${BICyan}$2: ${BIWhite}";
  42.         if [ "$3" = "hide" ] ; then
  43.             stty -echo;
  44.         fi
  45.         read VARIABLE_NAME_1;
  46.         stty echo;
  47.         if [ "$3" = "hide" ] ; then
  48.             printf "\n${BICyan}$2 (again) : ${BIWhite}";
  49.             stty -echo;
  50.             read VARIABLE_NAME_2;
  51.             stty echo;
  52.         else
  53.             VARIABLE_NAME_2=$VARIABLE_NAME_1;
  54.         fi
  55.         if [ $VARIABLE_NAME_1 != $VARIABLE_NAME_2 ] ; then
  56.             printf "\n${BIRed}Sorry, did not match!${BIWhite}\n";
  57.         else
  58.             break;
  59.         fi
  60.     done
  61.     readonly ${VARIABLE_NAME}=$VARIABLE_NAME_1;
  62.     if [ "$3" == "hide" ] ; then
  63.         printf "\n";
  64.     fi
  65. }
  66.  
  67. stopit=0
  68. other=0
  69. yes=0
  70. nohelp=0
  71. hideother=0
  72.  
  73. timecount(){
  74.     sec=30
  75.     while [ $sec -ge 0 ]; do
  76.         if [ $nohelp -eq 1 ]; then
  77.            
  78.             if [ $hideother -eq 1 ]; then
  79.                 printf "${BIPurple}Continue ${BIWhite}y${BIPurple}(es)/${BIWhite}n${BIPurple}(o)/${BIWhite}a${BIPurple}(ll)/${BIWhite}e${BIPurple}(nd)-  ${BIGreen}00:0$min:$sec${BIPurple} remaining\033[0K\r${BIWhite}"
  80.             else
  81.                 printf "${BIPurple}Continue ${BIWhite}y${BIPurple}(es)/${BIWhite}o${BIPurple}(ther)/${BIWhite}e${BIPurple}(nd)-  ${BIGreen}00:0$min:$sec${BIPurple} remaining\033[0K\r${BIWhite}"
  82.             fi
  83.         else
  84.             printf "${BIPurple}Continue ${BIWhite}y${BIPurple}(es)/${BIWhite}h${BIPurple}(elp)-  ${BIGreen}00:0$min:$sec${BIPurple} remaining\033[0K\r${BIWhite}"
  85.         fi
  86.         sec=$((sec-1))
  87.         trap '' 2
  88.         stty -echo
  89.         read -t 1 -n 1 user_response
  90.         stty echo
  91.         trap - 2
  92.         if [ -n  "$user_response" ]; then
  93.             break
  94.         fi
  95.     done
  96. }
  97.  
  98.  
  99. task_start(){
  100.     printf "\r\n"
  101.     printf "${BIGreen}%*s\n" $columns | tr ' ' -
  102.     printf "$1"
  103.     clean_stdin
  104.     skip=0
  105.     printf "\n${BIGreen}%*s${BIWhite}\n" $columns | tr ' ' -
  106.     elapsedTime="$(($(date +%s)-startTime))"
  107.     printf "Elapsed: %02d hrs %02d mins %02d secs\n" "$((elapsedTime/3600%24))" "$((elapsedTime/60%60))" "$((elapsedTime%60))"
  108.     clean_stdin
  109.     if [ "$user_response" != "a" ]; then
  110.         timecount
  111.     fi
  112.     echo -e "                                                                        \033[0K\r"
  113.     if  [ "$user_response" = "e" ]; then
  114.         printf "${BIWhite}"
  115.         exit 1
  116.     fi
  117.     if  [ "$user_response" = "n" ]; then
  118.         skip=1
  119.     fi
  120.     if  [ "$user_response" = "o" ]; then
  121.         other=1
  122.     fi
  123.     if  [ "$user_response" = "h" ]; then
  124.         stopit=1
  125.     fi
  126.     if  [ "$user_response" = "y" ]; then
  127.         yes=1
  128.     fi
  129.     if [ -n  "$2" ]; then
  130.         if [ $skip -eq 0 ]; then
  131.             printf "${BIYellow}$2${BIWhite}\n"
  132.         else
  133.             printf "${BICyan}%*s${BIWhite}\n" $columns '[SKIPPED]'
  134.         fi
  135.     fi
  136. }
  137.  
  138. task_end(){
  139.     printf "${BICyan}%*s${BIWhite}\n" $columns '[OK]'
  140. }
  141.  
  142.  
  143. # credits to DietPi
  144.  
  145. CPU_TEMP_CURRENT='Unknown'
  146. CPU_TEMP_PRINT='Unknown'
  147. ACTIVECORES=$(grep -c processor /proc/cpuinfo)
  148.  
  149. #Array to store possible locations for temp read.
  150. aFP_TEMPERATURE=(
  151.     '/sys/class/thermal/thermal_zone0/temp'
  152.     '/sys/devices/virtual/thermal/thermal_zone1/temp'
  153.     '/sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input'
  154.     '/sys/class/hwmon/hwmon0/device/temp_label'
  155. )
  156.  
  157. Obtain_Cpu_Temp(){
  158.     for ((i=0; i<${#aFP_TEMPERATURE[@]}; i++))
  159.     do
  160.         if [ -f "${aFP_TEMPERATURE[$i]}" ]; then
  161.             CPU_TEMP_CURRENT=$(cat "${aFP_TEMPERATURE[$i]}")
  162.             # - Some devices (pine) provide 2 digit output, some provide a 5 digit ouput.
  163.             #       So, If the value is over 1000, we can assume it needs converting to 1'c
  164.             if (( $CPU_TEMP_CURRENT == 0 )); then
  165.                 continue
  166.             fi
  167.             if (( $CPU_TEMP_CURRENT >= 1000 )); then
  168.                 CPU_TEMP_CURRENT=$( echo -e "$CPU_TEMP_CURRENT" | awk '{print $1/1000}' | xargs printf "%0.0f" )
  169.             fi
  170.             if (( $CPU_TEMP_CURRENT >= 70 )); then
  171.                 CPU_TEMP_PRINT="\e[91mWarning: $CPU_TEMP_CURRENT'c\e[0m"
  172.                 elif (( $CPU_TEMP_CURRENT >= 60 )); then
  173.                 CPU_TEMP_PRINT="\e[38;5;202m$CPU_TEMP_CURRENT'c\e[0m"
  174.                 elif (( $CPU_TEMP_CURRENT >= 50 )); then
  175.                 CPU_TEMP_PRINT="\e[93m$CPU_TEMP_CURRENT'c\e[0m"
  176.                 elif (( $CPU_TEMP_CURRENT >= 40 )); then
  177.                 CPU_TEMP_PRINT="\e[92m$CPU_TEMP_CURRENT'c\e[0m"
  178.                 elif (( $CPU_TEMP_CURRENT >= 30 )); then
  179.                 CPU_TEMP_PRINT="\e[96m$CPU_TEMP_CURRENT'c\e[0m"
  180.             else
  181.                 CPU_TEMP_PRINT="\e[96m$CPU_TEMP_CURRENT'c\e[0m"
  182.             fi
  183.             break
  184.         fi
  185.     done
  186. }
  187.  
  188. LOGFILE=$HOME/$0-`date +%Y-%m-%d_%Hh%Mm`.log
  189.  
  190. printl() {
  191.     printf $1
  192.     echo -e $1 >> $LOGFILE
  193. }
  194.  
  195. printstatus() {
  196.     Obtain_Cpu_Temp
  197.     h=$(($SECONDS/3600));
  198.     m=$((($SECONDS/60)%60));
  199.     s=$(($SECONDS%60));
  200.     printf "\r\n${BIGreen}==\r\n== ${BIYellow}$1"
  201.     printf "\r\n${BIGreen}== ${IBlue}Total: %02dh:%02dm:%02ds Cores: $ACTIVECORES Temperature: $CPU_TEMP_PRINT \r\n${BIGreen}==${IWhite}\r\n\r\n"  $h $m $s;
  202.     echo -e "############################################################" >> $LOGFILE
  203.     echo -e $1 >> $LOGFILE
  204. }
  205.  
  206.  
  207. ############################################################################
  208. ##
  209. ## MAIN SECTION OF SCRIPT - action begins here
  210. ##
  211. #############################################################################
  212. ##
  213.  
  214. printstatus "Welcome to THE SCRIPT!"
  215.  
  216. AQUIET=""
  217. NQUIET=""
  218.  
  219. # install sudo on devices without it
  220. [ ! -x /usr/bin/sudo ] && apt-get $AQUIET -y update > /dev/null 2>&1 && apt-get $AQUIET -y install sudo 2>&1 | tee -a $LOGFILE
  221.  
  222. # Whiptail menu may already be installed by default, on the other hand maybe not.
  223. sudo apt-get $AQUIET -y install whiptail ccze 2>&1 | tee -a $LOGFILE
  224. # Another way - Xenial should come up in upper case in $DISTRO
  225. . /etc/os-release
  226. OPSYS=${ID^^}
  227. echo -e OPSYS: $OPSYS >> $LOGFILE
  228.  
  229. printstatus "Grabbing some preliminaries..."
  230.  
  231. # test internet connection
  232. sudo chmod u+s /bin/ping
  233. if [[ "$(ping -c 1 23.1.68.60  | grep '100%' )" != "" ]]; then
  234.     printl "${IRed}!!!! No internet connection available, aborting! ${IWhite}\r\n"
  235.     exit 0
  236. fi
  237.  
  238. # install lsb_release on devices without it
  239. [ ! -x /usr/bin/lsb_release ] && apt-get $AQUIET -y update > /dev/null 2>&1 && apt-get $AQUIET -y install lsb-release 2>&1 | tee -a $LOGFILE
  240. DISTRO=$(/usr/bin/lsb_release -rs)
  241. CHECK64=$(uname -m)
  242. echo -e DISTRO: $DISTRO >> $LOGFILE
  243. echo -e CHECK64: $CHECK64 >> $LOGFILE
  244.  
  245.     printstatus "Installing Nodes (could take some time)"
  246.  
  247.     for addonnodes in
  248.     moment node-red-contrib-config node-red-contrib-grove node-red-contrib-diode node-red-contrib-bigtimer \
  249.     node-red-contrib-esplogin node-red-contrib-timeout node-red-node-openweathermap node-red-node-google node-red-contrib-advanced-ping \
  250.     node-red-node-sqlite node-red-node-emoncms node-red-node-geofence node-red-contrib-moment node-red-contrib-particle \
  251.     node-red-contrib-web-worldmap node-red-contrib-sqldbs node-red-contrib-ramp-thermostat node-red-contrib-graphs \
  252.     node-red-contrib-isonline node-red-node-ping node-red-node-random node-red-node-smooth node-red-contrib-npm node-red-node-arduino \
  253.     node-red-contrib-file-function node-red-contrib-boolean-logic node-red-contrib-blynk-ws node-red-contrib-telegrambot \
  254.     node-red-dashboard node-red-node-darksky node-red-contrib-owntracks node-red-contrib-alexa-local node-red-contrib-heater-controller ;
  255.     do
  256.         printstatus "Installing node \"${addonnodes}\""
  257.         npm $NQUIET install --save ${addonnodes} 2>&1 | tee -a $LOGFILE
  258.     done
Advertisement
Add Comment
Please, Sign In to add comment