-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart-server.py
executable file
·78 lines (67 loc) · 2.71 KB
/
start-server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
# This script requires the Python library Boto3 to be present on your system.
# Run `pip install boto3` or download from here: http://aws.amazon.com/sdk-for-python/
# User relevant defaults follow below
minecraftClient = "/usr/local/Minecraft.jar" # The path to your Minecraft client.
minecraftPort = 25565 # Port that Minecraft is running on. This is the default.
import boto3
import socket
import time
from contextlib import closing
from subprocess import call
ec2 = boto3.resource('ec2')
client = boto3.client('ec2')
def startServer(instance):
print "Starting instance"
instance.start()
waiter = client.get_waiter('instance_running')
print "Waiting for instance state to change to running"
waiter.wait(InstanceIds=[instance.id])
checkSocket(instance.public_ip_address, minecraftPort)
def launchMinecraft():
call(["java", "-jar", minecraftClient])
def fetchServerState():
print "Fetching information on the Minecraft server..."
instances = ec2.instances.filter(
Filters=[{'Name': 'tag:role', 'Values': ['minecraft']}]
)
id = []
for instance in instances:
id.append(instance)
if len(id) != 1:
print "You have %d Minecraft servers, so I don't know which you want to connect to." % len(id)
print "This is confusing, so I'm I'm stopping now."
else:
return id
def checkSocket(host, port):
print "Checking state of port %d on host %s." % (port, host)
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
if sock.connect_ex((host, port)) == 0:
print "Port is open, launching Minecraft client"
launchMinecraft()
else:
count = 0
while (count < 12):
print "Port shut, waiting 10 seconds."
count = count + 1
time.sleep(10)
print "Trying again: we've tried %d times so far." % count
if sock.connect_ex((host, port)) == 0:
print "Port is open, launching Minecraft client"
launchMinecraft()
if (sock.connect_ex((host, 22)) == 0):
print "Minecraft's not running but ssh is up. Go check out what's up w/ your server."
else:
print "Welp, something's not right there. Go check out what's up w/ your server"
print "This script checks if the Minecraft server is running, and starts it if needed!"
instance = fetchServerState()[0]
print "The instance's current state is %s" % instance.state['Name']
if (instance.state['Name'] == 'stopped'):
startServer(instance)
elif instance.state['Name'] in [ 'shutting-down', 'stopping' ]:
waiter = client.get_waiter('instance_stopped')
print "Instance is mid-state change; waiting for it to stabilise."
waiter.wait(InstanceIds=[instance.id])
startServer(instance)
else:
checkSocket(instance.public_ip_address, minecraftPort)