magicmirror-install.sh 7.7 KB

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