Example #1
0
    def run(self):
        # current instance params (default to reading local cache)
        cur_host = self.options.cur_host or self.local_cache.get('cur_host')
        if not cur_host:
            raise InvalidConfig('Must specify cur_host')
        cur_user = self.options.cur_user or self.local_cache.get('cur_user')
        if not cur_user:
            raise InvalidConfig('Must specify cur_user')
        key_path = self.options.cur_key or self.local_cache.get(
            'key', os.path.expanduser('~/.ssh/minecraft.pem'))
        cur_region = self.options.cur_region or self.local_cache.get(
            'aws_region', 'us-west-1')

        # start new instance
        StartCommand.run(self)

        # save current state
        log.info('Saving current world...')
        runner0 = ServerRunner(cur_host, cur_user, key_path)
        mcs0 = MineCraftServer(runner0)
        world = self._get_option('world', 'world')
        default_data_dir = os.path.join(
            pynecroud.__path__[0], os.pardir, 'data')
        local_folder = self._get_option('data_folder', default_data_dir)
        mcs0.save_world_to_local(world, local_folder)
        if self.options.kill:
            mcs0.stop()  # might as well end it now

        # load onto new server
        log.info('Loading data onto new server...')
        self.mcs.load_world_on_server(world, local_folder)
        self.local_cache.update({
            "data_folder": local_folder,
            "world": world
        })

        # optionally kill old
        if self.options.kill:
            log.info('Killing {}'.format(cur_host))
            self.config['aws_region'] = cur_region
            manager = self.manager_cls(self.config)
            manager.kill_instance(dns_name=cur_host)
Example #2
0
    def run(self):
        launcher = self.launch_instance()
        user = self._get_option('login_user', 'ubuntu')
        runner = ServerRunner(
            launcher.instance.dns_name,
            user,
            key_path=launcher.key_path)
        self.mcs = MineCraftServer(runner)
        self.mcs.install()

        # writing is ec2 specific
        self.local_cache.update({
            "instance_id": launcher.instance.id,
            "host": runner.host,
            "key": launcher.key_path,
        })
        log.critical('Instance is at {}'.format(runner.host))
Example #3
0
class StartCommand(BaseCommand):
    parser = argparse.ArgumentParser(
        prog='python manage.py start --',
        description='Start a server and install minecraft, optionally loading '
                    'a world unto it',
        parents=[BaseCommand.parser])

    # These are optional and can be supplied by the config and the local cache
    parser.add_argument('--ami', help="Amazon Machine Image ID")
    parser.add_argument('--aws_region', help="AWS Region (default us-west-1)")
    parser.add_argument(
        '--security_group', help="Security group, if missing will create")
    parser.add_argument(
        '--instance_type', help="Instance type")
    parser.add_argument('--key_name', help="Key name of the instance")
    parser.add_argument('--instance_name', help="Name of the instance")
    parser.add_argument('--login_user', help='OS user on remote server')

    manager_cls = EC2Manager

    def _get_launcher_args(self):
        """This is EC2 specific"""
        # defaults to ubuntu 12.04 us-west-1
        ami = self._get_option('ami', 'ami-11e6c854')
        args = (ami,)
        kwargs = {
            "group_name": self._get_option('security_group', 'minecraft'),
            "instance_type": self._get_option('instance_type', 't1.micro'),
            "instance_name": self._get_option('instance_name', 'minecraft'),
            "key_name": self._get_option('key_name', 'minecraft'),
            "login_user": self._get_option('login_user', 'ubuntu')
        }

        # update cache
        self.local_cache.update(kwargs)
        self.local_cache['ami'] = ami

        return args, kwargs

    def launch_instance(self, block=True):
        self.config['aws_region'] = self._get_option('aws_region', 'us-west-1')
        launcher = self.manager_cls(self.config)
        args, kwargs = self._get_launcher_args()
        kwargs['block'] = block
        log.debug('Launching instance with args {} and kwargs {}'.format(
            args, kwargs))
        launcher.launch_instance(*args, **kwargs)
        return launcher

    def run(self):
        launcher = self.launch_instance()
        user = self._get_option('login_user', 'ubuntu')
        runner = ServerRunner(
            launcher.instance.dns_name,
            user,
            key_path=launcher.key_path)
        self.mcs = MineCraftServer(runner)
        self.mcs.install()

        # writing is ec2 specific
        self.local_cache.update({
            "instance_id": launcher.instance.id,
            "host": runner.host,
            "key": launcher.key_path,
        })
        log.critical('Instance is at {}'.format(runner.host))