Skip to content

Commit 32cdde0

Browse files
committed
Added complete game server instance command and build
- Instance automatically created using OVH API - Startup scripts installed to server - Instance installed with required packages - Server automatically started in screen
1 parent e3c659f commit 32cdde0

File tree

19 files changed

+547
-17
lines changed

19 files changed

+547
-17
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "collections/ansible_collections/synthesio/ovh"]
2+
path = collections/ansible_collections/synthesio/ovh
3+
url = https://github.com/synthesio/infra-ovh-ansible-module

ansible.cfg

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[defaults]
2+
3+
stdout_callback = debug
4+
5+
# As we connect to new hosts for the first time
6+
host_key_checking = False
7+
8+
nocows = 1
9+
10+
[ssh_connection]
11+
pipelining = True

hosts

Lines changed: 0 additions & 2 deletions
This file was deleted.

playbook.yml

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,47 @@
22

33
- hosts: localhost
44

5+
vars_prompt:
6+
- name: game
7+
prompt: Game to install (any filename in vars/games without extension)
8+
private: no
9+
- name: dns
10+
prompt: DNS entry to create (server will be available at <dns>.games.zcraft.fr)
11+
private: no
12+
13+
pre_tasks:
14+
- name: Load vars from selected game ({{ game }})
15+
include_vars: "vars/games/{{ game }}.yml"
16+
17+
roles:
18+
- instance
19+
20+
- hosts: ovh # dynamically created by the instance role
21+
522
vars:
623
# Root directory where everything will be stored.
7-
root: /home/amaury/.tmp/dmc/
24+
root: "{{ ansible_user_dir }}"
825

926
# Where the actual Minecraft server will be installed on the machine.
1027
server: "{{ root }}/server"
1128

1229
# Where things that need to be build (e.g. plugins) will be.
1330
build: "{{ root }}/build"
1431

15-
vars_prompt:
16-
- name: game
17-
prompt: Game to install (any filename in vars/games without extension)
18-
private: no
19-
- name: dns
20-
prompt: DNS entry to create (server will be available at <dns>.games.zcraft.fr)
21-
private: no
32+
# User agent for web requests
33+
http_agent: "ansible-httpget/{{ ansible_version.string }} deploy-minecraft/1.0 (compatible; +https://amaury.carrade.eu/contact)"
34+
35+
# We wait for the host to be reachable before gathering facts
36+
gather_facts: no
2237

2338
pre_tasks:
24-
- name: Load vars from selected {{ game }}
39+
- name: Waiting for DNS propagation…
40+
wait_for_connection:
41+
42+
- name: Gather facts
43+
setup:
44+
45+
- name: Load vars from selected game ({{ game }})
2546
include_vars: "vars/games/{{ game }}.yml"
2647

2748
roles:

roles/gameserver/tasks/game.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
- name: Download latest Paper for {{ minecraft_version }}
2+
get_url:
3+
url: "https://papermc.io/api/v1/paper/{{ minecraft_version }}/latest/download"
4+
dest: "{{ server }}/paper.jar"
5+
http_agent: "{{ http_agent }}"
6+
7+
- name: Accept EULA
8+
copy:
9+
dest: "{{ server }}/eula.txt"
10+
content: |
11+
# Accepted by deploy-minecraft ansible playbook
12+
eula=true
13+
14+
- name: Generate server properties
15+
template:
16+
src: "server.properties.j2"
17+
dest: "{{ server }}/server.properties"
18+
19+
- name: Install plugins from Spigot
20+
include_role:
21+
name: install-plugin-spigot
22+
vars:
23+
plugin_name: "{{ plugin.name }}"
24+
spigot_id: "{{ plugin.spigot_id }}"
25+
loop: "{{ plugins }}"
26+
when: plugin.type == "spigot"
27+
loop_control:
28+
loop_var: plugin
29+
30+
- name: Install plugins from direct link
31+
include_role:
32+
name: install-plugin-link
33+
vars:
34+
plugin_name: "{{ plugin.name }}"
35+
plugin_link: "{{ plugin.link }}"
36+
loop: "{{ plugins }}"
37+
when: plugin.type == "link"
38+
loop_control:
39+
loop_var: plugin
40+
41+
- name: Install plugins from Maven build
42+
include_role:
43+
name: install-plugin-maven
44+
vars:
45+
plugin_name: "{{ plugin.name }}"
46+
plugin_repo: "{{ plugin.repo }}"
47+
plugin_branch: "{{ plugin.branch }}"
48+
loop: "{{ plugins }}"
49+
when: plugin.type == "maven"
50+
loop_control:
51+
loop_var: plugin
52+
53+
- name: Install management scripts
54+
copy:
55+
src: "{{ item }}"
56+
dest: "{{ root }}/"
57+
mode: u=rwx,g=rx,o=rx
58+
with_fileglob:
59+
- "templates/*.sh"
60+
61+
- name: Install time-broadcast cron
62+
cron:
63+
name: Time broadcast
64+
special_time: hourly
65+
job: "cd && ./ ./broadcast_time.sh > {{ root }}/log.log 2>&1"
66+
67+
- name: Start server
68+
shell: "./boot_server.sh server {{ minecraft_ram }}"

roles/gameserver/tasks/main.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
- name: Checks if everything is ok
2-
ansible.builtin.debug:
3-
msg: Creating game {{ game }} using Minecraft {{ minecraft_version }}
1+
- name: Install server
2+
import_tasks: system.yml
3+
4+
- name: Install game server
5+
import_tasks: game.yml
6+
7+
- name: Display SSH connection
8+
debug:
9+
msg: "Connect to console with ssh {{ ansible_user_id }}@{{ ansible_default_ipv4.address }} or {{ ansible_user_id }}@{{ dns }}.games.zcraft.fr, then run screen -r zcraft_server"
10+
11+
- name: Display Minecraft server connection
12+
debug:
13+
msg: "Minecraft server is available at {{ dns }}.games.zcraft.fr or {{ ansible_default_ipv4.address }} on {{ minecraft_version }}"

roles/gameserver/tasks/system.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
- name: Create folders
2+
file:
3+
path: "{{ item }}"
4+
state: directory
5+
loop:
6+
- "{{ server }}"
7+
- "{{ server }}/plugins"
8+
- "{{ build }}"
9+
10+
- name: Install packages
11+
apt:
12+
name:
13+
# Tools & monitoring
14+
- htop
15+
- tree
16+
17+
# Plugins build
18+
- git
19+
- maven
20+
21+
# Time broadcast scripts
22+
- jq
23+
24+
# To host/boot the Paper server
25+
- screen
26+
- default-jre
27+
become: true
28+
29+
- name: Install micro
30+
shell: curl https://getmic.ro | bash
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
# Starts a Minecraft server. Creates a screen named `zcraft_folder_name` and
4+
# runs the Minecraft server in it.
5+
6+
ARGC=$#
7+
8+
if [ $ARGC -ne 2 ]; then
9+
echo "Usage: $0 <folder_name> <RAM_in_MB>"
10+
fi
11+
12+
FOLDER=$1
13+
RAM=$2
14+
15+
SCREEN_NAME="zcraft_$FOLDER"
16+
17+
screen -dmS $SCREEN_NAME sh
18+
screen -S $SCREEN_NAME -X stuff "sh ./boot_server_screen.sh $FOLDER $RAM && exit
19+
"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
3+
# Boots a Minecraft server.
4+
5+
ARGC=$#
6+
7+
if [ $ARGC -ne 2 ]; then
8+
echo "Usage: $0 <folder_name> <RAM_in_MB>"
9+
exit
10+
fi
11+
12+
FOLDER=$1
13+
RAM=$2
14+
15+
REBOOT_DELAY=6
16+
17+
mkdir -p "$FOLDER"
18+
19+
cd "$FOLDER" || exit
20+
21+
while :
22+
do
23+
java -Xms${RAM} -Xmx${RAM} -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 \
24+
-XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch \
25+
-XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 \
26+
-XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 \
27+
-XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 \
28+
-XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs \
29+
-Daikars.new.flags=true -jar paper.jar --nogui
30+
31+
echo
32+
echo
33+
echo
34+
echo
35+
36+
echo "Restarting server in $REBOOT_DELAY seconds... (hit ^C then ^D to abort)"
37+
38+
sleep $REBOOT_DELAY
39+
done
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/sh
2+
3+
# Broadcasts a command to every running Minecraft server.
4+
5+
ARGC=$#
6+
7+
if [ $ARGC -lt 1 ]; then
8+
echo "Usage: $0 <command>"
9+
exit
10+
fi
11+
12+
USER=pablo
13+
14+
send_command () {
15+
screen -S $1 -X stuff "`printf "$2 \015"`"
16+
# Escapes are dropped. Why?
17+
# COMMAND=$2
18+
# echo $COMMAND
19+
# sudo -u $USER screen -S $1 -X stuff "$COMMAND"
20+
}
21+
22+
send_command_all () {
23+
for i in $(screen -ls | grep '[0-9]\.zcraft[^0-9]' | awk '{ print $1 }')
24+
do
25+
send_command $i "`echo "$*" | sed 's/"/\\\"/g'`"
26+
done
27+
}
28+
29+
send_command_all $@

0 commit comments

Comments
 (0)