<commit_before>#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

#
# Copyright (c) 2015, Joyent, Inc.
#

#
# scripts/common/services.sh: common routines for configuring a Manta zone.
# This script is typically included by submodule in each Manta component repo.
# Only a few of the functions contained here are public, and they're invoked by
# the setup script that runs when a Manta zone first boots.
#
# All functions in this file assume that ./util.sh is already sourced and that
# "errexit" is set.
#

#
# manta_setup_config_agent: write out the configuration file for the
# config-agent.
#
function manta_setup_config_agent {
    local prefix=/opt/smartdc/config-agent
    local tmpfile=/tmp/agent.$$.xml

    sed -e "s#@@PREFIX@@#${prefix}#g" \
       ${prefix}/smf/manifests/config-agent.xml > ${tmpfile}
    mv ${tmpfile} ${prefix}/smf/manifests/config-agent.xml

    mkdir -p ${prefix}/etc
    cat >${prefix}/etc/config.json <<EOF
{
    "logLevel": "info",
    "pollInterval": 10000,
    "sapi": {
        "url": "${SAPI_URL}"
    },
    "localManifestDirs": []
}
EOF
}

#
# manta_add_manifest_dir DIRECTORY: update the config-agent configuration to
# include local manifests located under DIRECTORY.
#
function manta_add_manifest_dir {
    local file=/opt/smartdc/config-agent/etc/config.json
    local dir=$1
    local tmpfile=/tmp/add_dir.$$.json

    cat ${file} | json -e "this.localManifestDirs.push('$dir')" >${tmpfile}
    mv ${tmpfile} ${file}
}


#
# manta_download_metadata: Fetch this zone's SAPI metadata and save it to
# the local path $METADATA.
#
function manta_download_metadata {
    local url="$SAPI_URL/configs/$(zonename)"
    local i=0

    while (( i++ < 30 )); do
        #
        # Make sure the temporary files do not exist:
        #
        rm -f "$METADATA.raw"
        rm -f "$METADATA.extracted"

        #
        # Download SAPI configuration for this instance:
        #
        if ! curl -sSf -o "$METADATA.raw" "$url"; then
            warn "could not download SAPI metadata (retrying)"
            sleep 2
            continue
        fi

        #
        # Extract the metadata object from the SAPI configuration:
        #
        if ! json -f "$METADATA.raw" metadata > "$METADATA.extracted"; then
            warn "could not parse SAPI metadata (retrying)"
            sleep 2
            continue
        fi

        #
        # Make sure we did not write an empty file:
        #
        if [[ ! -s "$METADATA.extracted" ]]; then
            fatal "metadata file was empty"
        fi

        #
        # Move the metadata file into place:
        #
        if ! mv "$METADATA.extracted" "$METADATA"; then
            fatal "could not move metadata file into place"
        fi

        rm -f "$METADATA.raw"
        return 0
    done

    fatal "failed to download SAPI configuration (too many retries)"
}

#
# manta_enable_config_agent: Enable this zone's configuration agent (by
# importing its SMF manifest and enabling the service).
#
function manta_enable_config_agent {
    svccfg import /opt/smartdc/config-agent/smf/manifests/config-agent.xml
    svcadm enable config-agent
}

<commit_msg>MANTA-2863 manta zone setup scripts need to wait for config-agent to start Reviewed by: <NAME> <<EMAIL>> Approved by: <NAME> <<EMAIL>><commit_after>#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

#
# Copyright (c) 2015, Joyent, Inc.
#

#
# scripts/common/services.sh: common routines for configuring a Manta zone.
# This script is typically included by submodule in each Manta component repo.
# Only a few of the functions contained here are public, and they're invoked by
# the setup script that runs when a Manta zone first boots.
#
# All functions in this file assume that ./util.sh is already sourced and that
# "errexit" is set.
#

#
# manta_setup_config_agent: write out the configuration file for the
# config-agent.
#
function manta_setup_config_agent {
    local prefix=/opt/smartdc/config-agent
    local tmpfile=/tmp/agent.$$.xml

    sed -e "s#@@PREFIX@@#${prefix}#g" \
       ${prefix}/smf/manifests/config-agent.xml > ${tmpfile}
    mv ${tmpfile} ${prefix}/smf/manifests/config-agent.xml

    mkdir -p ${prefix}/etc
    cat >${prefix}/etc/config.json <<EOF
{
    "logLevel": "info",
    "pollInterval": 10000,
    "sapi": {
        "url": "${SAPI_URL}"
    },
    "localManifestDirs": []
}
EOF
}

#
# manta_add_manifest_dir DIRECTORY: update the config-agent configuration to
# include local manifests located under DIRECTORY.
#
function manta_add_manifest_dir {
    local file=/opt/smartdc/config-agent/etc/config.json
    local dir=$1
    local tmpfile=/tmp/add_dir.$$.json

    cat ${file} | json -e "this.localManifestDirs.push('$dir')" >${tmpfile}
    mv ${tmpfile} ${file}
}


#
# manta_download_metadata: Fetch this zone's SAPI metadata and save it to
# the local path $METADATA.
#
function manta_download_metadata {
    local url="$SAPI_URL/configs/$(zonename)"
    local i=0

    while (( i++ < 30 )); do
        #
        # Make sure the temporary files do not exist:
        #
        rm -f "$METADATA.raw"
        rm -f "$METADATA.extracted"

        #
        # Download SAPI configuration for this instance:
        #
        if ! curl -sSf -o "$METADATA.raw" "$url"; then
            warn "could not download SAPI metadata (retrying)"
            sleep 2
            continue
        fi

        #
        # Extract the metadata object from the SAPI configuration:
        #
        if ! json -f "$METADATA.raw" metadata > "$METADATA.extracted"; then
            warn "could not parse SAPI metadata (retrying)"
            sleep 2
            continue
        fi

        #
        # Make sure we did not write an empty file:
        #
        if [[ ! -s "$METADATA.extracted" ]]; then
            fatal "metadata file was empty"
        fi

        #
        # Move the metadata file into place:
        #
        if ! mv "$METADATA.extracted" "$METADATA"; then
            fatal "could not move metadata file into place"
        fi

        rm -f "$METADATA.raw"
        return 0
    done

    fatal "failed to download SAPI configuration (too many retries)"
}

#
# manta_enable_config_agent: Enable this zone's configuration agent (by
# importing its SMF manifest and enabling the service).
#
function manta_enable_config_agent {
    svccfg import /opt/smartdc/config-agent/smf/manifests/config-agent.xml
    svcadm enable -s config-agent
}


{'fraction_non_alphanumeric': 0.10644299287410926, 'fraction_numerical': 0.006532066508313539, 'mean_word_length': 2.5457894736842106, 'pattern_counts': {'":': 10, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5057842', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2044, 'n_words': 688}