From 071570745240b419a3ecabe106d6a6b2b38a47c8 Mon Sep 17 00:00:00 2001 From: Salt Date: Sun, 10 Jan 2021 19:50:20 -0600 Subject: [PATCH] Add a hopefully functional implementation Time to test --- defaults/main.yml | 10 ++++++++-- handlers/main.yml | 9 +++++++++ tasks/main.yml | 31 +++++++++++++++++++++++++++++++ templates/install-server.sh | 16 ++++++++++++++++ templates/terraria@.service | 9 +++------ 5 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 handlers/main.yml create mode 100644 templates/install-server.sh diff --git a/defaults/main.yml b/defaults/main.yml index 30cbd9f..6abb034 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,6 +1,6 @@ # vim:ft=ansible: terraria_name: default -terraria_version: blah +terraria_version: 1412 terraria_enabled: yes terraria_restart_delay: 5 @@ -11,7 +11,13 @@ terraria_packages: terraria_packages_extra: [] # Server configuration -#terraria_downloadurl: +terraria_downloadurl: "https://terraria.org/system/dedicated_servers/archives/000/000/042/original/terraria-server-{{ terraria_version }}.zip" +terraria_config_default: + autocreate: 3 + difficulty: 0 + world: "{{ terraria_home }}/{{ terraria_name }}/worlds/world.wld" + worldpath: "{{ terraria_home }}/{{ terraria_name }}/worlds" +terraria_config: {} # Service configuration terraria_home: /opt/terraria diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..cae007a --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,9 @@ +#!/usr/bin/ansible-playbook +# vim:ft=ansible: +--- +- name: "restart terraria {{ terraria_name }}" + systemd: + name: "terraria@{{ terraria_name }}" + state: restarted + become: yes + when: terraria_enabled diff --git a/tasks/main.yml b/tasks/main.yml index 258a160..7bae1ea 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -16,5 +16,36 @@ block: - name: assure server directory file: path={{ terraria_home }}/{{ terraria_name }} state=directory + - name: download server zip + get_url: url={{ terraria_downloadurl }} dest={{ terraria_home }}/{{ terraria_name }}/server.zip + register: download_server + - name: install server + block: + - name: assure server unzip directory + file: path={{ terraria_home }}/{{ terraria_name }}/server state=directory + - name: unpack zip + unarchive: src={{ terraria_home }}/{{ terraria_name }}/server.zip dest={{ terraria_home }}/{{ terraria_name }}/server remote_src=yes + - name: template out installer + template: src=install-server.sh dest={{ terraria_home }}/{{ terraria_name }}/install-server.sh + - name: run installer + command: + chdir: "{{ terraria_home }}/{{ terraria_name }}" + argv: [ "bash", "./install-server.sh" ] + notify: "restart terraria {{ terraria_name }}" + when: download_server is changed + - name: configure server settings + lineinfile: + dest: "{{ terraria_home }}/{{ terraria_name }}/config" + line: "{{ item.key }}={{ item.value }}" + regex: "^{{ item.key }}=.*" + create: yes + with_dict: "{{ terraria_config_default | combine(terraria_config) }}" + notify: "restart terraria {{ terraria_name }} become: yes become_user: terraria +- name: enable service + systemd: name=terraria@{{ terraria_name }}.service enabled=yes state=started + when: terraria_enabled +- name: disable service + systemd: name=terraria@{{ terraria_name }}.service enabled=no state=stopped + when: not terraria_enabled diff --git a/templates/install-server.sh b/templates/install-server.sh new file mode 100644 index 0000000..51d01ee --- /dev/null +++ b/templates/install-server.sh @@ -0,0 +1,16 @@ +#! /bin/sh + +# Find out where our server lives +# Ideally, this won't change at all over time. @redigit please keep these +# naming conventions +directory="server/{{ terraria_version }}/Linux" +echo "Syncing from $directory/" + +# Now we rsync everything from that directory here, which should set us up +rsync -av "$directory"/ ./ + +# I'm not sure if it's just zip being zip or if it's Redigit not packing +# them properly, but I've found that the server executables tend to not have +# the right permissions +echo "Fixing permissions on server binaries" +find -maxdepth 1 -type f -iname "terrariaserver*" -exec chmod +x {} + diff --git a/templates/terraria@.service b/templates/terraria@.service index 7bf66dd..3461e63 100644 --- a/templates/terraria@.service +++ b/templates/terraria@.service @@ -11,7 +11,7 @@ After=network.target User=terraria Group=terraria WorkingDirectory={{ terraria_home }}/%i -EnvironmentFile={{ terraria_home }}/%i/env +#EnvironmentFile={{ terraria_home }}/%i/env PrivateUsers=true ProtectSystem=full ProtectHome=true @@ -22,13 +22,10 @@ ProtectKernelModules=true # Implies MountAPIVFS=yes ProtectControlGroups=true -ExecStart=/bin/sh -c '/usr/bin/screen -DmS %i /usr/bin/java -Xms${JRE_XMS} -Xmx${JRE_XMX} -jar server.jar nogui' - -ExecReload=/usr/bin/screen -p 0 -S %i -X eval 'stuff "reload"\\015' +ExecStart=/bin/sh -c '/usr/bin/screen -DmS %i ./TerrariaServer.bin.x86_64 -config config' 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=/usr/bin/screen -p 0 -S %i -X eval 'stuff "exit"\\015' Restart=always