Skip to content

Instantly share code, notes, and snippets.

@Nottt
Last active April 6, 2024 13:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Nottt/d7a3ae3c7b05f74947084ea3420141e2 to your computer and use it in GitHub Desktop.
Save Nottt/d7a3ae3c7b05f74947084ea3420141e2 to your computer and use it in GitHub Desktop.
Cloudbox install script
#!/bin/bash
# Docker Setup for streaming backend using cloudbox, intended to be run as root :
## To Do : Pass arguments to script if reboot is necessary, edit only the necessary line in plex-autoscan config file.
KERNEL=$(uname -r)
BASE=4.10
echo $KERNEL
if [ "$KERNEL" != "$BASE" ]; then
tput setaf 1; echo "You don't need to reboot, continuing install" && tput setaf 7;
else
tput setaf 1; echo "You have to reboot to update kernel, please wait" && tput setaf 7;
crontab -l | { cat; echo "@reboot /root/install.sh > /root/install.log 2>&1"; } | crontab -
apt-get update && apt install linux-generic-hwe-16.04 -y && tput setaf 1; echo "Rebooting now" && reboot
fi
## Set Variables
if [ -z "$user" ] || [ -z "$DOMAIN" ] || [ -z "$EMAIL" ] || [ -z "$RCLONEREMOTE" ] || [ -z "$RCLONEBACKUP" ] || [ -z "$PLEXDRIVE" ] || [ -z "$LOCALSTORAGE" ] || [ -z "$RCLONEID" ] || [ -z "$RCLONESECRET" ] || [ -z "$RCLONETOKEN" ] || [ -z "$PASSWORD" ]; then
$user=$1
$DOMAIN=$2
$EMAIL=$3
$RCLONEREMOTE=$4
$RCLONEBACKUP=$5
$PLEXDRIVE=$6
$LOCALSTORAGE=$7
$RCLONEID=$8
$RCLONESECRET=$8
$RCLONETOKEN=$9
$PASSWORD=$10
fi
while [ -z "$user" ]; do
>&2 echo -n 'Your desired User : '
read user
done
while [ -z "$DOMAIN" ]; do
>&2 echo -n 'Your domain : '
read DOMAIN
done
while [ -z "$EMAIL" ]; do
>&2 echo -n 'Your email : '
read EMAIL
done
while [ -z "$RCLONEREMOTE" ]; do
>&2 echo -n 'Desired name for rclone remote : '
read RCLONEREMOTE
done
while [ -z "$RCLONEBACKUP" ]; do
>&2 echo -n 'Desired path in google drive for backups : '
read RCLONEBACKUP
done
PLEXDRIVE=
>&2 echo -n 'If you have a plexdrive backup in google drive, insert rclone path here, or press enter : '
read PLEXDRIVE
while [ -z "$LOCALSTORAGE" ]; do
>&2 echo -n 'How much disk space to use to cache files : '
read LOCALSTORAGE
done
RCLONEID=
>&2 echo -n 'Your rclone ID, just press enter if empty : '
read RCLONEID
RCLONESECRET=
>&2 echo -n 'Your rclone secret, just press enter if empty : '
read RCLONESECRET
RCLONETOKEN=
>&2 echo -n 'Your rclone token, just press enter if empty : '
read RCLONETOKEN
while [ -z "$PASSWORD" ]; do
>&2 echo -n 'Your desired rutorrent password : '
read PASSWORD
done
# Install stuff, set time to UTC
timedatectl set-timezone Etc/UTC && apt-get update && apt-get upgrade -y && apt install linux-generic-hwe-16.04 -y && apt install fuse cifs-utils iotop lsof pv ncdu nload htop iotop git python-pip unattended-upgrades -y && easy_install -U pip && pip install ansible==2.3.1.0 && apt-get autoremove -y && apt-get autoclean -y
# Create user for cloudbox
groupadd docker --system
adduser --disabled-login --gecos "" $user && usermod -aG sudo $user && usermod -u 1010 $user && groupmod -g 1010 $user && usermod -aG docker $user
# Open necessary ports, assumes ssh port is 22
tput setaf 1; echo "WARNING : DO NOT USE A DIFFERENT PORT FOR SSH OTHER THAN 22 OR YOU WILL BE LOCKED OUT" && tput setaf 7;
apt-get install ufw && ufw default deny incoming && ufw default allow outgoing && ufw allow 7421 && ufw allow 9000 && ufw allow 9117 && ufw allow 5075 && ufw allow 7878 && ufw allow 8989 && ufw allow 3000 && ufw allow 8181 && ufw allow 80 && ufw allow 443 && ufw allow 6789 && ufw allow 3111 && ufw allow ssh && ufw allow 32400 && ufw allow 3468 && echo "y" | ufw enable
# Create necessary paths
mkdir -p /mnt/unionfs /mnt/plexdrive /mnt/local /opt/plexdrive /root/.config/rclone/ /home/$user/downloads/ && chown $user:$user -R /mnt && chmod 770 -R /mnt
# Install cloudbox
git clone http://github.com/l3uddz/cloudbox /home/$user/cloudbox/
## Change settings.yml
cat > /home/$user/cloudbox/settings.yml <<EOF
user: $user
passwd: ""
domain: $DOMAIN
email: $EMAIL
nzbget:
downloads: "/home/{{user}}/downloads/nzbget"
rutorrent:
passwd: $PASSWORD
downloads: "/home/{{user}}/downloads/rutorrent"
plex:
tag: public
transcodes: "/home/{{user}}/transcodes"
plex_autoscan:
ip: "0.0.0.0"
rclone:
version: 1.38
unionfs_cleaner:
max_local_gigabytes: $LOCALSTORAGE
size_check_mins: 15
rclone_remote: "$RCLONEREMOTE:"
backup:
tgz_dest: "/home/{{user}}/Backups"
rsync_dest: rsync://somehost.com/Backups
rclone_dest: $RCLONEBACKUP
use_rsync: false
use_rclone: true
cron_time: weekly
cron_state: absent
EOF
## Install
cd /home/$user/cloudbox/ && ansible-playbook cloudbox.yml --tags full
# Set rclone for user and root
if [ -z $RCLONETOKEN ]; then
rclone config && mkdir -p /opt/rclone/ && cp /root/.config/rclone/rclone.conf /opt/rclone/rclone.conf
else
mkdir -p /opt/rclone/
cat > /opt/rclone/rclone.conf <<EOF
[$RCLONEREMOTE]
type = drive
client_id = $RCLONEID
client_secret = $RCLONESECRET
token = $RCLONETOKEN
EOF
cat > /root/.config/rclone/rclone.conf <<EOF
[$RCLONEREMOTE]
type = drive
client_id = $RCLONEID
client_secret = $RCLONESECRET
token = $RCLONETOKEN
EOF
fi
# Setup plexdrive
if [ -z $PLEXDRIVE ]; then
tput setaf 1; echo "You'll be redirected to plexdrive setup, you have 4 minutes to complete it" && tput setaf 7;
timeout --foreground 240s /opt/plexdrive/plexdrive mount -v 3 --refresh-interval=1m --chunk-check-threads=8 --chunk-load-threads=8 --chunk-load-ahead=4 --max-chunks=250 --fuse-options=allow_other,read_only --config=/opt/plexdrive --cache-file=/opt/plexdrive/cache.bolt /mnt/plexdrive && systemctl enable plexdrive
else
rclone copy gdrive:$PLEXDRIVE /opt/plexdrive && systemctl enable plexdrive
fi
# Grab Plex Autoscan URL
IP=$(curl ipecho.net/plain)
sleep 5s
key=$(awk -vFS=/ '$0 ~ "http://0.0.0.0:3468/"{print $NF}' /opt/plex_autoscan/*.log)
tput setaf 1; echo "Your Plex Autoscan URL when adding to sonarr/etc is http://$IP:3468/$key - please save it" && tput setaf 7;
# Grab the Plex AutoScan Token
# credits: https://raw.githubusercontent.com/wernight/docker-plex-media-server/master/root/usr/local/bin/retrieve-plex-token
if [ -z "$PLEX_LOGIN" ] || [ -z "$PLEX_PASSWORD" ]; then
PLEX_LOGIN=$1
PLEX_PASSWORD=$2
fi
while [ -z "$PLEX_LOGIN" ]; do
>&2 echo -n 'Your Plex login (e-mail or username): '
read PLEX_LOGIN
done
while [ -z "$PLEX_PASSWORD" ]; do
>&2 echo -n 'Your Plex password: '
read PLEX_PASSWORD
done
>&2 echo 'Retrieving a X-Plex-Token using Plex login/password...'
curl -qu "${PLEX_LOGIN}":"${PLEX_PASSWORD}" 'https://plex.tv/users/sign_in.xml' \
-X POST -H 'X-Plex-Device-Name: PlexMediaServer' \
-H 'X-Plex-Provides: server' \
-H 'X-Plex-Version: 0.9' \
-H 'X-Plex-Platform-Version: 0.9' \
-H 'X-Plex-Platform: xcid' \
-H 'X-Plex-Product: Plex Media Server'\
-H 'X-Plex-Device: Linux'\
-H 'X-Plex-Client-Identifier: XXXX' --compressed >/tmp/plex_sign_in
X_PLEX_TOKEN=$(sed -n 's/.*<authentication-token>\(.*\)<\/authentication-token>.*/\1/p' /tmp/plex_sign_in)
if [ -z "$X_PLEX_TOKEN" ]; then
cat /tmp/plex_sign_in
rm -f /tmp/plex_sign_in
>&2 echo 'Failed to retrieve the X-Plex-Token.'
exit 1
fi
rm -f /tmp/plex_sign_in
>&2 echo "Your X_PLEX_TOKEN:"
# Edit plex_autoscan config files
cat > /opt/plex_autoscan/config/config.json <<EOF
{
"DOCKER_NAME": "plex",
"PLEX_DATABASE_PATH": "/opt/plex/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db",
"PLEX_EMPTY_TRASH": true,
"PLEX_EMPTY_TRASH_CONTROL_FILES": [
"/mnt/unionfs/mounted.bin"
],
"PLEX_EMPTY_TRASH_MAX_FILES": 100,
"PLEX_EMPTY_TRASH_ZERO_DELETED": false,
"PLEX_LD_LIBRARY_PATH": "/usr/lib/plexmediaserver",
"PLEX_LOCAL_URL": "http://plex.{{domain}}",
"PLEX_SCANNER": "/usr/lib/plexmediaserver/Plex\\ Media\\ Scanner",
"PLEX_SECTION_PATH_MAPPINGS": {
"1": [
"/Movies/"
],
"2": [
"/TV/"
]
},
"PLEX_SUPPORT_DIR": "/var/lib/plexmediaserver/Library/Application\\ Support",
"PLEX_TOKEN": "$X_PLEX_TOKEN",
"PLEX_USER": "plex",
"PLEX_WAIT_FOR_EXTERNAL_SCANNERS": true,
"SERVER_ALLOW_MANUAL_SCAN": false,
"SERVER_FILE_EXIST_PATH_MAPPINGS": {
"/mnt/unionfs/Media": [
"/data"
]
},
"SERVER_IP": "{{plex_autoscan.ip}}",
"SERVER_MAX_FILE_CHECKS": 10,
"SERVER_PASS": "{{plex_autoscan_pass.stdout}}",
"SERVER_PATH_MAPPINGS": {
"/data/TV/": [
"/tv/"
],
"/data/Movies/": [
"/movies/"
]
},
"SERVER_PORT": 3468,
"SERVER_SCAN_DELAY": 5,
"USE_DOCKER": true,
"USE_SUDO": false
}
EOF
# Make sure the permissions are correct
chown $user:$user -R /mnt && chmod 770 -R /mnt && chown $user:$user -R /opt && chmod 770 -R /opt
echo "Server will reboot now. Enjoy" && reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment