From f44ac239d9b06f52f0f25f990e1cf4233a6e417c Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Wed, 16 Jun 2021 22:49:01 -0400 Subject: [PATCH] Minecraft modpacks.ch server management --- dev/host_vars/minecraft.yml | 9 ++- roles/minecraft/defaults/main.yml | 4 +- roles/minecraft/tasks/java.yml | 47 +++++++++++++ roles/minecraft/tasks/main.yml | 67 ++++--------------- roles/minecraft/tasks/modpacks.yml | 33 +++++++++ roles/minecraft/tasks/service.yml | 17 +++++ roles/minecraft/tasks/system.yml | 26 +++++++ roles/minecraft/tasks/vanilla.yml | 10 +++ .../minecraft/templates/minecraft.service.j2 | 18 ++--- 9 files changed, 166 insertions(+), 65 deletions(-) create mode 100644 roles/minecraft/tasks/java.yml create mode 100644 roles/minecraft/tasks/modpacks.yml create mode 100644 roles/minecraft/tasks/service.yml create mode 100644 roles/minecraft/tasks/system.yml create mode 100644 roles/minecraft/tasks/vanilla.yml diff --git a/dev/host_vars/minecraft.yml b/dev/host_vars/minecraft.yml index f057b68..d978fa8 100644 --- a/dev/host_vars/minecraft.yml +++ b/dev/host_vars/minecraft.yml @@ -6,4 +6,11 @@ manage_network: false # 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 + +minecraft_onboot: vanilla +minecraft: + - name: vanilla # 1.16.5 (https://mcversions.net/download/1.16.5) + sha1: 1b557e7b033b583cd9f66746b7a9ab1ec1673ced + - name: revelation # 3.4.0 (https://api.modpacks.ch/public/modpack/35/174) + modpack: 35/174 + java: 8 diff --git a/roles/minecraft/defaults/main.yml b/roles/minecraft/defaults/main.yml index 2d368f1..503fa43 100644 --- a/roles/minecraft/defaults/main.yml +++ b/roles/minecraft/defaults/main.yml @@ -1,6 +1,8 @@ minecraft_eula: false minecraft_home: "/opt/minecraft" -minecraft_url: "https://launcher.mojang.com/v1/objects/{{ minecraft_sha1 }}/server.jar" +minecraft_java: 11 +minecraft_modpack_url: "https://api.modpacks.ch/public/modpack/{{ item.modpack }}/server/linux" +minecraft_url: "https://launcher.mojang.com/v1/objects/{{ item.sha1 }}/server.jar" minecraft_user: minecraft minecraft_xms: 1G minecraft_xmx: "{{ minecraft_xms }}" diff --git a/roles/minecraft/tasks/java.yml b/roles/minecraft/tasks/java.yml new file mode 100644 index 0000000..7ca6e6e --- /dev/null +++ b/roles/minecraft/tasks/java.yml @@ -0,0 +1,47 @@ +- name: Install GPG + apt: + name: gpg + state: present + when: item.java is defined and item.java == 8 + loop: "{{ minecraft }}" + +- name: Add AdoptOpenJDK's signing key + apt_key: + id: 8ED17AF5D7E675EB3EE3BCE98AC3B29174885C03 + url: https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public + when: item.java is defined and item.java == 8 + loop: "{{ minecraft }}" + +- name: Install AdoptOpenJDK repository + apt_repository: + repo: deb https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/ buster main + mode: 0644 + state: present + when: item.java is defined and item.java == 8 + loop: "{{ minecraft }}" + +- name: Install Java 8 + apt: + name: adoptopenjdk-8-hotspot + state: present + when: item.java is defined and item.java == 8 + loop: "{{ minecraft }}" + +- name: Install Java 11 + apt: + name: openjdk-11-jre + state: present + when: item.java is not defined or item.java == 11 + loop: "{{ minecraft }}" + +- name: Activate Java 8 + alternatives: + name: java + path: /usr/lib/jvm/adoptopenjdk-8-hotspot-amd64/jre/bin/java + when: minecraft_java == 8 + +- name: Activate Java 11 + alternatives: + name: java + path: /usr/lib/jvm/java-11-openjdk-amd64/bin/java + when: minecraft_java == 11 diff --git a/roles/minecraft/tasks/main.yml b/roles/minecraft/tasks/main.yml index 010eadd..c13ec8d 100644 --- a/roles/minecraft/tasks/main.yml +++ b/roles/minecraft/tasks/main.yml @@ -1,55 +1,14 @@ -- 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 +- import_tasks: system.yml + when: minecraft_eula + +- import_tasks: java.yml + when: minecraft_eula + +- import_tasks: vanilla.yml + when: minecraft_eula + +- import_tasks: modpacks.yml + when: minecraft_eula + +- import_tasks: service.yml when: minecraft_eula diff --git a/roles/minecraft/tasks/modpacks.yml b/roles/minecraft/tasks/modpacks.yml new file mode 100644 index 0000000..b474c7c --- /dev/null +++ b/roles/minecraft/tasks/modpacks.yml @@ -0,0 +1,33 @@ +- name: Download Minecraft modpack installer + get_url: + url: "{{ minecraft_modpack_url }}" + dest: "{{ minecraft_home }}/{{ item.name }}/serverinstall_{{ item.modpack | replace ('/', '_') }}" + owner: "{{ minecraft_user }}" + group: "{{ minecraft_user }}" + mode: 0700 + loop: "{{ minecraft }}" + when: item.modpack is defined and item.sha1 is not defined + +- name: Run Minecraft modpack installer + command: "sudo -u {{ minecraft_user }} ./serverinstall_{{ item.modpack | replace ('/', '_') }} --auto" + args: + creates: "{{ minecraft_home }}/{{ item.name }}/mods" + chdir: "{{ minecraft_home }}/{{ item.name }}" + loop: "{{ minecraft }}" + when: item.modpack is defined and item.sha1 is not defined + +- name: Find Minecraft Forge + find: + paths: "{{ minecraft_home }}/{{ item.name }}" + patterns: "forge*.jar" + register: minecraft_forge + loop: "{{ minecraft }}" + when: item.modpack is defined and item.sha1 is not defined + +- name: Link to Minecraft Forge + file: + src: "{{ item.files[0].path }}" + dest: "{{ minecraft_home }}/{{ item.item.name }}/minecraft_server.jar" + state: link + loop: "{{ minecraft_forge.results }}" + when: minecraft_forge is defined and item.files[0].path is defined diff --git a/roles/minecraft/tasks/service.yml b/roles/minecraft/tasks/service.yml new file mode 100644 index 0000000..a1ae669 --- /dev/null +++ b/roles/minecraft/tasks/service.yml @@ -0,0 +1,17 @@ +- name: Deploy Minecraft systemd service + template: + src: minecraft.service.j2 + dest: "/etc/systemd/system/minecraft@.service" + register: minecraft_systemd + +- name: Reload systemd manager configuration + systemd: + daemon_reload: true + when: minecraft_systemd.changed + +- name: Start Service + service: + name: "minecraft@{{ minecraft_onboot }}" + state: started + enabled: true + when: minecraft_eula and minecraft_onboot is defined diff --git a/roles/minecraft/tasks/system.yml b/roles/minecraft/tasks/system.yml new file mode 100644 index 0000000..5a8aeb1 --- /dev/null +++ b/roles/minecraft/tasks/system.yml @@ -0,0 +1,26 @@ +- name: Install Screen + apt: + name: screen + state: present + +- name: Create Minecraft user + user: + name: "{{ minecraft_user }}" + state: present + shell: /bin/bash + +- name: Create Minecraft directory + file: + path: "{{ minecraft_home }}/{{ item.name }}" + state: directory + owner: "{{ minecraft_user }}" + group: "{{ minecraft_user }}" + loop: "{{ minecraft }}" + +- name: Answer to Mojang's EULA + template: + src: eula.txt.j2 + dest: "{{ minecraft_home }}/{{ item.name }}/eula.txt" + owner: "{{ minecraft_user }}" + group: "{{ minecraft_user }}" + loop: "{{ minecraft }}" diff --git a/roles/minecraft/tasks/vanilla.yml b/roles/minecraft/tasks/vanilla.yml new file mode 100644 index 0000000..2d70038 --- /dev/null +++ b/roles/minecraft/tasks/vanilla.yml @@ -0,0 +1,10 @@ +- name: Download Minecraft + get_url: + url: "{{ minecraft_url }}" + dest: "{{ minecraft_home }}/{{ item.name }}/minecraft_server.jar" + checksum: "sha1:{{ item.sha1 }}" + owner: "{{ minecraft_user }}" + group: "{{ minecraft_user }}" + mode: 0700 + loop: "{{ minecraft }}" + when: item.sha1 is defined diff --git a/roles/minecraft/templates/minecraft.service.j2 b/roles/minecraft/templates/minecraft.service.j2 index 2879b9a..a509f45 100644 --- a/roles/minecraft/templates/minecraft.service.j2 +++ b/roles/minecraft/templates/minecraft.service.j2 @@ -1,26 +1,26 @@ [Unit] -Description=Minecraft Server +Description=Minecraft Server: %i After=network.target [Service] Type=simple -WorkingDirectory={{ minecraft_home }} +WorkingDirectory={{ minecraft_home }}/%i User={{ minecraft_user }} Group={{ minecraft_user }} Restart=always -ExecStart=/usr/bin/screen -DmS minecraft \ +ExecStart=/usr/bin/screen -DmS minecraft-%i \ /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' +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=/bin/sleep 15 -ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "say SERVER SHUTDOWN IN 15 SECONDS"\015' +ExecStop=/usr/bin/screen -p 0 -S minecraft-%i -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=/usr/bin/screen -p 0 -S minecraft-%i -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=/usr/bin/screen -p 0 -S minecraft-%i -X eval 'stuff "save-all"\015' ExecStop=/bin/sleep 1 -ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval 'stuff "stop"\015' +ExecStop=/usr/bin/screen -p 0 -S minecraft-%i -X eval 'stuff "stop"\015' ExecStop=/bin/sleep 3 [Install]