<commit_before>#!/usr/bin/env bash

#=============== dSIPRouter Management Script ==============#
#
# install, configure, and manage dsiprouter
#
#========================== NOTES ==========================#
#
# Supported OS:
# Debian 9 (stretch)
# Debian 8 (jessie)
# CentOS 7
# Amazon Linux 2
# Ubuntu 16.04 (xenial)
#
# Notes:
# In general exported variables & functions
# are used in externally called scripts / programs
#
# TODO:
# allow remote db configuration on install
# allow user to move carriers freely between carrier groups
# allow a carrier to be in more than one carrier group
# add ncurses selection menu for enabling / disabling modules
# seperate kam config into smaller sections and import from main cfg
# seperate gui routes into smaller sections and import as blueprints
# allow loading python configs and kam configs from db
# track, organize, and better manage dependencies
# allow hot-reloading password from python configs while dsiprouter running
#
#===========================================================#

# Set project dir (where src and install files go)
DSIP_PROJECT_DIR=$(git rev-parse --show-toplevel 2>/dev/null)
export DSIP_PROJECT_DIR=${DSIP_PROJECT_DIR:-$(dirname $(readlink -f "$0"))}
# Import dsip_lib utility / shared functions
. ${DSIP_PROJECT_DIR}/dsiprouter/dsip_lib.sh

# Uncomment for detailed debugging information
# - splits stdout, stderr, and trace streams into 3 files
# - output files are timestamped throughout process (cpu intensive)
# - useful for tracking down bugs, especially when a lot of output is produced
# - the gawk version seems to be more efficient but mawk is supported as well
#
#mkdir -p /tmp/debug && rm -f /tmp/debug/*.log 2>/dev/null
#
# - gawk version (alias awk='gawk')
#exec   > >(awk '{ print strftime("[%Y-%m-%d_%H:%M:%S] "), $0; fflush(); }' | tee -ia /tmp/debug/stdout.log)
#exec  2> >(awk '{ print strftime("[%Y-%m-%d_%H:%M:%S] "), $0; fflush(); }' | tee -ia /tmp/debug/stderr.log 1>&2)
#exec 19> >(awk '{ print strftime("[%Y-%m-%d_%H:%M:%S] "), $0; fflush(); }' > /tmp/debug/trace.log)
# - mawk version (alias awk='mawk')
#exec   > >(awk -v time=$(date +"[%Y-%m-%d_%H:%M:%S] ") '{ print time, $0; fflush(); }' | tee -ia /tmp/debug/stdout.log)
#exec  2> >(awk -v time=$(date +"[%Y-%m-%d_%H:%M:%S] ") '{ print time, $0; fflush(); }' | tee -ia /tmp/debug/stderr.log 1>&2)
#exec 19> >(awk -v time=$(date +"[%Y-%m-%d_%H:%M:%S] ") '{ print time, $0; fflush(); }' > /tmp/debug/trace.log)
#
#BASH_XTRACEFD="19"
#set -x
#

#================== USER_CONFIG_SETTINGS ===================#

# Define some global variables
FLT_CARRIER=8
FLT_PBX=9
FLT_OUTBOUND=8000
FLT_INBOUND=9000
WITH_SSL=0
DEBUG=0     # By default debugging is turned off
export SERVERNAT=0
export REQ_PYTHON_MAJOR_VER=3
export DSIP_SYSTEM_CONFIG_DIR="/etc/dsiprouter"
export DSIP_KAMAILIO_CONFIG_DIR="${DSIP_PROJECT_DIR}/kamailio"
export DSIP_KAMAILIO_CONFIG_FILE="${DSIP_SYSTEM_CONFIG_DIR}/kamailio51_dsiprouter.cfg"
export DSIP_DEFAULTS_DIR="${DSIP_KAMAILIO_CONFIG_DIR}/defaults"
export DSIP_CONFIG_FILE="${DSIP_PROJECT_DIR}/gui/settings.py"
export DSIP_RUN_DIR="/var/run/dsiprouter"
export SYSTEM_KAMAILIO_CONFIG_DIR="/etc/kamailio"
export SYSTEM_KAMAILIO_CONFIG_FILE="${SYSTEM_KAMAILIO_CONFIG_DIR}/kamailio.cfg" # will be symlinked
export SYSTEM_RTPENGINE_CONFIG_DIR="/etc/rtpengine"
export SYSTEM_RTPENGINE_CONFIG_FILE="${SYSTEM_RTPENGINE_CONFIG_DIR}/rtpengine.conf"
export PATH_UPDATE_FILE="/etc/profile.d/dsip_paths.sh" # updates paths required
export RTPENGINE_VER="mr6.1.1.1"
export SRC_DIR="/usr/local/src"
export BACKUPS_DIR="/var/backups"
export CLOUD_INSTALL_LOG="/var/log/dsip-cloud-install.log"

# Default MYSQL db root user values
MYSQL_ROOT_DEF_USERNAME="root"
MYSQL_ROOT_DEF_PASSWORD=""
MYSQL_ROOT_DEF_DATABASE="mysql"

# Default MYSQL db kamailio user values
MYSQL_KAM_DEF_USERNAME="kamailio"
MYSQL_KAM_DEF_PASSWORD="<PASSWORD>"
MYSQL_KAM_DEF_DATABASE="kamailio"

# Default SSL options
# If you created your own certs prior to installing set this to match your certs
if [ ${WITH_SSL} -eq 1 ]; then
    SYSTEM_SSL_CERT_DIR="/etc/ssl/certs"                                    # certs general location
    DSIP_SSL_CERT_DIR="${SYSTEM_SSL_CERT_DIR}/$(hostname -f)"               # domain specific cert dir
    DSIP_SSL_KEY="${DSIP_SSL_CERT_DIR}/key.pem"                             # private key
    DSIP_SSL_CHAIN="${DSIP_SSL_CERT_DIR}/chain.pem"                         # full chain cert
    DSIP_SSL_CERT="${DSIP_SSL_CERT_DIR}/cert.pem"                           # full chain + csr cert
    DSIP_SSL_EMAIL="admin@$(hostname -f)"                                   # email in certs (for renewal)
    DSIP_GUI_PROTOCOL="https"                                               # protocol GUI is served on
else
    DSIP_SSL_CERT_DIR=""                                                    # domain specific cert dir
    DSIP_SSL_KEY=""                                                         # private key
    DSIP_SSL_CHAIN=""                                                       # full chain cert
    DSIP_SSL_CERT=""                                                        # full chain + csr cert
    DSIP_SSL_EMAIL=""                                                       # email in certs (for renewal)
    DSIP_GUI_PROTOCOL="http"                                                # protocol GUI is served on
fi


# Force the installation of a Kamailio version by uncommenting
#KAM_VERSION=44 # Version 4.4.x
#KAM_VERSION=51 # Version 5.1.x

# Uncomment and set this variable to an explicit Python executable file name
# If set, the script will not try and find a Python version with 3.5 as the major release number
#export PYTHON_CMD=/usr/bin/python3.4

# Network configuration values
export RTP_PORT_MIN=10000
export RTP_PORT_MAX=20000
export KAM_SIP_PORT=5060

#===========================================================#

#================= DYNAMIC_CONFIG_SETTINGS =================#
# updated dynamically!

export DSIP_PORT=$(getConfigAttrib 'DSIP_PORT' ${DSIP_CONFIG_FILE})
export EXTERNAL_IP=$(getExternalIP)
export INTERNAL_IP=$(ip route get 8.8.8.8 | awk 'NR == 1 {print $7}')
export INTERNAL_NET=$(awk -F"." '{print $1"."$2"."$3".*"}' <<<$INTERNAL_IP)

#===========================================================#
DSIP_SERVER_DOMAIN="$(hostname -f)"    # DNS domain we are using

# Get Linux Distro and Version, and normalize values for later use
DISTRO=$(getDisto)
# check downstream Distro's first, then check upstream Distro's if no match
if [[ "$DISTRO" == "amzn" ]]; then
	export DISTRO="amazon"
	export DISTRO_VER=$(grep -w "VERSION_ID" /etc/os-release | cut -d '"' -f 2)
elif [[ "$DISTRO" == "ubuntu" ]]; then
	export DISTRO="ubuntu"
	export DISTRO_VER=$(grep -w "VERSION_ID" /etc/os-release | cut -d '"' -f 2)
else
    if [ -f /etc/redhat-release ]; then
        export DISTRO="centos"
        export DISTRO_VER=$(cat /etc/redhat-release | cut -d ' ' -f 4 | cut -d '.' -f 1)
    elif [ -f /etc/debian_version ]; then
        export DISTRO="debian"
        export DISTRO_VER=$(grep -w "VERSION_ID" /etc/os-release | cut -d '"' -f 2)
    fi
fi

# Check if we are on AWS Instance
export AWS_ENABLED=0
# Will try to access the AWS metadata URL and will return an exit code of 22 if it fails
# The -f flag enables this feature
curl -s --connect-timeout 2 http://169.254.169.254/latest/dynamic/instance-identity/document|grep ami &>/dev/null
ret=$?
#AWS Instance
if (( $ret == 0 )); then
    export AWS_ENABLED=0
    setConfigAttrib 'CLOUD_PLATFORM' 'AWS' ${DSIP_CONFIG_FILE} -q
#Native Install or some other cloud platform that's not supported as of yet
else
    setConfigAttrib 'CLOUD_PLATFORM' '' ${DSIP_CONFIG_FILE} -q

fi
echo $AWS_ENABLED
exit

function displayLogo {
echo "CiAgICAgXyAgX19fX18gX19fX18gX19fX18gIF9fX19fICAgICAgICAgICAgIF8gCiAgICB8IHwv
IF9fX198XyAgIF98ICBfXyBcfCAgX18gXCAgICAgICAgICAgfCB8ICAgICAgICAgICAKICBfX3wg
fCAoX19fICAgfCB8IHwgfF9fKSB8IHxfXykgfF9fXyAgXyAgIF98IHxfIF9fXyBfIF9fIAogLyBf
YCB8XF9fXyBcICB8IHwgfCAgX19fL3wgIF8gIC8vIF8gXHwgfCB8IHwgX18vIF8gXCAnX198Cnwg
KF98IHxfX19fKSB8X3wgfF98IHwgICAgfCB8IFwgXCAoXykgfCB8X3wgfCB8fCAgX18vIHwgICAK
IFxfXyxffF9fX19fL3xfX19fX3xffCAgICB8X3wgIFxfXF9fXy8gXF9fLF98XF9fXF9fX3xffCAg
IAoKQnVpbHQgaW4gRGV0cm9pdCwgVVNBIC0gUG93ZXJlZCBieSBLYW1haWxpbyAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgClN1cHBv
cnQgY2FuIGJlIHB1cmNoYXNlZCBmcm9tIGh0dHBzOi8vZE9wZW5Tb3VyY2UuY29tL2RzaXByb3V0
ZXIKClRoYW5rcyB0byBvdXIgc3BvbnNvcjogU2t5ZXRlbCAoc2t5ZXRlbC5jb20pCg==" \
| base64 -d \<commit_msg>Removed the test exit command from the install script<commit_after>#!/usr/bin/env bash

#=============== dSIPRouter Management Script ==============#
#
# install, configure, and manage dsiprouter
#
#========================== NOTES ==========================#
#
# Supported OS:
# Debian 9 (stretch)
# Debian 8 (jessie)
# CentOS 7
# Amazon Linux 2
# Ubuntu 16.04 (xenial)
#
# Notes:
# In general exported variables & functions
# are used in externally called scripts / programs
#
# TODO:
# allow remote db configuration on install
# allow user to move carriers freely between carrier groups
# allow a carrier to be in more than one carrier group
# add ncurses selection menu for enabling / disabling modules
# seperate kam config into smaller sections and import from main cfg
# seperate gui routes into smaller sections and import as blueprints
# allow loading python configs and kam configs from db
# track, organize, and better manage dependencies
# allow hot-reloading password from python configs while dsiprouter running
#
#===========================================================#

# Set project dir (where src and install files go)
DSIP_PROJECT_DIR=$(git rev-parse --show-toplevel 2>/dev/null)
export DSIP_PROJECT_DIR=${DSIP_PROJECT_DIR:-$(dirname $(readlink -f "$0"))}
# Import dsip_lib utility / shared functions
. ${DSIP_PROJECT_DIR}/dsiprouter/dsip_lib.sh

# Uncomment for detailed debugging information
# - splits stdout, stderr, and trace streams into 3 files
# - output files are timestamped throughout process (cpu intensive)
# - useful for tracking down bugs, especially when a lot of output is produced
# - the gawk version seems to be more efficient but mawk is supported as well
#
#mkdir -p /tmp/debug && rm -f /tmp/debug/*.log 2>/dev/null
#
# - gawk version (alias awk='gawk')
#exec   > >(awk '{ print strftime("[%Y-%m-%d_%H:%M:%S] "), $0; fflush(); }' | tee -ia /tmp/debug/stdout.log)
#exec  2> >(awk '{ print strftime("[%Y-%m-%d_%H:%M:%S] "), $0; fflush(); }' | tee -ia /tmp/debug/stderr.log 1>&2)
#exec 19> >(awk '{ print strftime("[%Y-%m-%d_%H:%M:%S] "), $0; fflush(); }' > /tmp/debug/trace.log)
# - mawk version (alias awk='mawk')
#exec   > >(awk -v time=$(date +"[%Y-%m-%d_%H:%M:%S] ") '{ print time, $0; fflush(); }' | tee -ia /tmp/debug/stdout.log)
#exec  2> >(awk -v time=$(date +"[%Y-%m-%d_%H:%M:%S] ") '{ print time, $0; fflush(); }' | tee -ia /tmp/debug/stderr.log 1>&2)
#exec 19> >(awk -v time=$(date +"[%Y-%m-%d_%H:%M:%S] ") '{ print time, $0; fflush(); }' > /tmp/debug/trace.log)
#
#BASH_XTRACEFD="19"
#set -x
#

#================== USER_CONFIG_SETTINGS ===================#

# Define some global variables
FLT_CARRIER=8
FLT_PBX=9
FLT_OUTBOUND=8000
FLT_INBOUND=9000
WITH_SSL=0
DEBUG=0     # By default debugging is turned off
export SERVERNAT=0
export REQ_PYTHON_MAJOR_VER=3
export DSIP_SYSTEM_CONFIG_DIR="/etc/dsiprouter"
export DSIP_KAMAILIO_CONFIG_DIR="${DSIP_PROJECT_DIR}/kamailio"
export DSIP_KAMAILIO_CONFIG_FILE="${DSIP_SYSTEM_CONFIG_DIR}/kamailio51_dsiprouter.cfg"
export DSIP_DEFAULTS_DIR="${DSIP_KAMAILIO_CONFIG_DIR}/defaults"
export DSIP_CONFIG_FILE="${DSIP_PROJECT_DIR}/gui/settings.py"
export DSIP_RUN_DIR="/var/run/dsiprouter"
export SYSTEM_KAMAILIO_CONFIG_DIR="/etc/kamailio"
export SYSTEM_KAMAILIO_CONFIG_FILE="${SYSTEM_KAMAILIO_CONFIG_DIR}/kamailio.cfg" # will be symlinked
export SYSTEM_RTPENGINE_CONFIG_DIR="/etc/rtpengine"
export SYSTEM_RTPENGINE_CONFIG_FILE="${SYSTEM_RTPENGINE_CONFIG_DIR}/rtpengine.conf"
export PATH_UPDATE_FILE="/etc/profile.d/dsip_paths.sh" # updates paths required
export RTPENGINE_VER="mr172.16.17.32"
export SRC_DIR="/usr/local/src"
export BACKUPS_DIR="/var/backups"
export CLOUD_INSTALL_LOG="/var/log/dsip-cloud-install.log"

# Default MYSQL db root user values
MYSQL_ROOT_DEF_USERNAME="root"
MYSQL_ROOT_DEF_PASSWORD=""
MYSQL_ROOT_DEF_DATABASE="mysql"

# Default MYSQL db kamailio user values
MYSQL_KAM_DEF_USERNAME="kamailio"
MYSQL_KAM_DEF_PASSWORD="<PASSWORD>"
MYSQL_KAM_DEF_DATABASE="kamailio"

# Default SSL options
# If you created your own certs prior to installing set this to match your certs
if [ ${WITH_SSL} -eq 1 ]; then
    SYSTEM_SSL_CERT_DIR="/etc/ssl/certs"                                    # certs general location
    DSIP_SSL_CERT_DIR="${SYSTEM_SSL_CERT_DIR}/$(hostname -f)"               # domain specific cert dir
    DSIP_SSL_KEY="${DSIP_SSL_CERT_DIR}/key.pem"                             # private key
    DSIP_SSL_CHAIN="${DSIP_SSL_CERT_DIR}/chain.pem"                         # full chain cert
    DSIP_SSL_CERT="${DSIP_SSL_CERT_DIR}/cert.pem"                           # full chain + csr cert
    DSIP_SSL_EMAIL="admin@$(hostname -f)"                                   # email in certs (for renewal)
    DSIP_GUI_PROTOCOL="https"                                               # protocol GUI is served on
else
    DSIP_SSL_CERT_DIR=""                                                    # domain specific cert dir
    DSIP_SSL_KEY=""                                                         # private key
    DSIP_SSL_CHAIN=""                                                       # full chain cert
    DSIP_SSL_CERT=""                                                        # full chain + csr cert
    DSIP_SSL_EMAIL=""                                                       # email in certs (for renewal)
    DSIP_GUI_PROTOCOL="http"                                                # protocol GUI is served on
fi


# Force the installation of a Kamailio version by uncommenting
#KAM_VERSION=44 # Version 4.4.x
#KAM_VERSION=51 # Version 5.1.x

# Uncomment and set this variable to an explicit Python executable file name
# If set, the script will not try and find a Python version with 3.5 as the major release number
#export PYTHON_CMD=/usr/bin/python3.4

# Network configuration values
export RTP_PORT_MIN=10000
export RTP_PORT_MAX=20000
export KAM_SIP_PORT=5060

#===========================================================#

#================= DYNAMIC_CONFIG_SETTINGS =================#
# updated dynamically!

export DSIP_PORT=$(getConfigAttrib 'DSIP_PORT' ${DSIP_CONFIG_FILE})
export EXTERNAL_IP=$(getExternalIP)
export INTERNAL_IP=$(ip route get 8.8.8.8 | awk 'NR == 1 {print $7}')
export INTERNAL_NET=$(awk -F"." '{print $1"."$2"."$3".*"}' <<<$INTERNAL_IP)

#===========================================================#
DSIP_SERVER_DOMAIN="$(hostname -f)"    # DNS domain we are using

# Get Linux Distro and Version, and normalize values for later use
DISTRO=$(getDisto)
# check downstream Distro's first, then check upstream Distro's if no match
if [[ "$DISTRO" == "amzn" ]]; then
	export DISTRO="amazon"
	export DISTRO_VER=$(grep -w "VERSION_ID" /etc/os-release | cut -d '"' -f 2)
elif [[ "$DISTRO" == "ubuntu" ]]; then
	export DISTRO="ubuntu"
	export DISTRO_VER=$(grep -w "VERSION_ID" /etc/os-release | cut -d '"' -f 2)
else
    if [ -f /etc/redhat-release ]; then
        export DISTRO="centos"
        export DISTRO_VER=$(cat /etc/redhat-release | cut -d ' ' -f 4 | cut -d '.' -f 1)
    elif [ -f /etc/debian_version ]; then
        export DISTRO="debian"
        export DISTRO_VER=$(grep -w "VERSION_ID" /etc/os-release | cut -d '"' -f 2)
    fi
fi

# Check if we are on AWS Instance
export AWS_ENABLED=0
# Will try to access the AWS metadata URL and will return an exit code of 22 if it fails
# The -f flag enables this feature
curl -s --connect-timeout 2 http://169.254.169.254/latest/dynamic/instance-identity/document|grep ami &>/dev/null
ret=$?
#AWS Instance
if (( $ret == 0 )); then
    export AWS_ENABLED=0
    setConfigAttrib 'CLOUD_PLATFORM' 'AWS' ${DSIP_CONFIG_FILE} -q
#Native Install or some other cloud platform that's not supported as of yet
else
    setConfigAttrib 'CLOUD_PLATFORM' '' ${DSIP_CONFIG_FILE} -q

fi
echo $AWS_ENABLED

function displayLogo {
echo "<KEY> \
| base64 -d \
{'fraction_non_alphanumeric': 0.15159297988136733, 'fraction_numerical': 0.020974744695162965, 'mean_word_length': 3.7034800115041704, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2301511', 'n_tokens_mistral': 5887, 'n_tokens_neox': 5534, 'n_words': 1416}