magicmirror-install.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. 1>&2 echo -en "${CROSS}${RD} No Network! "
  46. sleep $RETRY_EVERY
  47. ((NUM--))
  48. if [ $NUM -eq 0 ]
  49. then
  50. 1>&2 echo -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. if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else msg_error "Internet NOT Connected"; exit 1; fi;
  57. RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs)
  58. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to $RESOLVEDIP"; fi;
  59. msg_info "Updating Container OS"
  60. apt-get update &>/dev/null
  61. apt-get -y upgrade &>/dev/null
  62. msg_ok "Updated Container OS"
  63. msg_info "Installing Dependencies"
  64. apt-get install -y curl &>/dev/null
  65. apt-get install -y sudo &>/dev/null
  66. apt-get install -y git &>/dev/null
  67. msg_ok "Installed Dependencies"
  68. msg_info "Setting up Node.js Repository"
  69. curl -fsSL https://deb.nodesource.com/setup_16.x | bash - &>/dev/null
  70. msg_ok "Set up Node.js Repository"
  71. msg_info "Installing Node.js"
  72. apt-get install -y nodejs &>/dev/null
  73. msg_ok "Installed Node.js"
  74. msg_info "Setting up MagicMirror Repository"
  75. git clone https://github.com/MichMich/MagicMirror /opt/magicmirror &>/dev/null
  76. msg_ok "Set up MagicMirror Repository"
  77. msg_info "Installing MagicMirror"
  78. cd /opt/magicmirror &>/dev/null
  79. npm install --only=prod --omit=dev &>/dev/null
  80. cat <<EOF > /opt/magicmirror/config/config.js
  81. let config = {
  82. address: "0.0.0.0",
  83. port: 8080,
  84. basePath: "/",
  85. ipWhitelist: [],
  86. useHttps: false,
  87. httpsPrivateKey: "",
  88. httpsCertificate: "",
  89. language: "en",
  90. locale: "en-US",
  91. logLevel: ["INFO", "LOG", "WARN", "ERROR"],
  92. timeFormat: 24,
  93. units: "metric",
  94. serverOnly: true,
  95. modules: [
  96. {
  97. module: "alert",
  98. },
  99. {
  100. module: "updatenotification",
  101. position: "top_bar"
  102. },
  103. {
  104. module: "clock",
  105. position: "top_left"
  106. },
  107. {
  108. module: "calendar",
  109. header: "US Holidays",
  110. position: "top_left",
  111. config: {
  112. calendars: [
  113. {
  114. symbol: "calendar-check",
  115. url: "webcal://www.calendarlabs.com/ical-calendar/ics/76/US_Holidays.ics"
  116. }
  117. ]
  118. }
  119. },
  120. {
  121. module: "compliments",
  122. position: "lower_third"
  123. },
  124. {
  125. module: "weather",
  126. position: "top_right",
  127. config: {
  128. weatherProvider: "openweathermap",
  129. type: "current",
  130. location: "New York",
  131. locationID: "5128581", //ID from http://bulk.openweathermap.org/sample/city.list.json.gz; unzip the gz file and find your city
  132. apiKey: "YOUR_OPENWEATHER_API_KEY"
  133. }
  134. },
  135. {
  136. module: "weather",
  137. position: "top_right",
  138. header: "Weather Forecast",
  139. config: {
  140. weatherProvider: "openweathermap",
  141. type: "forecast",
  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: "newsfeed",
  149. position: "bottom_bar",
  150. config: {
  151. feeds: [
  152. {
  153. title: "New York Times",
  154. url: "https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml"
  155. }
  156. ],
  157. showSourceTitle: true,
  158. showPublishDate: true,
  159. broadcastNewsFeeds: true,
  160. broadcastNewsUpdates: true
  161. }
  162. },
  163. ]
  164. };
  165. /*************** DO NOT EDIT THE LINE BELOW ***************/
  166. if (typeof module !== "undefined") {module.exports = config;}
  167. EOF
  168. msg_ok "Installed MagicMirror"
  169. msg_info "Creating Service"
  170. service_path="/etc/systemd/system/magicmirror.service"
  171. echo "[Unit]
  172. Description=Magic Mirror
  173. After=network.target
  174. StartLimitIntervalSec=0
  175. [Service]
  176. Type=simple
  177. Restart=always
  178. RestartSec=1
  179. User=root
  180. WorkingDirectory=/opt/magicmirror/
  181. ExecStart=/usr/bin/node serveronly
  182. [Install]
  183. WantedBy=multi-user.target" > $service_path
  184. systemctl enable --now magicmirror &>/dev/null
  185. msg_ok "Created Service"
  186. PASS=$(grep -w "root" /etc/shadow | cut -b6);
  187. if [[ $PASS != $ ]]; then
  188. msg_info "Customizing Container"
  189. chmod -x /etc/update-motd.d/*
  190. touch ~/.hushlogin
  191. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  192. mkdir -p $(dirname $GETTY_OVERRIDE)
  193. cat << EOF > $GETTY_OVERRIDE
  194. [Service]
  195. ExecStart=
  196. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  197. EOF
  198. systemctl daemon-reload
  199. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  200. msg_ok "Customized Container"
  201. fi
  202. msg_info "Cleaning up"
  203. apt-get autoremove >/dev/null
  204. apt-get autoclean >/dev/null
  205. rm -rf /var/{cache,log}/* /var/lib/apt/lists/*
  206. msg_ok "Cleaned"