<commit_before>#
# catest_init.sh: Marlin-specific catest initialization file
#

agentconfig="/opt/smartdc/marlin/etc/agentconfig.json"

#
# resolve resolver_ip url: Return $url with the hostname part resolved using
# $resolver_ip as a DNS resolver.  On error, returns $url unmodified.
#
function resolve
{
	local url=$2
	local protocol
	local portsuffix
	local host
	local rv

	protocol=${url%%://*}
	portsuffix=${url##*:}
	if [[ -z "$protocol" || "$protocol" == "$url" || \
	    -z "$portsuffix" || "$portsuffix" == "$url" ]]; then
		echo "$url"
		return
	fi

	# First trim the leading "protocol" plus "://"
	host=${url:$(( 3 + ${#protocol}))}
	# Next trim the trailing $portsuffix plus ":"
	host=${host:0: $(( ${#host} - ${#portsuffix} - 1))}

	if [[ -z "$host" || "$host" == "$url" ]]; then
		echo "$url"
		return
	fi

	rv=$(set -o pipefail; \
	    host -t A "$host" $1 | tail -1 | awk '{print $4}') || rv=
	if [[ -z "$rv" ]]; then
		echo "$url"
		return
	fi

	echo "$protocol://$rv:$portsuffix"
}

#
# If we're in the global zone and there's an agent configuration file present,
# we initialize our own configuration from that one.
#
function catest_init_global
{
	[[ -f $agentconfig ]] || return 1

	echo -n "Attempting to configure based on GZ agent configuration ... "

	local nameserver=$(json dns.nameservers.0 < $agentconfig)
	[[ -n "$nameserver" ]] || return 1

	[[ -z $MANTA_URL ]] &&
	    export MANTA_URL=$(resolve $nameserver \
	        $(json manta.url < $agentconfig) | sed -e s'#^http:#https:#' | \
		sed -Ee s'#:80/?$##')
	[[ -z $MORAY_URL ]] &&
	    export MORAY_URL=$(resolve $nameserver \
	        $(json moray.url < $agentconfig))
	[[ -z $MANTA_USER ]] && export MANTA_USER="poseidon"
	[[ -z $MANTA_KEY_ID ]] && export MANTA_KEY_ID="$(sdc-ldap search -b \
	    "$(sdc-ldap search login=poseidon | head -1 | cut -c5-)" \
	    objectclass=sdckey | awk '/^fingerprint:/{ print $2 }')"

	#
	# The mahi URL is trickier because it's not actually in the agent
	# configuration.  But we can guess it by replacing the first component
	# of the unresolved $MANTA_URL with "auth".
	#
	[[ -z $MAHI_URL ]] &&
	    export MAHI_URL=$(resolve $nameserver \
		$(json manta.url < $agentconfig | sed -Ee \
		s'#https?://[a-zA-Z0-9]+([^:/]*)([:/].*)?#tcp://auth\1:6379#'))

	export MARLIN_METERING_LOG="$(svcs -L marlin-agent)"
	echo "done."
}

function catest_init
{
	nfailed=0

	if [[ $(zonename) == "global" ]]; then
		catest_init_global || \
		    echo "WARN: failed to configure from $agentconfig" >&2
	fi

	if [[ -n "$MARLIN_METERING_LOG" ]]; then<commit_msg>MANTA-1260 update manta tools for auth to authcache change<commit_after>#
# catest_init.sh: Marlin-specific catest initialization file
#

agentconfig="/opt/smartdc/marlin/etc/agentconfig.json"

#
# resolve resolver_ip url: Return $url with the hostname part resolved using
# $resolver_ip as a DNS resolver.  On error, returns $url unmodified.
#
function resolve
{
	local url=$2
	local protocol
	local portsuffix
	local host
	local rv

	protocol=${url%%://*}
	portsuffix=${url##*:}
	if [[ -z "$protocol" || "$protocol" == "$url" || \
	    -z "$portsuffix" || "$portsuffix" == "$url" ]]; then
		echo "$url"
		return
	fi

	# First trim the leading "protocol" plus "://"
	host=${url:$(( 3 + ${#protocol}))}
	# Next trim the trailing $portsuffix plus ":"
	host=${host:0: $(( ${#host} - ${#portsuffix} - 1))}

	if [[ -z "$host" || "$host" == "$url" ]]; then
		echo "$url"
		return
	fi

	rv=$(set -o pipefail; \
	    host -t A "$host" $1 | tail -1 | awk '{print $4}') || rv=
	if [[ -z "$rv" ]]; then
		echo "$url"
		return
	fi

	echo "$protocol://$rv:$portsuffix"
}

#
# If we're in the global zone and there's an agent configuration file present,
# we initialize our own configuration from that one.
#
function catest_init_global
{
	[[ -f $agentconfig ]] || return 1

	echo -n "Attempting to configure based on GZ agent configuration ... "

	local nameserver=$(json dns.nameservers.0 < $agentconfig)
	[[ -n "$nameserver" ]] || return 1

	[[ -z $MANTA_URL ]] &&
	    export MANTA_URL=$(resolve $nameserver \
	        $(json manta.url < $agentconfig) | sed -e s'#^http:#https:#' | \
		sed -Ee s'#:80/?$##')
	[[ -z $MORAY_URL ]] &&
	    export MORAY_URL=$(resolve $nameserver \
	        $(json moray.url < $agentconfig))
	[[ -z $MANTA_USER ]] && export MANTA_USER="poseidon"
	[[ -z $MANTA_KEY_ID ]] && export MANTA_KEY_ID="$(sdc-ldap search -b \
	    "$(sdc-ldap search login=poseidon | head -1 | cut -c5-)" \
	    objectclass=sdckey | awk '/^fingerprint:/{ print $2 }')"

	#
	# The mahi URL is trickier because it's not actually in the agent
	# configuration.  But we can guess it by replacing the first component
	# of the unresolved $MANTA_URL with "authcache".
	#
	local sedreg='https?://[a-zA-Z0-9]+([^:/]*)([:/].*)?'
	local sedrep='tcp://authcache\1:6379'
	[[ -z $MAHI_URL ]] &&
	    export MAHI_URL=$(resolve $nameserver \
		$(json manta.url < $agentconfig | sed -Ee \
		"s#$sedreg#$sedrep#"))

	export MARLIN_METERING_LOG="$(svcs -L marlin-agent)"
	echo "done."
}

function catest_init
{
	nfailed=0

	if [[ $(zonename) == "global" ]]; then
		catest_init_global || \
		    echo "WARN: failed to configure from $agentconfig" >&2
	fi

	if [[ -n "$MARLIN_METERING_LOG" ]]; then
{'fraction_non_alphanumeric': 0.1829803472619729, 'fraction_numerical': 0.009921770654455257, 'mean_word_length': 3.4957118353344767, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '195164', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1912, 'n_words': 541}