Skip to content

Commit 5e33d76

Browse files
authored
Merge pull request #14 from Bisa/master
Pull in update latest version from Bisa
2 parents 156d3d3 + 35d8219 commit 5e33d76

20 files changed

+1757
-533
lines changed

.github/workflows/ci.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Continuous Integration
2+
3+
on:
4+
push:
5+
branches: [ master, github-workflows-ci ]
6+
pull_request:
7+
branches: [ master ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
shellchecks:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@master
16+
- name: Shellchecks
17+
run: |
18+
docker run --rm -v "$GITHUB_WORKSPACE:/mnt" koalaman/shellcheck:stable extras/.githooks/*
19+
docker run --rm -v "$GITHUB_WORKSPACE:/mnt" koalaman/shellcheck:stable factorio
20+
bats:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: Checkout repo
24+
uses: actions/checkout@master
25+
- name: Checkout submodules
26+
uses: textbook/git-checkout-submodule-action@master
27+
- name: Prepare env
28+
run: |
29+
echo "factorio_version=1.1.30" >> $GITHUB_ENV
30+
echo "factorio_user=factorio" >> $GITHUB_ENV
31+
echo "factorio_group=factorio" >> $GITHUB_ENV
32+
- name: Add factorio user
33+
run: |
34+
sudo -E addgroup --system "${factorio_group}"
35+
sudo -E adduser --system --ingroup "${factorio_group}" "${factorio_user}"
36+
- name: Bats without resources
37+
run: sudo -E -u "${factorio_user}" extras/test/libs/bats-core/bin/bats extras/test --jobs 10
38+
- name: Fetch factorio
39+
run: |
40+
factorio_tar="/tmp/factorio_headless_x64_${factorio_version}.tar.xz"
41+
sudo -E wget -O "${factorio_tar}" "https://factorio.com/get-download/${factorio_version}/headless/linux64"
42+
echo "factorio_tar=${factorio_tar}" >> $GITHUB_ENV
43+
echo "FACTORIO_INIT_WITH_TEST_RESOURCES=1" >> $GITHUB_ENV
44+
- name: Bats with resources
45+
run: sudo -E -u "${factorio_user}" extras/test/libs/bats-core/bin/bats extras/test --jobs 10
46+
- name: Install factorio
47+
run: |
48+
sudo -E tar -xvf "${factorio_tar}" -C /opt
49+
sudo -E chown -R "${factorio_user}":"${factorio_group}" /opt/factorio
50+
sudo -E -u "${factorio_user}" /opt/factorio/bin/x64/factorio --create /opt/factorio/saves/server-save
51+
sudo -E -u "${factorio_user}" cp /opt/factorio/data/server-settings.example.json /opt/factorio/data/server-settings.json
52+
echo "FACTORIO_INIT_WITH_PRE_INSTALLED_GAME=1" >> $GITHUB_ENV
53+
- name: Bats with pre-installed game
54+
run: sudo -E -u "${factorio_user}" extras/test/libs/bats-core/bin/bats extras/test
55+
# docker-tests:
56+
# runs-on: ubuntu-latest
57+
# steps:
58+
# - name: Checkout repo
59+
# uses: actions/checkout@master
60+
# - name: Checkout submodules
61+
# uses: textbook/git-checkout-submodule-action@master
62+
# - name: foo
63+
# run: extras/.githooks/pre-commit

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
config
2-
factorio.service
3-
factorio-env.service
4-
systemd.env
2+
**/*.swp

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[submodule "test/libs/bats-support"]
2+
path = extras/test/libs/bats-support
3+
url = https://github.com/ztombol/bats-support
4+
[submodule "test/libs/bats-assert"]
5+
path = extras/test/libs/bats-assert
6+
url = https://github.com/ztombol/bats-assert
7+
[submodule "test/libs/bats-core"]
8+
path = extras/test/libs/bats-core
9+
url = https://github.com/bats-core/bats-core.git

README.md

Lines changed: 169 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,213 @@
1-
# Factorio Init Script
2-
A factorio init script for linux
1+
# Factorio Init
32

4-
# Dependencies
5-
Among others:
6-
- cURL
3+
A factorio init script in bash
74

8-
# Debugging
9-
If you find yourself wondering why stuff is not working the way you expect:
10-
- Check the logs, I suggest you `tail -f /opt/factorio/factorio-current.log` in a separate session
11-
- Enable debugging in the config and/or:
12-
- 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)
5+
## Requirements
6+
7+
The following software packages need to be installed:
8+
9+
- Wget
10+
- cURL
11+
- bash-completion (optional but recomended)
12+
13+
- for yum based systems do the following:
14+
15+
```bash
16+
sudo yum install curl wget bash-completion
17+
```
18+
19+
- for dnf based systems do the following:
20+
21+
```bash
22+
sudo dnf install curl wget bash-completion
23+
```
24+
25+
- for apt-get based systems do the following:
26+
27+
```bash
28+
sudo apt-get install curl wget bash-completion
29+
```
30+
31+
## Debugging
32+
33+
If you find yourself wondering why stuff is not working the way you expect:
34+
35+
- Check the logs, I suggest you `tail -f /opt/factorio/factorio-current.log` in a separate session
36+
- Enable debugging in the config and/or:
37+
- 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)
1338

1439
```bash
15-
$ /opt/factorio-init/factorio invocation
40+
/opt/factorio-init/factorio invocation
1641
# Run this as the factorio user, example:
17-
$ sudo -u factorio 'whatever invocation gave you'
42+
sudo -u factorio 'whatever invocation gave you'
1843
# You should see some output in your terminal here, hopefully giving
1944
# you a hint of what is going wrong
2045
```
2146

22-
# Install
47+
## Install
48+
2349
- Create a directory where you want to store this script along with configuration. (either copy-paste the files or clone from github):
2450

2551
```bash
26-
$ cd '/opt'
27-
$ git clone https://github.com/Bisa/factorio-init.git
52+
cd '/opt'
53+
git clone https://github.com/Bisa/factorio-init.git
2854
```
29-
- Rename config.example to config and modify the values within according to your setup.
3055

31-
## Notes for users with an OS that has a older glibc version:
56+
- Rename or copy `config.example` to `config` and modify the values within according to your setup.
57+
58+
- Unless you need files from the `extras` dir (check the below sections) you can safely remove it.
59+
60+
### Notes for users with an OS that has a older glibc version
3261

33-
- The config has options for declaring a 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 ) here:
34-
https://forums.factorio.com/viewtopic.php?t=54654#p324493
62+
The config has options for declaring an alternate glibc root. The user millisa over on the factorio forums has created a wonderful [guide](https://forums.factorio.com/viewtopic.php?t=54654#p324493) to follow on creating this alternate glibc root (side by side).
63+
64+
### First-run
3565

36-
## First-run
3766
- If you don't have Factorio installed already, use the `install` command:
3867

3968
```bash
40-
$ /opt/factorio-init/factorio install # see help for options
69+
/opt/factorio-init/factorio install # see help for options
4170
```
4271

4372
- The installation routine creates Factorio's `config.ini` automatically.
4473

4574
- If you previously ran Factorio without this script, the existing `config.ini` should work fine.
4675

47-
## Autocompletion
48-
- Copy/Symlink or source the bash_autocompletion file
76+
### Autocompletion
4977

50-
```bash
51-
$ ln -s /opt/factorio-init/bash_autocomplete /etc/bash_completion.d/factorio
52-
# OR:
53-
$ echo "source /opt/factorio-init/bash_autocomplete" >> ~/.bashrc
54-
# restart your shell to verify that it worked
55-
```
78+
- Copy/symlink or source the bash_autocompletion file
79+
- Ensure the factorio script is in your path
80+
81+
```bash
82+
# either symlink:
83+
ln -s /opt/factorio-init/extras/bash_autocomplete /etc/bash_completion.d/factorio
84+
# or source:
85+
echo "source /opt/factorio-init/extras/bash_autocomplete" >> ~/.bashrc
86+
87+
# then ensure factorio-init is added to your PATH, ie by:
88+
ln -s /opt/factorio-init/factorio /usr/local/bin/factorio
89+
90+
# restart your shell to verify that it worked
91+
```
92+
93+
### Systemd
5694

57-
## Systemd
5895
- Copy the example service, adjust & reload
5996

60-
```bash
61-
$ cp /opt/factorio-init/factorio.service.example /etc/systemd/system/factorio.service
62-
# Edit the service file to suit your environment then reload systemd
63-
$ systemctl daemon-reload
64-
```
97+
```bash
98+
cp /opt/factorio-init/extras/factorio.service.example /etc/systemd/system/factorio.service
99+
# Edit the service file to suit your environment then reload systemd
100+
systemctl daemon-reload
101+
```
65102

66103
- Verify that the server starts
67104

68-
```bash
69-
$ systemctl start factorio
70-
$ systemctl status -l factorio
71-
# Remember to enable the service at startup if you want that:
72-
$ systemctl enable factorio
73-
```
105+
```bash
106+
systemctl start factorio
107+
systemctl status -l factorio
108+
# Remember to enable the service at startup if you want that:
109+
systemctl enable factorio
110+
```
111+
112+
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.
113+
114+
### SysvInit
74115

75-
## SysvInit
76116
- Symlink the init script:
77117

78-
```bash
79-
$ ln -s /opt/factorio-init/factorio /etc/init.d/factorio
80-
# Make the script executable:
81-
$ chmod +x /opt/factorio-init/factorio
82-
# Try it out:
83-
$ service factorio help
84-
# Do not forget to enable the service at boot if you want that.
85-
```
118+
```bash
119+
ln -s /opt/factorio-init/factorio /etc/init.d/factorio
120+
# Make the script executable:
121+
chmod +x /opt/factorio-init/factorio
122+
# Try it out:
123+
service factorio help
124+
# Do not forget to enable the service at boot if you want that.
125+
```
126+
127+
## Contributing
128+
129+
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.
130+
131+
### Test the code
132+
133+
Testing is done using [bats-core](https://github.com/bats-core/bats-core), [bats-assert](https://github.com/ztombol/bats-assert) and [bats-support](https://github.com/ztombol/bats-support).
134+
135+
- Write a test case, example:
136+
137+
```bash
138+
@test "DEBUG=1 produces output" {
139+
# To access functions within ./factorio, source it then use the run command:
140+
source ./factorio
141+
export DEBUG=1
142+
run debug "TEST"
143+
# use the various asserts from bats-assert
144+
assert_output "DEBUG: TEST"
145+
}
146+
```
147+
148+
- init and update the submodules (if you did not already)
149+
150+
```bash
151+
git submodule init
152+
git submodule update
153+
```
154+
155+
- Then run the tests, see the following sections:
156+
157+
#### With .githooks
158+
159+
- Set the hooks path to our .githooks directory
160+
161+
```bash
162+
git config --local core.hooksPath extras/.githooks
163+
```
164+
165+
The ```extras/.githooks/pre-commit``` will run shellcheck, local tests as well as docker tests with and without resources.
166+
167+
#### With Docker
168+
169+
- Build the docker image(s) (a slightly modified ubuntu/centos)
170+
171+
```bash
172+
docker build --build-arg ubuntu_version=20.04 \
173+
--build-arg factorio_version=1.1.30 \
174+
--tag ubuntu-finit:latest - < extras/docker/Dockerfile.ubuntu
175+
docker build --build-arg centos_version=centos8 \
176+
--build-arg factorio_version=1.1.30 \
177+
--tag centos-finit:latest - < extras/docker/Dockerfile.centos
178+
```
179+
180+
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(!)
181+
182+
- Then run the image, mounting the current directory and removing the container once it's done
183+
184+
```bash
185+
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init ubuntu-finit:latest extras/test
186+
docker run -it --rm -v "$(pwd):/opt/factorio-init" --workdir /opt/factorio-init centos-finit:latest extras/test
187+
```
188+
189+
#### Manually
190+
191+
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.
192+
193+
- run the tests
194+
195+
```bash
196+
./extras/test/libs/bats-core/bin/bats test
197+
```
198+
199+
Using [parallel](https://www.gnu.org/software/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.
200+
201+
## Thank You
86202

87-
# Thank You
88203
- To all who find this script useful in one way or the other
89-
- A big thank you to [Wube](https://www.factorio.com/team) for making [Factorio](https://www.factorio.com/)
204+
- A big thank you to [Wube](https://www.factorio.com/game/about) for making [Factorio](https://www.factorio.com/)
90205
- A special thanks to NoPantsMcDance, Oxyd, HanziQ, TheFactorioCube and all other frequent users of the [#factorio](irc://irc.esper.net/#factorio) channel @ esper.net
91206
- Thank you to Salzig for pointing me in the right direction when it comes to input redirection
92207
- At last, but not least; Thank you to all [contributors](https://github.com/Bisa/factorio-init/graphs/contributors) and users posting [issues](https://github.com/Bisa/factorio-init/issues) in my [github](https://github.com/Bisa/factorio-init/) project or on the [factorio forums](https://forums.factorio.com/viewtopic.php?f=133&t=13874)
93208

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

96-
# License
211+
## License
212+
97213
This code is realeased with the MIT license, see the LICENSE file.

0 commit comments

Comments
 (0)