magicmirror-v5-install.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/usr/bin/env bash
  2. if [ "$VERBOSE" == "yes" ]; then set -x; STD=""; fi
  3. if [ "$VERBOSE" != "yes" ]; then STD="silent"; fi
  4. silent() { "$@" > /dev/null 2>&1; }
  5. if [ "$DISABLEIPV6" == "yes" ]; then echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf; $STD sysctl -p; fi
  6. YW=$(echo "\033[33m")
  7. RD=$(echo "\033[01;31m")
  8. BL=$(echo "\033[36m")
  9. GN=$(echo "\033[1;92m")
  10. CL=$(echo "\033[m")
  11. RETRY_NUM=10
  12. RETRY_EVERY=3
  13. CM="${GN}✓${CL}"
  14. CROSS="${RD}✗${CL}"
  15. BFR="\\r\\033[K"
  16. HOLD="-"
  17. set -Eeuo pipefail
  18. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  19. function error_handler() {
  20. local exit_code="$?"
  21. local line_number="$1"
  22. local command="$2"
  23. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  24. echo -e "\n$error_message\n"
  25. }
  26. function msg_info() {
  27. local msg="$1"
  28. echo -ne " ${HOLD} ${YW}${msg}..."
  29. }
  30. function msg_ok() {
  31. local msg="$1"
  32. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  33. }
  34. function msg_error() {
  35. local msg="$1"
  36. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  37. }
  38. msg_info "Setting up Container OS "
  39. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  40. locale-gen >/dev/null
  41. while [ "$(hostname -I)" = "" ]; do
  42. echo 1>&2 -en "${CROSS}${RD} No Network! "
  43. sleep $RETRY_EVERY
  44. ((NUM--))
  45. if [ $NUM -eq 0 ]; then
  46. echo 1>&2 -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  47. exit 1
  48. fi
  49. done
  50. msg_ok "Set up Container OS"
  51. msg_ok "Network Connected: ${BL}$(hostname -I)"
  52. set +e
  53. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else
  54. msg_error "Internet NOT Connected"
  55. read -r -p "Would you like to continue anyway? <y/N> " prompt
  56. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  57. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  58. else
  59. echo -e " 🖧 Check Network Settings"
  60. exit 1
  61. fi
  62. fi
  63. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  64. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi
  65. set -e
  66. msg_info "Updating Container OS"
  67. $STD apt-get update
  68. $STD apt-get -y upgrade
  69. msg_ok "Updated Container OS"
  70. msg_info "Installing Dependencies"
  71. $STD apt-get install -y curl
  72. $STD apt-get install -y sudo
  73. $STD apt-get install -y mc
  74. $STD apt-get install -y git
  75. msg_ok "Installed Dependencies"
  76. msg_info "Setting up Node.js Repository"
  77. $STD bash <(curl -fsSL https://deb.nodesource.com/setup_16.x)
  78. msg_ok "Set up Node.js Repository"
  79. msg_info "Installing Node.js"
  80. $STD apt-get install -y nodejs
  81. msg_ok "Installed Node.js"
  82. msg_info "Setting up MagicMirror Repository"
  83. $STD git clone https://github.com/MichMich/MagicMirror /opt/magicmirror
  84. msg_ok "Set up MagicMirror Repository"
  85. msg_info "Installing MagicMirror"
  86. cd /opt/magicmirror
  87. $STD npm install --only=prod --omit=dev
  88. cat <<EOF >/opt/magicmirror/config/config.js
  89. let config = {
  90. address: "0.0.0.0",
  91. port: 8080,
  92. basePath: "/",
  93. ipWhitelist: [],
  94. useHttps: false,
  95. httpsPrivateKey: "",
  96. httpsCertificate: "",
  97. language: "en",
  98. locale: "en-US",
  99. logLevel: ["INFO", "LOG", "WARN", "ERROR"],
  100. timeFormat: 24,
  101. units: "metric",
  102. serverOnly: true,
  103. modules: [
  104. {
  105. module: "alert",
  106. },
  107. {
  108. module: "updatenotification",
  109. position: "top_bar"
  110. },
  111. {
  112. module: "clock",
  113. position: "top_left"
  114. },
  115. {
  116. module: "calendar",
  117. header: "US Holidays",
  118. position: "top_left",
  119. config: {
  120. calendars: [
  121. {
  122. symbol: "calendar-check",
  123. url: "webcal://www.calendarlabs.com/ical-calendar/ics/76/US_Holidays.ics"
  124. }
  125. ]
  126. }
  127. },
  128. {
  129. module: "compliments",
  130. position: "lower_third"
  131. },
  132. {
  133. module: "weather",
  134. position: "top_right",
  135. config: {
  136. weatherProvider: "openweathermap",
  137. type: "current",
  138. location: "New York",
  139. locationID: "5128581", //ID from http://bulk.openweathermap.org/sample/city.list.json.gz; unzip the gz file and find your city
  140. apiKey: "YOUR_OPENWEATHER_API_KEY"
  141. }
  142. },
  143. {
  144. module: "weather",
  145. position: "top_right",
  146. header: "Weather Forecast",
  147. config: {
  148. weatherProvider: "openweathermap",
  149. type: "forecast",
  150. location: "New York",
  151. locationID: "5128581", //ID from http://bulk.openweathermap.org/sample/city.list.json.gz; unzip the gz file and find your city
  152. apiKey: "YOUR_OPENWEATHER_API_KEY"
  153. }
  154. },
  155. {
  156. module: "newsfeed",
  157. position: "bottom_bar",
  158. config: {
  159. feeds: [
  160. {
  161. title: "New York Times",
  162. url: "https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml"
  163. }
  164. ],
  165. showSourceTitle: true,
  166. showPublishDate: true,
  167. broadcastNewsFeeds: true,
  168. broadcastNewsUpdates: true
  169. }
  170. },
  171. ]
  172. };
  173. /*************** DO NOT EDIT THE LINE BELOW ***************/
  174. if (typeof module !== "undefined") {module.exports = config;}
  175. EOF
  176. msg_ok "Installed MagicMirror"
  177. msg_info "Creating Service"
  178. service_path="/etc/systemd/system/magicmirror.service"
  179. echo "[Unit]
  180. Description=Magic Mirror
  181. After=network.target
  182. StartLimitIntervalSec=0
  183. [Service]
  184. Type=simple
  185. Restart=always
  186. RestartSec=1
  187. User=root
  188. WorkingDirectory=/opt/magicmirror/
  189. ExecStart=/usr/bin/node serveronly
  190. [Install]
  191. WantedBy=multi-user.target" >$service_path
  192. $STD systemctl enable --now magicmirror
  193. msg_ok "Created Service"
  194. echo "export TERM='xterm-256color'" >>/root/.bashrc
  195. if ! getent shadow root | grep -q "^root:[^\!*]"; then
  196. msg_info "Customizing Container"
  197. if [ "$PCT_OSTYPE" == "debian" ]; then rm -rf /etc/motd /etc/update-motd.d/10-uname; else chmod -x /etc/update-motd.d/*; fi
  198. touch ~/.hushlogin
  199. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  200. mkdir -p $(dirname $GETTY_OVERRIDE)
  201. cat <<EOF >$GETTY_OVERRIDE
  202. [Service]
  203. ExecStart=
  204. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  205. EOF
  206. systemctl daemon-reload
  207. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  208. msg_ok "Customized Container"
  209. fi
  210. if [[ "${SSH_ROOT}" == "yes" ]]; then sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config; systemctl restart sshd; fi
  211. msg_info "Cleaning up"
  212. $STD apt-get autoremove
  213. $STD apt-get autoclean
  214. msg_ok "Cleaned"