magicmirror-install.sh 7.7 KB

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