Last active
March 12, 2018 20:58
-
-
Save samdphillips/ba1ad15ebc250196a6b93677fd655a1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# WIP | |
import boto3 | |
import logging | |
import requests | |
import spur | |
AMI_ID = "ami-1c1d9664" | |
SG_IDS = ['sg-f3d1e28b'] | |
MINECRAFT_VERSION = '1.12.2' | |
MINECRAFT_VERSION_URL = 'https://launchermeta.mojang.com/mc/game/version_manifest.json' | |
logging.basicConfig(level=logging.INFO) | |
ec2 = boto3.resource('ec2') | |
client = boto3.client('ec2') | |
def new_server(world_name): | |
rsp = client.run_instances( | |
ImageId=AMI_ID, | |
InstanceType='m4.large', | |
KeyName='mclaunch', | |
MaxCount=1, | |
MinCount=1, | |
SecurityGroupIds=SG_IDS, | |
TagSpecifications=[ | |
{ | |
'ResourceType': 'instance', | |
'Tags': [ {'Key': 'world', 'Value': world_name} ] | |
}, | |
{ | |
'ResourceType': 'volume', | |
'Tags': [ {'Key': 'world', 'Value': world_name} ] | |
} | |
] | |
# DryRun=True | |
) | |
instance_id = rsp.get('Instances')[0].get('InstanceId') | |
instance = ec2.Instance(instance_id) | |
instance.wait_until_running() | |
logging.info('new instance running') | |
return instance | |
def find_instance(world_name): | |
rsp = client.describe_instances( | |
Filters=[ | |
{ | |
'Name': 'instance-state-name', | |
'Values': [ | |
'pending', | |
'running', | |
'stopping' | |
'stopped' | |
] | |
}, | |
{ | |
'Name': 'tag:world', | |
'Values': [ world_name ] | |
} | |
] | |
) | |
instances = rsp.get('Reservations', [])[0].get('Instances', []) | |
if len(instances) > 1: | |
raise Exception('too many instances?!?', ids) | |
elif len(instances) == 0: | |
return None | |
return ec2.Instance(instances[0].get('InstanceId')) | |
def find_minecraft_server_url(version): | |
rpy = requests.get(MINECRAFT_VERSION_URL).json() | |
for v in rpy.get('versions', []): | |
if v.get('id') == version: | |
manifest_url = v.get('url') | |
rpy = requests.get(manifest_url).json() | |
return rpy.get('downloads').get('server').get('url') | |
# instance = find_instance('NovaMundo') | |
instance = new_server('robin') | |
sh = spur.SshShell( | |
instance.public_ip_address, | |
username='ubuntu', | |
missing_host_key=spur.ssh.MissingHostKey.accept, | |
private_key_file='/home/ubuntu/secrets/id_rsa') | |
# sudo apt-add-repository -y ppa:webupd8team/java | |
sh.run('sudo apt-get update'.split()) | |
# sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -uy | |
# echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | sudo debconf-set-selections | |
sh.run('sudo apt-get install -y oracle-java8-installer rlwrap') | |
server_url = find_minecraft_server_url(MINECRAFT_VERSION) | |
sh.run(['wget', '-O', 'server.jar', server_url]) | |
with sh.open('eula.txt', 'w') as eula_f: | |
eula_f.write('eula=true\n') | |
# sh.run('tmux new java -jar server.jar nogui'.split()) | |
print('tmux new java -Xms1G -Xmx7680M -jar server.jar nogui') | |
print(instance.public_ip_address) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment