Skip to content

Instantly share code, notes, and snippets.

@der-hugo
Created June 27, 2019 14:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save der-hugo/ee6cce6fe39fbac0e02e20bb00e6562c to your computer and use it in GitHub Desktop.
Save der-hugo/ee6cce6fe39fbac0e02e20bb00e6562c to your computer and use it in GitHub Desktop.
Run Sheepit-Client as a Linux Service
#!/bin/bash
###############################################################
# This Script creates a service and a dedicated user #
# for running sheepit-client (www.sheepit.com) on a headless #
# Linux-Server #
# #
# Author of this script is: jerome.poenisch@gmail.com #
###############################################################
###############################################################
# GLOBAL DEFINES #
###############################################################
BOLD=$(tput bold)
GREEN="${BOLD}$(tput setaf 2)"
RED="${BOLD}$(tput setaf 1)"
YELLOW="${BOLD}$(tput setaf 3)"
NORMAL=$(tput sgr0)
function SetBashTitle() {
echo -ne "\e]0; $1 \a"
}
###############################################################
# PHASE 0 - CHECK IF ROOT #
###############################################################
if [[ $EUID -ne 0 ]]
then
echo ""
echo ${RED}" ERROR: This script requires root permissions."${NORMAL}
echo "please run as: "${BOLD}"sudo "${NORMAL}"InstallSheepitAsClient.sh"
echo ""
exit 1
fi
###############################################################
# PAHSE 1 - CREATE SHEEPIT LINUX-USER #
###############################################################
SetBashTitle "Creating Sheepit Linux-User"
echo ""
echo ${GREEN}" __ CREATING SHEEPIT LINUX_USER __"${NORMAL}
echo ""
LINUX_USERNAME="sheepit"
echo ${BOLD}"Will create a dedicated linux user ${LINUX_USERNAME} for running the sheepit-client in a sandbox."${NORMAL}
user_exists=$(id -u ${LINUX_USERNAME} > /dev/null 2>&1; echo $?)
while [ $user_exists -eq 0 ]
do
echo ""
echo ${YELLOW}" Attention: "${NORMAL}"A user named "${LINUX_USERNAME}" is already present on this device!"
read -p "Was it created by running this script? [y/n]: " confirm
if [[ ${confirm} == 'y' ]]
then
echo ${YELLOW}" Attention: "${NORMAL}"If you continue the user "${LINUX_USERNAME}" and the according home directory will be deleted!"${NORMAL}
read -p ${YELOW}"Are you sure to continue? [y/n]: "${NORMAL} confirm
if [[ ${confirm} == 'y' ]]
then
# delete the user
userdel -r ${LINUX_USERNAME}
if [[ $? -ne 0 ]]
then
echo ""
echo ${RED}" ERROR: Could not delete user "${LINUX_USERNAME}"!"${NORMAL}
echo ""
exit 1
else
echo ""
echo ${GREEN}" Success: "${NORMAL}"User "${LINUX_USERNAME}" was deleted."
fi
fi
else
echo ""
read -p "Please enter a new name for the sheepit-client user: " LINUX_USERNAME
fi
user_exists=$(id -u ${LINUX_USERNAME} > /dev/null 2>&1; echo $?)
done
echo ""
echo ${BOLD}"Creating user ${LINUX_USERNAME}"${NORMAL}
read -s -p "Please enter a password for ${LINUX_USERNAME}: " LINUX_PASSWORD
echo ""
useradd -m -p ${LINUX_PASSWORD} -s /bin/bash ${LINUX_USERNAME}
if [[ $? -ne 0 ]]
then
echo ""
echo ${RED}" ERROR! Could not create user ${LINUX_USERNAME}!"${NORMAL}
echo ""
exit 1
fi
echo ""
echo ${GREEN}" Success: "${NORMAL}"New user "${LINUX_USERNAME}" was created."
###############################################################
# PHASE 3 - INSTALL REQUIRED LIBRARIES #
###############################################################
SetBashTitle "Installing required libraries"
echo ""
echo ""
echo ${GREEN}" __ INSTALLING REQUIRED LIBRARIES __"${NORMAL}
echo ""
echo "Will now install required libraries ... "
echo ""
# Java version might change in the future
apt update && apt install openjdk-8-jre-headless libglu1-mesa libsdl1.2debian
if [[ $? -ne 0 ]]
then
echo ""
echo ${RED}" ERROR: Could not install required libraries!"${NORMAL}
echo ""
exit 1
fi
echo ${GREEN}" Success: "${NORMAL}"Installed required libraries."
###############################################################
# PAHSE 4 - GET SHEEPIT CREDENTIALS #
###############################################################
SetBashTitle "Getting Sheepit.com Credentials"
echo ""
echo ""
echo ${GREEN}" __ GETTING SHEEPIT.COM CREDENTIALS __"${NORMAL}
echo ""
echo ${BOLD}"Please enter your credentials for www.sheepit.com"${NORMAL}
echo ${BOLD}"It is recommended to use an authentication-token instead of your password"${NORMAL}
echo ""
read -p "Sheepit.com Username: " SHEEPIT_USERNAME
read -s -p "Sheepit.com Password or Token: " SHEEPIT_PASSWORD
echo ""
###############################################################
# PHASE 5 - SETTING UP SERVICE #
###############################################################
SetBashTitle "Setting up Service"
echo ""
echo ""
echo ${GREEN}" __ SETTING UP SERVICE __"${NORMAL}
echo ""
cat << EOF | tee /home/${LINUX_USERNAME}/runSheepit.sh &> /dev/null
#!/bin/bash
rm -rf /home/${LINUX_USERNAME}/Sheepit &>/dev/null
mkdir /home/${LINUX_USERNAME}/Sheepit &>/dev/null
rm /home/${LINUX_USERNAME}/sheepitoutput &>/dev/null
wget -O /home/${LINUX_USERNAME}/sheepit-client "https://www.sheepit-renderfarm.com/media/applet/client-latest.php"
chown ${LINUX_USERNAME}:${LINUX_USERNAME} /home/${LINUX_USERNAME}/sheepit-client
chmod +x /home/${LINUX_USERNAME}/sheepit-client
java -jar /home/${LINUX_USERNAME}/sheepit-client -ui text -cache-dir /home/${LINUX_USERNAME}/Sheepit -compute-method CPU -login ${SHEEPIT_USERNAME} -password ${SHEEPIT_PASSWORD} > sheepitoutput
EOF
if [[ $EUID -ne 0 ]]
then
echo ""
echo ${RED}" ERROR: Could not write service files"${NORMAL}
echo ""
exit 1
fi
chown ${LINUX_USERNAME}:${LINUX_USERNAME} /home/${LINUX_USERNAME}/runSheepit.sh
if [[ $EUID -ne 0 ]]
then
echo ""
echo ${RED}" ERROR: Could not write service files"${NORMAL}
echo ""
exit 1
fi
chmod 776 /home/${LINUX_USERNAME}/runSheepit.sh
if [[ $EUID -ne 0 ]]
then
echo ""
echo ${RED}" ERROR: Could not write service files"${NORMAL}
echo ""
exit 1
fi
cat << EOF | tee /etc/systemd/system/sheepit.service &> /dev/null
[Unit]
Description=Sheepit Client
[Service]
User=${LINUX_USERNAME}
WorkingDirectory=/home/${LINUX_USERNAME}
ExecStart=/home/${LINUX_USERNAME}/runSheepit.sh
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
if [[ $EUID -ne 0 ]]
then
echo ""
echo ${RED}" ERROR: Could not write service files"${NORMAL}
echo ""
exit 1
fi
echo ${GREEN}" Success: "${NORMAL}"Service files created."
###############################################################
# PAHSE 6 - ENABLE AND START SERVICE #
###############################################################
SetBashTitle "Enabling and starting Service"
echo ""
echo ""
echo ${GREEN}" __ ENABLING AND STARTING SERVICE __"${NORMAL}
echo ""
systemctl enable sheepit.service
if [[ $EUID -ne 0 ]]
then
echo ""
echo ${RED}" ERROR: Could not enable sheepit service"${NORMAL}
echo ""
exit 1
fi
service sheepit start
service sheepit status
if [[ $? -eq 0 ]]
then
echo ""
echo ${GREEN}" __ DONE! __"${NORMAL}
echo ""
echo ${GREEN}" Sheepit-Client is now running as Service and will automatically start after booting the server."${NORMAL}
echo ${GREEN}" You can check the status using"${NORMAL}
echo " sudo service sheepit status"
echo ${GREEN}" or"${NORMAL}
echo " sudo tail -f /home/"${LINUX_USERNAME}"/sheepitoutput"
echo ""
exit 0
fi
echo ""
echo ${YELLOW}"Aparrentely the service failed! Please check the status via"${NORMAL}
echo " sudo service sheepit status"
echo ${YELLOW}"and"${NORMAL}
echo " sudo tail -f /home/"${LINUX_USERNAME}"/sheepitoutput"
echo ""
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment