How to: Automate Minecraft Server Installations Using Cloud-init and Ubuntu 14.04 or Debian 7.x

Introduction

In this tutorial, we will demonstrate how you can automate a Spigot or vanilla Minecraft server installation using cloud-init’s user data script: cloud-config. Additionally, the Spigot cloud-config file contains an example for automatically installing a plugin and writing a custom configuration file for it. Provided below are our examples for both types of Minecraft servers with line by line breakdowns for each cloud-config.

Prerequisites:

Ubuntu 14.04 or Debian 7.x with at least 1GB of RAM available to dedicate to the Minecraft server process.

Spigot
#cloud-config
chpasswd:
 list: |
   %%root_user%%:%%root_pass%%
 expire: False
ssh_pwauth: True
manage_etc_hosts: True
runcmd: 
 - apt-get clean all
 - apt-get update
 - DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
 - apt-get install -y openjdk-7-jre-headless git screen
 - mkdir -p /root/minecraft && wget -P /root/minecraft https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar
 - export HOME=/root/minecraft
 - cd /root/minecraft && java -jar BuildTools.jar 
 - java -Xms512M -Xmx1024M -XX:MaxPermSize=128M -jar spigot-1.8.3.jar && sed -i -e s/"eula=false"/"eula=true"/ /root/minecraft/eula.txt
 - wget -P /root/minecraft/plugins https://dl.dropboxusercontent.com/u/275952914/CustomMessages%20v1.0.jar
 - screen -dmS minecraft java -Xms512M -Xmx1024M -XX:MaxPermSize=128M -jar spigot-1.8.3.jar 
write_files:
  - path: /root/minecraft/plugins/CustomMessages/config.yml
    permissions: '0644'
    owner: root:root
    content: |
      JoinShout: '&b%player% &7has joined the server!'
      LeaveShout: '&b%player% &7left the server!'

      WelcomeMessage: '&eWelcome, &b%player% &fto MY AUTOMATED CLOUD-INIT MC SERVER'
      WelcomeMessage1: 'This is a Spigot Minecraft server installation'
      WelcomeMessage2: 'Visit www.ubiquityhosting.com/blog/automate-minecraft-spigot-server-installation-ubuntu-debian to setup your own'
Breakdown:

Default Directives (on our cloud platform)

#cloud-config:

This indicates that the user data script being utilized will be a cloud-config and is required when using one.

chpasswd::

Chpasswd is the directive that will need to be used to change passwords on accounts that already exist.

list: |:

The list directive denotes the list of users that the chpasswd command will run against. The pipe character ( | ) simply tells cloud-config to interpret the following text exactly how it is written. Notice that this line is indented with a single space indicating that it should be interpreted as part of the chpasswd directive.

%%root_user%%:%%root_pass%%:

The double percent signs indicate a variable to our internal API and will update the root user’s password accordingly.

expire: False:

Indicates whether or not you would like to require the password be updated after the initial login.

ssh_pwauth: True:

Enables the ability to use the root password for SSH authentication.

manage_etc_hosts: True:

Allows your hostname to be updated to what you set when you ordered the cloud.

runcmd::

This directive allows you to provide a list of shell commands you wish to have executed after booting in the order you provide.

apt-get clean all:

Executes the shell command to remove all packages from the apt package cache.

apt-get update:

Executes the shell command to ensure your apt package sources are up-to-date.

DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef"...:

Tells apt-get to allow the installation of packages without any user interaction. Additionally, it adds non-interactive logic for handling configuration file updates.

Non-default Runcmd Commands

apt-get install -y openjdk-7-jre-headless git screen:

Installs Java, git, and screen packages.

mkdir -p /root/minecraft && wget -P /root/minecraft https://hub.spigotmc.org/...:

Creates a folder for Minecraft in the root directory and then downloads the BuildTools.jar file to install Spigot.

export HOME=/root/minecraft:

Sets the HOME environment variable to /root/minecraft so that Java won’t fatally error when installing Spigot.

cd /root/minecraft && java -jar BuildTools.jar

Changes the directory to the one we created earlier and begins the Spigot installation process (takes several minutes to complete).

java -Xms512M -Xmx1024M -XX:MaxPermSize=128M -jar spigot-1.8.3.jar && sed...:

Starts the Spigot server for the first time so that the eula.txt and other necessary files are generated. Lastly, it updates the text to ‘true’ in eula.txt for accepting the end user license agreement to be able to run the server.

screen -dmS minecraft java -Xms512M -Xmx1024M -XX:MaxPermSize=128M -jar...:

Creates a screen session with Spigot running your server. The server console is then accessible in your terminal when you run screen -r minecraft or simply screen -r (as long as you have no other screen sessions running). Detach from the screen session by pressing ctrl+a+d.

write_files:

This directive allows you to write a text file.

- path: /root/minecraft/plugins/CustomMessages/config.yml:

Sets the file path for where you want to write the text file.

permissions: '0644':

Configures the Linux permissions you desire the text file to have.

owner: root:root:

Defines the Linux user and group that owns the text file.

content: |

A sub-directive of write_files, which determines the contents of the text file that you wish to have written by cloud-init. The pipe ( | ) tells cloud-init to interpret the contents exactly how they’re written below it. Each indented line will be a new line that is written to the specified text file.

JoinShout: '&b%player% &7has joined the server!:

This and the remaining text lines in the above cloud-config are what will be written to your config.yml for the CustomMessages plugin.

Vanilla Minecraft
#cloud-config
chpasswd:
 list: |
   %%root_user%%:%%root_pass%%
 expire: False
ssh_pwauth: True
manage_etc_hosts: True
runcmd: 
 - apt-get clean all
 - apt-get update
 - DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
 - apt-get install -y openjdk-7-jre-headless git screen
 - mkdir -p /root/minecraft && wget -P /root/minecraft https://s3.amazonaws.com/Minecraft.Download/versions/1.8.4/minecraft_server.1.8.4.jar
 - cd /root/minecraft && java -Xms512M -Xmx1024M -XX:MaxPermSize=128M -jar minecraft_server.1.8.4.jar && sed -i -e s/"eula=false"/"eula=true"/ /root/minecraft/eula.txt
 - screen -dmS minecraft java -Xms512M -Xmx1024M -XX:MaxPermSize=128M -jar minecraft_server.1.8.4.jar
Breakdown:

Default Directives (on our cloud platform)

#cloud-config:

This indicates that the user data script being utilized will be a cloud-config and is required when using one.

chpasswd::

Chpasswd is the directive that will need to be used to change passwords on accounts that already exist.

list: |:

The list directive denotes the list of users that the chpasswd command will run against. The pipe character ( | ) simply tells cloud-config to interpret the following text exactly how it is written. Notice that this line is indented with a single space indicating that it should be interpreted as part of the chpasswd directive.

%%root_user%%:%%root_pass%%:

The double percent signs indicate a variable to our internal API and will update the root user’s password accordingly.

expire: False:

Indicates whether or not you would like to require the password be updated after the initial login.

ssh_pwauth: True:

Enables the ability to use the root password for SSH authentication.

manage_etc_hosts: True:

Allows your hostname to be updated to what you set when you ordered the cloud.

runcmd::

This directive allows you to provide a list of shell commands you wish to have executed after booting in the order you provide.

apt-get clean all:

Executes the shell command to remove all packages from the apt package cache.

apt-get update:

Executes the shell command to ensure your apt package sources are up-to-date.

DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef"...:

Tells apt-get to allow the installation of packages without any user interaction. Additionally, it adds non-interactive logic for handling configuration file updates.

Non-default Runcmd Commands

apt-get install -y openjdk-7-jre-headless git screen:

Installs Java, git, and screen packages.

mkdir -p /root/minecraft && wget -P /root/minecraft https://s3.amazonaws.com/...:

Creates a directory for Minecraft in the root directory and then downloads the server application.

cd /root/minecraft && java -Xms512M -Xmx1024M -XX:MaxPermSize=128M -jar...:

Changes the directory to the one we created earlier. Then it starts the Minecraft server for the first time so that the eula.txt and other necessary files are generated. Lastly, it updates the text to ‘true’ in eula.txt for accepting the end user license agreement to be able to run the server.

screen -dmS minecraft java -Xms512M -Xmx1024M -XX:MaxPermSize=128M -jar...:

Creates a screen session with the server running in it. The server console is accessible in your terminal when you run screen -r minecraft or simply screen -r (as long as you have no other screen sessions running). Detach from the screen session by pressing ctrl+a+d to get back to your terminal.

Conclusion

Hopefully, you found the above examples useful in automating deployments for your Minecraft server community or game server hosting business. Keep in mind that you can further automate this process by utilizing our API’s cloud.create function by passing it your custom user data for cloud-config and fully automate your server setups on our cloud platform from beginning to end.

Written by
on May 13, 2015

Facebook Twitter Google+ LinkedIn Addthis