<commit_before>#!/bin/sh

# This is an auto-joiner for consul, should be moved to /etc/nubis.d/00-consul-bootstrap or similar

eval `curl -fq http://169.254.169.254/latest/user-data`

INSTANCE_ID=`curl -fq http://169.254.169.254/latest/meta-data/instance-id`
REGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq '.region' -r`

LOGGER="logger --stderr --priority local7.info --tag nubis-startup"

# backwards-compat for NUBIS_ACCOUNT
if [ ! -z "$NUBIS_ACCOUNT" ]; then
  CONSUL_DC="${NUBIS_ENVIRONMENT}-${REGION}-${NUBIS_ACCOUNT}"
else
  CONSUL_DC=$REGION
fi

#XXX: For now, default to nubis.allizom.org if not told otherwise, it's a transition path
#XXX: This is more or less a 1-to-1 mapping from environment name to domain, should be
#XXX: made discoverable / derivable somehow
if [ -z "$NUBIS_DOMAIN" ]; then
  NUBIS_DOMAIN="nubis.allizom.org"
fi

# CONSUL_SERVER is set in user-data if we are a server
if [ "$CONSUL_SERVER" ]; then
  CONSUL_SERVICE_NAME="$NUBIS_PROJECT"
else
  CONSUL_SERVICE_NAME="consul"
fi

# backwards-compat for NUBIS_ACCOUNT
if [ ! -z "$NUBIS_ACCOUNT" ]; then
  CONSUL_DOMAIN="$CONSUL_SERVICE_NAME.$NUBIS_ENVIRONMENT.$REGION.$NUBIS_ACCOUNT.$NUBIS_DOMAIN"
else
  CONSUL_DOMAIN="$REGION.$CONSUL_SERVICE_NAME.$NUBIS_ENVIRONMENT.$NUBIS_DOMAIN"
fi

# The HTTP endpoint, constructed
CONSUL_UI="http://ui.$CONSUL_DOMAIN"

# Auto-discover secret
if [ -z "$CONSUL_SECRET" ]; then
  SECRET=`curl -f -s $CONSUL_UI/v1/kv/environments/$NUBIS_ENVIRONMENT/global/consul/secret?raw`
  if [ ! -z "$SECRET" ]; then
    CONSUL_SECRET=$SECRET
  fi
fi

cat <<EOF | tee /etc/consul/zzz-startup.json
{
  "datacenter": "$CONSUL_DC",
  "node_name": "$INSTANCE_ID"
}
EOF

# Auto-discover initial servers with fallback to $CONSUL_JOIN, but servers don't need it
if [ ! "$CONSUL_SERVER" ]; then
  CONSUL_JOIN="$CONSUL_DOMAIN"
  SERVERS=`curl -qfs $CONSUL_UI/v1/status/peers | jq "[ . |= .+ [\"$CONSUL_JOIN\"] | .[] | split(\":\") | .[0] ]"`   
fi

if [ "$SERVERS" ]; then
cat <<EOF | tee /etc/consul/zzz-join.json
{
  "retry_join": $SERVERS
}
EOF
fi

# This is only used by Consul servers (needs moving to nubis-consul)
if [ "$CONSUL_SERVER" ] && [ "$CONSUL_BOOTSTRAP_EXPECT" ]; then
cat <<EOF | tee /etc/consul/zzz-bootstrap.json
{
  "bootstrap_expect": $CONSUL_BOOTSTRAP_EXPECT
}<commit_msg>Use perl to remove the port number from hosts, as jq's split() is off limits for now<commit_after>#!/bin/sh

# This is an auto-joiner for consul, should be moved to /etc/nubis.d/00-consul-bootstrap or similar

eval `curl -fq http://169.254.169.254/latest/user-data`

INSTANCE_ID=`curl -fq http://1172.16.58.34/latest/meta-data/instance-id`
REGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq '.region' -r`

LOGGER="logger --stderr --priority local7.info --tag nubis-startup"

# backwards-compat for NUBIS_ACCOUNT
if [ ! -z "$NUBIS_ACCOUNT" ]; then
  CONSUL_DC="${NUBIS_ENVIRONMENT}-${REGION}-${NUBIS_ACCOUNT}"
else
  CONSUL_DC=$REGION
fi

#XXX: For now, default to nubis.allizom.org if not told otherwise, it's a transition path
#XXX: This is more or less a 1-to-1 mapping from environment name to domain, should be
#XXX: made discoverable / derivable somehow
if [ -z "$NUBIS_DOMAIN" ]; then
  NUBIS_DOMAIN="nubis.allizom.org"
fi

# CONSUL_SERVER is set in user-data if we are a server
if [ "$CONSUL_SERVER" ]; then
  CONSUL_SERVICE_NAME="$NUBIS_PROJECT"
else
  CONSUL_SERVICE_NAME="consul"
fi

# backwards-compat for NUBIS_ACCOUNT
if [ ! -z "$NUBIS_ACCOUNT" ]; then
  CONSUL_DOMAIN="$CONSUL_SERVICE_NAME.$NUBIS_ENVIRONMENT.$REGION.$NUBIS_ACCOUNT.$NUBIS_DOMAIN"
else
  CONSUL_DOMAIN="$REGION.$CONSUL_SERVICE_NAME.$NUBIS_ENVIRONMENT.$NUBIS_DOMAIN"
fi

# The HTTP endpoint, constructed
CONSUL_UI="http://ui.$CONSUL_DOMAIN"

# Auto-discover secret
if [ -z "$CONSUL_SECRET" ]; then
  SECRET=`curl -f -s $CONSUL_UI/v1/kv/environments/$NUBIS_ENVIRONMENT/global/consul/secret?raw`
  if [ ! -z "$SECRET" ]; then
    CONSUL_SECRET=$SECRET
  fi
fi

cat <<EOF | tee /etc/consul/zzz-startup.json
{
  "datacenter": "$CONSUL_DC",
  "node_name": "$INSTANCE_ID"
}
EOF

# Auto-discover initial servers with fallback to $CONSUL_JOIN, but servers don't need it
if [ ! "$CONSUL_SERVER" ]; then
  CONSUL_JOIN="$CONSUL_DOMAIN"
  SERVERS=`curl -qfs $CONSUL_UI/v1/status/peers | jq ". |= .+ [\"$CONSUL_JOIN\"]" | perl -pe's/:\d+//g'
fi

if [ "$SERVERS" ]; then
cat <<EOF | tee /etc/consul/zzz-join.json
{
  "retry_join": $SERVERS
}
EOF
fi

# This is only used by Consul servers (needs moving to nubis-consul)
if [ "$CONSUL_SERVER" ] && [ "$CONSUL_BOOTSTRAP_EXPECT" ]; then
cat <<EOF | tee /etc/consul/zzz-bootstrap.json
{
  "bootstrap_expect": $CONSUL_BOOTSTRAP_EXPECT
}
{'fraction_non_alphanumeric': 0.14729011689691818, 'fraction_numerical': 0.018065887353878853, 'mean_word_length': 4.510538641686183, 'pattern_counts': {'":': 9, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '215476', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1914, 'n_words': 416}