一个python脚本解决安装mq的依赖问题

import tarfile
import sys,os
from _utils.patrol2 import run_cmd
import zipfile




def get_version(filename):
    cmd="rpm -U {}".format(filename)
    code,res=run_cmd(cmd,cwd=pkg_unzip_path)
    if 'is already installed' in res or '已安装' in res:
        return True
    else:
        return False


def install_pkg(filename):
    cmd = "yum -y localinstall {}".format(filename)
    code,res=run_cmd(cmd,cwd=pkg_unzip_path)
    if code:
        print res
        sys.exit(1)
    if get_version(filename):
        return True
    else:
        return False


def extract(tar_path, target_path):
    try:
        tar = tarfile.open(tar_path, "r:gz")
        file_names = tar.getnames()
        for file_name in file_names:
            tar.extract(file_name, target_path)
        tar.close()
    except Exception, e:
        print Exception, e
        sys.exit(1)


def search(path, word):
    for filename in os.listdir(path):
        fp = os.path.join(path, filename)
        if os.path.isfile(fp) and word in filename:
            return filename
        elif os.path.isdir(fp):
            search(fp, word)
    return False


def unpack_zip(filename, destpath, fix_path=None):
    zf_obj = zipfile.ZipFile(filename)
    if destpath.endswith(':'):
        destpath = destpath + os.sep
    zf_obj.extractall(path=destpath)
    zf_obj.close()

    if fix_path:
        destpath = os.path.join(destpath, fix_path)
    return destpath


if pkg_file.endswith('.gz'):
    extract(pkg_file, pkg_unzip_path)
elif pkg_file.endswith('.zip'):
    pkg_name=pkg_name.split('.zip')[0]
    pkg_unzip_path=unpack_zip(pkg_file, pkg_unzip_path,fix_path=pkg_name)
print '-->解压成功'



code,res=run_cmd('chmod 755 ./lap/jre/jre/bin/java', cwd=pkg_unzip_path)
if code:
    print 'java执行权限赋权失败',res
    sys.exit(1)
else:
    print 'java执行权限赋权成功'


#接受授权认证
cmd = "sh mqlicense.sh<<EOF
1
EOF"
code,res = run_cmd(cmd,cwd=pkg_unzip_path)
if code:
    print res
    sys.exit(1)
else:
    print res
print '-->权限认证成功'

#1、安装系统依赖包
print '开始安装rpm包,请等待'
names=["MQSeriesRuntime","MQSeriesSDK","MQSeriesJava","MQSeriesClient","MQSeriesSamples","MQSeriesServer"]
for name in names:
    filename=search(pkg_unzip_path, name)
    if not filename:
        print '{}相关的包不存在'.format(name)
        sys.exit(1)
    elif get_version(filename):
        print '{}已安装'.format(filename)
    elif filename and not get_version(filename):
        res=install_pkg(filename)
        if res:
            print '安装{}成功'.format(filename)
        else:
            print '安装{}失败'.format(filename)
            sys.exit(1)
print '-->依赖包安装成功'

#2、修改系统limits参数
with open('/etc/security/limits.conf','a') as f:
    f.write("mqm soft nofile 10240
mqm hard nofile 10240
mqm soft nproc 4096
")
print '-->修改系统limits参数成功'
#3、修改系统核心参数
with open('/etc/sysctl.conf','a') as f:
    f.write("kernel.sem = 500 256000 250 1024
fs.file-max = 524288
net.ipv4.tcp_keepalive_time = 300
")

cmd = "sysctl -p"
code,res=run_cmd(cmd,cwd=pkg_unzip_path)
if 'kernel.sem = 500 256000 250 1024
fs.file-max = 524288
net.ipv4.tcp_keepalive_time = 300' in res:
    print '-->修改系统核心参数成功' 
else:
    print '-->修改系统核心参数失败'
    sys.exit(1)

  

原文地址:https://www.cnblogs.com/slqt/p/9007063.html