12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import time
- import utils
- import logging
- import os
- slb_id = 'lb-bp1werfophtsjzfr76njm'
- # 修改负载均衡权限
- slb_client_params = {
- 'access_key_id': 'LTAIuPbTPL3LDDKN',
- 'access_key_secret': 'ORcNedKwWuwVtcq4IRFtUDZgS0b1le',
- 'region_id': 'cn-hangzhou'
- }
- # 购买机器权限
- create_client_params = {
- 'access_key_id': 'LTAI4GBWbFvvXoXsSVBe1o9f',
- 'access_key_secret': 'kRAikWitb4kDxaAyBqNrmLmllMEDO3',
- 'region_id': 'cn-hangzhou'
- }
- # 机器配置
- instance_config = {
- # 使用的镜像信息
- 'image_id': 'm-bp1e5jx8eqhq22l91xw7',
- # 设置实例规格
- 'instance_type': 'ecs.ic5.xlarge',
- # 选择的交换机
- 'vswitch_id': 'vsw-bp19lpjwtc6j0p0m9mdc2',
- # 当前VPC类型的安全组
- 'security_group_id': 'sg-bp1irhrkr4vfj272hk4y',
- # 硬盘的大小,单位:G
- 'disk_size': '200',
- # 服务器命名
- 'instance_name': 'ESS-rov-server-[1,2]',
- # 服务器所在区域
- 'zone_id': 'cn-hangzhou-h',
- # 磁盘类型:云盘
- 'disk_category': 'cloud_efficiency',
- # 密钥
- 'key_pair_name': 'stuuudy'
- }
- # 服务启动脚本
- start_sh_dir = os.path.dirname(os.path.realpath(__file__))
- start_sh_filename = 'rov_server_start.sh'
- with open(file=os.path.join(start_sh_dir, start_sh_filename), mode='r', encoding='utf-8') as rf:
- file_content = rf.read()
- start_sh = {
- 'target_dir': '/home/piaoquan_server_sh',
- 'name': start_sh_filename,
- 'content': file_content,
- }
- def ess_instance(create_client, slb_client, ess_count):
- """
- 扩容机器并运行新服务
- :param create_client: 购买机器客户端连接
- :param slb_client: 修改负载均衡权限
- :param ess_count: 扩容数量
- :return:
- """
- # 1. 购买机器并启动
- ess_instance_ids = utils.create_multiple_instances(
- amount=ess_count,
- client=create_client,
- **instance_config,
- )
- time.sleep(60)
- # 2. 发送启动脚本到机器上
- utils.send_file_to_ecs(client=create_client, instance_id_list=ess_instance_ids, **start_sh)
- # 3. 启动服务
- server_start_sh = os.path.join(start_sh['target_dir'], start_sh['name'])
- server_start_commend = f"sh {server_start_sh}"
- utils.run_command(client=create_client, instance_ids=ess_instance_ids, command=server_start_commend)
- def main():
- slb_client = utils.connect_client(access_key_id=slb_client_params['access_key_id'],
- access_key_secret=slb_client_params['access_key_secret'],
- region_id=slb_client_params['region_id'])
- create_client = utils.connect_client(access_key_id=create_client_params['access_key_id'],
- access_key_secret=create_client_params['access_key_secret'],
- region_id=create_client_params['region_id'])
- # 1. 获取slb下所有机器
- online_instance_ids = utils.get_instance_ids(client=slb_client, slb_id=slb_id)
- online_instance_count = len(online_instance_ids)
- logging.info(f"online instance count: {online_instance_count}.")
- logging.info(f"online instance ids: {online_instance_ids}")
- # 2. 扩容机器并启动新服务 扩容数量:线上机器数量/2
- ess_instance_count = online_instance_count / 2
- logging.info(f"ess instance count: {ess_instance_count}")
|