Dynamic Java options on Minecraft systemd service

This commit is contained in:
Kris Lamoureux 2021-06-21 22:36:35 -04:00
parent f44ac239d9
commit d87eb4f8cd
Signed by: kris
GPG Key ID: 3EDA9C3441EDA925
8 changed files with 50 additions and 11 deletions

2
Vagrantfile vendored
View File

@ -35,7 +35,7 @@ Vagrant.configure("2") do |config|
end end
config.vm.provider "virtualbox" do |vbox| config.vm.provider "virtualbox" do |vbox|
vbox.memory = 2048 vbox.memory = 4096
end end
# Provision with Ansible # Provision with Ansible

View File

@ -8,9 +8,16 @@ manage_network: false
#minecraft_eula: true #minecraft_eula: true
minecraft_onboot: vanilla minecraft_onboot: vanilla
minecraft_onboot_run: true
minecraft: minecraft:
- name: vanilla # 1.16.5 (https://mcversions.net/download/1.16.5) - name: vanilla # 1.16.5 (https://mcversions.net/download/1.16.5)
sha1: 1b557e7b033b583cd9f66746b7a9ab1ec1673ced sha1: 1b557e7b033b583cd9f66746b7a9ab1ec1673ced
java:
Xms: 1G
Xmx: 1G
- name: revelation # 3.4.0 (https://api.modpacks.ch/public/modpack/35/174) - name: revelation # 3.4.0 (https://api.modpacks.ch/public/modpack/35/174)
modpack: 35/174 modpack: 35/174
java: 8 java:
version: 8
Xms: 4G
Xmx: 4G

View File

@ -2,6 +2,7 @@ minecraft_eula: false
minecraft_home: "/opt/minecraft" minecraft_home: "/opt/minecraft"
minecraft_java: 11 minecraft_java: 11
minecraft_modpack_url: "https://api.modpacks.ch/public/modpack/{{ item.modpack }}/server/linux" minecraft_modpack_url: "https://api.modpacks.ch/public/modpack/{{ item.modpack }}/server/linux"
minecraft_onboot_run: false
minecraft_url: "https://launcher.mojang.com/v1/objects/{{ item.sha1 }}/server.jar" minecraft_url: "https://launcher.mojang.com/v1/objects/{{ item.sha1 }}/server.jar"
minecraft_user: minecraft minecraft_user: minecraft
minecraft_xms: 1G minecraft_xms: 1G

View File

@ -2,14 +2,14 @@
apt: apt:
name: gpg name: gpg
state: present state: present
when: item.java is defined and item.java == 8 when: item.java.version is defined and item.java.version == 8
loop: "{{ minecraft }}" loop: "{{ minecraft }}"
- name: Add AdoptOpenJDK's signing key - name: Add AdoptOpenJDK's signing key
apt_key: apt_key:
id: 8ED17AF5D7E675EB3EE3BCE98AC3B29174885C03 id: 8ED17AF5D7E675EB3EE3BCE98AC3B29174885C03
url: https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public url: https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public
when: item.java is defined and item.java == 8 when: item.java.version is defined and item.java.version == 8
loop: "{{ minecraft }}" loop: "{{ minecraft }}"
- name: Install AdoptOpenJDK repository - name: Install AdoptOpenJDK repository
@ -17,21 +17,21 @@
repo: deb https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/ buster main repo: deb https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/ buster main
mode: 0644 mode: 0644
state: present state: present
when: item.java is defined and item.java == 8 when: item.java.version is defined and item.java.version == 8
loop: "{{ minecraft }}" loop: "{{ minecraft }}"
- name: Install Java 8 - name: Install Java 8
apt: apt:
name: adoptopenjdk-8-hotspot name: adoptopenjdk-8-hotspot
state: present state: present
when: item.java is defined and item.java == 8 when: item.java.version is defined and item.java.version == 8
loop: "{{ minecraft }}" loop: "{{ minecraft }}"
- name: Install Java 11 - name: Install Java 11
apt: apt:
name: openjdk-11-jre name: openjdk-11-jre
state: present state: present
when: item.java is not defined or item.java == 11 when: item.java.version is not defined or item.java.version == 11
loop: "{{ minecraft }}" loop: "{{ minecraft }}"
- name: Activate Java 8 - name: Activate Java 8

View File

@ -28,6 +28,8 @@
file: file:
src: "{{ item.files[0].path }}" src: "{{ item.files[0].path }}"
dest: "{{ minecraft_home }}/{{ item.item.name }}/minecraft_server.jar" dest: "{{ minecraft_home }}/{{ item.item.name }}/minecraft_server.jar"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_user }}"
state: link state: link
loop: "{{ minecraft_forge.results }}" loop: "{{ minecraft_forge.results }}"
when: minecraft_forge is defined and item.files[0].path is defined when: minecraft_forge is defined and item.files[0].path is defined

View File

@ -4,14 +4,34 @@
dest: "/etc/systemd/system/minecraft@.service" dest: "/etc/systemd/system/minecraft@.service"
register: minecraft_systemd register: minecraft_systemd
- name: Deploy service environmental variables
template:
src: environment.conf.j2
dest: "{{ minecraft_home }}/{{ item.name }}/environment.conf"
owner: "{{ minecraft_user }}"
group: "{{ minecraft_user }}"
loop: "{{ minecraft }}"
- name: Reload systemd manager configuration - name: Reload systemd manager configuration
systemd: systemd:
daemon_reload: true daemon_reload: true
when: minecraft_systemd.changed when: minecraft_systemd.changed
- name: Start Service - name: Disable non-default service instances
service:
name: "minecraft@{{ item.name }}"
enabled: false
loop: "{{ minecraft }}"
when: item.name != minecraft_onboot
- name: Enable default service instance
service:
name: "minecraft@{{ minecraft_onboot }}"
enabled: true
when: minecraft_eula and minecraft_onboot is defined
- name: Run default service instance
service: service:
name: "minecraft@{{ minecraft_onboot }}" name: "minecraft@{{ minecraft_onboot }}"
state: started state: started
enabled: true when: minecraft_eula and minecraft_onboot is defined and minecraft_onboot_run
when: minecraft_eula and minecraft_onboot is defined

View File

@ -0,0 +1,8 @@
{% if item.java.version is defined and item.java.version == 8 %}
JAVA_HOME=/usr/lib/jvm/adoptopenjdk-8-hotspot-amd64/jre
{% else %}
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
{% endif %}
JAVA_XMS={{ item.java.Xms }}
JAVA_XMX={{ item.java.Xmx }}

View File

@ -8,9 +8,10 @@ WorkingDirectory={{ minecraft_home }}/%i
User={{ minecraft_user }} User={{ minecraft_user }}
Group={{ minecraft_user }} Group={{ minecraft_user }}
Restart=always Restart=always
EnvironmentFile={{ minecraft_home }}/%i/environment.conf
ExecStart=/usr/bin/screen -DmS minecraft-%i \ ExecStart=/usr/bin/screen -DmS minecraft-%i \
/usr/bin/java -Xms{{ minecraft_xms }} -Xmx{{ minecraft_xmx }} -jar minecraft_server.jar nogui ${JAVA_HOME}/bin/java -Xms${JAVA_XMS} -Xmx${JAVA_XMX} -jar minecraft_server.jar nogui
ExecReload=/usr/bin/screen -p 0 -S minecraft-%i -X eval 'stuff "reload"\\015' ExecReload=/usr/bin/screen -p 0 -S minecraft-%i -X eval 'stuff "reload"\\015'
ExecStop=/usr/bin/screen -p 0 -S minecraft-%i -X eval 'stuff "say SERVER SHUTDOWN IN 30 SECONDS"\015' ExecStop=/usr/bin/screen -p 0 -S minecraft-%i -X eval 'stuff "say SERVER SHUTDOWN IN 30 SECONDS"\015'
ExecStop=/bin/sleep 15 ExecStop=/bin/sleep 15