• Stars
    star
    411
  • Rank 105,247 (Top 3 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Factorio init script

Factorio Init

A factorio init script in bash

Requirements

Debugging

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 (/opt/factorio-init/factorio invocation will tell you what the factorio user tries to run at start)
/opt/factorio-init/factorio 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

  • Create a directory where you want to store this script along with configuration. (either copy-paste the files or clone from github):
cd '/opt'
git clone https://github.com/Bisa/factorio-init.git
  • Rename or copy config.example to config and modify the values within according to your setup.

  • Unless you need files from the extras dir (check the below sections) you can safely remove it.

Notes for users with an OS that has a older glibc version

The config has options for declaring an alternate glibc root. The user millisa over on the factorio forums has created a wonderful guide to follow on creating this alternate glibc root (side by side).

First-run

  • If you don't have Factorio installed already, use the install command:
/opt/factorio-init/factorio install  # see help for options
  • The installation routine creates Factorio's config.ini automatically.

  • If you previously ran Factorio without this script, the existing config.ini should work fine.

Autocompletion

  • Copy/symlink or source the bash_autocompletion file
  • Ensure the factorio script is in your path
# either symlink:
ln -s /opt/factorio-init/extras/bash_autocomplete /etc/bash_completion.d/factorio
# or source:
echo "source /opt/factorio-init/extras/bash_autocomplete" >> ~/.bashrc

# then ensure factorio-init is added to your PATH, ie by:
ln -s /opt/factorio-init/factorio /usr/local/bin/factorio

# restart your shell to verify that it worked

Systemd

  • Copy the example service, adjust & reload
cp /opt/factorio-init/extras/factorio.service.example /etc/systemd/system/factorio.service
# Edit the service file to suit your environment then reload systemd
systemctl daemon-reload
  • Verify that the server starts
systemctl start factorio
systemctl status -l factorio
# Remember to enable the service at startup if you want that:
systemctl enable factorio

Note that systemd won't be able to keep track of the server process if you use this script to restart during updates. Use the config option UPDATE_PREVENT_RESTART=1 and implement your own stop/start logic to work around this.

SysvInit

  • Symlink the init script:
ln -s /opt/factorio-init/factorio /etc/init.d/factorio
# Make the script executable:
chmod +x /opt/factorio-init/factorio
# Try it out:
service factorio help
# Do not forget to enable the service at boot if you want that.

Contributing

When contributing to this repo, please ensure your contribution is covered by at least one test in test/factorio.bats or the very least: do not create pull requests with failing tests, thank you.

Test the code

Testing is done using bats-core, bats-assert and bats-support.

  • Write a test case, example:
@test "DEBUG=1 produces output" {
    # To access functions within ./factorio, source it then use the run command:
    source ./factorio
    export DEBUG=1
    run debug "TEST"
    # use the various asserts from bats-assert
    assert_output "DEBUG: TEST"
}
  • init and update the submodules (if you did not already)
git submodule init
git submodule update
  • Then run the tests, see the following sections:

With .githooks

  • Set the hooks path to our .githooks directory
git config --local core.hooksPath extras/.githooks

The extras/.githooks/pre-push will run shellcheck, local tests as well as docker tests with and without resources.

With Docker

  • Build the docker image(s) (a slightly modified ubuntu/centos)
docker build --build-arg ubuntu_version=20.04 \
			 --build-arg factorio_version=1.1.61 \
			 --tag ubuntu-finit:latest - < extras/docker/Dockerfile.ubuntu
docker build --build-arg centos_version=centos8 \
			 --build-arg factorio_version=1.1.61 \
			 --tag centos-finit:latest - < extras/docker/Dockerfile.centos

Adding --target no-test-resources to the build command will avoid downloading test resources online but it will also skip tests that rely on the resources(!)

  • Then run the image, mounting the current directory and removing the container once it's done
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init ubuntu-finit:latest extras/test
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init centos-finit:latest extras/test

Manually

Please note that some tests will be skipped unless you run them with the docker image or via .githooks, but running them manually is a quick way to get started.

  • run the tests
./extras/test/libs/bats-core/bin/bats extras/test

Using parallel, adding --jobs 10 to the above (adjust the number accordingly) will allow you to execute more tests in parallel and in turn possibly decrease the total time required to complete the run.

Thank You

  • To all who find this script useful in one way or the other
  • A big thank you to Wube for making Factorio
  • A special thanks to NoPantsMcDance, Oxyd, HanziQ, TheFactorioCube and all other frequent users of the #factorio channel @ esper.net
  • Thank you to Salzig for pointing me in the right direction when it comes to input redirection
  • At last, but not least; Thank you to all contributors and users posting issues in my github project or on the factorio forums

You are all a great source of motivation, thank you.

License

This code is realeased with the MIT license, see the LICENSE file.