#!/bin/bash
#Nifty: Setup new shared webserver!

# Make sure the server even exists
getip=($(curl https://tools.zitcom.dk/api/sharedlinux_getmyid.php 2>/dev/null))

if [ "${getip[0]}" = "FAIL" ]; then
        echo "ERROR!"
        echo "Create the server in intranet before running this script"
        exit 1
fi

# Extend /opt
pvresize /dev/sda2
lvextend -l+100%FREE /dev/vg/opt
xfs_growfs /opt

# Scan for new disks
for i in /sys/class/scsi_host/*; do echo "- - -" > $i/scan ; done

sleep 3

if [ ! -b "/dev/sdb" ]; then
  echo "No /dev/sdb device found"
  exit
fi

if [ ! -b "/dev/sdc" ]; then
  echo "No /dev/sdc device found"
  exit
fi

pvcreate /dev/sdb
pvcreate /dev/sdc

vgcreate logvg /dev/sdb
vgcreate datavg /dev/sdc

lvcreate -l +100%FREE -n lv0 logvg
lvcreate -l +100%FREE -n lv0 datavg

mkfs.xfs -f /dev/logvg/lv0
mkfs.ext4 /dev/datavg/lv0

echo '/dev/mapper/logvg-lv0   /var/log/httpd   xfs defaults,noatime  1 2' >> /etc/fstab
echo '/dev/mapper/datavg-lv0   /var/www   ext4 defaults,usrjquota=aquota.user,jqfmt=vfsv0,noatime  1 2' >> /etc/fstab

mkdir /var/www
rm -rf /var/log/httpd/* 

mount -a

# Create quota file
quotacheck -cugv  /var/www/

yum install -y cagefs || exit 1

cagefsctl --init
cagefsctl --disable-all

# Install the job system
yum install svn -y
svn co --no-auth-cache --username=cron --password=t2eWrech http://svn.zitcom.dk/svn/Wannafind_Jobs /scripts/jobs

# Set cloudlinux params in sysctl
echo "fs.symlinkown_gid = 48" >> /etc/sysctl.conf

yum clean all

# Install puppet
bash <(curl -s https://nifty.sh/puppetcluster/universal/install_puppet.sh)

quotacheck -cugv  /var/www/
