ustbwang 发表于 2018-8-11 12:47:56

python 多线程上线

  楼主单位,之前svn上线非常慢,因为前台要有30台resin之多,分为3个业务,但每次上线30台服务器都要更新,所以时间是越来越慢,之前的上线脚本是我用shell编写的,要一台一台更新,可想而知更新速度慢的不行不行的,而且脚本非常长,不便于管理,楼主是这么想的,因为分为3个业务,每次更新3台resin,一个业务一台,多线程去跑,那就要节省多少时间了。话不多说 奉上脚本
  1. /opt/duoyongtu/shangxian.txt
  192.168.3.22
  192.168.3.23
  192.168.3.30
  192.168.3.31
  192.168.3.32
  2.上线脚本
  #!/usr/bin/python
  import threading
  import paramiko
  def shangxian():
  a=open("/opt/duoyongtu/shangxian.txt","r+")
  b=a.readlines()
  return b
  g=shangxian()
  version=""
  ggg=len(g)
  f=[]
  def caozuo(hostname,port,username,pkey):
  key=paramiko.RSAKey.from_private_key_file(pkey)
  s=paramiko.SSHClient()
  s.load_system_host_keys()
  s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  s.connect(hostname,port,username,pkey=key)
  global liebiao1
  liebiao1=[]
  test1="ifconfig"
  liebiao1.append(test1)
  test2="hostname"
  liebiao1.append(test2)
  for ii in liebiao1:
  stdin,stdout,stderr=s.exec_command(ii)
  print stdout.read()
  if __name__ == '__main__':
  ff=[]
  while True:
  if g:
  ff.append(g.pop(0))
  if len(ff) == 3:
  for i in ff:
  hostname=i
  port=22
  username="root"
  pkey='/root/.ssh/id_rsa'
  aa=threading.Thread(target=caozuo,args=(hostname,port,username,pkey))
  aa.start()
  ff=[]
  elif len(g) < 3:
  for i in ff:
  hostname=i
  port=22
  username="root"
  pkey='/root/.ssh/id_rsa'
  aa=threading.Thread(target=caozuo,args=(hostname,port,username,pkey))
  aa.start()
  ff=[]
  else:
  break
  因为还是在测试阶段,命令都是些简单的命令
  功能还不全,但是已经可以满足简单的上线需求,但是例如自动化上线,svn上线的一些bug问题还没有解决,楼主会在第二版解决这些问题
页: [1]
查看完整版本: python 多线程上线