首页 > linux > 多线程更新线上服务器

多线程更新线上服务器

2012年11月15日 eric 发表评论 阅读评论

好久没有更新过blog了,把自己写的一个多线程更新的脚本共享给大家,如果有什么疑问过着建议欢迎留言,以后我会把写的一个简单的通过页面更新线上服务的程序open 出来。

写这个脚本当然要说一下场景:
1,有一批web服务器,上面跑着相同的应用,目录结构一样。
2,更新时候注定要备份、删除缓存(不备份小心死人,删除缓存必要啊,不然新上的代码会不生效)
3,更新时必然我们要记录日志,是否连接服务器超时、备份时候成功、更新是否成功。

好了,有了结构,有了日志,有了备份,那么就不怕升级失败了。

先说代码为解决的一个问题,因为要开启多线程,所以日志记录的有点混乱,比如 ip 从 192.166.1.2 更新到 192.168.1.254 ,记录的日志可能第一行记录是 1.2 那么第二行可能记录 1.6 ,不过日志肯定每个服务的连接和返回日志都记录了。

脚本代码主要引入了 paramiko 类,有人说你为何不用 fabric,大哥我用了,他非要执行命令,结合 shell 来执行还是不错的,但是哥我测试想用单个脚本文件执行,我也做过测试,设置 env 环境变量后的确可以,但是比较慢,而且相当麻烦。所以哥我选择了 paramiko 用了一段时间,很稳定速度不错。

废话不多说,那么我们来看下代码:

1 #!/usr/bin/python2.6
  2 #-*- coding:utf-8 -*-
  3 import paramiko
  4 import commands
  5 import pexpect
  6 import  MySQLdb
  7 import logging
  8 import threading
  9 import Queue
 10 import csv
 11 import time
 12
 13 logging.basicConfig(level=logging.INFO,
 14                     format='%(asctime)s %(filename)s %(levelname)s %(message)s',
 15                     datefmt='%a,%d %b %Y %H %M:%s',
 16                     filename='paramiko.log',
 17                     filemode='w')
 18
 19 ## 创建 SSH 连接类
 20 class SSHClient(paramiko.SSHClient):
 21         def call(self, command, bufsize=-1):
 22                 chan = self._transport.open_session()
 23                 chan.exec_command(command)
 24                 stdin = chan.makefile('wb', bufsize)
 25                 stdout = chan.makefile('rb', bufsize)
 26                 stderr = chan.makefile_stderr('rb', bufsize)
 27 ## 获取命令放回状态代码
 28                 status = chan.recv_exit_status()
 29                 return stdin, stdout, stderr, status
 30
 31 ## 创建 rsync 连接类
 32 class rsyncThread():
 33    def __init__(self,host,passwd,code):
 34      self.host = host
 35      self.passwd = passwd
 36      self.code = code
 37    def run(self):
 38        ssh = pexpect.spawn("/usr/bin/rsync -a -c -v --delete %s -e '%s' %s:%s" % (self.code,'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null',self.host,self.code),timeout=600)
 39        log=open('test.log','a')
 40        ssh.logfile=log
 41        i = ssh.expect(['(yes/no)','password:',pexpect.TIMEOUT,pexpect.EOF])
 42        if i == 0:
 43          ssh.sendline('yes')
 44          ssh.expect('password:')
 45          ssh.sendline(self.passwd)
 46        elif i == 1:
 47          ssh.sendline(self.passwd)
 48        elif i == 2:
 49          return False
 50        elif i == 3:
 51          return False
 52        ssh.expect(pexpect.EOF)
 53
 54 class mysqlimport():
 55     def __init__(self,host):
 56       self.host = host
 57     def run(self):
 58       imp = commands.getstatusoutput('mysql -h %s -uuser -p123456 -S /tmp/mysql.sock< /tmp/sqlquery.sql' % self.host)
 59       self.status = imp[0]
 60       self.msg = imp[1]
 61
 62 class sshcommand():
 63      def __init__(self,host,passwd):
 64        self.host = host
 65        self.passwd = passwd
 66      def run(self):
 67        sc = SSHClient()
 68        sc.load_system_host_keys()
 69        sc._policy = paramiko.AutoAddPolicy()
 70        try:
 71           sc.connect(self.host, 22, 'root', self.passwd ,timeout=5000)
 72           stdin, stdout, stderr, status = sc.call('/data/htdoc/tools/backup.sh')
 73
 74           sc.close()
 75           message = stdout.read()
 76           if status == 0:
 77             logging.info('%s  %s' % (self.host,message))
 78           elif status == 1:
 79             logging.info('%s  %s' % (self.host,message))
 80           elif status == 2:
 81             logging.info('%s  %s' % (self.host,message))
 82           elif status == 3:
 83             logging.info('%s  %s' % (self.host,message))
 84           else:
 85             logging.info('%s  %s' % (self.host,message))
 86        except:
 87           logging.info('%s Connection timed out' % self.host)
 88
 89 class worker(threading.Thread):
 90    def __init__(self,queue):
 91       threading.Thread.__init__(self)
 92       self.queue = queue
 93    def run(self):
 94       while True:
 95         line = self.queue.get()
 96         ipadd,passwd,code_path=line[0],line[1],line[2]
 97         #ipadd,passwd=line[0],line[1]
 98         #print ipadd
 99         sc = sshcommand(ipadd,passwd)
100         sc.run()
101         ry = rsyncThread(ipadd,passwd,code_path)
102         ry.run()
103#         myimport =  mysqlimport(ipadd)
104 #        myimport.run()
105 #        print myimport.status
106 #        status = sc.status
107 #        if status == 0:
108 #          logging.warning(message)
109         self.queue.task_done()
110
111
112 if __name__ == '__main__':
113 # 读取主机列表
114    reader = csv.reader(file('host.txt'))
115 #启用队列函数
116    queue = Queue.Queue()
117    out_queue = Queue.Queue()
118 #开启 5 个线程
119    for i in range(5):
120      t = worker(queue)
121      t.setDaemon(True)
122      t.start()
123 # 把主机列表放入队列
124    for line in reader:
125      queue.put(line)
126    queue.join()
127 #     print "Elapsed Time: %s" % (time.time() - start)
128

host.txt 内容为
# ip passwd web htdoc
192.168.1.2,password,/data/htdoc/

转载请注明:文章转载自:Eric's linux and nginx! (http://www.nginxs.com)
本文地址:http://www.nginxs.com/linux/497.html

分类: linux 标签: , ,
  1. 虎子
    2012年11月15日15:05 | #1

    NB

  2. 2017年5月13日11:20 | #2

    My brother recommended I may like this website.

    He was totally right. This publish actually made my
    day. You can not imagine just how so much time I had spent for this info!
    Thank you!

  3. 2017年5月19日03:18 | #3

    старинка

    подошел к маме сзади ххх и порно рассказы младшая сестра соска

  4. 2017年5月19日18:54 | #4

    What i do not realize is actually how you are no longer actually
    much more smartly-preferred than you may be right now. You’re very intelligent.
    You understand therefore considerably in terms of this matter,
    produced me personally believe it from a lot of varied angles.
    Its like men and women don’t seem to be interested except it’s one thing to do with Girl gaga!
    Your personal stuffs outstanding. Always care for it up!

  5. 2017年5月22日02:39 | #5

    Такова жизнь. Ничего не поделаешь.

    секс розкази з тьотями а также секс расказы дядя

  6. 2017年5月28日12:38 | #6

    Excellent way of describing, and fastidious piece of writing to get information about my presentation subject matter,
    which i am going to convey in university.

  7. DorisTiems
    2017年7月15日05:49 | #7

    Revolutional update of SEO/SMM package “XRumer 16.0 + XEvil 3.0″:
    captchas breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another types of captcha,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

    Interested? There are a lot of demo videos about XEvil in YouTube.
    See you later ;)

    XRumer201707

  8. DorisTiems
    2017年7月17日16:02 | #8

    Revolutional update of SEO/SMM software “XRumer 16.0 + XEvil 3.0″:
    captcha solving of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another subtypes of captcha,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

    Interested? You can find a lot of introducing videos about XEvil in YouTube.
    See you later!

    XRumer201707

  9. DorothyRuirm
    2017年7月18日03:33 | #9

    Revolutional update of SEO/SMM software “XRumer 16.0 + XEvil”:
    captchas recognition of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another types of captcha,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms.

    Interested? You can find a lot of impessive videos about XEvil in YouTube.
    See you later!

    XRumer20170717

  10. DorothyRuirm
    2017年7月19日04:54 | #10

    Absolutely NEW update of SEO/SMM package “XRumer 16.0 + XEvil 3.0″:
    captchas solution of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another subtypes of captcha,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

    Interested? There are a lot of introducing videos about XEvil in YouTube.
    Good luck ;)

    XRumer20170718

  11. DorothyRuirm
    2017年7月19日10:22 | #11

    Revolutional update of SEO/SMM package “XRumer 16.0 + XEvil 3.0″:
    captchas regignizing of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another subtypes of captchas,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

    Interested? You can find a lot of introducing videos about XEvil in YouTube.
    Good luck!

    XRumer20170718

  12. ArleneBix
    2017年7月22日08:20 | #12

    Absolutely NEW update of SEO/SMM package “XRumer 16.0 + XEvil 3.0″:
    captchas regignizing of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another size-types of captchas,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

    Interested? There are a lot of introducing videos about XEvil in YouTube.
    Good luck!

    XRumer20170721

  13. ArleneBix
    2017年7月22日17:42 | #13

    Revolutional update of SEO/SMM package “XRumer 16.0 + XEvil 3.0″:
    captchas recognition of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another types of captchas,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

    Interested? There are a lot of introducing videos about XEvil in YouTube.
    See you later!

    XRumer20170721

  14. ArleneBix
    2017年7月22日23:05 | #14

    Absolutely NEW update of SEO/SMM package “XRumer 16.0 + XEvil”:
    captchas solving of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another subtypes of captchas,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

    Interested? You can find a lot of introducing videos about XEvil in YouTube.
    Good luck ;)

    XRumer20170721

  15. ArleneBix
    2017年7月23日04:18 | #15

    Absolutely NEW update of SEO/SMM package “XRumer 16.0 + XEvil 3.0″:
    captcha recognition of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another subtypes of captcha,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms.

    Interested? You can find a lot of demo videos about XEvil in YouTube.
    Good luck ;)

    XRumer20170721

  16. 2017年7月25日20:47 | #16

    Dissertation Writing Service – EssayErudite.com

    Our https://essayerudite.com/dissertation-writing-service/ provides a full-scale writing assistance accessible online 24/7.
    Apart from many other writing companies, we are rather picky when forming a professional staff of experts.
    Moreover, we try to make our every customer feel safe and pleased with the service.

  17. 2017年7月25日21:38 | #17

    Write My Paper – EssayErudite.com

    Looking for an expert to write my paper for you? You are at the right place.
    Providing superior writing service appears to be our main specialization and passion.
    Our website is the best destination for every English-speaking student who calls for assistance when handling his or her daily academic tasks.

  18. 2017年7月25日21:40 | #18

    Write My Essay – EssayErudite.com

    Fed up of typing “who can write my essay” in the search bar?
    Would you like to have a reliable helper always by your side?
    Our website will come as an excellent solution to write my essay

  19. 2017年7月26日21:16 | #19

    EssayErudite.com is the web’s leading provider of quality and professional academic writing.
    When it comes to essay writing, an in-depth research is a big deal. Our experienced writers are professional in many fields of knowledge so that they can assist you with virtually any academic task. We deliver papers of different types: essays, theses, book reviews, case studies, etc.
    We have thousands of satisfied customers who have already recommended us to their friends. Why not follow their example and place your order today?

  20. 2017年7月31日17:16 | #20

    Предлагаю http://safe-water.ru – пергидроль в Севастополе

  21. 2017年7月31日18:54 | #21

    Хорошо отдохнуть можно тут: http://www.jbereg.ru – Детский отдых в Крыму

  22. 2017年8月1日00:20 | #22

    http://www.copic-sketch.ru – онлайн обучение скетчингу

  23. 2017年8月1日06:31 | #23

    Day ago i read some cool info about Omega 3 stuff and now i very wanna buy it. But i need a quality, some premium omega 3 fish oil. I’ve got this carlson omega 3 . What do you think about this Carlson brand?

  24. 2017年8月1日11:06 | #24

    Good day I am so grateful I found your web site,
    I really found you by mistake, while I was researching on Digg for
    something else, Anyways I am here now and would just like to say many thanks for a incredible post and a all round thrilling blog (I also love the theme/design),
    I don’t have time to go through it all at the moment but I have
    saved it and also added your RSS feeds, so when I have time I will be back
    to read a great deal more, Please do keep up the superb job.

  25. 2017年8月1日22:42 | #25

    Excellent way of explaining, and good article to take facts about my presentation subject, which i am going to convey in university.

  26. 2017年8月1日23:09 | #26

    Day ago i read some shocking info about Omega 3 foods and now i very wanna buy it. But i’m not 100% sure which benefits will i have. I’ve found this omega 3 info , and now i want to get it with good price. Any idea?

  27. 2017年8月2日03:22 | #27

    This post is actually a nice one it assists new net visitors, who are wishing for blogging.

  28. 2017年8月2日04:04 | #28

    Highly descriptive blog, I loved that bit.

    Will there be a part 2?

  29. 2017年8月2日14:18 | #29

    Day ago i read some shocking info about Omega 3 foods and now i very wanna get it. But i’m not absolutely sure which benefits will i have. I’ve got this omega 3 info , and currently i want to purchase it with lowest price. Any idea?

  30. 2017年8月2日15:12 | #30

    Thank you for every other wonderful post. Where else may anybody get that kind of info
    in such a perfect means of writing? I’ve a presentation subsequent
    week, and I am on the search for such info.

  31. 2017年8月2日15:54 | #31

    Pretty! This was a really wonderful article. Thank you for supplying this information.

  32. 2017年8月2日17:29 | #32

    That is a good tip especially to those new to the blogosphere.
    Simple but very accurate information… Thank you
    for sharing this one. A must read article!

  33. 2017年8月3日01:32 | #33

    My brother suggested I might like this website. He used
    to be totally right. This put up truly made my day. You cann’t believe simply how so much time I had spent for this info!
    Thank you!

  34. 2017年8月3日04:28 | #34

    Yesterday i read some cool info about Omega 3 foods and now i very wanna buy it. But i’m not absolutely sure which benefits will i have. I’ve got this omega 3 info , and now i want to buy it with lowest price. Any idea?

  35. 2017年8月3日05:27 | #35

    It’s going to be end of mine day, but before ending I am reading this
    wonderful paragraph to improve my know-how.

  36. 2017年8月3日12:48 | #36

    Appreciate this post. Let me try it out.

  37. 2017年8月3日15:33 | #37

    This is very interesting, You’re a very skilled
    blogger. I have joined your feed and look forward
    to seeking more of your wonderful post. Also, I’ve shared your website in my social networks!

  38. 2017年8月3日19:16 | #38

    Yesterday i got some shocking info about Omega 3 foods and now i really wanna get it. But i’m not 100% sure which pluses will i have. I’ve found this omega 3 info , and currently i wanna purchase it with lowest price. Any idea?

  39. 2017年8月3日19:30 | #39

    Hi, I read your blog like every week. Your story-telling style is awesome, keep up the good work!

  40. 2017年8月3日20:18 | #40

    Hey there, You have done a fantastic job. I will definitely digg it and personally suggest to my friends.

    I am confident they’ll be benefited from this site.

  41. 2017年8月4日07:15 | #41

    Do you have a spam issue on this website; I
    also am a blogger, and I was wondering your situation; many of us
    have developed some nice procedures and we are looking to exchange methods with other folks, be sure
    to shoot me an email if interested.

  42. 2017年8月4日08:57 | #42

    Yesterday i got some interesting info about Omega 3 foods and now i really wanna get it. But i’m not absolutely sure which pluses will i have. I’ve found this omega 3 info , and now i wanna buy it with lowest price. Any idea?

  43. 2017年8月4日14:57 | #43

    I really like your blog.. very nice colors & theme.
    Did you design this website yourself or did you hire someone to do it for you?
    Plz answer back as I’m looking to create my own blog and would like to know where u got this from.
    thanks a lot

  44. 2017年8月4日22:02 | #44

    You really make it appear really easy with your presentation however I in finding this matter to
    be really one thing which I feel I would never understand.
    It kind of feels too complex and very huge for me.

    I am having a look ahead for your next publish, I’ll try
    to get the grasp of it!

  45. 2017年8月4日22:46 | #45

    Day ago i read some cool info about Omega 3 foods and now i very wanna get it. But i’m not really sure which benefits will i have. I’ve found this omega 3 info , and currently i wanna purchase it with best price. Any idea?

  46. Carolynmal
    2017年8月4日23:49 | #46

    Absolutely NEW update of SEO/SMM package “XRumer 16.0 + XEvil”:
    captchas breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another size-types of captchas,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms.

    Interested? There are a lot of introducing videos about XEvil in YouTube.
    You read it – then IT WORKS!
    Good luck!

    XRumer201708

  47. 2017年8月5日00:18 | #47

    This is my first time pay a quick visit at here and i am genuinely impressed to
    read everthing at alone place.

  48. 2017年8月5日04:33 | #48

    Right now it sounds like Drupal is the preferred blogging
    platform out there right now. (from what I’ve read) Is that what you are using on your blog?

  49. 2017年8月5日04:38 | #49

    I do consider all of the ideas you’ve introduced on your post.
    They’re very convincing and will definitely work.
    Still, the posts are too short for novices. Could you please extend them a bit from subsequent time?
    Thanks for the post.

  50. Carolynmal
    2017年8月5日04:55 | #50

    Absolutely NEW update of SEO/SMM package “XRumer 16.0 + XEvil”:
    captcha breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex,
    and more than 8400 another subtypes of captchas,
    with highest precision (80..100%) and highest speed (100 img per second).
    You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms.

    Interested? You can find a lot of introducing videos about XEvil in YouTube.
    You read it – then IT WORKS!
    Good luck ;)

    XRumer201708

评论分页
1 2 3 ... 7 497
订阅评论
5+3= (必填)