forked from extern/Proxmox
Add files via upload
This commit is contained in:
parent
de97e9b5e5
commit
0c6c9fa2fe
159
npm_container.sh
Normal file
159
npm_container.sh
Normal file
@ -0,0 +1,159 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
#!/usr/bin/env bash
|
||||
while true; do
|
||||
read -p "This will create a New Nginx Proxy Manager LXC Container. Proceed(y/n)?" yn
|
||||
case $yn in
|
||||
[Yy]* ) break;;
|
||||
[Nn]* ) exit;;
|
||||
* ) echo "Please answer yes or no.";;
|
||||
esac
|
||||
done
|
||||
|
||||
set -o errexit
|
||||
set -o errtrace
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
shopt -s expand_aliases
|
||||
alias die='EXIT=$? LINE=$LINENO error_exit'
|
||||
CHECKMARK='\033[0;32m\xE2\x9C\x94\033[0m'
|
||||
trap die ERR
|
||||
trap cleanup EXIT
|
||||
|
||||
function error_exit() {
|
||||
trap - ERR
|
||||
local DEFAULT='Unknown failure occured.'
|
||||
local REASON="\e[97m${1:-$DEFAULT}\e[39m"
|
||||
local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
|
||||
msg "$FLAG $REASON"
|
||||
[ ! -z ${CTID-} ] && cleanup_ctid
|
||||
exit $EXIT
|
||||
}
|
||||
function warn() {
|
||||
local REASON="\e[97m$1\e[39m"
|
||||
local FLAG="\e[93m[WARNING]\e[39m"
|
||||
msg "$FLAG $REASON"
|
||||
}
|
||||
function info() {
|
||||
local REASON="$1"
|
||||
local FLAG="\e[36m[INFO]\e[39m"
|
||||
msg "$FLAG $REASON"
|
||||
}
|
||||
function msg() {
|
||||
local TEXT="$1"
|
||||
echo -e "$TEXT"
|
||||
}
|
||||
function cleanup_ctid() {
|
||||
if [ ! -z ${MOUNT+x} ]; then
|
||||
pct unmount $CTID
|
||||
fi
|
||||
if $(pct status $CTID &>/dev/null); then
|
||||
if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
|
||||
pct stop $CTID
|
||||
fi
|
||||
pct destroy $CTID
|
||||
elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
|
||||
pvesm free $ROOTFS
|
||||
fi
|
||||
}
|
||||
function cleanup() {
|
||||
popd >/dev/null
|
||||
rm -rf $TEMP_DIR
|
||||
}
|
||||
function load_module() {
|
||||
if ! $(lsmod | grep -Fq $1); then
|
||||
modprobe $1 &>/dev/null || \
|
||||
die "Failed to load '$1' module."
|
||||
fi
|
||||
MODULES_PATH=/etc/modules
|
||||
if ! $(grep -Fxq "$1" $MODULES_PATH); then
|
||||
echo "$1" >> $MODULES_PATH || \
|
||||
die "Failed to add '$1' module to load at boot."
|
||||
fi
|
||||
}
|
||||
TEMP_DIR=$(mktemp -d)
|
||||
pushd $TEMP_DIR >/dev/null
|
||||
|
||||
wget -qL https://raw.githubusercontent.com/tteck/Proxmox/wip/npm_setup.sh
|
||||
|
||||
load_module overlay
|
||||
|
||||
while read -r line; do
|
||||
TAG=$(echo $line | awk '{print $1}')
|
||||
TYPE=$(echo $line | awk '{printf "%-10s", $2}')
|
||||
FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
|
||||
ITEM=" Type: $TYPE Free: $FREE "
|
||||
OFFSET=2
|
||||
if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
|
||||
MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
|
||||
fi
|
||||
STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
|
||||
done < <(pvesm status -content rootdir | awk 'NR>1')
|
||||
if [ $((${#STORAGE_MENU[@]}/3)) -eq 0 ]; then
|
||||
warn "'Container' needs to be selected for at least one storage location."
|
||||
die "Unable to detect valid storage location."
|
||||
elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
|
||||
STORAGE=${STORAGE_MENU[0]}
|
||||
else
|
||||
while [ -z "${STORAGE:+x}" ]; do
|
||||
STORAGE=$(whiptail --title "Storage Pools" --radiolist \
|
||||
"Which storage pool you would like to use for the container?\n\n" \
|
||||
16 $(($MSG_MAX_LENGTH + 23)) 6 \
|
||||
"${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
|
||||
done
|
||||
fi
|
||||
info "Using '$STORAGE' for storage location."
|
||||
|
||||
CTID=$(pvesh get /cluster/nextid)
|
||||
info "LXC ID is $CTID."
|
||||
|
||||
echo -e "${CHECKMARK} \e[1;92m Updating LXC Template List... \e[0m"
|
||||
pveam update >/dev/null
|
||||
echo -e "${CHECKMARK} \e[1;92m Downloading LXC Template... \e[0m"
|
||||
OSTYPE=debian
|
||||
OSVERSION=${OSTYPE}-11
|
||||
mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($OSVERSION.*\)/\1/p" | sort -t - -k 2 -V)
|
||||
TEMPLATE="${TEMPLATES[-1]}"
|
||||
pveam download local $TEMPLATE >/dev/null ||
|
||||
die "A problem occured while downloading the LXC template."
|
||||
|
||||
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
|
||||
case $STORAGE_TYPE in
|
||||
dir|nfs)
|
||||
DISK_EXT=".raw"
|
||||
DISK_REF="$CTID/"
|
||||
;;
|
||||
zfspool)
|
||||
DISK_PREFIX="subvol"
|
||||
DISK_FORMAT="subvol"
|
||||
;;
|
||||
esac
|
||||
DISK=${DISK_PREFIX:-vm}-${CTID}-disk-0${DISK_EXT-}
|
||||
ROOTFS=${STORAGE}:${DISK_REF-}${DISK}
|
||||
|
||||
echo -e "${CHECKMARK} \e[1;92m Creating LXC... \e[0m"
|
||||
DISK_SIZE=3G
|
||||
pvesm alloc $STORAGE $CTID $DISK $DISK_SIZE --format ${DISK_FORMAT:-raw} >/dev/null
|
||||
if [ "$STORAGE_TYPE" == "zfspool" ]; then
|
||||
warn "Some containers may not work properly due to ZFS not supporting 'fallocate'."
|
||||
else
|
||||
mkfs.ext4 $(pvesm path $ROOTFS) &>/dev/null
|
||||
fi
|
||||
ARCH=$(dpkg --print-architecture)
|
||||
HOSTNAME=npm
|
||||
TEMPLATE_STRING="local:vztmpl/${TEMPLATE}"
|
||||
pct create $CTID $TEMPLATE_STRING -arch $ARCH -features nesting=1 \
|
||||
-hostname $HOSTNAME -net0 name=eth0,bridge=vmbr0,ip=dhcp -onboot 1 -cores 1 -memory 1024 \
|
||||
-ostype $OSTYPE -rootfs $ROOTFS,size=$DISK_SIZE -storage $STORAGE >/dev/null
|
||||
|
||||
MOUNT=$(pct mount $CTID | cut -d"'" -f 2)
|
||||
ln -fs $(readlink /etc/localtime) ${MOUNT}/etc/localtime
|
||||
pct unmount $CTID && unset MOUNT
|
||||
|
||||
echo -e "${CHECKMARK} \e[1;92m Starting LXC...... \e[0m"
|
||||
pct start $CTID
|
||||
pct push $CTID npm_setup.sh /npm_setup.sh -perms 755
|
||||
pct exec $CTID /npm_setup.sh
|
||||
|
||||
IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
|
||||
info "Successfully created a NPM LXC to $CTID at IP Address ${IP}:81"
|
207
npm_setup.sh
Normal file
207
npm_setup.sh
Normal file
@ -0,0 +1,207 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -o errexit
|
||||
set -o errtrace
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
shopt -s expand_aliases
|
||||
alias die='EXIT=$? LINE=$LINENO error_exit'
|
||||
CHECKMARK='\033[0;32m\xE2\x9C\x94\033[0m'
|
||||
trap die ERR
|
||||
trap 'die "Script interrupted."' INT
|
||||
|
||||
function error_exit() {
|
||||
trap - ERR
|
||||
local DEFAULT='Unknown failure occured.'
|
||||
local REASON="\e[97m${1:-$DEFAULT}\e[39m"
|
||||
local FLAG="\e[91m[ERROR:LXC] \e[93m$EXIT@$LINE"
|
||||
msg "$FLAG $REASON"
|
||||
exit $EXIT
|
||||
}
|
||||
function msg() {
|
||||
local TEXT="$1"
|
||||
echo -e "$TEXT"
|
||||
}
|
||||
|
||||
LASTCMD=""
|
||||
WGETOPT="-t 1 -T 15 -q"
|
||||
|
||||
# Cleanup
|
||||
apt-get remove --purge -y build-essential python3-dev git -qq &>/dev/null
|
||||
apt-get autoremove -y -qq &>/dev/null
|
||||
apt-get clean
|
||||
|
||||
# Install dependencies
|
||||
echo -e "${CHECKMARK} \e[1;92m Installing Dependencies... \e[0m"
|
||||
echo "fs.file-max = 65535" > /etc/sysctl.conf
|
||||
apt-get update &>/dev/null
|
||||
apt-get -y install --no-install-recommends sudo curl wget gnupg openssl ca-certificates apache2-utils logrotate build-essential python3-dev git lsb-release &>/dev/null
|
||||
|
||||
# Install Python
|
||||
echo -e "${CHECKMARK} \e[1;92m Installing Python... \e[0m"
|
||||
apt-get install -y -q --no-install-recommends python3 python3-pip python3-venv &>/dev/null
|
||||
pip3 install --upgrade setuptools &>/dev/null
|
||||
pip3 install --upgrade pip &>/dev/null
|
||||
python3 -m venv /opt/certbot/ &>/dev/null
|
||||
if [ "$(getconf LONG_BIT)" = "32" ]; then
|
||||
python3 -m pip install --no-cache-dir -U cryptography==3.3.2 &>/dev/null
|
||||
fi
|
||||
python3 -m pip install --no-cache-dir cffi certbot &>/dev/null
|
||||
#fi
|
||||
# Install openresty
|
||||
echo -e "${CHECKMARK} \e[1;92m Installing Openresty... \e[0m"
|
||||
wget -O - https://openresty.org/package/pubkey.gpg | apt-key add - &>/dev/null
|
||||
codename=`grep -Po 'VERSION="[0-9]+ \(\K[^)]+' /etc/os-release` &>/dev/null
|
||||
echo "deb http://openresty.org/package/debian $codename openresty" | tee /etc/apt/sources.list.d/openresty.list &>/dev/null
|
||||
apt-get -y update &>/dev/null
|
||||
apt-get -y install --no-install-recommends openresty &>/dev/null
|
||||
|
||||
echo -e "${CHECKMARK} \e[1;92m Setting up Node.js Repository... \e[0m"
|
||||
sudo curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - &>/dev/null
|
||||
|
||||
echo -e "${CHECKMARK} \e[1;92m Installing Node.js... \e[0m"
|
||||
sudo apt-get install -y nodejs git make g++ gcc &>/dev/null
|
||||
|
||||
echo -e "${CHECKMARK} \e[1;92m Installing Yarn... \e[0m"
|
||||
npm install --global yarn &>/dev/null
|
||||
|
||||
# Download nginx-proxy-manager source
|
||||
echo -e "${CHECKMARK} \e[1;92m Downloading NPM v2.9.14... \e[0m"
|
||||
wget https://codeload.github.com/NginxProxyManager/nginx-proxy-manager/tar.gz/v2.9.14 -O - | tar -xz &>/dev/null
|
||||
cd ./nginx-proxy-manager-2.9.14
|
||||
|
||||
# Crate required symbolic links
|
||||
echo -e "${CHECKMARK} \e[1;92m Setting up Enviroment... \e[0m"
|
||||
ln -sf /usr/bin/python3 /usr/bin/python
|
||||
ln -sf /usr/bin/certbot /opt/certbot/bin/certbot
|
||||
ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx
|
||||
ln -sf /usr/local/openresty/nginx/ /etc/nginx
|
||||
|
||||
# Update NPM version in package.json files
|
||||
sed -i "s+0.0.0+#v2.9.14+g" backend/package.json
|
||||
sed -i "s+0.0.0+#v2.9.14+g" frontend/package.json
|
||||
|
||||
# Fix nginx config files for use with openresty defaults
|
||||
sed -i 's+^daemon+#daemon+g' docker/rootfs/etc/nginx/nginx.conf
|
||||
NGINX_CONFS=$(find "$(pwd)" -type f -name "*.conf")
|
||||
for NGINX_CONF in $NGINX_CONFS; do
|
||||
sed -i 's+include conf.d+include /etc/nginx/conf.d+g' "$NGINX_CONF"
|
||||
done
|
||||
|
||||
# Copy runtime files
|
||||
mkdir -p /var/www/html /etc/nginx/logs
|
||||
cp -r docker/rootfs/var/www/html/* /var/www/html/
|
||||
cp -r docker/rootfs/etc/nginx/* /etc/nginx/
|
||||
cp docker/rootfs/etc/letsencrypt.ini /etc/letsencrypt.ini
|
||||
cp docker/rootfs/etc/logrotate.d/nginx-proxy-manager /etc/logrotate.d/nginx-proxy-manager
|
||||
ln -sf /etc/nginx/nginx.conf /etc/nginx/conf/nginx.conf
|
||||
rm -f /etc/nginx/conf.d/dev.conf
|
||||
|
||||
# Create required folders
|
||||
mkdir -p /tmp/nginx/body \
|
||||
/run/nginx \
|
||||
/data/nginx \
|
||||
/data/custom_ssl \
|
||||
/data/logs \
|
||||
/data/access \
|
||||
/data/nginx/default_host \
|
||||
/data/nginx/default_www \
|
||||
/data/nginx/proxy_host \
|
||||
/data/nginx/redirection_host \
|
||||
/data/nginx/stream \
|
||||
/data/nginx/dead_host \
|
||||
/data/nginx/temp \
|
||||
/var/lib/nginx/cache/public \
|
||||
/var/lib/nginx/cache/private \
|
||||
/var/cache/nginx/proxy_temp
|
||||
|
||||
chmod -R 777 /var/cache/nginx
|
||||
chown root /tmp/nginx
|
||||
|
||||
# Dynamically generate resolvers file, if resolver is IPv6, enclose in `[]`
|
||||
echo resolver "$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print ($2 ~ ":")? "["$2"]": $2}' /etc/resolv.conf);" > /etc/nginx/conf.d/include/resolvers.conf
|
||||
|
||||
# Generate dummy self-signed certificate.
|
||||
if [ ! -f /data/nginx/dummycert.pem ] || [ ! -f /data/nginx/dummykey.pem ]; then
|
||||
echo -e "${CHECKMARK} \e[1;92m Generating dummy SSL Certificate... \e[0m"
|
||||
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
|
||||
fi
|
||||
|
||||
# Copy app files
|
||||
mkdir -p /app/global /app/frontend/images
|
||||
cp -r backend/* /app
|
||||
cp -r global/* /app/global
|
||||
|
||||
# Build the frontend
|
||||
echo -e "${CHECKMARK} \e[1;92m Building Frontend... \e[0m"
|
||||
cd ./frontend
|
||||
export NODE_ENV=development
|
||||
yarn install --network-timeout=30000 &>/dev/null
|
||||
yarn build &>/dev/null
|
||||
cp -r dist/* /app/frontend
|
||||
cp -r app-images/* /app/frontend/images
|
||||
|
||||
# Initialize backend
|
||||
echo -e "${CHECKMARK} \e[1;92m Initializing Backend... \e[0m"
|
||||
rm -rf /app/config/default.json &>/dev/null
|
||||
if [ ! -f /app/config/production.json ]; then
|
||||
cat << 'EOF' > /app/config/production.json
|
||||
{
|
||||
"database": {
|
||||
"engine": "knex-native",
|
||||
"knex": {
|
||||
"client": "sqlite3",
|
||||
"connection": {
|
||||
"filename": "/data/database.sqlite"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
EOF
|
||||
fi
|
||||
cd /app
|
||||
export NODE_ENV=development
|
||||
yarn install --network-timeout=30000
|
||||
|
||||
# Create NPM service
|
||||
echo -e "${CHECKMARK} \e[1;92m Creating NPM Service... \e[0m"
|
||||
cat << 'EOF' > /lib/systemd/system/npm.service
|
||||
[Unit]
|
||||
Description=Nginx Proxy Manager
|
||||
After=network.target
|
||||
Wants=openresty.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
Environment=NODE_ENV=production
|
||||
ExecStartPre=-mkdir -p /tmp/nginx/body /data/letsencrypt-acme-challenge
|
||||
ExecStart=/usr/bin/node index.js --abort_on_uncaught_exception --max_old_space_size=250
|
||||
WorkingDirectory=/app
|
||||
Restart=on-failure
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
echo -e "${CHECKMARK} \e[1;92m Customizing Container... \e[0m"
|
||||
rm /etc/motd
|
||||
rm /etc/update-motd.d/10-uname
|
||||
touch ~/.hushlogin
|
||||
GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
|
||||
mkdir -p $(dirname $GETTY_OVERRIDE)
|
||||
cat << EOF > $GETTY_OVERRIDE
|
||||
[Service]
|
||||
ExecStart=
|
||||
ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
|
||||
EOF
|
||||
systemctl daemon-reload
|
||||
systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
|
||||
|
||||
# Start services
|
||||
echo -e "${CHECKMARK} \e[1;92m Starting Services... \e[0m"
|
||||
systemctl enable npm &>/dev/null
|
||||
systemctl start openresty
|
||||
systemctl start npm
|
||||
|
||||
echo -e "${CHECKMARK} \e[1;92m Cleanup... \e[0m"
|
||||
rm -rf /npm_setup.sh /var/{cache,log}/* /var/lib/apt/lists/*
|
Loading…
Reference in New Issue
Block a user