# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.hostname = "haproxy1604"
  config.vm.network "public_network", ip: "192.168.2.124", bridge: "eth0"

  config.vm.provider "virtualbox" do |v|
    v.name = "haproxy1604"
    v.customize ["modifyvm", :id, "--memory","1024" ]
    v.customize ["modifyvm", :id, "--cpus","1" ]
  end

  config.vm.synced_folder "/home/fabian/Documents", "/Documents"

  config.vm.provision "shell", inline: <<-SHELL
apt-get install software-properties-common -y
add-apt-repository ppa:vbernat/haproxy-1.8 -y
apt-get update
apt-cache policy haproxy
apt-get install haproxy -y
haproxy -v
# haproxy user needs to be part of syslog group for writing to /var/log
usermod -a -G syslog haproxy
which haproxy

####################
# PPA is sufficient now, no need to build
#####################
#apt-get install build-essential libssl-dev -y
#cd /usr/src

#wget http://www.haproxy.org/download/1.8/src/snapshot/haproxy-ss-LATEST.tar.gz

#tar xfz haproxy-ss-LATEST.tar.gz
# directory name varies based on snapshot day, rename dir to 'haproxy'
#timestampDir=`find . -name "haproxy-ss-*" -type d`
#for f in $timestampDir; do
#  dirName="${f:2}"
#  mv $dirName haproxy
#done

#cd haproxy

#cat VERSION
#chmod ugo+w VERSION
#ver=`cat VERSION`
#found version $ver
#echo "${ver}mymod" > VERSION

#make TARGET=linux2628 CPU=native USE_LIBCRYPT=1 USE_LINUX_SPLICE=1 USE_LINUX_TPROXY=1 USE_OPENSSL=1 USE_LIBPCRE=1
#make install
#/usr/local/sbin/haproxy -v

#./haproxy -v

#systemctl stop haproxy.service
#cp ./haproxy /usr/sbin/haproxy

########################
# systemd file that comes with ppa just needs tweak now
#######################
#wget https://raw.githubusercontent.com/fabianlee/blogcode/master/haproxy/ubuntu1604/haproxy.service -O /lib/systemd/system/haproxy.service
sed -i '/After=network.target/i StartLimitInterval=0\nStartLimitBurst=0' /lib/systemd/system/haproxy.service
systemctl enable haproxy.service
systemctl daemon-reload

echo RELOADOPTS="-x /run/haproxy/admin.sock" >> /etc/default/haproxy

wget https://raw.githubusercontent.com/fabianlee/blogcode/master/haproxy/selfsigned.sh
chmod 755 selfsigned.sh
./selfsigned.sh

cd /etc/haproxy
touch haproxy.cfg
cp haproxy.cfg haproxy.cfg.orig
wget https://raw.githubusercontent.com/fabianlee/blogcode/master/haproxy/haproxy.cfg -O haproxy.cfg
sed -i "s/REPLACEME/`hostname -f`/" haproxy.cfg

wget https://raw.githubusercontent.com/fabianlee/blogcode/master/haproxy/haproxytest.sh -O /usr/sbin/haproxytest.sh
chmod 755 /usr/sbin/haproxytest.sh

touch /var/log/haproxy.log
chown haproxy:syslog /var/log/haproxy.log
chmod ug+r+w /var/log/haproxy.log

sed -i '/load=\"imudp\"/s/^#//' /etc/rsyslog.conf
sed -i '/type=\"imudp\"/s/^#//' /etc/rsyslog.conf
systemctl restart rsyslog

apt-get install nodejs nodejs-legacy -y
wget https://raw.githubusercontent.com/fabianlee/blogcode/master/haproxy/server.js -O /tmp/server.js
ufw allow 9000:9002/tcp
touch /tmp/server.log
chmod ugo+r+w /tmp/server.log
nohup node /tmp/server.js 9000 > /tmp/server.log &

systemctl restart haproxy.service
apt-get install curl -y
curl https://localhost --insecure

wget https://raw.githubusercontent.com/fabianlee/blogcode/master/haproxy/ubuntu1604/switchhaproxy.sh -O /usr/sbin/switchhaproxy.sh
chmod 755 /usr/sbin/switchhaproxy.sh

SHELL

end