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

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.hostname = "haproxy1404"
  config.vm.network "public_network", ip: "192.168.2.121", bridge: "eth0"

  config.vm.provider "virtualbox" do |v|
    v.name = "haproxy1404"
    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 python-software-properties 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
usermod -a -G syslog haproxy
which haproxy


#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 -s
#make install
#/usr/local/sbin/haproxy -v

#./haproxy -v

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

wget https://raw.githubusercontent.com/fabianlee/blogcode/master/haproxy/ubuntu1404/haproxy -O /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy


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

cd /etc/haproxy
touch haproxy.config
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 '/ModLoad imudp/s/^#//' /etc/rsyslog.conf
sed -i '/UDPServerRun/s/^#//' /etc/rsyslog.conf
service rsyslog restart

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 &

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

SHELL

end