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)