Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Wallin committed Jul 9, 2016
2 parents bd7c3c1 + c22036c commit 8ba8a8c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ A simple factorio init script for linux
If you find yourself wondering why stuff is not working the way you expect:
- Check the logs, I suggest you `tail -f /opt/factorio/factorio-current.log` in a separate session
- Enable debugging in the config and/or:
- Try running the same commands as the factorio user
- Try running the same commands as the factorio user (`/opt/factorio-init/factorio invocation` will tell you what the factorio user tries to run at start)

```bash
$ cd '/opt/factorio-init'
$ source ./config # Grab the config variables
$ echo $INVOCATION # Does this look correct to you?
$ $INVOCATION #Start the server, watch the log output for any Errors
$ /opt/factorio-init invocation
# Run this as the factorio user, example:
$ sudo -u factorio 'whatever invocation gave you'
# You should see some output in your terminal here, hopefully giving
# you a hint of what is going wrong
```

# Install
Expand Down Expand Up @@ -46,16 +47,11 @@ A simple factorio init script for linux

```bash
$ ln -s /opt/factorio-init/factorio /etc/init.d/factorio
```
- Make the script executable:

```bash
# Make the script executable:
$ chmod +x /opt/factorio-init/factorio
```
- Try it out!

```bash
# Try it out:
$ service factorio help
# Do not forget to enable the service at boot if you want that.
```

# Thank You
Expand Down
14 changes: 2 additions & 12 deletions config.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ USERNAME=factorio
USERGROUP=factorio

# The absolute path to the factorio directory
FACTORIO_PATH=/path/to/factorio
# The absolute path to the factorio binary
BINARY=${FACTORIO_PATH}/bin/x64/factorio
# Absolute path to factorios config.ini
FCONF=${FACTORIO_PATH}/config/config.ini
FACTORIO_PATH=/opt/factorio
# Server settings file, see data/server-settings.example.json
SERVER_SETTINGS=${FACTORIO_PATH}/data/server-settings.json

# Port on which you want to run the server
PORT=34197

Expand Down Expand Up @@ -64,10 +61,3 @@ UPDATE_TMPDIR=/tmp
# Extras
# Additional binary arguments, these will be sent to the binary when issuing the "start" command
EXTRA_BINARGS="--allow-commands admin-only"

#
# Refrain from changing the variables below, they are used internally by the script and should
# not be altered unless you know what you are doing
#
SAVE_NAME=factorio-init-save
INVOCATION="${BINARY} --config ${FCONF} --port ${PORT} --start-server-load-latest --server-settings ${SERVER_SETTINGS} --autosave-interval ${AUTOSAVE_INTERVAL} --autosave-slots ${AUTOSAVE_SLOTS} --latency-ms ${LATENCY} ${EXTRA_BINARGS}"
63 changes: 39 additions & 24 deletions factorio
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,20 @@ if [ -z "${HEADLESS}" ]; then
fi

if ! [ $1 == "install" ]; then
if [ -z ${FCONF} ]; then
echo "Missing FCONF, ensure it's defined in your config and try again"
if [ -z ${BINARY} ]; then
# Factorio headless only comes in x64 flavour - if you run anything else, override it in the config
BINARY="${FACTORIO_PATH}/bin/x64/factorio"
fi

if ! [ -e ${BINARY} ]; then
echo "Could not find factorio binary! ${BINARY}"
echo "(if you store your binary some place else, override BINARY='/your/path' in the config)"
exit 1
fi

if [ -z ${FCONF} ]; then
FCONF="${FACTORIO_PATH}/config/config.ini"
fi

if ! [ -e "${SERVER_SETTINGS}" ]; then
echo "Could not find factorio server settings file: ${SERVER_SETTINGS}"
Expand All @@ -54,16 +64,21 @@ if ! [ $1 == "install" ]; then

if ! [ -e ${FCONF} ]; then
echo "Could not find factorio config file: ${FCONF}"
echo "If this is the first time you run this script; You might need to generate the config.ini"
echo "(also make sure the save exist or the command will fail)"
echo "by running: sudo -u $USERNAME ${BINARY} --start-server ${SAVE_NAME}"
echo "If this is the first time you run this script you need to generate the config.ini by starting the server manually."
echo "(also make sure you have a save to run or the server will not start)"
echo
echo "Create save: sudo -u ${USERNAME} ${BINARY} --create ${FACTORIO_PATH}/saves/my_savegame"
echo "Start server: sudo -u ${USERNAME} ${BINARY} --start-server-load-latest"
echo
echo "(If you rather store the config.ini in another location, set FCONF='/your/path' in this scripts config file)"
exit 1
fi
if [ -z "${WRITE_DIR}" ]; then
# figure out the write-data path (where factorio looks for saves and mods)
# Note - this is a hefty little operation, possible cause of head ache down the road
# as it relies on the factorio write dir to live ../../ up from the binary if __PATH__executable__
# is used in the config file.. for now, that's the default so cross your fingers it will not change ;)
debug "Determining WRITE_DIR based on ${FCONF}, IF you edited write-data from the default, this probably fails"
WRITE_DIR=$(dirname "$(echo `grep "^write-data=" "$FCONF"` |cut -d'=' -f2 |sed -e 's#__PATH__executable__#'$(dirname "$BINARY")/..'#g')")
fi
debug "write path: $WRITE_DIR"
Expand All @@ -74,6 +89,9 @@ if ! [ $1 == "install" ]; then
FIFO="${WRITE_DIR}/server.fifo"
fi

# Finally, set up the invocation
INVOCATION="${BINARY} --config ${FCONF} --port ${PORT} --start-server-load-latest --server-settings ${SERVER_SETTINGS} --autosave-interval ${AUTOSAVE_INTERVAL} --autosave-slots ${AUTOSAVE_SLOTS} --latency-ms ${LATENCY} ${RCON} ${EXTRA_BINARGS}"

fi

usage(){
Expand All @@ -85,11 +103,12 @@ usage(){
echo -e " restart \t\t Restarts the server"
echo -e " status \t\t Displays server status"
echo -e " cmd command/message \t Send command/chat"
echo -e " new-game \t\t Stops the server and creates a new game"
echo -e " new-game name\t\t Stops the server and creates a new game with the specified name"
echo -e " save-game name \t Stops the server and saves game to specified save"
echo -e " load-save name \t Stops the server and loads the specified save"
echo -e " install tarball \t Installs the server with specified tarball"
echo -e " update [--dry-run] \t Updates the server"
echo -e " invocation \t\t Outputs the invocation for debugging purpose"
}

ME=`whoami`
Expand Down Expand Up @@ -264,21 +283,7 @@ install(){
exit 1
fi

if test_deps; then
if ! as_user "$BINARY --create ${SAVE_NAME}"; then
echo "Installation complete..."
echo
echo "but failed to create initial save game."
echo "Ensure you use: load-save [save-name] before starting the server for the first time."
else
echo "Install successfull!"
fi
else
echo "Installation complete..."
echo
echo "but $BINARY is missing required dependencies."
echo "Install the missing deps and ensure you use: load-save [save-name] before starting the server for the first time."
fi
echo "Installation complete, edit the config and start your server"
}

get_bin_version(){
Expand Down Expand Up @@ -459,6 +464,12 @@ case "$1" in
send_cmd "${@:2}"
;;
new-game)
if [ -z $2 ]; then
echo "You must specify a save name for your new game"
exit 1
fi
savename="${WRITE_DIR}/saves/$2"

# Stop Service
if is_running; then
send_cmd "Generating new save, please stand by"
Expand All @@ -468,11 +479,11 @@ case "$1" in
fi
fi

if ! as_user "$BINARY --create ${WRITE_DIR}/saves/${SAVE_NAME}"; then
if ! as_user "$BINARY --create ${savename}"; then
echo "Failed to create new game"
exit 1
else
echo "New game created"
echo "New game created: ${savename}.zip"
fi
;;

Expand All @@ -488,7 +499,8 @@ case "$1" in
fi
fi

if ! as_user "cp ${WRITE_DIR}/saves/${SAVE_NAME}.zip ${savename}"; then
lastsave=$(find "${WRITE_DIR}/saves" -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
if ! as_user "cp ${lastsave} ${savename}"; then
echo "Error! Failed to save game"
exit 1
fi
Expand Down Expand Up @@ -520,6 +532,9 @@ case "$1" in
update)
update "$2"
;;
inv|invocation)
echo ${INVOCATION}
;;
help|--help|-h)
usage
;;
Expand Down

0 comments on commit 8ba8a8c

Please sign in to comment.