'Create database if required for this server'

def create_database(self):

    if self.needs_database:
        conf_dir = os.path.join(self.test_dir, 'etc')
        utils.safe_mkdirs(conf_dir)
        conf_filepath = os.path.join(conf_dir, 'glance-manage.conf')
        with open(conf_filepath, 'w') as conf_file:
            conf_file.write('[DEFAULT]\n')
            conf_file.write(('sql_connection    =    %s' % self.sql_connection))
            conf_file.flush()
        glance_db_env = 'GLANCE_DB_TEST_SQLITE_FILE'
        if (glance_db_env in os.environ):
            db_location = os.environ[glance_db_env]
            os.system(('cp    %s    %s/tests.sqlite' % (db_location, self.test_dir)))
        else:
            cmd = ('%s    -m    glance.cmd.manage    --config-file    %s    db    sync' % (sys.executable, conf_filepath))
            execute(cmd, no_venv=self.no_venv, exec_env=self.exec_env, expect_exit=True)
            (osf, db_location) = tempfile.mkstemp()
            os.close(osf)
            os.system(('cp    %s/tests.sqlite    %s' % (self.test_dir, db_location)))
            os.environ[glance_db_env] = db_location
            def _delete_cached_db():
                try:
                    os.remove(os.environ[glance_db_env])
                except Exception:
                    glance_tests.logger.exception(('Error    cleaning    up    the    file    %s' % os.environ[glance_db_env]))
            atexit.register(_delete_cached_db)
