longvideoapi_update_new.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/u:sr/bin/env python
  2. #coding=utf-8
  3. #edite panwang
  4. import docker
  5. import sys
  6. import requests
  7. import json
  8. import queue
  9. import threading
  10. from aliyunsdkcore import client
  11. from aliyunsdkess.request.v20140828.DescribeScalingInstancesRequest import DescribeScalingInstancesRequest
  12. from aliyunsdkecs.request.v20140526.DescribeNetworkInterfacesRequest import DescribeNetworkInterfacesRequest
  13. from aliyunsdkcore.request import CommonRequest
  14. from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
  15. from aliyunsdkslb.request.v20140515.DescribeLoadBalancerAttributeRequest import DescribeLoadBalancerAttributeRequest
  16. import time
  17. AccessKey = 'LTAIuPbTPL3LDDKN'
  18. AccessSecret = 'ORcNedKwWuwVtcq4IRFtUDZgS0b1le'
  19. RegionId = 'cn-hangzhou'
  20. clt = client.AcsClient (AccessKey, AccessSecret, RegionId)
  21. #stress
  22. #slb_id = 'lb-bp12usgpoh04qsa6pw42r'
  23. #longvidleoapi
  24. #id_2 = 'lb-bp1lktzqe3mksoj8whjdf'
  25. id_2 = 'lb-bp1qk9mkvjtundlzz7owm'
  26. # id_3 = 'lb-bp1by407qxe4y7bkvye27'
  27. # id_4 = 'lb-bp1akia6vlguiwg4znaq8'
  28. id_5 = 'lb-bp1pj2v06ladvgftgxcp0'
  29. id_6 = 'lb-bp1y63rnrb2e64whryghz'
  30. class MyThread (threading.Thread):
  31. def __init__(self, func):
  32. threading.Thread.__init__ (self)
  33. self.func = func
  34. def run(self):
  35. self.func ()
  36. #健康检查
  37. def checkHealth(ipadd):
  38. while True:
  39. health_url = 'http://%s:8080/longvideoapi/test' %(ipadd)
  40. header = {"Content-Type":"application/json"}
  41. try:
  42. health_code = requests.get(health_url).status_code
  43. except Exception as e:
  44. continue
  45. if health_code == 200:
  46. return False
  47. #服务更新完之后逐步修改服务器的权重值,直接加载100会出现502。权重值每次增加10,每2s修改一次
  48. def setInstanceWeightProcess(instance_id):
  49. for i in range(1,6):
  50. weight = i*20
  51. setWeight(id_2,instance_id,weight)
  52. # setWeight(id_3,instance_id,weight)
  53. # setWeight(id_4,instance_id,weight)
  54. setWeight(id_5,instance_id,weight)
  55. setWeight(id_6,instance_id,weight)
  56. time.sleep(5)
  57. #设置权重 instance_id :服务器id,weight:权重值
  58. def setWeight(slb_id,instance_id ,weight):
  59. BackendServers = [{"ServerId": instance_id, "Weight": weight}]
  60. request = CommonRequest ()
  61. request.set_accept_format ('json')
  62. request.set_domain ('slb.aliyuncs.com')
  63. request.set_version ('2014-05-15')
  64. request.set_method ('POST')
  65. request.set_action_name ('SetBackendServers')
  66. request.add_query_param ('BackendServers', BackendServers)
  67. request.add_query_param ('LoadBalancerId', slb_id)
  68. try:
  69. response = clt.do_action (request)
  70. except Exception as e:
  71. print (e)
  72. #获取slb下所有服务器信息
  73. def getInstanceId(slb_id):
  74. request = DescribeLoadBalancerAttributeRequest()
  75. request.set_accept_format('json')
  76. request.set_LoadBalancerId(slb_id)
  77. response = clt.do_action_with_exception(request)
  78. return json.loads (response)
  79. #获取实例IP地址
  80. def getIpadd(instance_id):
  81. request = DescribeNetworkInterfacesRequest()
  82. request.set_accept_format('json')
  83. request.set_InstanceId(instance_id)
  84. response = clt.do_action_with_exception(request)
  85. request_content = json.loads(response)
  86. IpAddr = request_content['NetworkInterfaceSets']['NetworkInterfaceSet'][0]['PrivateIpAddress']
  87. return IpAddr
  88. #更新服务
  89. def update():
  90. time.sleep(10)
  91. global success_count
  92. # apps = 'vlogapi'
  93. apps = 'longvideoapi'
  94. version = sys.argv[1]
  95. # registry = 'registry-vpc.cn-hangzhou.aliyuncs.com/stuuudy/vlogapi:{}'.format(version)
  96. registry = 'registry.piaoquantv.com/piaoquan/longvideoapi:{}'.format(version)
  97. while not q2.empty():
  98. instance_id = q2.get()
  99. ipadd = getIpadd(instance_id)
  100. print("服务器信息:" + "%s/%s" %(instance_id, ipadd))
  101. client = docker.DockerClient(base_url='tcp://%s:2375' %(ipadd),timeout=60 )
  102. try:
  103. #更新前移除旧的容器
  104. id = client.containers.get(apps)
  105. id.remove(force = True)
  106. except Exception as e:
  107. print("容器不存在或者无法删除当前容器")
  108. try:
  109. #更新前删除旧的镜像
  110. images = client.images.list(all)
  111. for i in range(len(images)):
  112. if images[i]:
  113. client.images.remove(force=True, image=images[i].tags[0])
  114. time.sleep(2)
  115. #images = client.images.get(registry)
  116. #client.images.remove(force=True, image=registry)
  117. except Exception as e:
  118. print(e)
  119. print("镜像不存在,无法获取到镜像ID")
  120. sys.exit(0)
  121. try:
  122. #登录镜像仓库
  123. # client.login(username='stuuudys' ,password='Qingqu@2019', registry='registry-vpc.cn-hangzhou.aliyuncs.com')
  124. client.login(username='admin' ,password='Harbor12345', registry='registry.piaoquantv.com')
  125. #启动一个容器
  126. client.containers.run(registry, detach = True, cap_add = 'SYS_PTRACE', network_mode = 'host', name = apps,volumes={'/datalog/': {'bind': '/datalog/', 'mode': 'rw'}})
  127. checkHealth(ipadd)
  128. print("%s :权重修改中......" %(ipadd))
  129. setInstanceWeightProcess(instance_id)
  130. success_count = success_count + 1
  131. print("更新进度" + "%s/%s" %(success_count, total))
  132. except Exception as e:
  133. raise e
  134. sys.exit()#容器启动失败立即退出更新
  135. if __name__ == '__main__':
  136. #更新完成计数
  137. success_count = 0
  138. threads = []
  139. thread_num = 1
  140. res = getInstanceId (id_2)
  141. #slb下服务器总数
  142. total = len(res["BackendServers"]["BackendServer"])
  143. q1 = queue.Queue()
  144. q2 = queue.Queue()
  145. if res["BackendServers"]["BackendServer"]:
  146. for i in range ((len (res["BackendServers"]["BackendServer"]))):
  147. instance_id = res["BackendServers"]["BackendServer"][i]["ServerId"]
  148. q1.put(instance_id)
  149. while not q1.empty():
  150. for i in range (thread_num ):
  151. if q1.empty():
  152. break
  153. instance_id = q1.get ()
  154. setWeight (id_2,instance_id, 0)
  155. # setWeight (id_3,instance_id, 0)
  156. # setWeight (id_4,instance_id, 0)
  157. setWeight (id_5,instance_id, 0)
  158. setWeight (id_6,instance_id, 0)
  159. q2.put(instance_id)
  160. for i in range (thread_num ):
  161. # 开启线程
  162. thread = MyThread (update)
  163. thread.start ()
  164. threads.append(thread)
  165. for thread in threads:
  166. thread.join ()