<commit_before>#!/bin/bash
#
#    Licensed to the Apache Software Foundation (ASF) under one
#    or more contributor license agreements.  See the NOTICE file
#    distributed with this work for additional information
#    regarding copyright ownership.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#

# Sets up the messaging backend service needed by the AMQP 1.0
# transport (amqp://)
#
# Environment Configuration
#
# AMQP1_SERVICE - identifies the messaging backend to use.  Should be
#    one of 'qpid', ...
#    @TODO(kgiusti) add qpid-dispatch, rabbitmq, etc
# AMQP1_HOST - the host:port used to connect to the messaging service.
#    Defaults to 127.0.0.1:5672
# AMQP1_{USERNAME,PASSWORD} - for authentication with AMQP1_HOST
#

# Save trace setting
XTRACE=$(set +o | grep xtrace)
set +o xtrace

# builds transport url string
function _get_amqp1_transport_url {
    echo "amqp://$AMQP1_USERNAME:$AMQP1_PASSWORD@$AMQP1_HOST:5672/"
}


# install packages necessary for support of the oslo.messaging AMQP
# 1.0 driver
function _install_pyngus {
    # Install pyngus client API
    if is_fedora; then
        # TODO(kgiusti) due to a bug in the way pip installs wheels,
        # do not let pip install the proton python bindings as it will
        # put them in the wrong path:
        # https://github.com/pypa/pip/issues/2940
        install_package python-qpid-proton
    elif is_ubuntu; then
        # ditto
        install_package python-qpid-proton
    fi
    pip_install_gr pyngus
}


# remove packages used by oslo.messaging AMQP 1.0 driver
function _remove_pyngus {
    # TODO(kgiusti) no way to pip uninstall?
    # pip_install_gr pyngus
    :
}


# Set up the various configuration files used by the qpidd broker
function _configure_qpid {

    # the location of the configuration files have changed since qpidd 0.14
    local qpid_conf_file
    if [ -e /etc/qpid/qpidd.conf ]; then
        qpid_conf_file=/etc/qpid/qpidd.conf
    elif [ -e /etc/qpidd.conf ]; then
        qpid_conf_file=/etc/qpidd.conf
    else
        exit_distro_not_supported "qpidd.conf file not found!"
    fi

    # force the ACL file to a known location
    local qpid_acl_file=/etc/qpid/qpidd.acl
    if [ ! -e $qpid_acl_file ]; then
        sudo mkdir -p -m 755 `dirname $qpid_acl_file`
        sudo touch $qpid_acl_file
        sudo chmod o+r $qpid_acl_file
    fi
    sudo sed -i.bak '/^acl-file=/d' $qpid_conf_file
    echo "acl-file=$qpid_acl_file" | sudo tee --append $qpid_conf_file

    sudo sed -i '/^auth=/d' $qpid_conf_file
    if [ -z "$AMQP1_USERNAME" ]; then
        # no QPID user configured, so disable authentication
        # and access control
        echo "auth=no" | sudo tee --append $qpid_conf_file
        cat <<EOF | sudo tee $qpid_acl_file
acl allow all all
EOF
    else
        # Configure qpidd to use PLAIN authentication, and add
        # AMQP1_USERNAME to the ACL:
        echo "auth=yes" | sudo tee --append $qpid_conf_file
        if [ -z "$AMQP1_PASSWORD" ]; then
            read_password AMQP1_PASSWORD "ENTER A PASSWORD FOR QPID USER $AMQP1_USERNAME"
        fi
        # Create ACL to allow $AMQP1_USERNAME full access
        cat <<EOF | sudo tee $qpid_acl_file
group admin ${AMQP1_USERNAME}@QPID
acl allow admin all
acl deny all all
EOF
        # Add user to SASL database
        local sasl_conf_file=/etc/sasl2/qpidd.conf
        sudo sed -i.bak '/PLAIN/!s/mech_list: /mech_list: PLAIN /' $sasl_conf_file
        local sasl_db=`sudo grep sasldb_path $sasl_conf_file | cut -f 2 -d ":" | tr -d [:blank:]`
        if [ ! -e $sasl_db ]; then
            sudo mkdir -p -m 755 `dirname $sasl_db`
        fi
        echo $AMQP1_PASSWORD | sudo saslpasswd2 -c -p -f $sasl_db -u QPID $AMQP1_USERNAME
        sudo chmod o+r $sasl_db
    fi

    # Ensure that the version of the broker can support AMQP 1.0 and
    # configure the queue and topic address patterns used by
    # oslo.messaging.
    QPIDD=$(type -p qpidd)
    if ! $QPIDD --help | grep -q "queue-patterns"; then
        exit_distro_not_supported "qpidd with AMQP 1.0 support"
    fi
    if ! grep -q "queue-patterns=exclusive" $qpid_conf_file; then
        cat <<EOF | sudo tee --append $qpid_conf_file
queue-patterns=exclusive
queue-patterns=unicast
topic-patterns=broadcast
EOF
    fi

    # ensure that the qpidd service can read its config
    sudo chmod o+r $qpid_conf_file
}


# install and configure the qpidd broker
function _install_qpid_backend {

    if is_fedora; then
        # expects epel is already added to the yum repos
        install_package cyrus-sasl-lib
        install_package cyrus-sasl-plain
        install_package qpid-cpp-server
    elif is_ubuntu; then
        install_package sasl2-bin
        # newer qpidd and proton only available via the qpid PPA
        sudo add-apt-repository -y ppa:qpid/testing
        #sudo apt-get update
        REPOS_UPDATED=False
        update_package_repo
        install_package qpidd<commit_msg>need to be able to read config earlier<commit_after>#!/bin/bash
#
#    Licensed to the Apache Software Foundation (ASF) under one
#    or more contributor license agreements.  See the NOTICE file
#    distributed with this work for additional information
#    regarding copyright ownership.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#

# Sets up the messaging backend service needed by the AMQP 1.0
# transport (amqp://)
#
# Environment Configuration
#
# AMQP1_SERVICE - identifies the messaging backend to use.  Should be
#    one of 'qpid', ...
#    @TODO(kgiusti) add qpid-dispatch, rabbitmq, etc
# AMQP1_HOST - the host:port used to connect to the messaging service.
#    Defaults to 127.0.0.1:5672
# AMQP1_{USERNAME,PASSWORD} - for authentication with AMQP1_HOST
#

# Save trace setting
XTRACE=$(set +o | grep xtrace)
set +o xtrace

# builds transport url string
function _get_amqp1_transport_url {
    echo "amqp://$AMQP1_USERNAME:$AMQP1_PASSWORD@$AMQP1_HOST:5672/"
}


# install packages necessary for support of the oslo.messaging AMQP
# 1.0 driver
function _install_pyngus {
    # Install pyngus client API
    if is_fedora; then
        # TODO(kgiusti) due to a bug in the way pip installs wheels,
        # do not let pip install the proton python bindings as it will
        # put them in the wrong path:
        # https://github.com/pypa/pip/issues/2940
        install_package python-qpid-proton
    elif is_ubuntu; then
        # ditto
        install_package python-qpid-proton
    fi
    pip_install_gr pyngus
}


# remove packages used by oslo.messaging AMQP 1.0 driver
function _remove_pyngus {
    # TODO(kgiusti) no way to pip uninstall?
    # pip_install_gr pyngus
    :
}


# Set up the various configuration files used by the qpidd broker
function _configure_qpid {

    # the location of the configuration files have changed since qpidd 0.14
    local qpid_conf_file
    if [ -e /etc/qpid/qpidd.conf ]; then
        qpid_conf_file=/etc/qpid/qpidd.conf
    elif [ -e /etc/qpidd.conf ]; then
        qpid_conf_file=/etc/qpidd.conf
    else
        exit_distro_not_supported "qpidd.conf file not found!"
    fi

    # ensure that the qpidd service can read its config
    sudo chmod o+r $qpid_conf_file

    # force the ACL file to a known location
    local qpid_acl_file=/etc/qpid/qpidd.acl
    if [ ! -e $qpid_acl_file ]; then
        sudo mkdir -p -m 755 `dirname $qpid_acl_file`
        sudo touch $qpid_acl_file
        sudo chmod o+r $qpid_acl_file
    fi
    sudo sed -i.bak '/^acl-file=/d' $qpid_conf_file
    echo "acl-file=$qpid_acl_file" | sudo tee --append $qpid_conf_file

    sudo sed -i '/^auth=/d' $qpid_conf_file
    if [ -z "$AMQP1_USERNAME" ]; then
        # no QPID user configured, so disable authentication
        # and access control
        echo "auth=no" | sudo tee --append $qpid_conf_file
        cat <<EOF | sudo tee $qpid_acl_file
acl allow all all
EOF
    else
        # Configure qpidd to use PLAIN authentication, and add
        # AMQP1_USERNAME to the ACL:
        echo "auth=yes" | sudo tee --append $qpid_conf_file
        if [ -z "$AMQP1_PASSWORD" ]; then
            read_password AMQP1_PASSWORD "ENTER A PASSWORD FOR QPID USER $AMQP1_USERNAME"
        fi
        # Create ACL to allow $AMQP1_USERNAME full access
        cat <<EOF | sudo tee $qpid_acl_file
group admin ${AMQP1_USERNAME}@QPID
acl allow admin all
acl deny all all
EOF
        # Add user to SASL database
        local sasl_conf_file=/etc/sasl2/qpidd.conf
        sudo sed -i.bak '/PLAIN/!s/mech_list: /mech_list: PLAIN /' $sasl_conf_file
        local sasl_db=`sudo grep sasldb_path $sasl_conf_file | cut -f 2 -d ":" | tr -d [:blank:]`
        if [ ! -e $sasl_db ]; then
            sudo mkdir -p -m 755 `dirname $sasl_db`
        fi
        echo $AMQP1_PASSWORD | sudo saslpasswd2 -c -p -f $sasl_db -u QPID $AMQP1_USERNAME
        sudo chmod o+r $sasl_db
    fi

    # Ensure that the version of the broker can support AMQP 1.0 and
    # configure the queue and topic address patterns used by
    # oslo.messaging.
    QPIDD=$(type -p qpidd)
    if ! $QPIDD --help | grep -q "queue-patterns"; then
        exit_distro_not_supported "qpidd with AMQP 1.0 support"
    fi
    if ! grep -q "queue-patterns=exclusive" $qpid_conf_file; then
        cat <<EOF | sudo tee --append $qpid_conf_file
queue-patterns=exclusive
queue-patterns=unicast
topic-patterns=broadcast
EOF
    fi
}


# install and configure the qpidd broker
function _install_qpid_backend {

    if is_fedora; then
        # expects epel is already added to the yum repos
        install_package cyrus-sasl-lib
        install_package cyrus-sasl-plain
        install_package qpid-cpp-server
    elif is_ubuntu; then
        install_package sasl2-bin
        # newer qpidd and proton only available via the qpid PPA
        sudo add-apt-repository -y ppa:qpid/testing
        #sudo apt-get update
        REPOS_UPDATED=False
        update_package_repo
        install_package qpidd
{'fraction_non_alphanumeric': 0.07576723431381477, 'fraction_numerical': 0.011292232037155086, 'mean_word_length': 2.779077770130764, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2802809', 'n_tokens_mistral': 3790, 'n_tokens_neox': 3559, 'n_words': 1214}