#!/bin/bash
CPWD=`cd $(dirname  $0);pwd`
cd $CPWD
rpm -qa |grep jq
if [ $? = 0 ];then
        echo "The package has been installed"
else
        yum install jq -y
fi
ipmi_ip=$(ipmitool lan print |grep "IP Address  " |awk -F ": " '{print$2}')
SN=$(dmidecode -s system-serial-number |grep -v ^#  | sed s/[[:space:]]//g)
passwd=$(curl -s -H "Content-Type:application/json" -X POST --data  '{"data":  ["'${SN}'"],"type": "SN"}' http://udcm.ucloudadmin.com/api/ipmi/v1/getpwd/ | jq -r '.[]|.IPMIPassword')
[[ -n $passwd ]] || exit 1
netmask=$(ipmitool lan print |grep "Subnet Mask" |awk -F ": " '{print$2}')
gateway=$(ipmitool lan print |grep "Default Gateway IP" |awk -F ": " '{print$2}')

function BMCUpdating() {
 uname -a |grep el6
        if [ "$?" = 0 ];then
                service ipmi restart
        else
                systemctl restart ipmi.service
        fi
        echo "*** BMC updating ***"
        bin_file=$(ls *.ima)
	chmod +x socflash_x64
	echo YY|./socflash_x64 option=g if=${bin_file} gpio_b=B70 gpio_f=B71
	ipmitool mc reset cold
	sleep 120
}

function UserSetting() {
        ipmitool user list 1 |grep root
        if [ $? = 0 ];then
                echo "Root user is existed..."
        else
                sleep 3
                echo "*** Config User related settings ***"
                #//Create new user named "root"
                ipmitool user set name 2 root
                #//Enabled the new user
                ipmitool user enable 2
                #//Set new password to root
                ipmitool user set password 2 $passwd
                #//Set the administrator priv to root
                ipmitool user priv 2 4 1
                #//Set the highest access priv to root
                ipmitool channel setaccess 1 2 callin=on link=on ipmi=on
	fi
}
function IPMISetting() {
        echo "*** Config IPMI related settings ***"
        if [ $(ipmitool lan print |grep "IP Address Source" |awk -F ": " '{print$2}' |awk -F " " '{print$1}') = "Static" ];then
                echo "IPMI has been set completely"
	else
                ipmitool lan set 1 ipsrc static
                ipmitool lan set 1 ipaddr $ipmi_ip
                ipmitool lan set 1 netmask $netmask
                ipmitool lan set 1 defgw ipaddr $gateway
                #ipmitool user set password 2 $passwd
        fi
}

BMCUpdating
UserSetting
IPMISetting

