rov_server_update.py 3.3 KB

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