def zip_file_add(zip_file, new_file_path_list, new_file_path_rel_list): """ 将文件添加更新到zip文件 :param param1: this is a first param :param param2: ['WEB-INF/classes/META-INF/global.properties'] :returns: this is a description of what is returned :raises keyError: raises an exception @author: jhuang @time:1/24/2018 """ time_start = time.time() logger.debug(new_file_path_list) n = 0 for file in new_file_path_list: exec_shell('zip -d %s %s' % (zip_file, new_file_path_rel_list[n])) n = +1 f = zipfile.ZipFile(zip_file, 'a', zipfile.ZIP_DEFLATED) n = 0 for file in new_file_path_list: f.write(file, new_file_path_rel_list[n]) n = +1 f.close() logger.debug('更新压缩文件用时:%s' % (time.time() - time_start))
def get_netspeed(eth=''): """ |##desc: 网卡网速获取 |##:param: None |##:return: None |##@author: jhuang |##@time:2017-07-20 """ RX = 0 TX = 0 if is_linux_system() is True: # return RX, TX if eth == '': eth = psutil.net_io_counters(pernic=True).keys()[1] ret = exec_shell('/sbin/ifconfig %s | grep bytes' % (eth)) X1 = re.findall('bytes.(\d+)', ret['msg']) sleep(1) ret = exec_shell('/sbin/ifconfig %s | grep bytes' % (eth)) X2 = re.findall('bytes.(\d+)', ret['msg']) print X2 if len(X2) > 0: # RX 为下行流量 TX 为上行流量 RX = covert_bytes(int(X2[0]) - int(X1[0])) TX = covert_bytes(int(X2[1]) - int(X1[1])) print RX, RX return RX, TX
def ssh_key_install(password, username, hostname): """ |##desc: 安装ssh key |##:param: None |##:return: None |##@author: jhuang |##@time:2017/7/14 """ exec_shell( "sshpass -p %s ssh-copy-id -i /root/.ssh/id_rsa.pub %s@%s" % (password, username, hostname), 5)
def basic_auth(self, URL, username, passwd): """ |##@Function purpose:基础HTTP认证方式登陆 |##@Parameter description:None |##@Return value:None |##@Function logic:None |##@author: jhuang |##@time:2017/6/5 """ exec_shell('curl -u %s:%s -L -c %s --user-agent Mozilla/4.0 %s' % (username, passwd, self.cookieFile, URL))
def is_master_process(self, uwsgi_server_port='8002'): """ 判断自身是否为uwsgi 中的mater 进程 :param param1: this is a first param :param param2: this is a second param :returns: this is a description of what is returned :raises keyError: raises an exception @author: jhuang @time:9/26/2018 """ import os self.self_pid = str(os.getpid()) if is_linux_system(): self.uwsgi_master_pid = str(exec_shell( """netstat -anp|grep %s|awk '{printf $7}'|cut -d/ -f1""" % (uwsgi_server_port))['msg']) self.self_ppid = str(os.getppid()) else: self.self_ppid = '-1' self.uwsgi_master_pid = '0' logger.debug('uwsgi_master_pid:{uwsgi_master_pid},self_ppid:{self_ppid},self_pid:{self_pid}'.format( uwsgi_master_pid=self.uwsgi_master_pid, self_ppid=self.self_ppid, self_pid=self.self_pid)) if self.uwsgi_master_pid == self.self_ppid: return True else: return False
def get_host_all_ip(): """ 获取主机全部IP地址 :param param1: this is a first param :param param2: this is a second param :returns: ip list :raises keyError: raises an exception @author: jhuang @time:05/02/2018 """ ip_list = [] if is_linux_system(): ip_dic = exec_shell( "/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d 'addr:'") ip_list = str(ip_dic['msg']).split('\n') else: hostname = socket.gethostname() addrs = socket.getaddrinfo(hostname, None) for item in addrs: if item[0] == 2: ip_list.append(item[4][0]) return ip_list
def download(self, URL, saveFile, maxtime=60 * 60, shellFile=True): """ |##@Function purpose:download |##@Parameter description:None |##@Return value:None |##@Function logic:None |##@author: jhuang |##@time:2017/6/5 """ cmd = 'curl --max-time %s -c %s -b %s --user-agent Mozilla/4.0 -o "%s" "%s" ' % ( maxtime, self.cookieFile, self.cookieFile, saveFile, URL) if shellFile: shelFile = tempfile.mktemp() file_writes(shelFile, cmd) os.system('sh %s' % (shelFile)) else: exec_shell(cmd, timeout=maxtime)
def __init__(self, LocalRepoPath=None): gitLog = os.path.join(GlobalVar.project_dir_log, '', 'git.log') self.gitLog = gitLog self.LocalRepoPath = LocalRepoPath ret = exec_shell('git --version') logger.debug('Git版本:%s' % ret['msg']) if not os.path.isdir(LocalRepoPath): logger.debug('创建GIT仓库根目录:%s' % LocalRepoPath) os.makedirs(LocalRepoPath)
def version(self): """ | ##@函数目的: 获得git 版本 | ##@参数说明: | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ ret = exec_shell('git --version') return ret['msg']
def unzip(zip_file_path, unzip_to_dir): """ | ##@函数目的: 解压zip文件到指定目录 | ##@参数说明:zipfilename为zip文件路径,unziptodir为解压文件后的文件目录 | ##@返回值:无 | ##@开发人:jhuang | ##@函数逻辑: """ if not os.path.isfile(zip_file_path): logger.error('待解压文件不存在:%s' % (zip_file_path)) return False logger.debug( zip_file_path + '->' + unzip_to_dir) ret = exec_shell('unzip') if 'Usage' in str(ret['msg']): logger.debug('存在unzip命令,使用unzip命令解压...') if is_linux_system(): exec_shell('unzip -o %s -d %s > %s_unzip.log 2>&1' % (zip_file_path, unzip_to_dir, zip_file_path)) else: exec_shell('unzip -o %s -d %s > %s_unzip.log' % (zip_file_path, unzip_to_dir, zip_file_path)) else: if not os.path.exists(unzip_to_dir): os.mkdir(unzip_to_dir) zfobj = zipfile.ZipFile(zip_file_path) for name in zfobj.namelist(): name = name.replace('\\', '/') if name.endswith('/'): p = os.path.join(unzip_to_dir, name[:-1]) if os.path.exists(p): # 如果文件夹存在,就删除之:避免有新更新无法复制 shutil.rmtree(p) os.mkdir(p) else: ext_filename = os.path.join(unzip_to_dir, name) ext_dir = os.path.dirname(ext_filename) if not os.path.exists(ext_dir): os.mkdir(ext_dir, 0777) outfile = open(ext_filename, 'wb') outfile.write(zfobj.read(name)) outfile.close()
def jboss_shutdown(jboss_cli_home, server_ip, jboss_admin_port, jboss_admin, jboss_admin_pwd, timeout='60000'): """ | ##@函数目的: Jboss卸载WAR包 | ##@参数说明:timeout 毫秒 对于网络差或目标服务器差的适当加大 | ##@返回值: | ##@函数逻辑:./jboss-cli.sh --connect command=:shutdown | ##@开发人:jhuang | ##@时间: """ jboss_cli = 'jboss-cli.sh' if jboss_cli_home[-1] != '/': jboss_cli_home = jboss_cli_home + '/' ret = exec_shell( '/bin/sh %sbin/%s --connect --controller=%s:%s --user=%s --password=%s --timeout=%s --command=:shutdown' % ( jboss_cli_home, jboss_cli, server_ip, jboss_admin_port, jboss_admin, jboss_admin_pwd, timeout)) return ret
def jboss_status(jboss_cli_home, server_ip, jboss_admin_port, jboss_admin, jboss_admin_pwd, timeout='60000'): """ | ##@函数目的: Jboss状态 | ##@参数说明: | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ time_start = time.time() jboss_cli = 'jboss-cli.sh' if jboss_cli_home[-1] != '/': jboss_cli_home = jboss_cli_home + '/' ret = exec_shell( 'sh %sbin/%s --connect --controller=%s:%s --user=%s --password=%s --command="deployment-info" --timeout=%s' % ( jboss_cli_home, jboss_cli, server_ip, jboss_admin_port, jboss_admin, jboss_admin_pwd, timeout)) logger.debug('获取Jboss状态用时:%s' % (time.time() - time_start)) return ret
def jboss_deploy(jboss_cli_home, server_ip, jboss_admin_port, jboss_admin, jboss_admin_pwd, warpath, timeout='60000'): """ | ##@函数目的: Jboss部署WAR包 | ##@参数说明:timeout 毫秒 对于网络差或目标服务器差的适当加大 | ##@返回值: | ##@函数逻辑: | ##@开发人:jhuang | ##@时间: """ time_start = time.time() jboss_cli = 'jboss-cli.sh' if jboss_cli_home[-1] != '/': jboss_cli_home = jboss_cli_home + '/' ret = exec_shell( 'sh %sbin/%s --connect --controller=%s:%s --user=%s --password=%s --command="deploy --force %s" --timeout=%s' % ( jboss_cli_home, jboss_cli, server_ip, jboss_admin_port, jboss_admin, jboss_admin_pwd, warpath, timeout)) logger.debug('部署用时:%s' % (time.time() - time_start)) return ret
def jboss_disable(jboss_cli_home, server_ip, jboss_admin_port, jboss_admin, jboss_admin_pwd, artifact_id, timeout='60000'): """ | ##@函数目的: 禁用WAR包 | ##@参数说明:timeout 毫秒 对于网络差或目标服务器差的适当加大 | ##@返回值: | ##@函数逻辑:./jboss-cli.sh --connect command=:shutdown | ##@开发人:jhuang | ##@时间: """ time_start = time.time() jboss_cli = 'jboss-cli.sh' if jboss_cli_home[-1] != '/': jboss_cli_home = jboss_cli_home + '/' ret = exec_shell( '/bin/sh %sbin/%s --connect --controller=%s:%s --user=%s --password=%s --timeout=%s --command="undeploy %s --keep-content"' % ( jboss_cli_home, jboss_cli, server_ip, jboss_admin_port, jboss_admin, jboss_admin_pwd, timeout, artifact_id)) logger.debug('禁用Artifact用时:%s' % (time.time() - time_start)) return ret
def kill_repeat_jboss(server_ip, jboss_admin_port, warName): logger.debug('关闭相同任务进程...') jboss_cli_kill_path = os.path.join(GlobalVar.app_dir, 'plugins', 'shell', 'jboss_cli_kill.sh') exec_shell('chmod +x %s' % (jboss_cli_kill_path)) exec_shell(jboss_cli_kill_path + ' %s:%s %s' % (server_ip, jboss_admin_port, warName))