def sc_ddns_link(args): db = db_read() try: ddns_url = db['ddns'][args.domain] except KeyError: print('ERROR: no ddns entry with that domain', file=stderr) return 3 try: instance_spec = db['servers'][args.name] except KeyError: print('ERROR: no server with that name', file=stderr) return 1 instance_spec['ddns_url'] = ddns_url instance = ec2.Instance.from_dict(instance_spec) try: instance.update_ddns() except TimeoutError as e: print(e, file=stderr) return 5 db['servers'][args.name] = instance.to_dict() db_write(db)
def sc_ddns_remove(args): db = db_read() try: del db['ddns'][args.domain] except KeyError: print('ERROR: no ddns entry with that domain', file=stderr) return 3 db_write(db)
def sc_terminate(args): db = db_read() try: instance = db['servers'].pop(args.name) instance = ec2.Instance.from_dict(instance) instance.terminate() except KeyError: print('ERROR: no server with that name', file=stderr) return 1 db_write(db)
def _ddns_add(domain, url): db = db_read() try: if domain in db['ddns']: print('ERROR: ddns entry with that domain already exists', file=stderr) return 4 except KeyError: db['ddns'] = dict() db['ddns'][domain] = url db_write(db)
def sc_ddns_unlink(args): db = db_read() try: instance_spec = db['servers'][args.name] except KeyError: print('ERROR: no server with that name', file=stderr) return 1 try: instance_spec.pop('ddns_url') except KeyError: print('ERROR: server does not have ddns configured', file=stderr) return 8 db_write(db)
def _run_cmd(server_nickname, cmd): db = db_read() try: instance = ec2.Instance.from_dict(db['servers'][args.name]) except KeyError: print('ERROR: no server with that name', file=stderr) return 1 if not instance.last_ip: try: instance.wait_ip() except TimeoutError as e: print(e, file=stderr) return 5 # last_ip may have updated db['servers'][args.name] = instance.to_dict() db_write(db) ssh(instance.last_ip, instance.keypair.private, cmd)
def sc_launch(args): db = db_read() ddns_url = None if args.ddns: try: ddns_url = db['ddns'][args.ddns] except KeyError: print('ERROR: no ddns entry with that domain', file=stderr) return 3 try: if args.name in db['servers']: print('ERROR: server with that name already exists', file=stderr) return 2 except KeyError: db['servers'] = dict() ops = args.ops.split(',') memory = INSTANCE_TYPES[args.type]["jvm_memory"] icon = args.icon or DEFAULT_ICON motd = args.motd or DEFAULT_MOTD config = generate_config(memory, icon, ops, motd) ami = get_ami(args.region) cost = INSTANCE_TYPES[args.type]["hourly_price"] print( f"You're launching a {args.type} instance and allocating {memory} of RAM to the JVM.\nThis will cost around:\n ${cost:8.2f}/hr\n ${cost*24:8.2f}/day\n ${cost*24*31:8.2f}/mo\n ${cost*24*365.25:8.2f}/yr\nuntil you turn it off. Okay? [y/N]", file=stderr) if input().strip().lower() not in ('y', 'yes', 'ok', 'sure', 'fine', 'k', 'whatever', 'whatevs', 'ok boomer'): print("Whew, that was close!", file=stderr) return 6 new_server = ec2.Instance.launch(config, args.region, args.type, ami, DEFAULT_OPEN_PORTS, ddns_url) db['servers'][args.name] = new_server.to_dict() db_write(db)
def sc_info(args): db = db_read() try: instance = ec2.Instance.from_dict(db['servers'][args.name]) except KeyError as e: print(e) print('ERROR: no server with that name', file=stderr) return 1 if args.get_ip: try: instance.wait_ip() except TimeoutError as e: print(e, file=stderr) return 5 # last_ip may have updated db['servers'][args.name] = instance.to_dict() db_write(db) pprint(db['servers'][args.name])
def sc_ddns_update(args): db = db_read() try: instance_spec = db['servers'][args.name] except KeyError: print('ERROR: no server with that name', file=stderr) return 1 if not instance_spec.get('ddns_url'): print('ERROR: ddns not set up for that server', file=stderr) return 7 instance = ec2.Instance.from_dict(instance_spec) try: instance.update_ddns() except TimeoutError as e: print(e, file=stderr) return 5 # last_ip may have updated db['servers'][args.name] = instance.to_dict() db_write(db)
def sc_mc_save(args): db = db_read() try: instance = ec2.Instance.from_dict(db['servers'][args.name]) except KeyError: print('ERROR: no server with that name', file=stderr) return 1 if not instance.last_ip: try: instance.wait_ip() except TimeoutError as e: print(e, file=stderr) return 5 # last_ip may have updated db['servers'][args.name] = instance.to_dict() db_write(db) worlds_dir = xdg_data_home() / 'emc' / 'worlds' worlds_dir.mkdir(parents=True, exist_ok=True) world_name = datetime.utcnow().isoformat(timespec='seconds').replace( ':', '') world_path = worlds_dir / ('minecraft_world_' + world_name + '.tar.gz') print("pausing minecraft process...", file=stderr, end=' ', flush=True) mc_stop(instance) print("ok") print( f"downloading world from server {args.name} to local path {world_path}:", file=stderr) print("scp connecting...", file=stderr, flush=True, end='\r') mc_download_world(instance, world_path) print(f"download succeeded", file=stderr) print("resuming minecraft process...", file=stderr, end=' ', flush=True) mc_start(instance) print("ok", file=stderr)