<commit_before>#!/bin/bash
# -*- mode: shell-script; fill-column: 80; -*-
#
# 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) 2014, Joyent, Inc.
#

# Boostraps the consistent hash ring for electric-moray.

set -o xtrace
set -o errexit
set -o pipefail

SOURCE="${BASH_SOURCE[0]}"
if [[ -h $SOURCE ]]; then
    SOURCE="$(readlink "$SOURCE")"
fi
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
PROFILE=/root/.bashrc
SVC_ROOT=/opt/smartdc/electric-moray

source ${DIR}/scripts/util.sh
source ${DIR}/scripts/services.sh

export PATH=$SVC_ROOT/bin:$SVC_ROOT/build/node/bin:/opt/local/bin:/usr/sbin/:/usr/bin:$PATH
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

FASH=/opt/smartdc/electric-moray/node_modules/.bin/fash
LEVELDB_DIR_PARENT=/electric-moray/chash
LEVELDB_DIR=$LEVELDB_DIR_PARENT/leveldb-
SAPI_URL=$(mdata-get SAPI_URL)
[[ -n $SAPI_URL ]] || fatal "no SAPI_URL found"
sleep 10 # wait 10 seconds for dns to setup, this is so lame but otherwise will resolve in dns resolution errors.
MANTA_APPLICATION=$(curl --connect-timeout 10 -sS -i -H accept:application/json \
    -H content-type:application/json --url "$SAPI_URL/applications?name=manta&include_master=true" | json -Ha)
[[ -n $MANTA_APPLICATION ]] || fatal "no MANTA_APPLICATION found"
HASH_RING_IMAGE=$(echo $MANTA_APPLICATION | json metadata.HASH_RING_IMAGE)
[[ -n $HASH_RING_IMAGE ]] || fatal "no HASH_RING_IMAGE found"
HASH_RING_FILE=/var/tmp/$(uuid -v4).tar.gz
export SDC_IMGADM_URL=$(echo $MANTA_APPLICATION | json metadata.HASH_RING_IMGAPI_SERVICE)
[[ -n $SDC_IMGADM_URL ]] || fatal "no SDC_IMGADM_URL found"
ZONE_UUID=$(/usr/bin/zonename)
ZFS_PARENT_DATASET=zones/$ZONE_UUID/data
ZFS_DATASET=$ZFS_PARENT_DATASET/electric-moray

function manta_hack_syslog_conf {
    # Hack.  See MANTA-2165
    local conf=/etc/syslog.conf
    if [[ -e $conf ]]; then
        sed -ir 's/\/var\/log\/authlog/\/var\/log\/auth\.log/' $conf
        sed -ir 's/\/var\/log\/maillog/\/var\/log\/postfix\.log/' $conf
    fi
}

function manta_setup_determine_instances {
    ELECTRIC_MORAY_INSTANCES=1
    local size=`json -f ${METADATA} SIZE`
    if [ "$size" = "lab" ] || [ "$size" = "production" ]
    then
        ELECTRIC_MORAY_INSTANCES=4
    fi<commit_msg>MANTA-2866 electric-moray setup failed: name service lookup timeout Reviewed by: <NAME> <<EMAIL>> Approved by: <NAME> <<EMAIL>><commit_after>#!/bin/bash
#
# 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) 2017, Joyent, Inc.
#

#
# Bootstraps the consistent hash ring for electric-moray.
#

set -o xtrace
set -o errexit
set -o pipefail

SOURCE="${BASH_SOURCE[0]}"
if [[ -h $SOURCE ]]; then
    SOURCE="$(readlink "$SOURCE")"
fi
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
PROFILE=/root/.bashrc
SVC_ROOT=/opt/smartdc/electric-moray

source ${DIR}/scripts/util.sh
source ${DIR}/scripts/services.sh

export PATH=$SVC_ROOT/bin:$SVC_ROOT/build/node/bin:/opt/local/bin:/usr/sbin/:/usr/bin:$PATH
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

FASH=/opt/smartdc/electric-moray/node_modules/.bin/fash
LEVELDB_DIR_PARENT=/electric-moray/chash
LEVELDB_DIR=$LEVELDB_DIR_PARENT/leveldb-
SAPI_URL=$(mdata-get SAPI_URL)
[[ -n $SAPI_URL ]] || fatal "no SAPI_URL found"
sleep 10 # wait 10 seconds for dns to setup, this is so lame but otherwise will resolve in dns resolution errors.

#
# Load the Manta Application object from SAPI.  If this request fails, it will
# be retried until the smf(5) method timeout expires for the "mdata:execute"
# service.
#
while :; do
    if ! sapi_res=$(curl --max-time 60 --ipv4 -sSf \
      -H 'Accept: application/json' -H 'Content-Type: application/json' \
      "$SAPI_URL/applications?name=manta&include_master=true"); then
        printf 'WARNING: could not download SAPI application (retrying)\n' >&2
        sleep 2
        continue
    fi

    if ! napps=$(json length <<< "$sapi_res") || [[ $napps != '1' ]]; then
        printf 'WARNING: found "%s" SAPI applications (retry)\n' "$napps" >&2
        sleep 2
        continue
    fi

    if ! manta_app=$(json 0 <<< "$sapi_res") || [[ -z $manta_app ]]; then
        printf 'WARNING: failed to parse "manta" SAPI application (retry)\n' >&2
        sleep 2
        continue
    fi

    break
done

HASH_RING_IMAGE=$(json metadata.HASH_RING_IMAGE <<< "$manta_app")
[[ -n $HASH_RING_IMAGE ]] || fatal "no HASH_RING_IMAGE found"
HASH_RING_FILE=/var/tmp/$(uuid -v4).tar.gz
export SDC_IMGADM_URL=$(json metadata.HASH_RING_IMGAPI_SERVICE <<< "$manta_app")
[[ -n $SDC_IMGADM_URL ]] || fatal "no SDC_IMGADM_URL found"
ZONE_UUID=$(/usr/bin/zonename)
ZFS_PARENT_DATASET=zones/$ZONE_UUID/data
ZFS_DATASET=$ZFS_PARENT_DATASET/electric-moray

function manta_hack_syslog_conf {
    # Hack.  See MANTA-2165
    local conf=/etc/syslog.conf
    if [[ -e $conf ]]; then
        sed -ir 's/\/var\/log\/authlog/\/var\/log\/auth\.log/' $conf
        sed -ir 's/\/var\/log\/maillog/\/var\/log\/postfix\.log/' $conf
    fi
}

function manta_setup_determine_instances {
    ELECTRIC_MORAY_INSTANCES=1
    local size=`json -f ${METADATA} SIZE`
    if [ "$size" = "lab" ] || [ "$size" = "production" ]
    then
        ELECTRIC_MORAY_INSTANCES=4
    fi
{'fraction_non_alphanumeric': 0.15801844151148076, 'fraction_numerical': 0.01193274272283493, 'mean_word_length': 3.7854671280276815, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3043182', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2090, 'n_words': 484}