#!/usr/bin/python import sys import os import socket import random import subprocess import boto3 import json import signal from ec2_metadata import ec2_metadata # global variables region='us-west' public_hostname='' public_port='' private_ipv4='' # Get the service resource sqs_cli=boto3.resource('sqs',region_name='us-west-2') # Get the queue queuename=os.environ['QUEUENAME'] queue = sqs_cli.get_queue_by_name(QueueName=queuename) # Get the server group group=os.environ['GROUP'] def sigterm_handler(_signo, _stack_frame): print 'in sigterm_handler' publish_game_server_status('terminating','gs') def publish_game_server_status(status,server_type): print 'in publish_game_server_status with hostname='+public_hostname+' port='+str(public_port)+' region='+region+' status='+status+' type='+server_type+' private_ipv4='+private_ipv4+' group='+group data={ 'public_hostname':public_hostname, 'public_port':str(public_port), 'region':region, 'status':status, 'type':server_type, 'private_ipv4':private_ipv4, 'group':group } print str(data) try: # Send the message to the queue response = queue.send_message( MessageBody=str(json.dumps(data)) ) # The response is NOT a resource, but gives you a message ID and MD5 print('response message id is '+response.get('MessageId')) except Exception as e: print 'error publishing server status via SQS' print str(e) def get_rand_port(): print 'in get random port' # Attempting to get random port try: s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'socket created' s.bind(('',0)) except socket.error as msg: print 'bind failed. Error is '+str(msg[0])+' Msg '+msg[1] print 'socket bind complete ' # Capture the port and release the socket port=s.getsockname()[1] s.close() print 'dynamic port to start the game server is '+str(port) return port if __name__ == '__main__': # Catch SIGTERM to report game-server status signal.signal(signal.SIGTERM, sigterm_handler) # Getting ports and hostname from the platform public_port=get_rand_port() print 'got server port '+str(public_port) os.environ['SERVER_PORT'] = str(public_port) print 'populated the SERVER_PORT environment variable before launching the game-server' public_hostname=ec2_metadata.public_hostname print 'The public hostname of the game server is '+public_hostname private_ipv4=ec2_metadata.private_ipv4 print 'The local hostname of the game server is '+private_ipv4 # Publishing game-server init status publish_game_server_status('init','gs') # Starting the game-server subprocess.call(['/mockup-udp-server.py'])