Last active
October 27, 2024 23:04
-
-
Save quonic/e242a25a2548986a2caab5539887f408 to your computer and use it in GitHub Desktop.
Updates Factorio Linux server along with any mods added via git.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# This script will update a Factorio server mods that where installed via git | |
# and update the Factorio server via LinuxGSM if updates are available | |
# Requirements: | |
# * jq, git, and curl packages | |
# * Factorio server installed via LinuxGSM or web-lgsm | |
# * Change the factorio_dir variable to your Factorio server directory | |
# * Run as the same user that runs the Factorio server | |
# Features: | |
# * Updates all mods that where installed via git | |
# * Updates Factorio server via LinuxGSM if updates are available | |
# * Stops and Starts Factorio server before and after updating mods, if it was running | |
# * Uses LinuxGSM to update Factorio server | |
# Define escape colors | |
RED='\e[0;31m' | |
GREEN='\e[0;32m' | |
NC='\e[0m' # No Color | |
INFO="[${GREEN}Info${NC}]" | |
ERROR="[${RED}Error${NC}]" | |
# Update all the mods in the mods directory | |
# Define base Factorio directory | |
# If different, change this to your Factorio directory | |
factorio_dir="/home/$USER/web-lgsm/Factorio" ######## CHANGE THIS AS NEEDED ######## | |
# Define mods directory | |
mods_dir="${factorio_dir}/serverfiles/mods" | |
# Get folders in mods directory | |
folders=$(ls -d "${mods_dir}"/*/) | |
# Get Running state of the Factorio server | |
was_server_running="false" | |
# Get the running state of the Factorio server | |
# | |
# | fctrserver details | Filter the Status | Convert to lowercase | Remove ANSI color codes | |
# | ----------------- | ----------------- | --------------------- | --------------------- | |
state=$("$factorio_dir"/fctrserver details | grep --color=never -Po '(?<=Status\: ).*' | awk '{print tolower($1)}' | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g") | |
if [ "$state" == "started" ]; then | |
was_server_running="true" | |
fi | |
# Change directory to Factorio directory | |
if ! cd "$factorio_dir"; then | |
echo -e "${ERROR} Failed to change directory to: ${factorio_dir}" | |
exit 1 | |
fi | |
if [[ "${was_server_running}" == "true" ]]; then | |
# Stop Factorio server before updating mods via LinuxGSM | |
if ! ./fctrserver sp; then | |
echo -e "${ERROR} Failed to stop Factorio server" | |
exit 1 | |
fi | |
fi | |
# Go into each folder and run git pull | |
for folder in $folders; do | |
# Get leaf folder name | |
echo -e "${INFO} Updating mod: $(basename "$folder")" | |
if pushd "$folder" &>/dev/null; then | |
# Check if folder is a git repository | |
if ! git rev-parse --is-inside-work-tree &>/dev/null; then | |
echo -e "${ERROR} Not a git repository: ${folder}" | |
if ! popd; then | |
echo -e "${ERROR} Failed to leave folder: ${folder}" | |
exit 1 | |
fi | |
# Skip to next folder | |
continue | |
fi | |
# Run git pull | |
if git fetch &>/dev/null; then | |
if [[ $(git rev-parse HEAD) == $(git rev-parse "@{u}") ]]; then | |
echo -e "${INFO} Already up to date: $(basename "$folder")" | |
else | |
if git pull; then | |
echo -e "${INFO} Updated mod: $(basename "$folder")" | |
else | |
echo -e "${ERROR} Failed to update mod in folder: ${folder}" | |
fi | |
fi | |
else | |
echo -e "${ERROR} Failed to update mod in folder: ${folder}" | |
fi | |
if ! popd &>/dev/null; then | |
echo -e "${ERROR} Failed to leave folder: ${folder}" | |
exit 1 | |
fi | |
fi | |
done | |
if ! cd "$factorio_dir"; then | |
echo -e "${ERROR} Failed to change directory to: ${factorio_dir}" | |
exit 1 | |
fi | |
# Check if Factorio updates are available | |
online=$(curl -s https://factorio.com/api/latest-releases | jq -crM '.stable.headless') | |
installed=$("$factorio_dir"/serverfiles/bin/x64/factorio --version | grep --color=never -Po '(?<=Version\: )[0-9.]+' | head -n1) | |
if [ "$online" == "$installed" ]; then | |
echo -e "${INFO} Factorio is up to date" | |
else | |
echo -e "${INFO} Factorio updates are available" | |
# Update Factorio via LinuxGSM | |
if ./fctrserver u; then | |
echo -e "${INFO} Factorio updated successfully" | |
else | |
echo -e "${ERROR} Failed to update Factorio" | |
exit 1 | |
fi | |
fi | |
# If server was running start it again | |
if [ "$was_server_running" == "true" ]; then | |
# Start Factorio server after updating mods via LinuxGSM | |
if ! ./fctrserver st; then | |
echo -e "${ERROR} Failed to start Factorio server" | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment