travis/flow_autoconfig.sh
# Flow Test Autoconfig
#
# Script not meant to be run on personal machines (may break some configs)
# Intended use case is a fresh sys (tested on ubuntu18.04desktop)
# which can easily be run in a virtualbox VM.


# later OS versions have later versions of pip that require convincing to actually install.
. /etc/os-release
if [ "${VERSION_ID}" \> "22.04" ]; then
        pip_install="pip3 install --break-system-packages --user "
else
        pip_install="pip3 install "
fi

# Install and configure dependencies
sudo apt-key adv --keyserver "hkps.pool.sks-keyservers.net" --recv-keys "0x6B73A36E6026DFCA"
sudo add-apt-repository -y ppa:ssc-hpc-chp-spc/metpx
sudo apt update
sudo apt -y upgrade
sudo apt -y install python3-setuptools python3-magic python-setuptools python3-paramiko python3-requests python3-pyftpdlib
sudo apt -y install metpx-libsr3c metpx-libsr3c-dev metpx-sr3c
sudo apt -y install metpx-libsr3c metpx-libsr3c-dev metpx-sr3c
sudo apt -y install erlang-nox erlang-diameter erlang-eldap findutils git librabbitmq4 net-tools openssh-client openssh-server python3-pip rabbitmq-server xattr wget 
sudo apt -y install wget ncftp


${pip_install} -U pip

# The dependencies that are installed using apt are only available to system default Python versions (e.g. Python 3.8 on Ubuntu 20.04)
# If we are testing on a non-default Python version, we need to ensure these dependencies are still installed, so we use pip.
# See issue #407, #445.
echo "Checking for missing Python packages and installing with pip"
for PKG in amqp appdirs dateparser flufl.lock humanize jsonpickle netifaces paho-mqtt psutil rangehttpserver watchdog xattr paramiko pyftpdlib net-tools; do
    PKG_INSTALLED="`pip3 list | grep ${PKG}`"
    if [ "$?" == "0" ] ; then
        echo "$PKG is already installed"
    else
        ${pip_install} ${PKG}
    fi
done

# Need paho > 2.1.0 https://github.com/MetPX/sarracenia/pull/1119
${pip_install} --upgrade paho-mqtt

# in case it was installed as a dependency.
sudo apt -y remove metpx-sr3

${pip_install} .

# Setup basic configs
mkdir -p ~/.config/sarra ~/.config/sr3

cat > ~/.config/sarra/default.conf << EOF
expire 7h
declare env FLOWBROKER=localhost
declare env SFTPUSER=${USER}
declare env TESTDOCROOT=${HOME}/sarra_devdocroot
declare env MQP=amqp
declare env several=3
logEvents after_accept,after_work,on_housekeeping,post,after_post
EOF
cp ~/.config/sarra/default.conf ~/.config/sr3


ADMIN_PASSWORD=$(openssl rand -hex 6)
OTHER_PASSWORD=$(openssl rand -hex 6)
cat > ~/.config/sarra/credentials.conf << EOF
amqp://bunnymaster:${ADMIN_PASSWORD}@localhost/
amqp://tsource:${OTHER_PASSWORD}@localhost/
amqp://tsub:${OTHER_PASSWORD}@localhost/
amqp://tfeed:${OTHER_PASSWORD}@localhost/
amqp://anonymous:${OTHER_PASSWORD}@localhost/
amqps://anonymous:anonymous@dd.weather.gc.ca
amqps://anonymous:anonymous@dd1.weather.gc.ca
amqps://anonymous:anonymous@dd2.weather.gc.ca
amqps://anonymous:anonymous@hpfx.collab.science.gc.ca
ftp://anonymous:anonymous@localhost:2121/
EOF
cp ~/.config/sarra/credentials.conf ~/.config/sr3

cat > ~/.config/sarra/admin.conf << EOF
cluster localhost
admin amqp://bunnymaster@localhost/
feeder amqp://tfeed@localhost/
declare source tsource
declare subscriber tsub
declare subscriber anonymous
EOF
cp ~/.config/sarra/admin.conf ~/.config/sr3

echo

check_wsl=$(ps --no-headers -o comm 1)

# Manage RabbitMQ
if [[ $(($check_wsl == "init" )) ]]; then
	sudo service rabbitmq-server restart
else
	sudo systemctl restart rabbitmq-server
fi
sudo rabbitmq-plugins enable rabbitmq_management

sudo rabbitmqctl delete_user guest

for USER_NAME in "bunnymaster" "tsource" "tsub" "tfeed" "anonymous"; do
sudo rabbitmqctl delete_user ${USER_NAME}
done

sudo rabbitmqctl add_user bunnymaster ${ADMIN_PASSWORD}
sudo rabbitmqctl set_permissions bunnymaster ".*" ".*" ".*"
sudo rabbitmqctl set_user_tags bunnymaster administrator

echo

for p in $HOME/.local/bin /usr/local/bin; do

    if [[ ":$PATH:" != *":$p:"* ]]; then
       export PATH=$p:$PATH
    fi
done


if [[ $(($check_wsl == "init" )) ]]; then
	sudo service rabbitmq-server restart
else 
	sudo systemctl restart rabbitmq-server
fi

cd /usr/local/bin
sudo mv rabbitmqadmin rabbitmqadmin.1
sudo wget http://localhost:15672/cli/rabbitmqadmin
sudo chmod 755 rabbitmqadmin
cd 

echo

# Configure users
sr3 --users declare
echo "dir: +${PWD}+"
git clone https://github.com/MetPX/sr_insects

