def bootstrap()

in mysqloperator/sidecar_main.py [0:0]


def bootstrap(pod: MySQLPod, datadir: str, logger: Logger) -> int:
    """
    Prepare MySQL instance for InnoDB cluster.

    This function must be idempotent, because the sidecar container can get
    restarted in many different scenarios. It's also possible that the
    whole pod gets deleted and recreated while its underlying PV is reused.
    In that case, the Pod will look brand new (so we can't rely on any data
    stored in the Pod object), but the instance will be already prepared and
    not be in the expected initial state with initial defaults.

    Returns 1 if bootstrapped, 0 if already configured and -1 on error
    """
    name = pod.name
    namespace = pod.namespace

    # Check if the Pod is already configured according to itself
    gate = pod.get_member_readiness_gate("configured")
    if gate:
        logger.info(f"MySQL server was already initialized configured={gate}")
        return 0

    cluster = pod.get_cluster()

    # In most cases we have a fresh datadir at this point, where only a user
    # localroot exists. A restore using MySQL Shell would only be done later.
    # However if a restore was done using MEB the datadir will be populated
    # with whatever data there was in the backup during init before this runs.
    # In that case we have to get in using credentials passed by the user.
    # We also must be careful about changs we do, as a PITR may follow and
    # changes may break application of binlogs.
    if pod.index == 0 and cluster.get_create_time() is None and cluster.parsed_spec.initDB and cluster.parsed_spec.initDB.meb:
        logger.info("Detected MEB restore, giving ourselves access")
        mebsession = None
        user, _, password = get_root_account_info(cluster)
        logger.info("Using account %s", user)
        try:
            mebsession = connect(user, password, logger, timeout=None)
            create_local_accounts(mebsession, logger)
        finally:
            if mebsession:
                mebsession.close()

    # Connect using localroot and check if the metadata schema already exists

    # note: we may have to wait for mysqld to startup, since the sidecar and
    # mysql containers are started at the same time.
    session = connect("localroot", "", logger, timeout=None)

    # Restore from MEB is done in init and the Backup may contain old metadata
    # mabe we could only check cluster create time in all cases, but that would
    # need a lot more testing in restart situations
    mdver = metadata_schema_version(session, logger)
    initdb_spec = cluster.parsed_spec.initDB
    if mdver and not (initdb_spec and initdb_spec.meb and cluster.get_create_time() is None):
        logger.info(f"InnoDB Cluster metadata (version={mdver}) found, skipping configuration...")
        pod.update_member_readiness_gate("configured", True)
        return 0

    # Check if the datadir already existed

    logger.info(
        f"Configuring mysql pod {namespace}/{name}, configured={gate} datadir={datadir}")

    try:
        initialize(session, datadir, pod, cluster, logger)

        pod.update_member_readiness_gate("configured", True)

        logger.info("Configuration finished")
    except Exception as e:
        import traceback
        traceback.print_exc()
        logger.critical(f"Unhandled exception while bootstrapping MySQL: {e}")
        # TODO post event to the Pod and the Cluster object if this is the seed
        return -1

    session.close()

    return 1