#!/bin/bash
# ********************************************** #
SCRIPT_VERSION=100B01

#*****************************************#
#变量定义
#*****************************************#
#节点IP列表
declare -a node_ip_list
#补丁包名字列表
declare -a patch_list

#用于打热补丁内存检查
MEM_LIMIT_SIZE=100
G_DEV_IP=localhost

FILE_PATH=$(cd `dirname $0`; pwd)

#---------------------------------------------------------------------------------------------------
# FUNCTION NAME: ComExpectSendCmd
#---------------------------------------------------------------------------------------------------
ComExpectSendCliCmd()
{
    local log_ip=$1
    local time_out_time=$2
    local pass_word="Equip@storage"

    shift 2
    local send_cmd=$@

    if [ -z "$time_out_time" ] ; then
        time_out_time=600
    fi
    #下面只能用TAB缩进不能用空格
	expect <<-EOF
	set timeout ${time_out_time}
	spawn ssh -o StrictHostKeyChecking=no admin@${log_ip}
	
	expect "*password:" {
		exec sleep 1
		send "${pass_word}\r"
		expect {
			"admin:/>" {
				exec sleep 0.1
				send "\r"
			}
			"*password:" {
				exec usleep 100
				exit 1
			}
		}
	}

	expect 	"admin:/>" {
		send "change cli more_enabled=no silent_enabled=yes\r"
		expect "admin:/>" {
				send "${send_cmd}\r"	
		}
		exec sleep 1
		expect "admin:/>" {
			send "exit\r"; exit 0
		}
		exit
		expect {
			"*y/n*" {
				exec sleep 1
				send "y\r"
			}
		}
	}
	EOF
    return $?
}

main ()
{
	local product_model=""
	local patch_name=""
	local memory_size=""
	local patch_file=""
	local current_path=""
	local file_path=${FILE_PATH}
	
	##读取设备型号
	ComExpectSendCliCmd "${G_DEV_IP}" "600" "show system general" | tee ${file_path}/get_product_model.txt
	dos2unix ${file_path}/get_product_model.txt
	dos2unix -f ${file_path}/get_product_model.txt
	
	product_model=$(cat ${file_path}/get_product_model.txt | grep -A 10000 "show system general" | grep "Product Model" | awk -F ":" '{print $2}' | sort -u | head -n 1 | sed 's/^[ \r\t]*//g' | sed 's/[ \r\t]*$//g')
	
	if [ -z "${product_model}" ];then
		echo "get product model is null" >> ${file_path}/hotpatch_log.txt
		return 1
	fi
	
	#读取设备内存规格
	ComExpectSendCliCmd "${G_DEV_IP}" "600" "show controller general" | tee ${file_path}/get_mem_config.txt
	dos2unix ${file_path}/get_mem_config.txt
	dos2unix -f ${file_path}/get_mem_config.txt
	
	memory_size=$(cat ${file_path}/get_mem_config.txt | grep -A 10000 "show controller general" | grep "Cache Capacity" | awk -F ":" '{print $2}' | sort -u | head -n 1 | sed 's/^[ \r\t]*//g' | sed 's/[ \r\t]*$//g' )
	memory_size=$(echo "${memory_size}" | awk -F "GB" '{print $1}' | awk -F "." '{print $1}')
	
	#2600 V3 for Video在合集包中特殊配置为NONAS的
    if [ "${product_model}" = "2600 V3 for Video" ];then
		patch_name=$(cat ${file_path}/version.conf | grep "PatchConfig NoNAS:" | awk -F ":" '{print $2}')
	elif [ "${product_model}" = "2200 V3" -a "${memory_size}" = "8" ];then
		patch_name=$(cat ${file_path}/version.conf | grep "PatchConfig NoNAS:" | awk -F ":" '{print $2}')
	elif [ "${product_model}" = "2100 V3" ];then
		patch_name=$(cat ${file_path}/version.conf | grep "PatchConfig NoNAS:" | awk -F ":" '{print $2}')
	else
		patch_name=$(cat ${file_path}/version.conf | grep "PatchConfig NAS:" | awk -F ":" '{print $2}')
	fi
	
	if [ -z "${patch_name}" ];then
		echo "get patch name is null" >> ${file_path}/hotpatch_log.txt
		return 1
	fi
	
	#文件中有&等字符，linux无法识别先转化一下
	patch_file=$(echo "${file_path}/${patch_name}" | dos2unix)

	/OSM/script/os_upgrade_hotpatch.sh -m $MEM_LIMIT_SIZE 2>&1 >> ${file_path}/hotpatch_log.txt
	if [ $? -ne 0 ];then
		echo "execute /OSM/script/os_upgrade_hotpatch.sh -m $MEM_LIMIT_SIZE fail" >> ${file_path}/hotpatch_log.txt
		return 1
	fi
	
	/OSM/script/os_upgrade_hotpatch.sh -w install ${patch_file} 2>&1 >> ${file_path}/hotpatch_log.txt
	if [ $? -ne 0 ];then
		echo "execute /OSM/script/os_upgrade_hotpatch.sh -w install ${patch_file}" >> ${file_path}/hotpatch_log.txt
		return 1
	fi
	
	/OSM/script/os_upgrade_hotpatch.sh -p ${patch_file} 2>&1 >> ${file_path}/hotpatch_log.txt
	if [ $? -ne 0 ];then
		#第一次打失败，等待10s
		sleep 10
	
		/OSM/script/os_upgrade_hotpatch.sh -p ${patch_file} 2>&1 >> ${file_path}/hotpatch_log.txt
		if [ $? -ne 0 ];then
			echo "execute /OSM/script/os_upgrade_hotpatch.sh -p ${patch_file}" >> ${file_path}/hotpatch_log.txt
			return 1
		fi
	fi
	
	#热补丁打成功之后，睡1分钟后，检查是否打成功
    sleep 60
    /OSM/script/os_upgrade_hotpatch.sh -c 2>&1 >> ${file_path}/hotpatch_log.txt
	if [ $? -ne 0 ];then
		echo "execute /OSM/script/os_upgrade_hotpatch.sh -c" >> ${file_path}/hotpatch_log.txt
		return 1
	fi
	
	return 0
}

main ${@}

exit $?