This is a small Python script I've written to be able to automate deallocating my PaperMC virtual machine if there was no one playing at the moment.
Along with it there is a startup shellscript (startup.sh
) meant to be executed as a cronjob and an Ansible playbook (mc_playbook.yaml
) to set it all up.
startup.sh
spawns a new tmux session with PaperMC on the left side, andstopper.py
on the right side. It redirects the output of PaperMC tostopper.py
viatmux pipe-pane
command piping to a named pipe, which in turn gets redirected tostopper.py
. It also redirects the stdout and stderr ofstopper.py
to a logfile (whose location is defined invars.yaml
).stopper.py
reads from standard input and counts the number of active players. If that number drops to 0, it starts a timer. If the timer is not cancelled by a player join and it finishes, it starts a deallocation procedure (dealloc_vm). Since I've hosted my virtual machine on Azure, I've set up a small Azure Logic App that deallocates the virtual machine on a HTTP GET request (hencedealloc_url
invars.yaml
).
I will assume you know how to use Ansible. If you don't, Ansible docs are one of the best I've come across: https://docs.ansible.com/
Just change the necessary variables in vars.yaml
and mc_playbook.yaml
. All of them start with your_.
I would also advise to skim through the rest of the variables, not all of the default values are guaranteed to be fitting for your usecase.
- PaperMC's console log timestamp doesn't match system time (and therefore scripts' logfiles timestamps, which can lead to confusion reviewing those)