一個weblogic自動打補丁自動卸載沖突補丁的腳本,拖拖踏踏寫了100多行,總算是勉強可以用了,對于刪除多級沖突(不知道weblogic是不是有這種多級依賴,目前遇到的是一級沖突,沖突兩個舊補?。]有真實測試環(huán)境,寫的時候是考慮的,不過又是沖突列表又要按順序刪除,寫的自己都不太確定了,加上沒多級沖突的測試環(huán)境,所以遇上復雜的情況不保證可以完成操作,不過應付一下簡單的還是夠了
腳本運行環(huán)境linux + python2(windows的里面有些路徑要修改/ 為\,應該也是可以的),程序中寫死了補丁的壓縮文件名和新補丁的ID,修改為參數(shù)提供會適用性更強,但是本次只針對這個補丁,所以暫時不修改這些
腳本運行需提供兩個參數(shù):MW_HOME 和 WL_HOME
例如:
python dufg.py /servyouapp/bea/ /servyouapp/bea/wlserver_10.3/
腳本代碼如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import commands
import os
import stat
import string
#import time
import zipfile
from sys import argv
def change_mem():
ch_dir()
with open("bsu.sh", "r") as f1,open("bsu.bak", "w") as f2:
for line in f1.readlines():
if "-Xms" in line:
line = '''MEM_ARGS="-Xms1500m -Xmx1500m"'''+'\n'
f2.write(line)
os.rename("bsu.sh", "bsu.sh-bak")
os.rename("bsu.bak", "bsu.sh")
os.chmod("bsu.sh", stat.S_IRWXU)
def ch_dir():
name, MW_HOME, WL_HOME = argv
if MW_HOME.endswith('/') == False:
MW_HOME = '%s%s' % (MW_HOME, '/')
if WL_HOME.endswith('/') == False:
WL_HOME = '%s%s' % (WL_HOME, '/')
bsu_dir = MW_HOME + '''utils/bsu'''
os.chdir(bsu_dir)
def copy_file(): #這里涉及到linux和windows路徑分隔符的問題,沒考慮這么多
name, MW_HOME, WL_HOME = argv
cur_dir = os.getcwd()
if MW_HOME.endswith('/') == False:
MW_HOME = '%s%s' % (MW_HOME, '/')
file_path = "%s%s" % (cur_dir, '/p26519424_3506_Generic.zip')#這里寫死了補丁壓縮包的名字
des_path = "%s%s" % (MW_HOME, 'utils/bsu/cache_dir/')
f = zipfile.ZipFile(file_path, 'r')
for file in f.namelist():
f.extract(file, des_path)
print "Unzip and copy file success"
def is_ok(str):
res = str.splitlines()[-1].split(':')[-1].strip().split(',')
if res[-1] == "Success" or res[-1] == "成功":
return 1
else:
return 0
def install_patch(patch_id):
name, MW_HOME, WL_HOME = argv
if MW_HOME.endswith('/') == False:
MW_HOME = '%s%s' % (MW_HOME, '/')
if WL_HOME.endswith('/') == False:
WL_HOME = '%s%s' % (WL_HOME, '/')
comm = "sh bsu.sh -install -patch_download_dir=%sutils/bsu/cache_dir -patchlist=%s -prod_dir=%s" % (MW_HOME, patch_id, WL_HOME)
print "Will start %s" % (comm)
print "Check conflict and install new patch, about 20 mins ..."
try:
[stat, rtv] = commands.getstatusoutput(comm)
print rtv
except Exception, e:
print "error"
exit(0)
if is_ok(rtv):
print "Success install patch"
else:
print "Need remove old patch %s" % (get_depend_patch_list(rtv))
old_patch_remove(get_depend_patch_list(rtv))
install_patch(patch_id)
def get_depend_patch_list(str):
list = str.splitlines()[-1].split(':')[-1].strip().split(',')
list1 = []
for i in list:
list1.insert(0, i)
return list1
def remove_patch(patch_id):
print "Remove patch_id:%s, about 25 mins ..." % (patch_id)
comm = "sh bsu.sh -remove -verbose -patchlist=%s -prod_dir=%s" % (patch_id, WL_HOME)
print comm
try:
[stat, rtv] = commands.getstatusoutput(comm)
except Exception, e:
print "error"
exit(0)
print rtv
return rtv
def old_patch_remove(patch_list):
global flag
if len(patch_list) == 1:
print "Start remove patch %s ..." % (patch_list[-1])
temp_res = remove_patch(patch_list[-1])
temp_list.append(patch_list[-1])
if is_ok(temp_res):
print "Patch %s is already removed..." % (patch_list[-1])
flag = 1
else:
depend_patch_list = get_depend_patch_list(temp_res)
print "The patch %s hava depend patch:%s" % (patch_id, depend_patch_list[-1])
old_patch_remove(depend_patch_list)
else:
for patch_id in patch_list:
flag = 0
if patch_id in temp_list:
continue
print patch_id
res = remove_patch(patch_id)
if is_ok(res):
flag = 1
print "Patch %s is already removed ..." % (patch_id)
while flag == 0:
depend_patch_list = get_depend_patch_list(res)
print "The patch %s hava depend patch:%s" % (patch_id, depend_patch_list[-1])
old_patch_remove(depend_patch_list)
#time.sleep(2)
if flag == 1:
tt = remove_patch(patch_id)
if is_ok(tt):
print "Patch %s is already removed ..." % (patch_id)
else:
print "Patch %s remove faild ... will exit" % (patch_id)
exit(0)
if __name__ == '__main__':
MW_HOME = ''
WL_HOME = ''
global flag
flag = 0
temp_list = []
if len(argv) == 1:
print "parameter MW_HOME AND WL_HOME"
exit(0)
else:
name, MW_HOME, WL_HOME = argv
copy_file()
ch_dir()
change_mem()
install_patch("FMJJ") #這里寫死了新補丁的ID
腳本的健壯性比較差,很多異常處理沒有做,只是勉強可以用而已,記錄,有需求再改進