Manage Minecraft servers with systemd

This commit is contained in:
Kris Lamoureux 2021-05-13 03:35:00 -04:00
parent 32455e8016
commit 3dc0e04ab4
Signed by: kris
GPG Key ID: 3EDA9C3441EDA925
6 changed files with 94 additions and 1 deletions

View File

@ -3,4 +3,7 @@ allow_reboot: false
manage_network: false
# minecraft
minecraft_eula: true
# Agree to EULA to start service in Vagrant
# i.e. sed -i 's/^#minecraft_eula/minecraft_eula/' ./dev/host_vars/minecraft.yml
#minecraft_eula: true
minecraft_sha1: 1b557e7b033b583cd9f66746b7a9ab1ec1673ced # 1.16.5

1
roles/.gitignore vendored
View File

@ -7,6 +7,7 @@
!gitea*/
!jenkins*/
!libvirt*/
!minecraft*/
!nextcloud*/
!nginx*/
!prometheus*/

View File

@ -0,0 +1,6 @@
minecraft_eula: false
minecraft_home: "/opt/minecraft"
minecraft_url: "https://launcher.mojang.com/v1/objects/{{ minecraft_sha1 }}/server.jar"
minecraft_user: minecraft
minecraft_xms: 1G
minecraft_xmx: "{{ minecraft_xms }}"

View File

@ -0,0 +1,55 @@
- name: Install Screen
apt:
name: screen
state: present
- name: Install Java
apt:
name: default-jre
state: present
- name: Create Minecraft user
user:
name: "{{ minecraft_user }}"
state: present
shell: /bin/bash
- name: Create Minecraft directory
file:
path: "{{ minecraft_home }}"
state: directory
owner: "{{ minecraft_user }}"
group: "{{ minecraft_user }}"
- name: Download Minecraft
get_url:
url: "{{ minecraft_url }}"
dest: "{{ minecraft_home }}/minecraft_server.jar"
checksum: "sha1:{{ minecraft_sha1 }}"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_user }}"
mode: 0700
- name: Deploy Minecraft systemd service
template:
src: minecraft.service.j2
dest: "/etc/systemd/system/minecraft.service"
register: minecraft_systemd
- name: Answer to Mojang's EULA
template:
src: eula.txt.j2
dest: "{{ minecraft_home }}/eula.txt"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_user }}"
- name: Reload systemd manager configuration
systemd:
daemon_reload: true
when: minecraft_systemd.changed
- name: Start Service
service:
name: minecraft
state: started
when: minecraft_eula

View File

@ -0,0 +1 @@
eula={{ minecraft_eula | lower }}

View File

@ -0,0 +1,27 @@
[Unit]
Description=Minecraft Server
After=network.target
[Service]
Type=simple
WorkingDirectory={{ minecraft_home }}
User={{ minecraft_user }}
Group={{ minecraft_user }}
Restart=always
ExecStart=/usr/bin/screen -DmS minecraft \
/usr/bin/java -Xms{{ minecraft_xms }} -Xmx{{ minecraft_xmx }} -jar minecraft_server.jar nogui
ExecReload=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "reload"\\015'
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "say SERVER SHUTDOWN IN 30 SECONDS"\015'
ExecStop=/bin/sleep 15
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "say SERVER SHUTDOWN IN 15 SECONDS"\015'
ExecStop=/bin/sleep 10
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "say SERVER SHUTDOWN IN 5 SECONDS"\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "save-all"\015'
ExecStop=/bin/sleep 1
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "stop"\015'
ExecStop=/bin/sleep 3
[Install]
WantedBy=multi-user.target