From e0eeb4fa5b1ddc2cd8d907097a77798ddc3aa3c2 Mon Sep 17 00:00:00 2001 From: Salt Date: Fri, 20 Nov 2020 04:09:09 -0600 Subject: [PATCH] Add Minecraft role --- roles/minecraft/defaults/main.yml | 41 ++++++++++ roles/minecraft/handlers/main.yml | 8 ++ roles/minecraft/tasks/main.yml | 82 ++++++++++++++++++++ roles/minecraft/templates/eula.txt | 2 + roles/minecraft/templates/install-modpack.sh | 15 ++++ roles/minecraft/templates/minecraft@.service | 39 ++++++++++ 6 files changed, 187 insertions(+) create mode 100644 roles/minecraft/defaults/main.yml create mode 100644 roles/minecraft/handlers/main.yml create mode 100644 roles/minecraft/tasks/main.yml create mode 100644 roles/minecraft/templates/eula.txt create mode 100644 roles/minecraft/templates/install-modpack.sh create mode 100644 roles/minecraft/templates/minecraft@.service diff --git a/roles/minecraft/defaults/main.yml b/roles/minecraft/defaults/main.yml new file mode 100644 index 0000000..77c7f5b --- /dev/null +++ b/roles/minecraft/defaults/main.yml @@ -0,0 +1,41 @@ +# vim:ft=ansible: +minecraft_name: default +minecraft_version: 1.16.3 +# minecraft_forge_version: You tell me + +# Packages +# Put any extra dependencies (for mods, etc.) in _extra +minecraft_packages: + - openjdk-8-jre + - screen +minecraft_packages_extra: [] + +# Addon configuration +# You'll probably want to enable one of these +minecraft_paper_install: no +minecraft_paper_downloadurl: "https://papermc.io/api/v1/paper/{{ minecraft_version }}/latest/download" +minecraft_forge_install: no +minecraft_forge_downloadurl: "https://files.minecraftforge.net/maven/net/minecraftforge/forge/{{ minecraft_version }}-{{ minecraft_forge_version }}/forge-{{ minecraft_version }}-{{ minecraft_forge_version }}-installer.jar" +# Define this if you already have a modpack zipped up and ready to go +#minecraft_forge_packurl: https://path.to.your/modpack.zip +# Define THIS if you want to download a bunch of mods by JAR URL: +minecraft_forge_mods: [] +minecraft_vanilla_install: no +minecraft_vanilla_downloadurl: "https://launcher.mojang.com/v1/objects/35139deedbd5182953cf1caa23835da59ca3d7cd/server.jar" + +# Service configuration +minecraft_home: /opt/minecraft +minecraft_stop_message: "Server is going down for maintenance" + +# Java configuration +minecraft_jre_xms: 2048m +minecraft_jre_xmx: 4096m + +# Minecraft configuration +# Contains key-value pairs that will be lineinfile'd into server.properties +# Example: +# minecraft_server_properties: +# - { opt: allow-flight, value: "false" } +# - opt: gamemode +# value: 0 +minecraft_server_properties: {} diff --git a/roles/minecraft/handlers/main.yml b/roles/minecraft/handlers/main.yml new file mode 100644 index 0000000..a9003dc --- /dev/null +++ b/roles/minecraft/handlers/main.yml @@ -0,0 +1,8 @@ +#!/usr/bin/ansible-playbook +# vim:ft=ansible: +--- +- name: "restart minecraft {{ minecraft_name }}" + systemd: + name: "minecraft@{{ minecraft_name }}" + state: restarted + become: yes diff --git a/roles/minecraft/tasks/main.yml b/roles/minecraft/tasks/main.yml new file mode 100644 index 0000000..4d5f10e --- /dev/null +++ b/roles/minecraft/tasks/main.yml @@ -0,0 +1,82 @@ +#!/usr/bin/ansible-playbook +# vim:ft=ansible: +--- +- name: set up system + block: + - name: install required packages + apt: name="{{ minecraft_packages + minecraft_packages_extra }}" + - name: assure server user + user: name=minecraft system=yes home={{ minecraft_home }} + - name: assure operational directory + file: path={{ minecraft_home }} state=directory owner=minecraft group=minecraft mode="3775" + - name: assure systemd unit + template: src=minecraft@.service dest=/etc/systemd/system/minecraft@.service + notify: "restart minecraft {{ minecraft_name }}" +- name: set up minecraft + block: + - name: assure server directory + file: path={{ minecraft_home }}/{{ minecraft_name }} state=directory + - name: accept eula + template: src=eula.txt dest={{ minecraft_home }}/{{ minecraft_name }}/eula.txt + - name: configure server properties + lineinfile: + dest: "{{ minecraft_home }}/{{ minecraft_name }}/server.properties" + line: "{{ item.opt }}={{ item.value }}" + regex: "^{{ item.opt }}=.*" + create: yes + loop: "{{ minecraft_server_properties }}" + notify: "restart minecraft {{ minecraft_name }}" + become: yes + become_user: minecraft +- name: set up paper + block: + - name: download paper + get_url: url={{ minecraft_paper_downloadurl }} dest={{ minecraft_home }}/{{ minecraft_name }}/server.jar + notify: "restart minecraft {{ minecraft_name }}" + # TODO: Manage configs for plugins, auto-install plugins + become: yes + become_user: minecraft + when: minecraft_fabric_install +- name: set up forge + block: + - name: template out modpack installer + template: src=install-modpack.sh dest={{ minecraft_home }}/{{ minecraft_name }}/install-modpack.sh mode="0755" + - name: download forge + get_url: dest={{ minecraft_home }}/{{ minecraft_name }}/installer.jar url={{ minecraft_forge_downloadurl }} + register: download_forge + - name: install forge + command: + chdir: "{{ minecraft_home }}/{{ minecraft_name }}" + argv: [ "/usr/bin/java", "-jar", "./installer.jar", "--installServer" ] + notify: restart minecraft {{ minecraft_name }} + when: download_forge is changed + - name: link to forge jar + file: path={{ minecraft_home }}/{{ minecraft_name }}/server.jar src=forge-{{ minecraft_version }}-{{ minecraft_forge_version }}.jar state=link + - name: install modpack + block: + - name: download modpack + get_url: dest={{ minecraft_home }}/{{ minecraft_name }}/modpack.zip url={{ minecraft_forge_packurl }} + register: download_modpack + - name: install modpack + block: + - name: assure modpack unzip directory + file: path={{ minecraft_home }}/{{ minecraft_name }}/modpack state=directory + - name: unpack zip + unarchive: src={{ minecraft_home }}/{{ minecraft_name }}/modpack.zip dest={{ minecraft_home }}/{{ minecraft_name }}/modpack remote_src=yes + - name: run installer + command: + chdir: "{{ minecraft_home }}/{{ minecraft_name }}" + argv: [ "bash", "./install-modpack.sh" ] + notify: "restart minecraft {{ minecraft_name }}" + when: download_modpack is changed + when: minecraft_forge_packurl is defined + - name: install mods + get_url: dest="{{ minecraft_home }}/{{ minecraft_name }}/mods" url={{ item }} + loop: "{{ minecraft_forge_mods }}" + notify: restart minecraft {{ minecraft_name }} + when: minecraft_forge_mods is iterable and minecraft_forge_mods != None + become: yes + become_user: minecraft + when: minecraft_forge_install and minecraft_forge_version is defined +- name: enable service + systemd: name=minecraft@{{ minecraft_name }}.service enabled=yes state=started diff --git a/roles/minecraft/templates/eula.txt b/roles/minecraft/templates/eula.txt new file mode 100644 index 0000000..b0cf95f --- /dev/null +++ b/roles/minecraft/templates/eula.txt @@ -0,0 +1,2 @@ +# Tacos best food yadda yadda +eula=true diff --git a/roles/minecraft/templates/install-modpack.sh b/roles/minecraft/templates/install-modpack.sh new file mode 100644 index 0000000..8c56734 --- /dev/null +++ b/roles/minecraft/templates/install-modpack.sh @@ -0,0 +1,15 @@ +#! /bin/sh + +# Find out where our modpack lives +# +# This assumes that we have no directories adjacent to the actual pack dir +# that contain a directory called "mods" +# +# If that's not the case, the onus is on you to fix your shit and/or repack +# the server zip +# +directory="$(find modpack/ -type d -iname "mods" -execdir pwd \; | head -n 1)" +echo "Syncing from $directory/" + +# Now we rsync everything from that directory here, which should set us up +rsync -av "$directory"/ ./ diff --git a/roles/minecraft/templates/minecraft@.service b/roles/minecraft/templates/minecraft@.service new file mode 100644 index 0000000..c13de59 --- /dev/null +++ b/roles/minecraft/templates/minecraft@.service @@ -0,0 +1,39 @@ +# +# Licensed under the terms of the MIT license +# Source: https://github.com/agowa338/MinecraftSystemdUnit/ +# vim:ft=dosini: +# + +[Unit] +Description=Minecraft Server %i +After=network.target + +[Service] +User=minecraft +Group=minecraft +WorkingDirectory={{ minecraft_home }}/%i +PrivateUsers=true +ProtectSystem=full +ProtectHome=true +# Implies MountFlags=slave +ProtectKernelTunables=true +# Implies NoNewPrivileges=yes +ProtectKernelModules=true +# Implies MountAPIVFS=yes +ProtectControlGroups=true + +ExecStart=/bin/sh -c '/usr/bin/screen -DmS %i /usr/bin/java -Xms{{ minecraft_jre_xms }} -Xmx{{ minecraft_jre_xmx }} -jar server.jar nogui' + +ExecReload=/usr/bin/screen -p 0 -S %i -X eval 'stuff "reload"\\015' + +ExecStop=/usr/bin/screen -p 0 -S %i -X eval 'stuff "say Server is going down: Service was stopped"\\015' +ExecStop=/usr/bin/screen -p 0 -S %i -X eval 'stuff "save-all"\\015' +ExecStop=/usr/bin/screen -p 0 -S %i -X eval 'stuff "stop"\\015' +ExecStop=/bin/sleep 5 + +Restart=always +# Restart after 24hrs +RuntimeMaxSecs=86400 + +[Install] +WantedBy=multi-user.target