<commit_before>#!/bin/bash

set -e

cd /var/vcap/packages/vmr_config_scripts

tar zxf vmr_config_scripts.tgz
cd src/vmr_config_scripts
export PATH=`pwd`:$PATH
export HOME=`pwd`

chmod +x run-router-script
chmod +x rs-config-vmr-for-maas
chmod +x rs-config-server-cert

echo "DEDICATED_VMR_LIST = <%= properties.dedicated_vmr_list %>"
echo "SHARED_VMR_LIST = <%= properties.shared_vmr_list %>"
echo "STARTING_PORT = <%= properties.starting_port %>"
echo "BROKER_USER = <%= properties.broker_user %>"
echo "BROKER_PASSWORD = <%= properties.broker_password %>"
echo "BROKER_HOSTNAME = <%= properties.broker_hostname %>"
echo "SSH_PORT = <%= properties.ssh_port %>"
echo "ADMIN_PASSWORD = <%= properties.admin_password %>"

# Installs perl modules required to run rs scripts, and jq so we
# are able to easily parse JSON from this bash script
sudo apt-get install -y libexpect-perl libterm-readkey-perl sshpass ruby

STARTING_PORT=<%= properties.starting_port %>
BROKER_USER=<%= properties.broker_user %>
BROKER_PASSWORD=<%= properties.broker_password %>
BROKER_HOSTNAME=<%= properties.broker_hostname %>
SSH_PORT=<%= properties.ssh_port %>
ADMIN_USER=admin
ADMIN_PASSWORD=<%= properties.admin_password %>

<% if_p("dedicated_vmr_list") do |vmrs| %>
  echo "List of dedicated VMRs:"
  <% for vmr in vmrs %>
    echo <%= vmr %>
  <% end %>
<% end %>

<% if_p("shared_vmr_list") do |vmrs| %>
  echo "List of shared VMRs:"
  <% for vmr in vmrs %>
    echo <%= vmr %>
  <% end %>
<% end %>

TIMEOUT=600

echo "Waiting for Solace Service Broker to be available..."
# Wait until Service Broker is ready
RESULT=0
while [ $RESULT -eq 0 ]
do
  RES=`curl -sL -X GET -w "%{http_code}\\n" "$BROKER_USER:$BROKER_PASSWORD@$BROKER_HOSTNAME/solace/status" -o body.txt`
  if [ $RES -eq "200" ]; then
    echo "Service Broker is UP.  Initial State: "
    cat body.txt
    RESULT=1
  else
    TIMEOUT=$((TIMEOUT-10))
    if [ $TIMEOUT -lt 0 ]; then
      echo "FATAL: Timed out while waiting for Service Broker to start."
      exit 1
    else
      echo "Service Broker still DOWN, HTTP Response Code: $RES.  Will retry in 10 seconds."
    fi
    sleep 10
  fi
done


MANAGED_ROUTER_JSON_LIST="["

<% if_p("dedicated_vmr_list") do |vmrs| %>
  <% vmrs.each do |vmr_ip| %>
  VMR_IP=<%= vmr_ip %>

  echo "Configuring $VMR_IP:$SSH_PORT as a Dedicated VMR"

  #Generate a random password for the file user of this VMR
  FILEUSER_PWD=`openssl rand -base64 12`

  echo "This VMR's file user credentials are : fileuser / $FILEUSER_PWD"

  if [ "$MANAGED_ROUTER_JSON_LIST" != "[" ]; then
    MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST},"
  fi
  MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST}""{ \"sempHost\": \"$VMR_IP\",  \"sempPort\": \"8080\", \"sempUser\": \"$ADMIN_USER\", \"sempPassword\": \"$<PASSWORD>\", \"planId\": \"9bd51219-9cee-4570-99ab-ebe8<PASSWORD>2c854\", \"sshPort\" : \"$SSH_PORT\" }"

  <% end %>
<% end %>

<% if_p("shared_vmr_list") do |vmrs| %>
  <% vmrs.each do |vmr_ip| %>
  VMR_IP=<%= vmr_ip %>

  echo "Configuring $VMR_IP as a Shared VMR"

  #Generate a random password for the file user of this VMR
  FILEUSER_PWD=`openssl rand -base64 12`

  echo "This VMR's file user credentials are : fileuser / $FILEUSER_PWD"

  if [ "$MANAGED_ROUTER_JSON_LIST" != "[" ]; then
    MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST},"
  fi
  MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST}""{ \"sempHost\": \"$VMR_IP\",  \"sempPort\": \"8080\", \"sempUser\": \"$ADMIN_USER\", \"sempPassword\": \"$<PASSWORD>\", \"planId\": \"af308299-102f-47a3-acb0-7de72be192bf\", \"sshPort\" : \"$SSH_PORT\" }"

  <% end %>
<% end %>

MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST}]"

echo "curl -X GET $BROKER_USER:$BROKER_PASSWORD@$BROKER_HOSTNAME/solace/manage/solace_message_routers -d "'"'"${MANAGED_ROUTER_JSON_LIST}"'"'

RESPONSE=`curl -X GET $BROKER_USER:$BROKER_PASSWORD@$BROKER_HOSTNAME/solace/manage/solace_message_routers -d "${MANAGED_ROUTER_JSON_LIST}" -H "Content-Type: application/json;charset=UTF-8"`
echo "Service Broker: $RESPONSE"
echo $RESPONSE | ruby /var/vcap/jobs/config_vmr/bin/extractIpPort.rb | while read i; do
   CURRENT_HOST=`echo "$i" | awk '{print $1}'`
   CURRENT_PORT=`echo "$i" | awk '{print $2}'`
   CURRENT_PLAN_ID=`echo "$i" | awk '{print $3}'`

   if [ "$CURRENT_PLAN_ID" = "9bd51219-9cee-4570-99ab-ebe80d<PASSWORD>" ]; then
      echo "Configuring a dedicated VMR... ${CURRENT_HOST}:${CURRENT_PORT}"
      ./rs-config-vmr-for-maas --cli-username $ADMIN_USER  --cli-password $ADMIN_PASSWORD $CURRENT_HOST:$CURRENT_PORT plan=dedicated portSeed=$STARTING_PORT fileUserPass=$FILE<PASSWORD> adminPass=$ADMIN_PASSWORD
   else
      echo "Configuring a shared VMR... ${CURRENT_HOST}:${CURRENT_PORT}"
      ./rs-config-vmr-for-maas --cli-username $ADMIN_USER --cli-password $ADMIN_PASSWORD $CURRENT_HOST:$CURRENT_PORT plan=shared portSeed=$STARTING_PORT fileUserPass=$FILE<PASSWORD>PWD adminPass=$ADMIN_PASSWORD
   fi<commit_msg>Made the errand install the included server.pem server certificate.<commit_after>#!/bin/bash

set -e

cd /var/vcap/packages/vmr_config_scripts

tar zxf vmr_config_scripts.tgz
cd src/vmr_config_scripts
export PATH=`pwd`:$PATH
export HOME=`pwd`

chmod +x run-router-script
chmod +x rs-config-vmr-for-maas
chmod +x rs-config-server-cert

echo "DEDICATED_VMR_LIST = <%= properties.dedicated_vmr_list %>"
echo "SHARED_VMR_LIST = <%= properties.shared_vmr_list %>"
echo "STARTING_PORT = <%= properties.starting_port %>"
echo "BROKER_USER = <%= properties.broker_user %>"
echo "BROKER_PASSWORD = <%= properties.broker_password %>"
echo "BROKER_HOSTNAME = <%= properties.broker_hostname %>"
echo "SSH_PORT = <%= properties.ssh_port %>"
echo "ADMIN_PASSWORD = <%= properties.admin_password %>"

# Installs perl modules required to run rs scripts, and jq so we
# are able to easily parse JSON from this bash script
sudo apt-get install -y libexpect-perl libterm-readkey-perl sshpass ruby

STARTING_PORT=<%= properties.starting_port %>
BROKER_USER=<%= properties.broker_user %>
BROKER_PASSWORD=<%= properties.broker_password %>
BROKER_HOSTNAME=<%= properties.broker_hostname %>
SSH_PORT=<%= properties.ssh_port %>
ADMIN_USER=admin
ADMIN_PASSWORD=<%= properties.admin_password %>

<% if_p("dedicated_vmr_list") do |vmrs| %>
  echo "List of dedicated VMRs:"
  <% for vmr in vmrs %>
    echo <%= vmr %>
  <% end %>
<% end %>

<% if_p("shared_vmr_list") do |vmrs| %>
  echo "List of shared VMRs:"
  <% for vmr in vmrs %>
    echo <%= vmr %>
  <% end %>
<% end %>

TIMEOUT=600

echo "Waiting for Solace Service Broker to be available..."
# Wait until Service Broker is ready
RESULT=0
while [ $RESULT -eq 0 ]
do
  RES=`curl -sL -X GET -w "%{http_code}\\n" "$BROKER_USER:$BROKER_PASSWORD@$BROKER_HOSTNAME/solace/status" -o body.txt`
  if [ $RES -eq "200" ]; then
    echo "Service Broker is UP.  Initial State: "
    cat body.txt
    RESULT=1
  else
    TIMEOUT=$((TIMEOUT-10))
    if [ $TIMEOUT -lt 0 ]; then
      echo "FATAL: Timed out while waiting for Service Broker to start."
      exit 1
    else
      echo "Service Broker still DOWN, HTTP Response Code: $RES.  Will retry in 10 seconds."
    fi
    sleep 10
  fi
done


MANAGED_ROUTER_JSON_LIST="["

<% if_p("dedicated_vmr_list") do |vmrs| %>
  <% vmrs.each do |vmr_ip| %>
  VMR_IP=<%= vmr_ip %>

  echo "Configuring $VMR_IP:$SSH_PORT as a Dedicated VMR"

  #Generate a random password for the file user of this VMR
  FILEUSER_PWD=`openssl rand -base64 12`

  echo "This VMR's file user credentials are : fileuser / $FILEUSER_PWD"

  if [ "$MANAGED_ROUTER_JSON_LIST" != "[" ]; then
    MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST},"
  fi
  MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST}""{ \"sempHost\": \"$VMR_IP\",  \"sempPort\": \"8080\", \"sempUser\": \"$ADMIN_USER\", \"sempPassword\": \"$<PASSWORD>\", \"planId\": \"9bd51219-9cee-4570-99ab-ebe80d82c854\", \"sshPort\" : \"$SSH_PORT\" }"

  <% end %>
<% end %>

<% if_p("shared_vmr_list") do |vmrs| %>
  <% vmrs.each do |vmr_ip| %>
  VMR_IP=<%= vmr_ip %>

  echo "Configuring $VMR_IP as a Shared VMR"

  #Generate a random password for the file user of this VMR
  FILEUSER_PWD=`openssl rand -base64 12`

  echo "This VMR's file user credentials are : fileuser / $FILEUSER_PWD"

  if [ "$MANAGED_ROUTER_JSON_LIST" != "[" ]; then
    MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST},"
  fi
  MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST}""{ \"sempHost\": \"$VMR_IP\",  \"sempPort\": \"8080\", \"sempUser\": \"$ADMIN_USER\", \"sempPassword\": \"$<PASSWORD>\", \"planId\": \"af308299-102f-47a3-acb0-7de72be192bf\", \"sshPort\" : \"$SSH_PORT\" }"

  <% end %>
<% end %>

MANAGED_ROUTER_JSON_LIST="${MANAGED_ROUTER_JSON_LIST}]"

echo "curl -X GET $BROKER_USER:$BROKER_PASSWORD@$BROKER_HOSTNAME/solace/manage/solace_message_routers -d "'"'"${MANAGED_ROUTER_JSON_LIST}"'"'

RESPONSE=`curl -X GET $BROKER_USER:$BROKER_PASSWORD@$BROKER_HOSTNAME/solace/manage/solace_message_routers -d "${MANAGED_ROUTER_JSON_LIST}" -H "Content-Type: application/json;charset=UTF-8"`
echo "Service Broker: $RESPONSE"
echo $RESPONSE | ruby /var/vcap/jobs/config_vmr/bin/extractIpPort.rb | while read i; do
   CURRENT_HOST=`echo "$i" | awk '{print $1}'`
   CURRENT_PORT=`echo "$i" | awk '{print $2}'`
   CURRENT_PLAN_ID=`echo "$i" | awk '{print $3}'`

   if [ "$CURRENT_PLAN_ID" = "9bd51219-9cee-4570-99ab-ebe80d82c854" ]; then
      echo "Configuring a dedicated VMR... ${CURRENT_HOST}:${CURRENT_PORT}"
      ./rs-config-vmr-for-maas --cli-username $ADMIN_USER  --cli-password $ADMIN_PASSWORD $CURRENT_HOST:$CURRENT_PORT plan=dedicated portSeed=$STARTING_PORT fileUserPass=$FILE<PASSWORD>_PWD adminPass=$ADMIN_PASSWORD
   else
      echo "Configuring a shared VMR... ${CURRENT_HOST}:${CURRENT_PORT}"
      ./rs-config-vmr-for-maas --cli-username $ADMIN_USER --cli-password $ADMIN_PASSWORD $CURRENT_HOST:$CURRENT_PORT plan=shared portSeed=$STARTING_PORT fileUserPass=$FILEUSER_PWD adminPass=$ADMIN_PASSWORD
   fi

   # Install the server certificate on this VMR
   sudo SSHPASS=$FILEUSER_PWD sshpass -e scp -P $CURRENT_PORT server.pem fileuser@$CURRENT_HOST:/certs
   ./rs-config-server-cert --cli-username $user --cli-password $ADMIN_PASSWORD $CURRENT_HOST:$CURRENT_PORT certificate="server.pem"
{'fraction_non_alphanumeric': 0.1547085201793722, 'fraction_numerical': 0.018034704620783777, 'mean_word_length': 4.590735694822889, 'pattern_counts': {'":': 20, '<': 80, '<?xml version=': 0, '>': 80, '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': '3095923', 'n_tokens_mistral': 4270, 'n_tokens_neox': 3891, 'n_words': 848}