
{
    "name"  : "cpu误报专项-升级条件检查",
    "precondition" : ["isFix"],
    "problems" : {
        "patch_fail" : {
            "desc"      : "【问题】当前版本未检测到使用补丁进行过修复，工具尝试修复，但修复失败，请再次尝试。",
            "advice"    : "【建议】对当前版本进行热补丁升级，升级补丁过程不断网。"
        },
		"patch_ok" : {
            "desc"      : "【问题】当前版本未检测到使用补丁进行过修复，工具尝试修复，已经修复成功。",
            "advice"    : "【建议】再次检测看是否有热补丁问题。"
        }
    },
	"versionList" : [
		"NBS5500_RGOS 11.4(1)B3P3",
		"NBS5500_RGOS 11.4(1)B3P4",
		"NBS5600_RGOS 11.4(1)B1",
		"NBS5600_RGOS 11.4(1)B1P1",
		"S2910_RGOS 11.4(1)B1",
		"S2910_RGOS 11.4(1)B1P1",
		"S2910_RGOS 11.4(1)B3",
		"S2910_RGOS 11.4(1)B4",
		"S2910-NONE-POE_RGOS 11.4(1)B1",
		"S5750_RGOS 11.4(1)B1",
		"S5750_RGOS 11.4(1)B1P1",
		"S5750_RGOS 11.4(1)B3P1",
		"S6000E_RGOS 11.4(1)B2",
		"S5700H_RGOS 11.4(1)B2"
	]
}

---------------------------code-------------------------------
#S_versionList = ['S5750_RGOS 11.4(1)B3P1','S5750_RGOS 11.4(1)B1P1','S2910_RGOS 11.4(1)B1P1','NBS5600_RGOS 11.4(1)B1P1','NBS5500_RGOS 11.4(1)B3P4','S2910_RGOS 11.4(1)B1','S5750_RGOS 11.4(1)B1','NBS5600_RGOS 11.4(1)B1','S5700H_RGOS 11.4(1)B2','S6000E_RGOS 11.4(1)B2','S2910-NONE-POE_RGOS 11.4(1)B1']
S_versionList = get_configure_value('versionList')
precondition_result = 'pass'
precondition_message = ''
precondition_message_advice = ''

log_version = cli('show version detail')
log_versionList = log_version.split('\r\n')

#用户不同意检查
isFixed = precondition('isFix')
if !isFixed || isFixed == undefined :
	precondition_result = 'fail'
	precondition_message = '用户不同意检查，跳过检查！'
	precondition_message_advice = '若要检查此项，开始检测时请选择‘是’'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end
if !isRunningInToolkit():
	precondition_result = 'fail'
	precondition_message = '非工具端环境，不支持此项检测'
	precondition_message_advice = '非工具端环境，不支持此项检测'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end

#版本一致性检查
if !isSameVer():
	precondition_result = 'fail'
	precondition_message = '设备板卡版本与系统版本不一致，不能打补丁，请版本升级一致后再重试！'
	precondition_message_advice = '请确保环境中板卡一致后再进行尝试'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end

#是否在cpu-100%专项范围内
if !isPatchVersion():
	precondition_result = 'fail'
	precondition_message = '版本无cpu误报问题，无需打补丁！'
	precondition_message_advice = '版本无cpu-100%问题，无需打补丁'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end

#检查设备是否处于upgrading状态
if isUpgrading():
	precondition_result = 'fail'
	precondition_message = '设备正处在升级状态，请等待状态不是upgrading时再尝试！'
	precondition_message_advice = '等升级状态不是upgrading时再尝试'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end

set_share_value('precondition_result',precondition_result)
return 'skip'

def isPatchVersion():
	result = 0
	version = getVersion()
	foreach data in S_versionList:
		if data == version :
			result = 1
			break
		end
	end
	return result
end

#T版本另外给建议
def isT_version():
	result = 0
	version = getVersion()
	foreach data in T_versionList:
		if version == data :
			result = 1
			break
		end
	end
	return result
end

def isUpdate(version):
	result = 0
	foreach data in S_versionList:
		if data == version :
			result = 1
		end
	end
	return result
end

def getVersionDate():
	result = ''
	reg = /System\s+software\s+number\s+:\s+(\w{15})/
	foreach data in log_versionList:
		r = data.match(reg)
		if r != null:
			result = r[1]
		end
	end
	return result
end

def isSameVer():
	result = 1
	soft_ver = getVersion()
	reg = /Software\s+version\s+:\s+([^,^\r\n]+)/
	foreach data in log_versionList:
		r = data.match(reg)
		if r != null:
			num = r[1]
			if num != soft_ver:
				result = 0
				break
			end
		end
	end
	return result
end

def getVersion():
	result = ''
	reg = /System\s+software\s+version\s+:\s+([^,^\r\n]+)/
	foreach data in log_versionList:
		r = data.match(reg)
		if r != null:
			result = r[1]
		end
	end
	return result
end

def isUpgrading():
	result = 0
	log_upgrade = cli('show upgrade status')
	log_upgradeList = log_upgrade.split('\r\n')
	foreach data in log_upgradeList:
		if data.indexOf('upgrading') != -1:
			result = 1
		end
	end
	return result
end

def get_date_version(str):
	date = ''
	strList = str.split('\r\n')
	reg = /System software number\s+:\s+\w{1}\d{6}(\d{8})/
	foreach data in strList:
		r = data.match(reg)
		if r != null:
			date = r[1]
			break
		end
	end
	return date
end

def str_to_num(str):
	num = ''
	strList = str.split(",")
	foreach data in strList:
		num = num+data
	end
	return num
end
