<commit_before>#!/bin/bash

set -e
set -o pipefail

## NOTE: This script MUST ALWAYS run as root user.

datadir=/var/vcap/store/mysql
CONFIG_DIR=/etc/mysql
LOG_DIR=/var/vcap/sys/log/mysql
LOG_FILE=$LOG_DIR/pre-start.stdout.log
SLOW_QUERY_LOG_FILE=$LOG_DIR/mysql_slow_query.log
HEALTHCHECK_LOG_DIR="/var/vcap/sys/log/galera-healthcheck"
MARIADB_JOB_DIR=/var/vcap/jobs/mysql
JOB_INDEX=<%= index %>
TMP_DIR=/var/vcap/data/mysql/tmp
PERSISTENT_TMP_DIR=/var/vcap/data/tmp
SECURE_FILE_PRIV=/var/vcap/data/mysql/files

source /var/vcap/packages/cf-mysql-common/pid_utils.sh

log "pre-start setup script: set up ENV and logging"

if [[ ! -d "$TMP_DIR" ]]; then
  log "pre-start setup script: directory $TMP_DIR does not exist, creating it now"
  mkdir -p $TMP_DIR
fi
chown -R vcap:vcap $TMP_DIR
chown -R vcap:vcap $PERSISTENT_TMP_DIR

mkdir -p $SECURE_FILE_PRIV
chmod 0750 $SECURE_FILE_PRIV
chown -R vcap:vcap $SECURE_FILE_PRIV

mkdir -p $LOG_DIR
touch $LOG_FILE
touch $SLOW_QUERY_LOG_FILE
chown -R vcap:vcap $LOG_DIR
date >> $LOG_FILE 2>> $LOG_FILE
date >> $SLOW_QUERY_LOG_FILE 2>> $SLOW_QUERY_LOG_FILE
mkdir -p "${HEALTHCHECK_LOG_DIR}"
chown -R vcap:vcap "${HEALTHCHECK_LOG_DIR}"

<% if_p("syslog_aggregator.address", "syslog_aggregator.port", "syslog_aggregator.transport") do %>
# Start syslog forwarding
/var/vcap/packages/syslog_aggregator/setup_syslog_forwarder.sh $MARIADB_JOB_DIR/config
<% end %>

# It is surprisingly hard to get the config file location passed in
# on the command line to the mysql.server script. This is easier.
mkdir -p $CONFIG_DIR
chown vcap:vcap $CONFIG_DIR
rm -f $CONFIG_DIR/my.cnf
ln -sf $MARIADB_JOB_DIR/config/my.cnf $CONFIG_DIR/my.cnf

rm -f /etc/profile.d/disable_mysql_cli_history.sh
<% unless p('cf_mysql.mysql.cli_history') %>
ln -sf $MARIADB_JOB_DIR/config/disable_mysql_cli_history.sh /etc/profile.d/disable_mysql_cli_history.sh
<% end %>

ulimit -n <%= p('cf_mysql.mysql.max_open_files') %>

<% if p('cf_mysql.mysql.disable_auto_sst') %>
if [ -d ${datadir} ]; then
  export DISABLE_SST=1
fi
<% end %>

function check_mysql_filesystem() {
  if [[ $(mountpoint -d "${datadir}") == $(mountpoint -d /) ]]
  then
    echo "###################################################################################################################################" >> "${LOG_FILE}"
    echo "!!! Data directory (${datadir}) is on the same filesystem as /. Have you attached persistent disk? !!!" >> "${LOG_FILE}"
    echo "####################################################################################################################################" >> "${LOG_FILE}"
    exit 1
  fi
}

function check_mysql_disk_capacity() {
  local datadir_capacity=$(df -B1 --output=target,size "${datadir}" | awk ' NR==2 { print $2 } ')
  local minimum_required_space=10737418240  # 10GiB
  if [[ "${datadir_capacity}" < "${minimum_required_space}" ]]
  then
    echo "########################################################################################" >> "${LOG_FILE}"
    echo "!!! Datadir capacity is ${datadir_capacity}, which is under the minimum required: ${minium_required_space} !!!" >> "${LOG_FILE}"
    echo "########################################################################################" >> "${LOG_FILE}"<commit_msg>Check disk space against base 10 rather than base 2 [#145971959]<commit_after>#!/bin/bash

set -e
set -o pipefail

## NOTE: This script MUST ALWAYS run as root user.

datadir=/var/vcap/store/mysql
CONFIG_DIR=/etc/mysql
LOG_DIR=/var/vcap/sys/log/mysql
LOG_FILE=$LOG_DIR/pre-start.stdout.log
SLOW_QUERY_LOG_FILE=$LOG_DIR/mysql_slow_query.log
HEALTHCHECK_LOG_DIR="/var/vcap/sys/log/galera-healthcheck"
MARIADB_JOB_DIR=/var/vcap/jobs/mysql
JOB_INDEX=<%= index %>
TMP_DIR=/var/vcap/data/mysql/tmp
PERSISTENT_TMP_DIR=/var/vcap/data/tmp
SECURE_FILE_PRIV=/var/vcap/data/mysql/files

source /var/vcap/packages/cf-mysql-common/pid_utils.sh

log "pre-start setup script: set up ENV and logging"

if [[ ! -d "$TMP_DIR" ]]; then
  log "pre-start setup script: directory $TMP_DIR does not exist, creating it now"
  mkdir -p $TMP_DIR
fi
chown -R vcap:vcap $TMP_DIR
chown -R vcap:vcap $PERSISTENT_TMP_DIR

mkdir -p $SECURE_FILE_PRIV
chmod 0750 $SECURE_FILE_PRIV
chown -R vcap:vcap $SECURE_FILE_PRIV

mkdir -p $LOG_DIR
touch $LOG_FILE
touch $SLOW_QUERY_LOG_FILE
chown -R vcap:vcap $LOG_DIR
date >> $LOG_FILE 2>> $LOG_FILE
date >> $SLOW_QUERY_LOG_FILE 2>> $SLOW_QUERY_LOG_FILE
mkdir -p "${HEALTHCHECK_LOG_DIR}"
chown -R vcap:vcap "${HEALTHCHECK_LOG_DIR}"

<% if_p("syslog_aggregator.address", "syslog_aggregator.port", "syslog_aggregator.transport") do %>
# Start syslog forwarding
/var/vcap/packages/syslog_aggregator/setup_syslog_forwarder.sh $MARIADB_JOB_DIR/config
<% end %>

# It is surprisingly hard to get the config file location passed in
# on the command line to the mysql.server script. This is easier.
mkdir -p $CONFIG_DIR
chown vcap:vcap $CONFIG_DIR
rm -f $CONFIG_DIR/my.cnf
ln -sf $MARIADB_JOB_DIR/config/my.cnf $CONFIG_DIR/my.cnf

rm -f /etc/profile.d/disable_mysql_cli_history.sh
<% unless p('cf_mysql.mysql.cli_history') %>
ln -sf $MARIADB_JOB_DIR/config/disable_mysql_cli_history.sh /etc/profile.d/disable_mysql_cli_history.sh
<% end %>

ulimit -n <%= p('cf_mysql.mysql.max_open_files') %>

<% if p('cf_mysql.mysql.disable_auto_sst') %>
if [ -d ${datadir} ]; then
  export DISABLE_SST=1
fi
<% end %>

function check_mysql_filesystem() {
  if [[ $(mountpoint -d "${datadir}") == $(mountpoint -d /) ]]
  then
    echo "###################################################################################################################################" >> "${LOG_FILE}"
    echo "!!! Data directory (${datadir}) is on the same filesystem as /. Have you attached persistent disk? !!!" >> "${LOG_FILE}"
    echo "####################################################################################################################################" >> "${LOG_FILE}"
    exit 1
  fi
}

function check_mysql_disk_capacity() {
  local datadir_capacity=$(df -BMB --output=target,size "${datadir}" | awk ' NR==2 { print $2 } ')
  local minimum_required_space=10000
  if [[ "${datadir_capacity}" < "${minimum_required_space}" ]]
  then
    echo "########################################################################################" >> "${LOG_FILE}"
    echo "!!! Datadir capacity is ${datadir_capacity}, which is under the minimum required: ${minimum_required_space} !!!" >> "${LOG_FILE}"
    echo "########################################################################################" >> "${LOG_FILE}"
{'fraction_non_alphanumeric': 0.25968873970094597, 'fraction_numerical': 0.00778150747635032, 'mean_word_length': 5.799792531120332, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 59, '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': '398923', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2216, 'n_words': 484}