npm_setup.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/usr/bin/env bash
  2. set -o errexit
  3. set -o errtrace
  4. set -o nounset
  5. set -o pipefail
  6. shopt -s expand_aliases
  7. alias die='EXIT=$? LINE=$LINENO error_exit'
  8. CROSS='\033[1;31m\xE2\x9D\x8C\033[0m'
  9. CHECKMARK='\033[0;32m\xE2\x9C\x94\033[0m'
  10. RETRY_NUM=5
  11. RETRY_EVERY=3
  12. NUM=$RETRY_NUM
  13. trap die ERR
  14. trap 'die "Script interrupted."' INT
  15. function error_exit() {
  16. trap - ERR
  17. local DEFAULT='Unknown failure occured.'
  18. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  19. local FLAG="\e[91m[ERROR:LXC] \e[93m$EXIT@$LINE"
  20. msg "$FLAG $REASON"
  21. exit $EXIT
  22. }
  23. function msg() {
  24. local TEXT="$1"
  25. echo -e "$TEXT"
  26. }
  27. echo -e "${CHECKMARK} \e[1;92m Setting up Container OS... \e[0m"
  28. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  29. locale-gen >/dev/null
  30. while [ "$(hostname -I)" = "" ]; do
  31. 1>&2 echo -e "${CROSS} \e[1;31m No Network: \e[0m $(date)"
  32. sleep $RETRY_EVERY
  33. ((NUM--))
  34. if [ $NUM -eq 0 ]
  35. then
  36. 1>&2 echo -e "${CROSS} \e[1;31m No Network After $RETRY_NUM Tries \e[0m"
  37. exit 1
  38. fi
  39. done
  40. echo -e "${CHECKMARK} \e[1;92m Network Connected: \e[0m $(hostname -I)"
  41. echo -e "${CHECKMARK} \e[1;92m Updating Container OS... \e[0m"
  42. apt-get update &>/dev/null
  43. apt-get -qqy upgrade &>/dev/null
  44. echo -e "${CHECKMARK} \e[1;92m Installing Dependencies... \e[0m"
  45. apt-get update &>/dev/null
  46. apt-get -qqy install \
  47. sudo \
  48. curl \
  49. wget \
  50. gnupg \
  51. openssl \
  52. ca-certificates \
  53. apache2-utils \
  54. logrotate \
  55. build-essential \
  56. python3-dev \
  57. git \
  58. lsb-release &>/dev/null
  59. echo -e "${CHECKMARK} \e[1;92m Installing Python... \e[0m"
  60. apt-get install -y -q --no-install-recommends python3 python3-pip python3-venv &>/dev/null
  61. pip3 install --upgrade setuptools &>/dev/null
  62. pip3 install --upgrade pip &>/dev/null
  63. python3 -m venv /opt/certbot/ &>/dev/null
  64. if [ "$(getconf LONG_BIT)" = "32" ]; then
  65. python3 -m pip install --no-cache-dir -U cryptography==3.3.2 &>/dev/null
  66. fi
  67. python3 -m pip install --no-cache-dir cffi certbot &>/dev/null
  68. echo -e "${CHECKMARK} \e[1;92m Installing Openresty... \e[0m"
  69. wget -q -O - https://openresty.org/package/pubkey.gpg | apt-key add - &>/dev/null
  70. codename=`grep -Po 'VERSION="[0-9]+ \(\K[^)]+' /etc/os-release` &>/dev/null
  71. echo "deb http://openresty.org/package/debian $codename openresty" | tee /etc/apt/sources.list.d/openresty.list &>/dev/null
  72. apt-get -y update &>/dev/null
  73. apt-get -y install --no-install-recommends openresty &>/dev/null
  74. echo -e "${CHECKMARK} \e[1;92m Setting up Node.js Repository... \e[0m"
  75. sudo curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - &>/dev/null
  76. echo -e "${CHECKMARK} \e[1;92m Installing Node.js... \e[0m"
  77. sudo apt-get install -y nodejs git make g++ gcc &>/dev/null
  78. echo -e "${CHECKMARK} \e[1;92m Installing Yarn... \e[0m"
  79. npm install --global yarn &>/dev/null
  80. echo -e "${CHECKMARK} \e[1;92m Downloading NPM v2.9.16... \e[0m"
  81. wget -q https://codeload.github.com/NginxProxyManager/nginx-proxy-manager/tar.gz/v2.9.16 -O - | tar -xz &>/dev/null
  82. cd ./nginx-proxy-manager-2.9.16
  83. echo -e "${CHECKMARK} \e[1;92m Setting up Enviroment... \e[0m"
  84. ln -sf /usr/bin/python3 /usr/bin/python
  85. ln -sf /usr/bin/certbot /opt/certbot/bin/certbot
  86. ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx
  87. ln -sf /usr/local/openresty/nginx/ /etc/nginx
  88. sed -i "s+0.0.0+#v2.9.16+g" backend/package.json
  89. sed -i "s+0.0.0+#v2.9.16+g" frontend/package.json
  90. sed -i 's+^daemon+#daemon+g' docker/rootfs/etc/nginx/nginx.conf
  91. NGINX_CONFS=$(find "$(pwd)" -type f -name "*.conf")
  92. for NGINX_CONF in $NGINX_CONFS; do
  93. sed -i 's+include conf.d+include /etc/nginx/conf.d+g' "$NGINX_CONF"
  94. done
  95. mkdir -p /var/www/html /etc/nginx/logs
  96. cp -r docker/rootfs/var/www/html/* /var/www/html/
  97. cp -r docker/rootfs/etc/nginx/* /etc/nginx/
  98. cp docker/rootfs/etc/letsencrypt.ini /etc/letsencrypt.ini
  99. cp docker/rootfs/etc/logrotate.d/nginx-proxy-manager /etc/logrotate.d/nginx-proxy-manager
  100. ln -sf /etc/nginx/nginx.conf /etc/nginx/conf/nginx.conf
  101. rm -f /etc/nginx/conf.d/dev.conf
  102. mkdir -p /tmp/nginx/body \
  103. /run/nginx \
  104. /data/nginx \
  105. /data/custom_ssl \
  106. /data/logs \
  107. /data/access \
  108. /data/nginx/default_host \
  109. /data/nginx/default_www \
  110. /data/nginx/proxy_host \
  111. /data/nginx/redirection_host \
  112. /data/nginx/stream \
  113. /data/nginx/dead_host \
  114. /data/nginx/temp \
  115. /var/lib/nginx/cache/public \
  116. /var/lib/nginx/cache/private \
  117. /var/cache/nginx/proxy_temp
  118. chmod -R 777 /var/cache/nginx
  119. chown root /tmp/nginx
  120. echo resolver "$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print ($2 ~ ":")? "["$2"]": $2}' /etc/resolv.conf);" > /etc/nginx/conf.d/include/resolvers.conf
  121. if [ ! -f /data/nginx/dummycert.pem ] || [ ! -f /data/nginx/dummykey.pem ]; then
  122. echo -e "${CHECKMARK} \e[1;92m Generating dummy SSL Certificate... \e[0m"
  123. openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -subj "/O=Nginx Proxy Manager/OU=Dummy Certificate/CN=localhost" -keyout /data/nginx/dummykey.pem -out /data/nginx/dummycert.pem &>/dev/null
  124. fi
  125. mkdir -p /app/global /app/frontend/images
  126. cp -r backend/* /app
  127. cp -r global/* /app/global
  128. echo -e "${CHECKMARK} \e[1;92m Building Frontend... \e[0m"
  129. cd ./frontend
  130. export NODE_ENV=development
  131. yarn install --network-timeout=30000 &>/dev/null
  132. yarn build &>/dev/null
  133. cp -r dist/* /app/frontend
  134. cp -r app-images/* /app/frontend/images
  135. echo -e "${CHECKMARK} \e[1;92m Initializing Backend... \e[0m"
  136. rm -rf /app/config/default.json &>/dev/null
  137. if [ ! -f /app/config/production.json ]; then
  138. cat << 'EOF' > /app/config/production.json
  139. {
  140. "database": {
  141. "engine": "knex-native",
  142. "knex": {
  143. "client": "sqlite3",
  144. "connection": {
  145. "filename": "/data/database.sqlite"
  146. }
  147. }
  148. }
  149. }
  150. EOF
  151. fi
  152. cd /app
  153. export NODE_ENV=development
  154. yarn install --network-timeout=30000 &>/dev/null
  155. echo -e "${CHECKMARK} \e[1;92m Creating NPM Service... \e[0m"
  156. cat << 'EOF' > /lib/systemd/system/npm.service
  157. [Unit]
  158. Description=Nginx Proxy Manager
  159. After=network.target
  160. Wants=openresty.service
  161. [Service]
  162. Type=simple
  163. Environment=NODE_ENV=production
  164. ExecStartPre=-mkdir -p /tmp/nginx/body /data/letsencrypt-acme-challenge
  165. ExecStart=/usr/bin/node index.js --abort_on_uncaught_exception --max_old_space_size=250
  166. WorkingDirectory=/app
  167. Restart=on-failure
  168. [Install]
  169. WantedBy=multi-user.target
  170. EOF
  171. echo -e "${CHECKMARK} \e[1;92m Customizing Container... \e[0m"
  172. rm /etc/motd
  173. rm /etc/update-motd.d/10-uname
  174. touch ~/.hushlogin
  175. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  176. mkdir -p $(dirname $GETTY_OVERRIDE)
  177. cat << EOF > $GETTY_OVERRIDE
  178. [Service]
  179. ExecStart=
  180. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  181. EOF
  182. systemctl daemon-reload
  183. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  184. echo -e "${CHECKMARK} \e[1;92m Starting Services... \e[0m"
  185. systemctl enable npm &>/dev/null
  186. systemctl start openresty
  187. systemctl start npm
  188. echo -e "${CHECKMARK} \e[1;92m Cleanup... \e[0m"
  189. rm -rf /npm_setup.sh /var/{cache,log}/* /var/lib/apt/lists/*