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