SaltStack & Minecraft

SaltStack & Minecraft

This began as a challenge from my friend Russell proposed to for me to create a minecraft Salt state for one of our favorite pastimes: Minecraft The only assumption is that your Salt master has the minecraft-_serverXX.jar. I like to keep the version in pillar, since we can then easily update it. It’s rather fast, and also takes into account the daemon as an upstart service.

We’re doing things right:

  • Create user & group for the minecraft daemon
  • Create a daemon using upstart and a .conf file
  • Install the .conf into /etc/init/

Here’s a Salt state in all of its glory:

salt/minecraft/init.sls


{#
    # Future home of my minecraft server
#}

{# Minecraft server #}
minecraft-jar:
  file.managed:
    - name: {{ pillar['minecraft']['server']['path'] }}/minecraft_server.{{
pillar['minecraft']['server']['version'] }}.jar
    - source: salt://minecraft/minecraft_server.{{
pillar['minecraft']['server']['version'] }}.jar
    - owner: minecraft
    - group: minecraft
    - mode: 755
    - require:
      - user: minecraft

{# minecraft user - UID/GID left blank intentionally #}
minecraft-user:
  user.present:
    - name: {{ pillar['minecraft']['server']['user'] }}
    - fullname: Minecraft Server
    - shell: /bin/false
    - home: /opt/minecraft
    - groups:
      - minecraft
      - games
    - require:
      - group: minecraft

{# Minecraft Group #}
minecraft-group:
  group.present:
    - name: {{ pillar['minecraft']['server']['group'] }}

{# Minecraft upstart script #}
minecraft-upstart:
  file.managed:
    - name: /etc/init/minecraft.conf
    - user: root
    - group: root
    - mode: 644
    - source: salt://minecraft/minecraft.conf
    - template: jinja

{# Service #}
minecraft-service:
  service:
    - name: minecraft
    - running
    - require:
      - file: minecraft-upstart

And a view of the upstart script to make it a daemon on Ubuntu systems (or any system that supports Upstart)

salt/minecraft/minecraft.conf

description “minecraft server”

Basic settings for the server

env max_mem={{ pillar[‘minecraft’][‘server’][‘max_mem’] }} env min_mem={{ pillar[‘minecraft’][‘server’][‘max_mem’] }} env port={{ pillar[‘minecraft’][‘server’][‘port’] }}

Run as

setuid {{ pillar[‘minecraft’][‘server’][‘user’] }} setgid {{ pillar[‘minecraft’][‘server’][‘group’] }}

Runlevel support

start on runlevel [2345] stop on runlevel [!2345]

Output log to /var/log/upstart/ directory

console log

Minecraft

script cd {{ pillar[‘minecraft’][‘server’][‘path’] }} java -Xmx$max_mem -Xms$min_mem -jar {{ pillar[‘minecraft’][‘server’][‘path’] }}/minecraft_server.{{pillar[‘minecraft’][‘server’][‘version’] }}.jar end script

respawn

respawn limit 10 5

You’ll notice that there’s liberal use of pillar within the Jinja template, but that’s just so we don’t have to work so hard later.

Last but not least let’s look at the pillar file:

pillar/minecraft.sls


    minecraft:
      server:
        port: 25565
        md5: 39df9f29e6904ea7b351ffb4fe949881
        version: 1.6.2
        path: /opt/minecraft
        user: minecraft
        group: minecraft
        max_mem: 1024M
        min_mem: 1024M

bobby

SRE, former Release Engineer, Pythonista, Gopher, Rube Goldberg Machine Untangler, Amateur Photographer. My thick skulled opinions are my own.

USA