Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit f1845e5

Browse files
committed
Add example systemd-files for running multiple instances
1 parent accd3bd commit f1845e5

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

examples/systemd/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
System D on Ubuntu
2+
==================
3+
4+
**IMPORTANT**: These files are written for Unity Cache Server 5.0.
5+
6+
These files are for setting up per-project cache servers on the same machine
7+
using different ports. This way, the contents of a corrupted server can be
8+
removed without affecting all other builds.
9+
10+
Setting up multiple instances of the same service with different configurations
11+
is called *template units* in systemd parlance.
12+
13+
* Fedora Magazine on [Template unit
14+
files](https://fedoramagazine.org/systemd-template-unit-files/)
15+
* Digital Ocean on [Template
16+
specifiers](https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files#creating-instance-units-from-template-unit-files)
17+
* System D [full unit
18+
documentation](https://www.freedesktop.org/software/systemd/man/systemd.unit.html)
19+
20+
Setup
21+
-----
22+
23+
Install Node.js and git. Then check out the cache server itself and install
24+
it's dependencies:
25+
26+
# From https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions
27+
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
28+
sudo apt-get install -y nodejs git
29+
30+
# Add unitycache user + homedir
31+
adduser --system unitycacheserver
32+
33+
# Get server code
34+
sudo -u unitycacheserver git clone https://github.com/Unity-Technologies/unity-cache-server ~unitycacheserver/unity-cache-server
35+
cd ~unitycacheserver/unity-cache-server
36+
sudo -u unitycacheserver npm --cache ~unitycacheserver/.npm install --production
37+
cd -
38+
39+
Next up is a configuration file for each. Since The cache server does not
40+
actually accept a configuration file, we will write is as command-line
41+
arguments passed to each instance, eg. for *GAME NAME* on port 5002, we add a
42+
file named `5002-GAME-NAME.sh`:
43+
44+
OPTIONS=--port=5002
45+
46+
The shell-file is read by systemd on service startup and the exported
47+
`$OPTIONS` is appended to the command-line arguments at startup. Only exception
48+
is the `--path` argument which is deduced from the base name of the file
49+
(eg. `5002-GAME-NAME` here) and set uses that as a sub-folder in
50+
unitycacheserver's home directory, eg. `--path
51+
/home/unitycacheserver/5002-GAME-NAME`, as it saves a lot of typing and
52+
copy/paste errors.
53+
54+
Now, move all these files into the `unitycacheserver`-users' directory:
55+
56+
sudo mv unitycacheserver/*.sh ~/unitycacheserver
57+
sudo chown -R unitycacheserver ~/unitycacheserver/*.sh
58+
59+
Finally, we copy over the systemd-scripts and set up a service instance per
60+
server we want running:
61+
62+
# Set up server + service-script for each
63+
cp systemd-services/* /etc/systemd/system/
64+
65+
# Set up a service instance per server we want
66+
for i in `ls ~unitycacheserver/*.sh`; do systemctl enable unity-cache-server@`basename $i .sh`; done
67+
68+
# Start all servers by starting the 'parent' service
69+
systemctl start unity-cache-server
70+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=Start all unity-cache-server instances
3+
[Service]
4+
Type=oneshot
5+
ExecStart=/bin/true
6+
RemainAfterExit=yes
7+
8+
[Install]
9+
WantedBy=multi-user.target
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[Unit]
2+
Description=Unity Cache Server listening on '%i'
3+
Requires=unity-cache-server.service
4+
Before=unity-cache-server.service
5+
BindsTo=unity-cache-server.service
6+
7+
[Service]
8+
Type=simple
9+
SyslogIdentifier=unity-cache-server-%i
10+
TimeoutStartSec=300
11+
ExecStart=/home/unitycacheserver/unity-cache-server/main.js --path /home/unitycacheserver/cache-%i $OPTIONS
12+
EnvironmentFile=/home/unitycacheserver/%i.sh
13+
Restart=on-failure
14+
User=unitycacheserver
15+
16+
[Install]
17+
WantedBy=unity-cache-server.service

0 commit comments

Comments
 (0)