-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
80 lines (66 loc) · 2.38 KB
/
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
79
80
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
from mcstatus import MinecraftServer
import boto3
import logging
import time
import os
app = Flask(__name__)
CORS(app)
logger = logging.getLogger()
# Create session to access AWS environment
session = boto3.Session(
aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"],
region_name=os.environ["REGION_NAME"]
)
# Create ec2 resource
ec2 = session.resource("ec2")
# Create instance object
instance = ec2.Instance(os.environ["INSTANCE_ID"])
# Main endpoint for loading the webpage
@app.route('/')
def index():
# Update instance object
instance = ec2.Instance(os.environ["INSTANCE_ID"])
if instance.state['Code'] == 16:
# Attempt to ping Minecraft server... if failed, assume server is booting up
try:
lookup = MinecraftServer.lookup(instance.public_ip_address).query()
server = {
'status': 'online',
'current_players': lookup.players.online,
'max_players': lookup.players.max,
'players': lookup.players.names,
'ip': instance.public_ip_address + ':25565'
}
except Exception:
server = {
'status': 'starting',
'ip': instance.public_ip_address + ':25565'
}
else:
server = {
'status': 'offline'
}
return render_template('index.html', server=server)
@app.route('/init', methods=['POST'])
def init_server_minecraft():
input_pass = request.form['password']
if input_pass == os.environ["SERVER_PASSWORD"]:
logger.info("Password correct... initiating server startup")
response = instance.start()
state_code = instance.state['Code']
# Wait until instance has completely started
while not (state_code == 16):
time.sleep(3)
logger.info("Received AWS EC2 start response: {}".format(str(response)))
state_code = instance.state['Code']
logger.info("Server instance initiated: {}".format(instance))
ip_address = instance.public_ip_address
return jsonify({'login': True, 'ip': ip_address + ':25565'})
else:
logger.info("Password incorrect")
return jsonify({'login': False})
if __name__ == "__main__":
app.run()