rov_server_update.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import time
  2. import requests
  3. import utils
  4. import logging
  5. import os
  6. slb_id = 'lb-bp1werfophtsjzfr76njm'
  7. # 修改负载均衡权限
  8. slb_client_params = {
  9. 'access_key_id': 'LTAIuPbTPL3LDDKN',
  10. 'access_key_secret': 'ORcNedKwWuwVtcq4IRFtUDZgS0b1le',
  11. 'region_id': 'cn-hangzhou'
  12. }
  13. # 购买机器权限
  14. create_client_params = {
  15. 'access_key_id': 'LTAI4GBWbFvvXoXsSVBe1o9f',
  16. 'access_key_secret': 'kRAikWitb4kDxaAyBqNrmLmllMEDO3',
  17. 'region_id': 'cn-hangzhou'
  18. }
  19. # 机器配置
  20. instance_config = {
  21. # 使用的镜像信息
  22. 'image_id': 'm-bp1e5jx8eqhq22l91xw7',
  23. # 设置实例规格
  24. 'instance_type': 'ecs.ic5.xlarge',
  25. # 选择的交换机
  26. 'vswitch_id': 'vsw-bp19lpjwtc6j0p0m9mdc2',
  27. # 当前VPC类型的安全组
  28. 'security_group_id': 'sg-bp1irhrkr4vfj272hk4y',
  29. # 硬盘的大小,单位:G
  30. 'disk_size': '200',
  31. # 服务器命名
  32. 'instance_name': 'ESS-rov-server-[1,2]',
  33. # 服务器所在区域
  34. 'zone_id': 'cn-hangzhou-h',
  35. # 磁盘类型:云盘
  36. 'disk_category': 'cloud_efficiency',
  37. # 密钥
  38. 'key_pair_name': 'stuuudy'
  39. }
  40. # 服务启动脚本
  41. start_sh_dir = os.path.dirname(os.path.realpath(__file__))
  42. start_sh_filename = 'rov_server_start.sh'
  43. with open(file=os.path.join(start_sh_dir, start_sh_filename), mode='r', encoding='utf-8') as rf:
  44. file_content = rf.read()
  45. start_sh = {
  46. 'target_dir': '/home/piaoquan_server_sh',
  47. 'name': start_sh_filename,
  48. 'content': file_content,
  49. }
  50. def server_health_check(instance_id, health_check_url):
  51. """
  52. 服务健康检查
  53. :param instance_id: instanceId
  54. :param health_check_url: 服务健康检查url
  55. :return:
  56. """
  57. ip_address = get_ip_address(client=client, instance_id=instance_id)
  58. while True:
  59. health_url = f"http://{ip_address}:5001/healthcheck"
  60. try:
  61. http_code = requests.get(health_check_url).status_code
  62. except:
  63. logging.info("images is downloading")
  64. http_code = 0
  65. if http_code == 200:
  66. break
  67. else:
  68. time.sleep(20)
  69. def ess_instance(create_client, slb_client, ess_count):
  70. """
  71. 扩容机器并运行新服务
  72. :param create_client: 购买机器客户端连接
  73. :param slb_client: 修改负载均衡权限
  74. :param ess_count: 扩容数量
  75. :return:
  76. """
  77. # 1. 购买机器并启动
  78. ess_instance_ids = utils.create_multiple_instances(
  79. amount=ess_count,
  80. client=create_client,
  81. **instance_config,
  82. )
  83. time.sleep(60)
  84. # 2. 发送启动脚本到机器上
  85. utils.send_file_to_ecs(client=create_client, instance_id_list=ess_instance_ids, **start_sh)
  86. # 3. 启动服务
  87. server_start_sh = os.path.join(start_sh['target_dir'], start_sh['name'])
  88. server_start_commend = f"sh {server_start_sh}"
  89. utils.run_command(client=create_client, instance_ids=ess_instance_ids, command=server_start_commend)
  90. # 4. 探活
  91. health_instance_ids = []
  92. def main():
  93. slb_client = utils.connect_client(access_key_id=slb_client_params['access_key_id'],
  94. access_key_secret=slb_client_params['access_key_secret'],
  95. region_id=slb_client_params['region_id'])
  96. create_client = utils.connect_client(access_key_id=create_client_params['access_key_id'],
  97. access_key_secret=create_client_params['access_key_secret'],
  98. region_id=create_client_params['region_id'])
  99. # 1. 获取slb下所有机器
  100. online_instance_ids = utils.get_instance_ids(client=slb_client, slb_id=slb_id)
  101. online_instance_count = len(online_instance_ids)
  102. logging.info(f"online instance count: {online_instance_count}.")
  103. logging.info(f"online instance ids: {online_instance_ids}")
  104. # 2. 扩容机器并启动新服务 扩容数量:线上机器数量/2
  105. ess_instance_count = online_instance_count / 2
  106. logging.info(f"ess instance count: {ess_instance_count}")