weblogic安装补丁脚本

一个weblogic自动打补丁自动卸载冲突补丁的脚本,拖拖踏踏写了100多行,总算是勉强可以用了,对于删除多级冲突(不知道weblogic是不是有这种多级依赖,目前遇到的是一级冲突,冲突两个旧补丁)没有真实测试环境,写的时候是考虑的,不过又是冲突列表又要按顺序删除,写的自己都不太确定了,加上没多级冲突的测试环境,所以遇上复杂的情况不保证可以完成操作,不过应付一下简单的还是够了
脚本运行环境linux + python2(windows的里面有些路径要修改/ 为\,应该也是可以的),程序中写死了补丁的压缩文件名和新补丁的ID,修改为参数提供会适用性更强,但是本次只针对这个补丁,所以暂时不修改这些
脚本运行需提供两个参数: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

脚本的健壮性比较差,很多异常处理没有做,只是勉强可以用而已,记录,有需求再改进

你可能感兴趣的:(weblogic安装补丁脚本)