#!/bin/bash
##########################################
# CentOS6-to-OpenNode6 conversion script #
# for OpenNode 6.0 - script version 1.0  #
# OpenNode LLC - opennodecloud.com       #        
##########################################
#
# OpenNode 6.0 bootstrap from CentOS 6.x
# ---------------------------------------
# 1) Install minimal default CentOS 6 system with http://ftp.estpak.ee/pub/centos/6.2/isos/x86_64/CentOS-6.0-x86_64-minimal.iso
# 2) Bootstrap to OpenNode 6.0 with centos6-to-opennode6.sh
#
# Bootstrapping:
# yum install -y wget
# wget -q -O - http://opennodecloud.com/download/centos6-to-opennode6.sh | bash
# ---------------------------------------

check_user()
{
_USER_NAME1=`whoami`
_USER_NAME2=$1
if [ "$_USER_NAME1" = "$_USER_NAME2" ]; then
        return 0
else
        echo -e "\n You must run this script as a root user\n"
        exit 1
fi
}

#1 -check for user root
if check_user root ; then

#Add opennode repos
rpm -ivh http://opennodecloud.com/CentOS/6/opennode/x86_64/RPMS/opennode-release-6.0-2.asys.x86_64.rpm

#Install yum priorities and merge-conf plugins
yum install yum-priorities yum-merge-conf pkgconfig -y

#Disable kernel and some other stuff updates from CentOS base repository and set yum priorities for other repos
sed -i 's/\[base\]/\[base\]\nexclude=kernel libvirt* anaconda* redhat-logos*\npriority=1/' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's/\[updates\]/\[updates\]\nexclude=kernel libvirt* anaconda* redhat-logos*\npriority=1/' /etc/yum.repos.d/CentOS-Base.repo	
sed -i 's/\[addons\]/\[addons\]\nexclude=kernel libvirt* anaconda* redhat-logos*\npriority=1/' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's/\[extras\]/\[extras\]\nexclude=kernel libvirt* anaconda* redhat-logos*\npriority=1/' /etc/yum.repos.d/CentOS-Base.repo	
sed -i 's/\[centosplus\]/\[centosplus\]\nexclude=kernel libvirt* anaconda* redhat-logos*\npriority=1/' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's/\[contrib\]/\[contrib\]\nexclude=kernel libvirt* anaconda* redhat-logos*\npriority=1/' /etc/yum.repos.d/CentOS-Base.repo

#Now lets pull and install all the needed packages for OpenNode Compatible Host
yum groupinstall -y 'Virtualization' 'Virtualization Client' 'Virtualization Platform' 'Virtualization Tools' 'OpenVZ Virtualization' 'OpenNode'

#Update the system
yum update -y

#Create vmbr0 bridge configuration - we are copying eth0 interface info into vmbr0
echo "ONBOOT=yes" > /etc/sysconfig/network-scripts/ifcfg-vmbr0
echo "TYPE=Bridge" >> /etc/sysconfig/network-scripts/ifcfg-vmbr0
echo "DEVICE=vmbr0" >> /etc/sysconfig/network-scripts/ifcfg-vmbr0
grep BOOTPROTO /etc/sysconfig/network-scripts/ifcfg-eth0 >> /etc/sysconfig/network-scripts/ifcfg-vmbr0
grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 >> /etc/sysconfig/network-scripts/ifcfg-vmbr0
grep NETMASK /etc/sysconfig/network-scripts/ifcfg-eth0 >> /etc/sysconfig/network-scripts/ifcfg-vmbr0
grep GATEWAY /etc/sysconfig/network-scripts/ifcfg-eth0 >> /etc/sysconfig/network-scripts/ifcfg-vmbr0

#Create temporary eth0 configuration
echo "DEVICE=eth0" > /tmp/network-config
echo "BOOTPROTO=none" >> /tmp/network-config
grep HWADDR /etc/sysconfig/network-scripts/ifcfg-eth0 >> /tmp/network-config
echo "ONBOOT=yes" >> /tmp/network-config
echo "TYPE=Ethernet" >> /tmp/network-config
echo "BRIDGE=vmbr0" >> /tmp/network-config

#Override existing eth0 conf
cat /tmp/network-config > /etc/sysconfig/network-scripts/ifcfg-eth0

#Change system control variables for OpenVZ
cat >> /etc/sysctl.conf << "EOF"

# OpenVZ
net.ipv4.ip_forward = 1
net.ipv6.conf.default.forwarding = 1
net.ipv6.conf.all.forwarding = 1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.conf.all.rp_filter = 1
kernel.sysrq = 1
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.conf.default.forwarding=1
EOF

#vznetaddbr configuration
echo '#!/bin/bash' > /etc/vz/vznet.conf
echo "EXTERNAL_SCRIPT=\"/usr/sbin/vznetaddbr\"" >> /etc/vz/vznet.conf

#Disable unneeded services
/sbin/chkconfig --level 345 certmaster off
/sbin/chkconfig --level 345 ip6tables off
/sbin/chkconfig --level 345 iptables off

#IF certmaster service was somehow running please stop it
service certmaster stop

#For now it might be good idea to clear iptables - less hassles with VM networking (like DNS inside VE-s) - 
#once you get it all running you can re-enable iptables and have some firewalling rules if you know what you are doing...
service iptables stop

#For the same reason please disable SELinux for now
#system-config-securitylevel-tui
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

#Restart networking in order to have vmbr0 device up and running - and this bridge will hold now your box IP address etc.
echo "Conversion is done!!! Please reboot in order to activate OpenNode kernel!"
sleep 3

fi
exit

