#!/bin/sh

# script for kloudbuster

# Tune the Linux kernel for scale test
echo "* soft nofile 102400" >> /etc/security/limits.conf
echo "* hard nofile 102400" >> /etc/security/limits.conf

cat << EOF >> /etc/sysctl.conf
fs.file-max=6553550
net.core.wmem_max=8388608
net.core.wmem_default=8388608
net.core.rmem_max=33554432
net.core.rmem_default=33554432
net.core.netdev_max_backlog=100000
net.ipv4.icmp_ratelimit=0
net.ipv4.tcp_tw_recycle=1
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_max_tw_buckets=65536
net.ipv4.tcp_fin_timeout=15
net.ipv4.tcp_max_syn_backlog=65536
net.ipv4.tcp_syncookies=1
net.ipv4.neigh.default.gc_thresh1=4096
net.ipv4.neigh.default.gc_thresh2=4096
net.ipv4.neigh.default.gc_thresh3=4096
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.all.arp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_filter=0
EOF
sysctl -p

# do not autostart the redis server or the nginx server
# let the KB agent start the appropriate server (if applicable)
update-rc.d -f redis-server remove
update-rc.d -f nginx remove

# Change permission to 777 for kb_test
chmod -R 777 /kb_test

# Create nginx server root
mkdir -p /data/www
chmod -R 777 /data

# redis server should listen on all interfaces
sed -i "s/127.0.0.1/0.0.0.0/g" /etc/redis/redis.conf

# if started nginx should be allowed to open more file descriptors
sed -i 's/start-stop-daemon\ --start/ulimit\ \-n\ 102400\n\t\0/g' /etc/init.d/nginx

# Auto start the KloudBuster Agent, with user-data
sed -i "s/^exit\s0/cd \/kb_test\n\0/g" /etc/rc.local
sed -i "s/^exit\s0/if wget http\:\/\/169.254.169.254\/latest\/user-data; then \:; fi\n\0/g" /etc/rc.local
sed -i "s/^exit\s0/python kb_vm_agent.py \&\n\0/g" /etc/rc.local

# =================
# KloudBuster Proxy
# =================
cd /kb_test
git clone git://github.com/openstack/kloudbuster.git
cd kloudbuster
pip install -r requirements.txt

# ======
# Client
# ======
# python redis client, HdrHistorgram_py
pip install redis hdrhistogram

# Install HdrHistorgram_c
cd /tmp
git clone git://github.com/HdrHistogram/HdrHistogram_c.git
cd HdrHistogram_c
cmake .
make install

# Install the http traffic generator
cd /tmp
git clone git://github.com/yicwang/wrk2.git
cd wrk2
make
mv wrk /usr/local/bin/wrk2

# Move the HdrHistogram library to /usr/lib
mv /usr/local/lib/libhdr_histogram.so /usr/lib/

# Install fio
cd /tmp
FIO='fio-2.16'
wget http://brick.kernel.dk/snaps/$FIO.tar.gz
tar xzf $FIO.tar.gz
cd $FIO
make
mv fio /usr/local/bin/fio

# Install nuttcp
# cd /kb_test/kloudbuster/kb_dib/elements/nuttcp
# gcc nuttcp-7.3.3.c -o nuttcp
# mv nuttcp /usr/local/bin/nuttcp

# =======
# Cleanup
# =======
# Remove HdrHistogram_c, wrk2, and fio builds
rm -rf /tmp/HdrHistogram_c
rm -rf /tmp/wrk2
rm -rf /tmp/fio

# Uninstall unneeded packages
apt-get -y --purge remove libyaml-dev
apt-get -y --purge remove libssl-dev
apt-get -y --purge remove zlib1g-dev
apt-get -y --purge remove libaio-dev
apt-get -y --purge remove python-pip
apt-get -y --purge remove python-dev
apt-get -y --purge remove build-essential
apt-get -y --purge remove cmake
apt-get -y --purge autoremove
apt-get -y autoclean
