#!/bin/bash # This script attempts to automate everything in the following forum posts # # Sage install on linux - http://forums.sagetv.com/forums/showthread.php?p=585243#post585243 # Sage OpenDCT install # # updated 7 March 2016 - 2221 # updated 10 July 2016 - 2247 - update download links to current version # DEBUG_LEVEL=2 SAGE_SERVER_DIR="/opt/sagetv/server" dbg_echo () { if [ $DEBUG_LEVEL -ge 2 ]; then echo -e "-- $1"; fi; } # TODO: # 1) Is Sage currently installed and running # 1) Detect and Backup a current install # - look for /opt/sagetv/server/ directory # - if exists, continue, else prompt user if current install exists? Allow them to enter location # - if no current install go to step to # - if current install exists, backup Wiz.Bin and Sage.Properties files main() { echo "" echo "========= SageTV Autoinstall ========" echo "" # #---------------------------------------------------------- #---------- Print a greeting and explanation -------------- #---------------------------------------------------------- # echo "Welcome to the SageTV auto install script for Ubuntu Linux (currently)." echo "" echo " This script will attempt to install or upgrade to the latest SageTV" echo " with the following steps:" echo " 1) Veriy a valid OS (script knows how to install)" echo " 2) Verify SageTV is not running" echo " 3) If Sage is installed, check if a new version is available (or stop)" echo " 4) If Sage is installed, backup Wiz.bin and Sage.properties files" echo " 5) Update the OS" echo " 6) Verify IP info" echo " 7) Download and install Java 7" echo " 8) Download and install the latest SageTV build" echo " 9) Adjust your firewall to allow clients and network encoders" echo " 10) Install and configure network encoders" echo " 11) prompt for reboot" Verify_OS Is_Sage_Running #Version_Check Verify_Space Backup_Files Verify_IP Update_OS Install_Java Get_Install_Sage Adjust_Firewall Get_Install_OpenDCT echo "" echo " PLEASE PLEASE PLEASE Restart your computer so that every thing comes up happy" echo "" echo " Run the command: sudo init 6" echo "" dbg_echo " For folks testing in a VM, make sure you have enough storage as 25 GB are required" # to fix this reduce the following line: seeker/video_storage=/var/media/tv,25000000000,2; # grep -n video_storage Sage.properties # sudo nano -c Sage.properties (make sure to stop sagetv first) } Verify_OS() { # #---------------------------------------------------------- #---------- Check for supported linux version ------------ #---------------------------------------------------------- # # Specifically, is this a version of linux I've actually tested the script with? echo "" echo "========= 1) Determine Linux Version ========" echo "" # Does this appear to be Ubuntu if grep -q ubuntu /proc/version; then # if this looks like ubuntu, check the relase information LINUX_VERSION="/etc/lsb-release" # if the file exists if [ -f $LINUX_VERSION ] then UBUNTU_VERSION=$( grep DESCRIP /etc/lsb-release | cut -d '=' -f 2 | tr -d '"' ) if [ "$UBUNTU_VERSION" != "Ubuntu 16.04.1 LTS" ] && [ "$UBUNTU_VERSION" != "Ubuntu 14.04.5 LTS" ] then echo "" echo "Ubuntu Version $UBUNTU_VERSION, This script has not been tested with this version" echo " !!!! Proceed at your own risk, this may or may not work !!!!" echo "" read -n1 -p "Press [C] to [C]ontinue, Press any other key to exit. \n" yn case $yn in [Cc]* ) echo "Continuing"; break;; * ) echo ""; exit;; esac else echo "" echo "[GOOD] OS appears to be a supported version of Ubuntu: [$UBUNTU_VERSION]" fi fi else # This does not appear to be Ubuntu, warn the user and then let them decide what to do echo "" echo "[WARNING] Could not validate this to be an UBUNTU build. This script has only been tested on" echo " Ubuntu 14.04.3 LTS - 64 bit. It will probably work on other Debian 64bit builds." echo "" read -n1 -p "Press [C] to [C]ontinue, Press any other key to exit. " yn case $yn in [Cc]* ) echo "Continuing"; break;; * ) echo ""; exit;; esac fi echo "" read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 echo "" } Is_Sage_Running() { # #---------------------------------------------------------- #---------- Look for a SageTV PID file -------------------- #---------------------------------------------------------- # echo "" echo "========= 2) Is Sage already installed and/or running ========" echo "" dbg_echo "See if Sage.jar is running and get its PID" # beware not to store the pid of the grep for Sage.jar #SAGEPID=$(ps aux | grep -m 1 Sage.jar | tr -s ' ' | cut -d ' ' -f 2) SAGEPID=$(ps aux | grep -v 'grep' | grep Sage.jar | tr -s ' ' | cut -d ' ' -f 2) if [ -z "$SAGEPID" ] then dbg_echo " No, there is no apparent running process\n"; SAGEPID=0 else dbg_echo " Yes, there Sage appears to be running with PID: $SAGEPID\n"; fi # #---------------------------------------------------------- #---------- Look for a SageTV PID file -------------------- #---------------------------------------------------------- # PID_FILE="/var/run/sagetv.pid" dbg_echo "See if a SageTV PID file exists: $PID_FILE" if [ -f $PID_FILE ] then # extract the value of the pid file into a variable # check if there was a value SAGEPIDFILE=$(<$PID_FILE) if [ -z "$SAGEPIDFILE" ] then # No PID in the file... wierd, stop out of caution dbg_echo " PID file found, but is empty\n" dbg_echo " The file should not be empty, this script will stop out of an abundance of caution." dbg_echo " If you have verified that Sage is not running, delete the sagetv.pid file, or ask for help\n" SAGEPIDFILE=1 else # Do the process IDs match (and are the not 0, which I'm using as invalid) if [ "$SAGEPID" -eq "$SAGEPIDFILE" ] && [ "$SAGEPIDFILE" -ne "0" ] then # Great, the PIDs match... Sage is running, stop script below dbg_echo " Yes, PID file found and matches running process number : $SAGEPID\n" else # The PIDs don't match, but we got this far... stop out of caution dbg_echo " Yes, PID file found, but does not match running process or invalid" dbg_echo " File PID: $SAGEPIDFILE" dbg_echo " Process PID: $SAGEPID\n" dbg_echo " If File PID above is 0, this script will stop out of an abundance of caution." dbg_echo " If you have verified that Sage is not running, delete the sagetv.pid file, or ask for help\n" SAGEPIDFILE=1 fi fi else # hurray! I'm fairly certain sage is not running, allow install to continue. dbg_echo " No PID file found\n" SAGEPIDFILE=0 fi # Check the above configured variables and only continue if there does not appear to be a running Sage. if [ "$SAGEPIDFILE" -gt "0" ] || [ "$SAGEPID" -gt "0" ] then echo "[ERROR] SageTV appears to be running. Please stop SageTV and re-run this script" echo " options include: sudo /opt/sagetv/server/stopsage" echo " sudo service sagetv stop" echo "" exit else echo "[GOOD] SageTV does not appear to be running." echo "" fi read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 echo "" } Backup_Files() { # #---------------------------------------------------------- #------------- See if this is an overinstall -------------- #------------- Backup files as appropriate ---------------- #---------------------------------------------------------- # echo "" echo "========= 3) Backup files if needed ========" echo "" dbg_echo " See if this is an overinstall/upgrade. If so, backup config and db" dbg_echo " Script will only continue automatically if both or neither file exist\n" FILE_DB="$SAGE_SERVER_DIR/Wiz.bin" FILE_CFG="$SAGE_SERVER_DIR/Sage.properties" if [ ! -f $FILE_DB ] && [ ! -f $FILE_CFG ] then echo "[GOOD] Neither Wiz.bin or Sage.properties exist" echo "" elif [ -f $FILE_DB ] && [ -f $FILE_CFG ] then echo "Backup Wiz.bin and Sage.properties exist" echo "" dbg_echo " Back up Wiz.bin" cp $FILE_DB ~/ if [ ! $? -eq 0 ] then echo "[ERROR] Backup of $FILE_DB failed (cp error $?). Stopping script." exit fi dbg_echo " Back up Sage.properties" cp $FILE_CFG ~/ if [ ! $? -eq 0 ] then echo "[ERROR] Backup of $FILE_CFG failed (cp error $?). Stopping script." exit fi echo "" echo "[GOOD] Both files backed up with no errors" echo "" else if [ -f $FILE_DB ]; then echo "Only Wiz.bin exists. This is odd, script will now stop"; fi if [ -f $FILE_CFG ]; then echo "Only Sage.properties exists. This is odd, script will now stop"; fi echo "" exit fi read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 echo "" } Verify_Space() { ## Verify Space # space to copy wiz.bin, sage.properties, to backup sage dir and install new sage # echo "" echo "========= 4) Verify Free Space ========" echo "" # # Space for Java # Space for updates # Space for Sage # Space for Phoenix # Space for Comskip # echo "This does not currently do anything, continuing to next section" } Verify_IP() { # 5) get my ip information HOST_IP=$(ifconfig eth0 | grep "inet " | awk -F'[: ]+' '{ print $4 }') # get my netmask HOST_NETMASK=$(ifconfig eth0 | grep "inet " | awk -F'[: ]+' '{ print $8 }') # get my gateway HOST_GATEWAY=$(route -n | grep UG | awk '{print $2}') echo "" echo "========= 5) Check System Network Settings ========" echo " IP = $HOST_IP (eth0)" echo " NETMASK = $HOST_NETMASK (eth0)" echo " GATEWAY = $HOST_GATEWAY" echo "" if [ ! -z "$HOST_IP" ] && [ ! -z "$HOST_NETMASK" ] && [ ! -z "$HOST_GATEWAY" ] then echo "[GOOD] You appear to have a good network connection" echo "" else echo "[ERROR] You do not appear to have network connectivity." echo " This script is looking for eth0 connectivity to the internet to" echo " allow the download of all appropriate software. Verify your network" echo " connectivity with the ifconfig command. If eth0 exists or if is a " echo " different network id (ex. eth1) please post the output of the ifconfig" echo " command in the forum. This script will now stop." echo "" exit fi read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 } Update_OS() { # 6) Check for and install updates # Log in as the user you created during installation and with the configured password. Run the below commands. echo "" echo "========= 6) Update the OS ========" echo "" echo " Check for and get OS updates" echo "" dbg_echo " apt-get update (quiet)" sudo apt-get -y --force-yes -qq update dbg_echo " apt-get upgrade (quiet)" sudo apt-get -y --force-yes -qq upgrade dbg_echo " apt-get dist-upgrade (quiet)" sudo apt-get -y --force-yes -qq dist-upgrade dbg_echo " apt-get autoclean (quiet)" sudo apt-get -y --force-yes -qq autoclean dbg_echo " apt-get autoremove (quiet)" sudo apt-get -y --force-yes -qq autoremove # missing dependancy for ffmpeg sudo apt-get install libfaad2 echo "" read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 } Enable_32bit(){ # enable 32 bit executables ex ffmpg #sudo dpkg --add-architecture i386 #sudo apt-get update #sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 } Install_Java() { # get ready to install Java JAVA_VERSION=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}') echo "" echo "========= 7) Verify / Install Java 8 ========" echo "" echo "If you are running less than Java 1.8.0_x this script" echo " will attempt to install a new/newer version" echo "" echo "[$JAVA_VERSION]" if [ "$JAVA_VERSION" contains "1.8.0" ] then echo "" echo "[GOOD] Java is already installed ($JAVA_VERSION)" else echo "" echo "[WARNING] Java is either not installed or older than 1.8.0" echo " " dbg_echo " The latest Java will now be installed\n" dbg_echo " Add the Java 8 apt repo" sudo add-apt-repository -y ppa:webupd8team/java echo "" dbg_echo " Do an update to know what is in repository (quiet)" sudo apt-get -y --force-yes -qq update echo "" dbg_echo " Install Java 8" sudo apt-get install -y --force-yes oracle-java8-installer #sudo apt-get install -y --force-yes oracle-java8-set-default fi # install old java 7? #apt-get install default-jre-headless #dpkg -i opendct_x.x.x-x_arch.deb echo "" read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 } Get_Install_Sage() { echo "" echo "========= 8) Download and install the latest SageTV build" echo "" # # -------- Get ready to install sage... what is the latest version? # # Thank you STUCKLESS for this awsome bit of code! LATEST_VERSION=`curl -s https://dl.bintray.com/opensagetv/sagetv/sagetv/ | sed 's/^.*[^0-9]\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*$/\1/' | egrep -e '9\.[0-9]+' | sort | tail -1` PKG_NUM=`curl -s https://dl.bintray.com/opensagetv/sagetv/sagetv/ | sed 's/^.*[^0-9]\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*$/\1/' | egrep -e '9\.[0-9]+' | sort | tail -1 | cut -d'.' -f1-3` LATEST_PKG="sagetv-server_" LATEST_PKG+="$PKG_NUM" LATEST_PKG+="_amd64.deb" WGET_PATH="sudo wget https://dl.bintray.com/opensagetv/sagetv/sagetv/" WGET_PATH+="$LATEST_VERSION" WGET_PATH+="/" WGET_PATH+="$LATEST_PKG" echo "The newest version available is : SageTV version - $LATEST_VERSION" echo "" # if a SageTV install exists, is it this version? if so... skip? SAGE_PROPERTIES="/opt/sagetv/server/Sage.properties" # if the file exists if [ -f $SAGE_PROPERTIES ] then CURRENT_SAGE_VERSION=$(grep "version=SageTV" /opt/sagetv/server/Sage.properties | awk '{print $2}') if [ "$CURRENT_SAGE_VERSION" = "$LATEST_VERSION" ] then echo "" echo "[WARNING] You appear to already have SageTV installed" echo "Currently Installed Version : SageTV version - $CURRENT_SAGE_VERSION)" echo "" else #if I am not that same version... do an install echo "You do not appear to have Sage installed, installing now" echo "" dbg_echo "========= Download SageTV .deb ($LATEST_VERSION)========\n" `$WGET_PATH` #sudo wget https://bintray.com/artifact/download/opensagetv/sagetv/sagetv/$LATEST_VERSION/sagetv-server_9.0.4_amd64.deb dbg_echo "" dbg_echo "========= Install SageTV .deb ========\n" # Install the package to /opt/sagetv/server/ sudo dpkg -i $LATEST_PKG fi else echo "" dbg_echo "========= Download SageTV .deb ($LATEST_VERSION)========\n" `$WGET_PATH` #sudo wget https://bintray.com/artifact/download/opensagetv/sagetv/sagetv/9.0.4.287/sagetv-server_9.0.4_amd64.deb echo "" echo "========= Install SageTV .deb ========" # Install the package to /opt/sagetv/server/ sudo dpkg -i $LATEST_PKG fi echo "" read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 } Adjust_Firewall() { echo "" echo "========= 9) Adjust your firewall to allow clients and network encoders" echo "" # open network ports sudo ufw allow proto tcp to any port 22 2>/dev/null sudo ufw allow proto tcp to any port 7760 2>/dev/null sudo ufw allow proto tcp to any port 7818 2>/dev/null sudo ufw allow proto tcp to any port 8018 2>/dev/null sudo ufw allow proto tcp to any port 8080 2>/dev/null sudo ufw allow proto udp to any port 8271 2>/dev/null sudo ufw allow proto tcp to any port 31099 2>/dev/null sudo ufw allow proto udp to any port 31100 2>/dev/null sudo ufw allow proto tcp to any port 42024 2>/dev/null sudo iptables -I INPUT -p udp -m udp --sport 65001 -j ACCEPT #/etc/init.d/iptables save echo "" read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 } Get_Install_OpenDCT() { echo "" echo "========= 10) Install and configure network encoders" # install OpenDCT or PrimeNetEncoder # prompt the user #echo "" #while true; do # read -p "Do you wish to install [O]penDCT, [P]rimeNetEncoder or [S]top? " yn # case $yn in # [Oo]* ) echo "OpenDCT"; break;; # [Pp]* ) echo "PNE"; break;; # [Ss]* ) exit;; # * ) echo "Please answer Open, Prime or Stop.";; # esac #done # If OpenDCT is the same version OPENDCT_OLD="/opt/opendct/conf/opendct.properties" OPENDCT_NEW="/etc/opendct/conf/opendct.properties" LATEST_VERSION=`curl -s https://dl.bintray.com/opendct/Beta/releases/ | sed 's/^.*[^0-9]\([0-9]*\.[0-9]*\.[0-9]*\).*$/\1/' | egrep -e '0\.[0-9]+' | sort --version-sort | tail -1` # 0.5.13 echo "Latest version of OpenDCT is $LATEST_VERSION" # opendct_0.5.13-1_amd64.deb LATEST_PKG="opendct_" LATEST_PKG+="$LATEST_VERSION" LATEST_PKG+="-1_amd64.deb" WGET_PATH="sudo wget https://dl.bintray.com/opendct/Beta/releases/" WGET_PATH+="$LATEST_VERSION" WGET_PATH+="/" WGET_PATH+="$LATEST_PKG" # if the opendct dir exists, tell folks the version, see if we should reinstall? if [ -d OPENDCT_OLD ]; then # Control will enter here if $DIRECTORY exists. # print version number. then ask if we want to reinstall OPENDCT_VER=$(grep version.program /opt/opendct/conf/opendct.properties | cut -d = -f 2) fi if [ -d OPENDCT_NEW ]; then OPENDCT_VER=$(grep version.program /etc/opendct/conf/opendct.properties | cut -d = -f 2) fi if [ -n "$OPENDCT_VER" ]; then echo "" echo "========= OpenDCT v$OPENDCT_VER is already installed =========" else echo "" echo "========= Installing OpenDCT =========" echo "" echo "========= Download OpenDCT .deb Beta ========" `$WGET_PATH` #sudo wget https://bintray.com/opendct/Beta/download_file?file_path=releases%2F0.5.7%2Fopendct_0.5.7-1_amd64.deb -O opendct_0.5.7-1_amd64.deb echo "" echo "========= Install OpenDCT .deb Beta ========" sudo dpkg -i $LATEST_PKG #sudo dpkg -i opendct_0.5.7-1_amd64.deb echo "" echo "========= Update OpenDCT Firewall Rules ========" sudo ufw allow proto tcp to any port 9000:9100 2>/dev/null sudo ufw allow proto udp to any port 8300:8500 2>/dev/null sudo ufw allow proto tcp to any port 7818 2>/dev/null sudo ufw allow proto udp to any port 8271 2>/dev/null echo "" echo "========= OpenDCT First Run (~30 seconds) ========" sudo service opendct stop read -t 5 -p "Waiting for opendct service to stop" -n1 # Because of possible access issues, chown (change ownership) of the main opendct directories to root # Because this is being run as root, this shouldn't be an issue # TODO: install and run all this as a different user #sudo chown -R root:root /var/log/opendct/ #sudo chown -R root:root /etc/opendct/ #sudo chown -R root:root /opt/opendct/ #sudo chmod a+x /opt/opendct/console-only timeout -k 20s 20s "sudo" "/opt/opendct/console-only" fi echo "" read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 echo "" echo "========= Update Sage for Network Encoders ========" # Make sure sage is shut down sudo service sagetv stop echo "" echo "========= Validate discovery port ========" # is encoding discovery port set? # this returns either the port, or is "" if the variable does not exist ENCODING_PORT=$(grep encoding_discovery_port /opt/sagetv/server/Sage.properties | cut -d = -f 2) # if there is no encoding port... add it to the end of the file if [ -z "$ENCODING_PORT" ]; then echo " Adding network encoder port to Sage.properties file" echo "" >> /opt/sagetv/server/Sage.properties echo "encoding_discovery_port=8271" >> /opt/sagetv/server/Sage.properties fi # now print the port in the Sage.properties file ENCODING_PORT=$(grep encoding_discovery_port /opt/sagetv/server/Sage.properties | cut -d = -f 2) echo " Sage.properties - encoding_discovery_port=$ENCODING_PORT" # what if it is there but a different port? # currently it should be left as is. echo "" read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 echo "" echo "========= Validate Encoder Discovery ========" # If exists and set to false, remove line from config file sed -i.bak '/network_encoder_discovery=false/d' /opt/sagetv/server/Sage.properties # If network encoder discovery not exist, set it. # this returns either true/false, or is "" if the variable does not exist NETWORK_DISCOVERY=$(grep network_encoder_discovery /opt/sagetv/server/Sage.properties | cut -d = -f 2) # if there is no encoding port... add it to the end of the file if [ -z "$NETWORK_DISCOVERY" ]; then echo " Adding network discovery to Sage.properties file" echo "" >> /opt/sagetv/server/Sage.properties echo "network_encoder_discovery=true" >> /opt/sagetv/server/Sage.properties fi # now print the discovery status in the Sage.properties file NETWORK_DISCOVERY=$(grep network_encoder_discovery /opt/sagetv/server/Sage.properties | cut -d = -f 2) echo " Sage.properties - network_encoder_discovery=$NETWORK_DISCOVERY" echo "" read -t 5 -p "Press any key to continue, install will auto resume in 5 seconds" -n1 echo "" echo "========= Ensure OpenDCT auto starts at boot ========" sudo update-rc.d -f opendct defaults } #echo "" #echo "========= Start OpenDCT Service ========" #sudo service opendct start #echo "" #echo "========= Start SageTV Service ========" #sudo service sagetv start main