Update for AWS
This commit is contained in:
parent
6d22b4d999
commit
98e610704e
61
README.md
61
README.md
|
@ -1,31 +1,76 @@
|
|||
# Minecraft server automatic startup and shutdown
|
||||
|
||||
A collection of scripts to start a Minecraft server on Digitalocean on demand
|
||||
A collection of scripts to start a Minecraft server on Amazon AWS on demand
|
||||
and automatically shut it down when not in use.
|
||||
|
||||
## Installation and Usage
|
||||
|
||||
These packages require PHP, Python3, the PIP module mcstatus, and a webserver.
|
||||
These packages require PHP, Python3, the PIP module mcstatus, the AWS cli, and a webserver.
|
||||
Install this on a server different from your minecraft server, since you'll be shutting that down.
|
||||
|
||||
`python3 -m pip install mcstatus`
|
||||
Run as root: `python3 -m pip install mcstatus`
|
||||
|
||||
Install the .php file on a webserver and configure the variables as desired.
|
||||
|
||||
Place the remaining files on a server with CRON (could be the same server, just
|
||||
not inside the web folder) and run the shell script every 5 minutes:
|
||||
Place the remaining files the same server in the `/var/minecraft/` folder
|
||||
and use CRON to run the shell script every 5 minutes:
|
||||
|
||||
`*/5 * * * * root /your/path/here/mineautoshutdown.sh 2>&1 | logger`
|
||||
`*/5 * * * * root /var/minecraft/mineautoshutdown.sh 2>&1 | logger`
|
||||
|
||||
Ensure the same password is present in the php script and the shutdown script.
|
||||
Review all other files to make sure passwords/keys/hostnames are correct.
|
||||
Review all other files to make sure passwords/keys/hostnames/regions are correct.
|
||||
|
||||
Call the php script via HTTP using the correct password when players want to play:
|
||||
|
||||
`curl http://your.webserver.here/minecraft.php?action=on&password=your_password_here`
|
||||
`curl -X POST -F "action=on" http://your.webserver.here/minecraft.php?password=your_password_here`
|
||||
|
||||
Or just direct them to <http://your.webserver.here/minecraft.php?password=your_password_here>
|
||||
|
||||
If the CRON job runs properly and has all the right variables/permissions/dependencies,
|
||||
it will shut down the DO droplet about 30-40 minutes after no players are detected in the game.
|
||||
|
||||
### AWS Setup
|
||||
|
||||
Install Minecraft Java Edition on an AWS instance. Give it an Elastic IP so it stays consistent.
|
||||
Set Minecraft to start on boot and ensure it has the correct ports open in its Security Group.
|
||||
|
||||
Create an IAM policy with these permissions (edit the instance ID to be your minecraft instance)
|
||||
|
||||
```
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Sid": "VisualEditor0",
|
||||
"Effect": "Allow",
|
||||
"Action": [
|
||||
"ec2:StartInstances",
|
||||
"ec2:StopInstances"
|
||||
],
|
||||
"Resource": "arn:aws:ec2:*:*:instance/your_instance_id_here"
|
||||
},
|
||||
{
|
||||
"Sid": "VisualEditor1",
|
||||
"Effect": "Allow",
|
||||
"Action": "ec2:DescribeInstanceStatus",
|
||||
"Resource": "*"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Create an IAM user attached to that policy and create an access key for it.
|
||||
On a the server where you've downloaded Minecraft-Minder, save the key
|
||||
and id to `/var/www/.aws/credentials` (assuming you use Ubuntu/Apache) with:
|
||||
|
||||
```
|
||||
[default]
|
||||
aws_access_key_id = AKIA_your_key_id_here
|
||||
aws_secret_access_key = your_secret_key_here
|
||||
```
|
||||
|
||||
Finally `chown -r www-data:www-data /var/www/.aws/`
|
||||
|
||||
## License
|
||||
|
||||
Copyright (C) 2023 zyphlar
|
||||
|
|
|
@ -4,8 +4,8 @@ DO_STATUS_URL="http://example.com/minecraft.php"
|
|||
PASSWORD="your_password_here"
|
||||
status=$(curl -s -X POST -F "action=status" "$DO_STATUS_URL?password=$PASSWORD")
|
||||
|
||||
if [[ $status == "active" ]]; then
|
||||
players=$(python3 $(dirname $0)minestatus.py | jq ".players")
|
||||
if [[ $status == "running" ]]; then
|
||||
players=$(python3 $(dirname $0)/minestatus.py | jq ".players")
|
||||
|
||||
if [[ $players == 0 ]]; then
|
||||
|
||||
|
|
|
@ -1,47 +1,35 @@
|
|||
<?php
|
||||
|
||||
$DIGITALOCEAN_TOKEN="dop_your_digitalocean_token_here";
|
||||
$DROPLET_ID="your_droplet_id_here";
|
||||
$REGION="us-west-1"; // change as needed
|
||||
$INSTANCE_ID="your_instance_id_here";
|
||||
$PASSWORD="your_password_here";
|
||||
|
||||
if ($_GET['password'] !== $PASSWORD) {
|
||||
usleep(rand(0,999999));
|
||||
exit("Invalid password.");
|
||||
}
|
||||
|
||||
if ($_POST['action'] == "on") {
|
||||
|
||||
$out = `curl -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
|
||||
-d '{"type":"power_on"}' \
|
||||
"https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`;
|
||||
$out = `aws ec2 start-instances --region=$REGION --instance-id $INSTANCE_ID 2>&1`;
|
||||
|
||||
} elseif ($_POST['action'] == "off") {
|
||||
|
||||
$out = `curl -X POST \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
|
||||
|
||||
-d '{"type":"power_off"}' \
|
||||
"https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`;
|
||||
$out = `aws ec2 stop-instances --region=$REGION --instance-id $INSTANCE_ID`;
|
||||
|
||||
} elseif ($_POST['action'] == "status") {
|
||||
|
||||
$out = `curl -X GET \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
|
||||
"https://api.digitalocean.com/v2/droplets/$DROPLET_ID"`;
|
||||
$out = `aws ec2 describe-instance-status --region=$REGION --include-all-instances --instance-id $INSTANCE_ID`;
|
||||
|
||||
}
|
||||
|
||||
if ($out) {
|
||||
$json = json_decode($out, true);
|
||||
|
||||
if ($json['action']) {
|
||||
$status = $json['action']['status'];
|
||||
} elseif ($json['droplet']) {
|
||||
$status = $json['droplet']['status'];
|
||||
if ($_POST['action'] == "on") {
|
||||
echo "<a href='javascript:history.back()'>Go back</a><br/><br/><b>Result:</b> ";
|
||||
}
|
||||
|
||||
if ($json['StartingInstances']) {
|
||||
exit($json['StartingInstances'][0]['CurrentState']['Name']);
|
||||
} elseif ($json['StoppingInstances']) {
|
||||
exit($json['StoppingInstances'][0]['CurrentState']['Name']);
|
||||
} elseif ($json['InstanceStatuses']) {
|
||||
exit($json['InstanceStatuses'][0]['InstanceState']['Name']);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,12 +37,32 @@ if ($out) {
|
|||
<head><title>MineCollective Minecraft Server</title></head>
|
||||
<body>
|
||||
<p>
|
||||
Press this button to turn on the server. It'll take up to 5 minutes to boot, and turn off
|
||||
after about 30 minutes of inactivity.
|
||||
Press this button to turn on the server. It'll take a couple minutes to fully turn on,
|
||||
and it'll turn off automatically after about 30 minutes of inactivity.
|
||||
</p>
|
||||
<form action="#" method="POST">
|
||||
<input type="hidden" name="action" value="on" />
|
||||
<input type="submit" value="Turn On" />
|
||||
</form>
|
||||
<b>Current status:</b>
|
||||
<i><?php
|
||||
|
||||
$status = `aws ec2 describe-instance-status --region=$REGION --include-all-instances --instance-id $INSTANCE_ID 2>&1`;
|
||||
$json = json_decode($status, true);
|
||||
echo $json['InstanceStatuses'][0]['InstanceState']['Name'];
|
||||
|
||||
if ($json['InstanceStatuses'][0]['InstanceState']['Name'] == "running") {
|
||||
$players = `python3 /var/minecraft/minestatus.py 2>&1`;
|
||||
$json = json_decode($players, true);
|
||||
if ($json['players'] !== null) {
|
||||
echo " with ".$json['players']." players";
|
||||
} else {
|
||||
echo " but not reachable yet.";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?></i>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user