node-red_setup.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env bash
  2. set -o errexit
  3. set -o errtrace
  4. set -o nounset
  5. set -o pipefail
  6. shopt -s expand_aliases
  7. alias die='EXIT=$? LINE=$LINENO error_exit'
  8. CROSS='\033[1;31m\xE2\x9D\x8C\033[0m'
  9. CHECKMARK='\033[0;32m\xE2\x9C\x94\033[0m'
  10. RETRY_NUM=5
  11. RETRY_EVERY=3
  12. NUM=$RETRY_NUM
  13. trap die ERR
  14. trap 'die "Script interrupted."' INT
  15. function error_exit() {
  16. trap - ERR
  17. local DEFAULT='Unknown failure occured.'
  18. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  19. local FLAG="\e[91m[ERROR:LXC] \e[93m$EXIT@$LINE"
  20. msg "$FLAG $REASON"
  21. exit $EXIT
  22. }
  23. function msg() {
  24. local TEXT="$1"
  25. echo -e "$TEXT"
  26. }
  27. echo -e "${CHECKMARK} \e[1;92m Setting up Container OS... \e[0m"
  28. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  29. locale-gen >/dev/null
  30. while [ "$(hostname -I)" = "" ]; do
  31. 1>&2 echo -e "${CROSS} \e[1;31m No Network: \e[0m $(date)"
  32. sleep $RETRY_EVERY
  33. ((NUM--))
  34. if [ $NUM -eq 0 ]
  35. then
  36. 1>&2 echo -e "${CROSS} \e[1;31m No Network After $RETRY_NUM Tries \e[0m"
  37. exit 1
  38. fi
  39. done
  40. echo -e "${CHECKMARK} \e[1;92m Network Connected: \e[0m $(hostname -I)"
  41. echo -e "${CHECKMARK} \e[1;92m Updating Container OS... \e[0m"
  42. apt-get update &>/dev/null
  43. apt-get -qqy upgrade &>/dev/null
  44. echo -e "${CHECKMARK} \e[1;92m Installing Dependencies... \e[0m"
  45. apt-get update &>/dev/null
  46. apt-get -qqy install \
  47. curl \
  48. sudo &>/dev/null
  49. echo -e "${CHECKMARK} \e[1;92m Installing Node-Red... \e[0m"
  50. bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered) --confirm-root --confirm-install --skip-pi &>/dev/null
  51. echo -e "${CHECKMARK} \e[1;92m Customizing Container... \e[0m"
  52. rm /etc/motd
  53. rm /etc/update-motd.d/10-uname
  54. touch ~/.hushlogin
  55. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  56. mkdir -p $(dirname $GETTY_OVERRIDE)
  57. cat << EOF > $GETTY_OVERRIDE
  58. [Service]
  59. ExecStart=
  60. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  61. EOF
  62. systemctl daemon-reload
  63. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  64. systemctl start nodered.service &>/dev/null
  65. systemctl enable nodered.service &>/dev/null
  66. echo -e "${CHECKMARK} \e[1;92m Cleanup... \e[0m"
  67. rm -rf /node-red_setup.sh /var/{cache,log}/* /var/lib/apt/lists/*