def install_percona_xtradb_cluster():
    '''Attempt PXC install based on seeding of passwords for users'''
    if pxc_installed():
        log('MySQL already installed, skipping')
        return

    _root_password = root_password()
    _sst_password = sst_password()
    if not _root_password or not _sst_password:
        log('Passwords not seeded, unable to install MySQL at this'
            ' point so deferring installation')
        return
    configure_mysql_root_password(_root_password)

    apt_install(determine_packages(), fatal=True)

    configure_sstuser(_sst_password)
    if config('harden') and 'mysql' in config('harden'):
        run_mysql_checks()
def install_percona_xtradb_cluster():
    '''Attempt PXC install based on seeding of passwords for users'''
    if pxc_installed():
        log('MySQL already installed, skipping')
        return

    if not is_leader() and not is_leader_bootstrapped():
        log('Non-leader waiting on leader bootstrap, skipping percona install',
            DEBUG)
        return

    _root_password = root_password()
    _sst_password = sst_password()
    if not _root_password or not _sst_password:
        log('Passwords not seeded, unable to install MySQL at this'
            ' point so deferring installation')
        return
    configure_mysql_root_password(_root_password)

    apt_install(determine_packages(), fatal=True)

    configure_sstuser(_sst_password)
    if config('harden') and 'mysql' in config('harden'):
        run_mysql_checks()
def series_upgrade():

    # Set this unit to series upgrading
    set_unit_upgrading()

    # The leader will "bootstrap" with no wrep peers
    # Non-leaders will point only at the newly upgraded leader until the
    # cluster series upgrade is completed.
    # Set cluster_series_upgrading for the duration of the cluster series
    # upgrade. This will be unset with the action
    # complete-cluster-series-upgrade on the leader node.
    if (leader_get('cluster_series_upgrade_leader') == get_relation_ip(
            'cluster')):
        hosts = []
    else:
        hosts = [leader_get('cluster_series_upgrade_leader')]

    # New series after series upgrade and reboot
    _release = lsb_release()['DISTRIB_CODENAME'].lower()

    if _release == "xenial":
        # Guarantee /var/run/mysqld exists
        _dir = '/var/run/mysqld'
        mkdir(_dir, owner="mysql", group="mysql", perms=0o755)

    # Install new versions of the percona packages
    apt_install(determine_packages())
    service_stop("mysql")

    if _release == "bionic":
        render_config(hosts)

    if _release == "xenial":
        # Move the packaged version empty DB out of the way.
        cmd = [
            "mv", "/var/lib/percona-xtradb-cluster",
            "/var/lib/percona-xtradb-cluster.dpkg"
        ]
        subprocess.check_call(cmd)

        # Symlink the previous versions data to the new
        cmd = ["ln", "-s", "/var/lib/mysql", "/var/lib/percona-xtradb-cluster"]
        subprocess.check_call(cmd)

    # Start mysql temporarily with no wrep for the upgrade
    cmd = ["mysqld"]
    if _release == "bionic":
        cmd.append("--skip-grant-tables")
        cmd.append("--user=mysql")
    cmd.append("--wsrep-provider=none")
    log("Starting mysqld --wsrep-provider='none' and waiting ...")
    proc = subprocess.Popen(cmd, stderr=subprocess.PIPE)

    # Wait for the mysql socket to exist
    check_for_socket(MYSQL_SOCKET, exists=True)

    # Execute the upgrade process
    log("Running mysql_upgrade")
    cmd = ['mysql_upgrade']
    if _release == "xenial":
        cmd.append('-p{}'.format(root_password()))
    subprocess.check_call(cmd)

    # Terminate the temporary mysql
    proc.terminate()

    # Wait for the mysql socket to be removed
    check_for_socket(MYSQL_SOCKET, exists=False)

    # Clear states
    clear_unit_paused()
    clear_unit_upgrading()

    if _release == "xenial":
        # Point at the correct my.cnf
        cmd = [
            "update-alternatives", "--set", "my.cnf",
            "/etc/mysql/percona-xtradb-cluster.cnf"
        ]
        subprocess.check_call(cmd)

    # Render config
    render_config(hosts)

    resume_unit_helper(register_configs())

    # finally update the sstuser if needed.
    # BUG: #1838044
    _sst_password = sst_password()
    if _sst_password:
        configure_sstuser(_sst_password)
def series_upgrade():

    # Set this unit to series upgrading
    set_unit_upgrading()

    # The leader will "bootstrap" with no wrep peers
    # Non-leaders will point only at the newly upgraded leader until the
    # cluster series upgrade is completed.
    # Set cluster_series_upgrading for the duration of the cluster series
    # upgrade. This will be unset with the action
    # complete-cluster-series-upgrade on the leader node.
    if (leader_get('cluster_series_upgrade_leader') ==
            get_relation_ip('cluster')):
        hosts = []
    else:
        hosts = [leader_get('cluster_series_upgrade_leader')]

    # New series after series upgrade and reboot
    _release = lsb_release()['DISTRIB_CODENAME'].lower()

    if _release == "xenial":
        # Guarantee /var/run/mysqld exists
        _dir = '/var/run/mysqld'
        mkdir(_dir, owner="mysql", group="mysql", perms=0o755)

    # Install new versions of the percona packages
    apt_install(determine_packages())
    service_stop("mysql")

    if _release == "bionic":
        render_config(hosts)

    if _release == "xenial":
        # Move the packaged version empty DB out of the way.
        cmd = ["mv", "/var/lib/percona-xtradb-cluster",
               "/var/lib/percona-xtradb-cluster.dpkg"]
        subprocess.check_call(cmd)

        # Symlink the previous versions data to the new
        cmd = ["ln", "-s", "/var/lib/mysql", "/var/lib/percona-xtradb-cluster"]
        subprocess.check_call(cmd)

    # Start mysql temporarily with no wrep for the upgrade
    cmd = ["mysqld"]
    if _release == "bionic":
        cmd.append("--skip-grant-tables")
        cmd.append("--user=mysql")
    cmd.append("--wsrep-provider=none")
    log("Starting mysqld --wsrep-provider='none' and waiting ...")
    proc = subprocess.Popen(cmd, stderr=subprocess.PIPE)

    # Wait for the mysql socket to exist
    check_for_socket(MYSQL_SOCKET, exists=True)

    # Execute the upgrade process
    log("Running mysql_upgrade")
    cmd = ['mysql_upgrade']
    if _release == "xenial":
        cmd.append('-p{}'.format(root_password()))
    subprocess.check_call(cmd)

    # Terminate the temporary mysql
    proc.terminate()

    # Wait for the mysql socket to be removed
    check_for_socket(MYSQL_SOCKET, exists=False)

    # Clear states
    clear_unit_paused()
    clear_unit_upgrading()

    if _release == "xenial":
        # Point at the correct my.cnf
        cmd = ["update-alternatives", "--set", "my.cnf",
               "/etc/mysql/percona-xtradb-cluster.cnf"]
        subprocess.check_call(cmd)

    # Render config
    render_config(hosts)

    resume_unit_helper(register_configs())