def status(): instance_id = flask.request.args["q"] if instance_id == "test": return json.dumps({"progress": 100, "ip": "123.123.123.123"}) logging.info("status: fetching instance") x = ecs.describe_tasks(cluster="craftassist", tasks=[instance_id]) try: attachment_id = x["tasks"][0]["containers"][0]["networkInterfaces"][0][ "attachmentId"] attachment = next(y for y in x["tasks"][0]["attachments"] if y["id"] == attachment_id) except: return json.dumps({"progress": 30}) try: eni = next(y for y in attachment["details"] if y["name"] == "networkInterfaceId")["value"] ip = ec2.NetworkInterface( eni).private_ip_addresses[0]["Association"]["PublicIp"] except: return json.dumps({"progress": 50, "ip": None}) try: logging.info("status: trying socket connect") s = socket.socket() s.settimeout(10) s.connect((ip, 25565)) s.close() except: return json.dumps({"progress": 75, "ip": ip}) try: logging.info("status: trying ping") ping_cuberite.ping(ip, 25565, timeout=1) except: return json.dumps({"progress": 90, "ip": ip}) logging.info("status: success") # register subdomain to proxy instance IP if os.getenv("CLOUDFLARE_TOKEN") and os.getenv("CLOUDFLARE_ZONE_ID"): logging.info("registering subdomain on craftassist.io") cloudflare_token = os.getenv("CLOUDFLARE_TOKEN") zone_id = os.getenv("CLOUDFLARE_ZONE_ID") cf = CloudFlare.CloudFlare(email='*****@*****.**', token=cloudflare_token) dns_records = cf.zones.dns_records.get(zone_id) register_dashboard_subdomain(cf, zone_id, ip) return json.dumps({"progress": 100, "ip": ip})
def status(): instance_id = flask.request.args["q"] if instance_id == "test": return json.dumps({"progress": 100, "ip": "123.123.123.123"}) logging.info("status: fetching instance") x = ecs.describe_tasks(cluster="craftassist", tasks=[instance_id]) try: attachment_id = x["tasks"][0]["containers"][0]["networkInterfaces"][0][ "attachmentId"] attachment = next(y for y in x["tasks"][0]["attachments"] if y["id"] == attachment_id) except: return json.dumps({"progress": 30}) try: eni = next(y for y in attachment["details"] if y["name"] == "networkInterfaceId")["value"] ip = ec2.NetworkInterface( eni).private_ip_addresses[0]["Association"]["PublicIp"] except: return json.dumps({"progress": 50, "ip": None}) try: logging.info("status: trying socket connect") s = socket.socket() s.settimeout(10) s.connect((ip, 25565)) s.close() except: return json.dumps({"progress": 75, "ip": ip}) try: logging.info("status: trying ping") ping_cuberite.ping(ip, 25565, timeout=1) except: return json.dumps({"progress": 90, "ip": ip}) logging.info("status: success") return json.dumps({"progress": 100, "ip": ip})
def is_instance_up(instance_id): try: x = ecs.describe_tasks(cluster="craftassist", tasks=[instance_id]) attachment_id = x["tasks"][0]["containers"][0]["networkInterfaces"][0]["attachmentId"] attachment = next(y for y in x["tasks"][0]["attachments"] if y["id"] == attachment_id) eni = next(y for y in attachment["details"] if y["name"] == "networkInterfaceId")["value"] ip = ec2.NetworkInterface(eni).private_ip_addresses[0]["Association"]["PublicIp"] s = socket.socket() s.settimeout(10) s.connect((ip, 25565)) s.close() ping_cuberite.ping(ip, 25565, timeout=1) except: return False return True
return r["tasks"][0]["taskArn"] def describe_task(task_arn): x = ecs.describe_tasks(cluster="craftassist", tasks=[task_arn]) logging.info(x) attachment_id = x["tasks"][0]["containers"][0]["networkInterfaces"][0][ "attachmentId"] attachment = next(y for y in x["tasks"][0]["attachments"] if y["id"] == attachment_id) eni = next(y for y in attachment["details"] if y["name"] == "networkInterfaceId")["value"] ip = ec2.NetworkInterface( eni).private_ip_addresses[0]["Association"]["PublicIp"] return ip if __name__ == "__main__": # task_arn = launch_task() # logging.info('Launched task {}'.format(task_arn)) task_arn = "55394421-9b47-4fa1-aa8e-0fabb3b0765a" for _ in range(120): time.sleep(1) try: ip = describe_task(task_arn) logging.info("IP {}".format(ip)) ping_cuberite.ping(ip, 25565, timeout=1) logging.info("DONE") except Exception as e: logging.info("Not yet... {}".format(e))
def wait_for_cuberite(host, port): wait_for_server(host, port) ping_cuberite.ping(host, port, timeout=10)