From 031320890fef1b9d5d0a92b9eed7190f2491628c Mon Sep 17 00:00:00 2001 From: Elton Renda Date: Thu, 13 May 2021 09:57:59 +0200 Subject: [PATCH] resolves #11, resolves #13, resolves #14, resolves #15, resolves #19, resolves #20, resolves #21, resolves #22; major changes to NPM --- .gitignore | 1 + lxc/nginx-proxy-manager/setup.sh | 169 +++++++++++++++++++------------ 2 files changed, 104 insertions(+), 66 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d54c198 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test* \ No newline at end of file diff --git a/lxc/nginx-proxy-manager/setup.sh b/lxc/nginx-proxy-manager/setup.sh index 03333db..c40c1f7 100644 --- a/lxc/nginx-proxy-manager/setup.sh +++ b/lxc/nginx-proxy-manager/setup.sh @@ -1,41 +1,54 @@ #!/usr/bin/env sh -set -e +set -euo pipefail + +trap trapexit EXIT + +GREPDIR=$(cd ${0%/*} && pwd -P) +TEMPDIR=$(mktemp -d) +TEMPLOG="$TEMPDIR/tmplog" +TEMPERR="$TEMPDIR/tmperr" +LASTCMD="" +WGETOPT="-t 1 -T 15 -q" +DEVDEPS="jq npm g++ make gcc git python3-dev musl-dev libffi-dev openssl-dev" + +cd $TEMPDIR +touch $TEMPLOG # Helpers -function info { echo -e "\e[32m[info] $*\e[39m"; } - -_temp_dir=$(mktemp -d) -cd $_temp_dir - -. /etc/os-release -_alpine_version=${VERSION_ID%.*} -_npm_url="https://github.com/jc21/nginx-proxy-manager" - -# add openresty repo -if [ ! -f /etc/apk/keys/admin@openresty.com-5ea678a6.rsa.pub ]; then - wget -q -P /etc/apk/keys/ 'http://openresty.org/package/admin@openresty.com-5ea678a6.rsa.pub' &>/dev/null - echo "http://openresty.org/package/alpine/v$_alpine_version/main" >> /etc/apk/repositories -fi - -# Update container OS -info "Updating container OS..." -apk update >/dev/null -apk upgrade &>/dev/null - -echo "fs.file-max = 65535" > /etc/sysctl.conf - -# Install prerequisites -info "Installing prerequisites..." -apk add python3 git certbot jq openresty nodejs npm yarn openssl apache2-utils &>/dev/null -python3 -m ensurepip &>/dev/null +log() { logs=$(cat $TEMPLOG | sed -e "s/34/32/g" | sed -e "s/info/success/g"); printf "\033c$logs\n\e[34m[info] $*\e[0m\n" | tee $TEMPLOG; } +runcmd() { + LASTCMD=$(grep -n "$*" "$GREPDIR/$0" | sed "s/[[:blank:]]*runcmd//"); + if [[ "$#" -eq 1 ]]; then + eval "$@" 2>$TEMPERR; + else + $@ 2>$TEMPERR; + fi +} +trapexit() { + status=$? + if [[ $status -eq 0 ]]; then + logs=$(cat $TEMPLOG | sed -e "s/34/32/g" | sed -e "s/info/success/g") + printf "\033c$logs\n"; + elif [[ -s $TEMPERR ]]; then + logs=$(cat $TEMPLOG | sed -e "s/34/31/g" | sed -e "s/info/error/g") + err=$(cat $TEMPERR | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g' | rev | cut -d':' -f1 | rev | cut -d' ' -f2-) + printf "\033c$logs\e[33m\n$0: line $LASTCMD\n\e[33;2;3m$err\e[0m\n" + else + printf "\e[33muncaught error occurred\n\e[0m" + fi + # Cleanup + rm -rf $TEMPDIR + apk del $DEVDEPS &>/dev/null +} +# Check for previous install if [ -f /etc/init.d/npm ]; then - info "Stoping services..." + log "Stoping services" rc-service npm stop &>/dev/null rc-service openresty stop &>/dev/null sleep 2 - info "Cleaning old files..." + log "Cleaning old files" # Cleanup for new install rm -rf /app \ /var/www/html \ @@ -43,21 +56,59 @@ if [ -f /etc/init.d/npm ]; then /var/log/nginx \ /var/lib/nginx \ /var/cache/nginx &>/dev/null + + log "Removing old dependancies" + apk del certbot $DEVDEPS &>/dev/null fi -# Get latest version information for nginx-proxy-manager -#_latest_release=$(wget "$_npm_url/releases/latest" -q -O - | grep -wo "jc21/.*.tar.gz") -#_latest_version=$(basename $_latest_release .tar.gz) -#_latest_version=${_latest_version#v*} +log "Checking for latest openresty repository" +. /etc/os-release +_alpine_version=${VERSION_ID%.*} +_npm_url="https://github.com/jc21/nginx-proxy-manager" +# add openresty public key +if [ ! -f /etc/apk/keys/admin@openresty.com-5ea678a6.rsa.pub ]; then + runcmd 'wget $WGETOPT -P /etc/apk/keys/ http://openresty.org/package/admin@openresty.com-5ea678a6.rsa.pub' +fi -# 2.8.1 is the last version that used alpine linux -_latest_version=2.8.1 +# Get the latest openresty repository +_repository_version=$(wget $WGETOPT "http://openresty.org/package/alpine/" -O - | grep -Eo "[0-9]{1}\.[0-9]{1,2}" | sort -uVr | head -n1) +_repository_version=$(printf "$_repository_version\n$_alpine_version" | sort -V | head -n1) +_repository="http://openresty.org/package/alpine/v$_repository_version/main" + +# Update/Insert openresty repository +grep -q 'openresty.org' /etc/apk/repositories && + sed -i "/openresty.org/c\\$_repository/" /etc/apk/repositories || echo $_repository >> /etc/apk/repositories + +# Update container OS +log "Updating container OS" +runcmd apk update +runcmd apk upgrade +echo "fs.file-max = 65535" > /etc/sysctl.conf + +# Install dependancies +log "Installing dependancies" +runcmd 'apk add python3 openresty nodejs yarn openssl apache2-utils $DEVDEPS' + +# Setup python env and PIP +log "Setting up python" +ln -sf /usr/bin/python3 /usr/bin/python +python -m venv /opt/certbot/ +runcmd 'wget $WGETOPT -c https://bootstrap.pypa.io/get-pip.py -O - | python' +# Install certbot and python dependancies +runcmd pip install --no-cache-dir -U cryptography==3.3.2 +runcmd pip install --no-cache-dir cffi certbot +ln -sf /usr/bin/certbot /opt/certbot/bin/certbot + +log "Checking for latest NPM version" +# Get latest version information for nginx-proxy-manager +runcmd 'wget $WGETOPT -O ./_latest_release $_npm_url/releases/latest' +_latest_version=$(basename $(cat ./_latest_release | grep -wo "jc21/.*.tar.gz") .tar.gz | cut -d'v' -f2) # Download nginx-proxy-manager source -info "Downloading NPM v$_latest_version..." -wget -qc $_npm_url/archive/v$_latest_version.tar.gz -O - | tar -xz +log "Downloading NPM v$_latest_version" +runcmd 'wget $WGETOPT -c $_npm_url/archive/v$_latest_version.tar.gz -O - | tar -xz' -cd nginx-proxy-manager-$_latest_version +cd ./nginx-proxy-manager-$_latest_version # Copy runtime files _rootfs=docker/rootfs mkdir -p /var/www/html && cp -r $_rootfs/var/www/html/* /var/www/html @@ -98,16 +149,8 @@ echo resolver "$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print ($2 ~ ":")? "["$2"] # Generate dummy self-signed certificate. if [ ! -f /data/nginx/dummycert.pem ] || [ ! -f /data/nginx/dummykey.pem ] then - info "Generating dummy SSL certificate..." - 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 + log "Generating dummy SSL certificate" + runcmd 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 fi # Copy app files @@ -116,16 +159,15 @@ cp -r backend/* /app cp -r global/* /app/global # Build the frontend -info "Building frontend..." +log "Building frontend" mkdir -p /app/frontend/images -cd frontend -yarn install &>/dev/null -yarn build &>/dev/null +cd ./frontend +runcmd yarn install +runcmd yarn build cp -r dist/* /app/frontend cp -r app-images/* /app/frontend/images -cd /app -info "Initalizing backend..." +log "Initalizing backend" rm -rf /app/config/default.json &>/dev/null if [ ! -f /app/config/production.json ]; then cat << 'EOF' > /app/config/production.json @@ -142,13 +184,13 @@ cat << 'EOF' > /app/config/production.json } EOF fi -yarn install &>/dev/null +runcmd cd /app && yarn install # Create required folders mkdir -p /data # Update openresty config -info "Configuring openresty..." +log "Configuring openresty" cat << 'EOF' > /etc/conf.d/openresty # Configuration for /etc/init.d/openresty @@ -161,10 +203,10 @@ rc-service openresty stop &>/dev/null if [ -f /usr/sbin/nginx ]; then rm /usr/sbin/nginx fi -ln -s /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx +ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx # Create NPM service -info "Creating NPM service..." +log "Creating NPM service" cat << 'EOF' > /etc/init.d/npm #!/sbin/openrc-run description="Nginx Proxy Manager" @@ -203,11 +245,6 @@ chmod a+x /etc/init.d/npm rc-update add npm boot &>/dev/null # Start services -info "Starting services..." -rc-service openresty start &>/dev/null -rc-service npm start &>/dev/null - -# Cleanup -info "Cleaning up..." -rm -rf $_temp_dir/nginx-proxy-manager-${_latest_version} &>/dev/null -apk del git jq npm &>/dev/null +log "Starting services" +runcmd rc-service openresty start +runcmd rc-service npm start \ No newline at end of file