utils.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. import logging
  2. import json
  3. import sys
  4. import time
  5. from asyncio import wait_for
  6. import requests
  7. import asyncio
  8. import time
  9. from alibabacloud_tea_util.client import Client as UtilClient
  10. from aliyunsdkcore.client import AcsClient
  11. from aliyunsdkecs.request.v20140526.RunInstancesRequest import RunInstancesRequest
  12. from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
  13. from aliyunsdkecs.request.v20140526.DescribeNetworkInterfacesRequest import DescribeNetworkInterfacesRequest
  14. from aliyunsdkecs.request.v20140526.RunCommandRequest import RunCommandRequest
  15. from aliyunsdkecs.request.v20140526.SendFileRequest import SendFileRequest
  16. from aliyunsdkecs.request.v20140526.StopInstancesRequest import StopInstancesRequest
  17. from aliyunsdkecs.request.v20140526.DeleteInstancesRequest import DeleteInstancesRequest
  18. from aliyunsdkecs.request.v20140526.DescribeInstanceStatusRequest import DescribeInstanceStatusRequest
  19. from aliyunsdkecs.request.v20140526.ModifySecurityGroupRuleRequest import ModifySecurityGroupRuleRequest
  20. from alibabacloud_alb20200616.client import Client as Alb20200616Client
  21. from alibabacloud_tea_openapi import models as open_api_models
  22. from alibabacloud_alb20200616 import models as alb_models
  23. from alibabacloud_alb20200616 import models as alb_20200616_models
  24. from alibabacloud_tea_util import models as util_models
  25. logging.basicConfig(level=logging.INFO,
  26. format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
  27. datefmt='%a, %d %b %Y %H:%M:%S')
  28. def send_msg_to_feishu(webhook, key_word, msg_text):
  29. """发送消息到飞书"""
  30. headers = {'Content-Type': 'application/json'}
  31. payload_message = {
  32. "msg_type": "text",
  33. "content": {
  34. "text": '{}: {}'.format(key_word, msg_text)
  35. }
  36. }
  37. response = requests.request('POST', url=webhook, headers=headers, data=json.dumps(payload_message))
  38. logging.info(response.text)
  39. def connect_client(access_key_id, access_key_secret, region_id):
  40. """
  41. 初始化账号,连接客户端
  42. :param access_key_id: access key Id, type-string
  43. :param access_key_secret: access key secret, type-string
  44. :param region_id: region_id
  45. :return: clt
  46. """
  47. try:
  48. clt = AcsClient(ak=access_key_id, secret=access_key_secret, region_id=region_id)
  49. return clt
  50. except Exception as e:
  51. # 失败,记录报错信息,发送通知,停止并退出
  52. logging.error(e)
  53. sys.exit()
  54. def connect_alb_client(access_key_id, access_key_secret, endpoint):
  55. """
  56. 初始化ALB客户端
  57. :param access_key_id: access key Id, type-string
  58. :param access_key_secret: access key secret, type-string
  59. :return: alb_client
  60. """
  61. config = open_api_models.Config(
  62. access_key_id=access_key_id,
  63. access_key_secret=access_key_secret,
  64. endpoint=endpoint
  65. )
  66. alb_client = Alb20200616Client(config)
  67. return alb_client
  68. def build_create_instances_request(image_id, vswitch_id, security_group_id, zone_id, instance_type, instance_name,
  69. disk_size, disk_category, key_pair_name, tags):
  70. """
  71. 购买服务器参数配置
  72. :param image_id: 使用的镜像信息 type-string
  73. :param vswitch_id: 选择的交换机 type-string
  74. :param security_group_id: 当前vpc类型的安全组 type-string
  75. :param zone_id: 服务器所在区域 type-string
  76. :param instance_type: 实例规格 type-string
  77. :param instance_name: 实例命名 type-string
  78. :param disk_size: 磁盘大小,单位:G,type-string
  79. :param disk_category: 磁盘类型 type-string
  80. :param key_pair_name: 密钥对名称 type-string
  81. :param tags: 标签 type-list, eg: [{"Key": "ecs", "Value": "rov-server.prod"}, ...]
  82. :return: request
  83. """
  84. request = RunInstancesRequest()
  85. request.set_ImageId(image_id)
  86. request.set_VSwitchId(vswitch_id)
  87. request.set_SecurityGroupId(security_group_id)
  88. request.set_ZoneId(zone_id)
  89. request.set_InstanceType(instance_type)
  90. request.set_InstanceName(instance_name)
  91. request.set_SystemDiskSize(disk_size)
  92. request.set_SystemDiskCategory(disk_category)
  93. request.set_KeyPairName(key_pair_name)
  94. request.set_Tags(tags)
  95. return request
  96. def send_req(client, request):
  97. """
  98. 发送API请求
  99. :param client: 客户端连接
  100. :param request: 请求配置
  101. :return: response
  102. """
  103. request.set_accept_format('json')
  104. response = client.do_action_with_exception(request)
  105. #print(response)
  106. response = json.loads(response)
  107. print(response)
  108. # logging.info(response)
  109. print(response.get('Code'))
  110. return response
  111. #except Exception as e:
  112. # 失败,记录报错信息,发送通知,停止并退出
  113. #logging.error(e)
  114. #sys.exit()
  115. def check_instance_running(ecs_client, instance_ids):
  116. """
  117. 检查服务器运行状态
  118. :param ecs_client: 客户端连接
  119. :param instance_ids: 实例id列表, type-list
  120. :return: running_count,Status为Running的实例数
  121. """
  122. try:
  123. request = DescribeInstancesRequest()
  124. request.set_InstanceIds(json.dumps(instance_ids))
  125. request.set_PageSize(100)
  126. response = send_request(ecs_client=ecs_client, request=request)
  127. if response.get('Code') is None:
  128. instances_list = response.get('Instances').get('Instance')
  129. running_count = 0
  130. running_instances = []
  131. for instance_detail in instances_list:
  132. if instance_detail.get('Status') == "Running":
  133. running_count += 1
  134. running_instances.append(instance_detail.get('InstanceId'))
  135. return running_count, running_instances
  136. else:
  137. # 失败,记录报错信息,发送通知,停止并退出
  138. logging.error(response)
  139. sys.exit()
  140. except Exception as e:
  141. # 失败,记录报错信息,发送通知,停止并退出
  142. logging.error(e)
  143. sys.exit()
  144. def get_ip_address(ecs_client, instance_id):
  145. """
  146. 获取实例IP地址
  147. :param ecs_client: 客户端连接
  148. :param instance_id: 实例id, type-string
  149. :return: ip_address, type-string
  150. """
  151. request = DescribeNetworkInterfacesRequest()
  152. request.set_accept_format('json')
  153. request.set_InstanceId(instance_id)
  154. response = send_request(ecs_client=ecs_client, request=request)
  155. ip_address = response['NetworkInterfaceSets']['NetworkInterfaceSet'][0]['PrivateIpAddress']
  156. return ip_address
  157. def create_multiple_instances(amount, ecs_client,
  158. image_id, vswitch_id, security_group_id, zone_id, instance_type, instance_name,
  159. disk_size, disk_category, key_pair_name, tags):
  160. """
  161. 创建多个ECS实例
  162. :param amount: 创建实例数 type-int 取值范围:[1, 100]
  163. :param ecs_client: 购买机器客户端连接
  164. :param image_id: 使用的镜像信息 type-string
  165. :param vswitch_id: 选择的交换机 type-string
  166. :param security_group_id: 当前vpc类型的安全组 type-string
  167. :param zone_id: 服务器所在区域 type-string
  168. :param instance_type: 实例规格 type-string
  169. :param instance_name: 实例命名 type-string
  170. :param disk_size: 磁盘大小,单位:G,type-string
  171. :param disk_category: 磁盘类型 type-string
  172. :param key_pair_name: 密钥对名称 type-string
  173. :param tags: 标签 type-list, eg: [{"Key": "ecs", "Value": "rov-server.prod"}, ...]
  174. :return:
  175. """
  176. logging.info(f"create instances start, request amount: {amount}.")
  177. # 1. 连接客户端
  178. # create_instances_clt = connect_client(
  179. # access_key_id=access_key_id, access_key_secret=access_key_secret, region_id=region_id
  180. # )
  181. # 2. 请求参数配置
  182. request = build_create_instances_request(
  183. image_id=image_id, vswitch_id=vswitch_id, security_group_id=security_group_id, zone_id=zone_id,
  184. instance_type=instance_type, instance_name=instance_name, disk_size=disk_size, disk_category=disk_category,
  185. key_pair_name=key_pair_name, tags=tags
  186. )
  187. request.set_Amount(amount)
  188. # 3. 发送API请求,购买机器并启动
  189. response = send_request(ecs_client=ecs_client, request=request)
  190. if response.get('Code') is None:
  191. instance_ids = response.get('InstanceIdSets').get('InstanceIdSet')
  192. logging.info(f"success amount: {len(instance_ids)}, instance ids: {instance_ids}.")
  193. # 获取机器运行状态
  194. running_amount = 0
  195. while running_amount < amount:
  196. time.sleep(10)
  197. running_amount, running_instances = check_instance_running(ecs_client=ecs_client, instance_ids=instance_ids)
  198. logging.info(f"running amount: {running_amount}, running instances: {running_instances}.")
  199. return instance_ids
  200. else:
  201. # 失败,记录报错信息,发送通知,停止并退出
  202. logging.error(response)
  203. sys.exit()
  204. def release_instances(ecs_client, instance_ids, force=False):
  205. """
  206. 释放实例
  207. :param ecs_client:
  208. :param instance_ids: instance_id, type-list
  209. :param force: 是否强制释放, True-强制释放, False-正常释放, type-bool
  210. :return:
  211. """
  212. request = DeleteInstancesRequest()
  213. request.set_InstanceIds(instance_ids)
  214. request.set_Force(force)
  215. response = send_request(ecs_client=ecs_client, request=request)
  216. return response
  217. def get_instances_status(ecs_client, instance_ids):
  218. """
  219. 获取实例运行状态
  220. :param ecs_client:
  221. :param instance_ids: instance_id, type-list
  222. :return:
  223. """
  224. request = DescribeInstanceStatusRequest()
  225. request.set_InstanceIds(instance_ids)
  226. request.set_PageSize(50)
  227. response = send_request(ecs_client=ecs_client, request=request)
  228. return response
  229. def stop_instances(ecs_client, instance_ids, force_stop=False):
  230. """
  231. 停止实例
  232. :param ecs_client:
  233. :param instance_ids: 实例ID, type-list
  234. :param force_stop: 是否强制关机, True-强制关机, False-正常关机, type-bool
  235. :return:
  236. """
  237. request = StopInstancesRequest()
  238. request.set_InstanceIds(instance_ids)
  239. request.set_ForceStop(force_stop)
  240. response = send_request(ecs_client=ecs_client, request=request)
  241. return response
  242. def send_request(ecs_client, request):
  243. """
  244. 发送API请求
  245. :param ecs_client: 客户端连接
  246. :param request: 请求配置
  247. :return: response
  248. """
  249. print(ecs_client, request)
  250. print(2222222222)
  251. request.set_accept_format('json')
  252. try:
  253. response = ecs_client.do_action_with_exception(request)
  254. response = json.loads(response)
  255. # logging.info(response)
  256. return response
  257. except Exception as e:
  258. # 失败,记录报错信息,发送通知,停止并退出
  259. logging.error(e)
  260. sys.exit()
  261. def run_command(ecs_client, instance_ids, command):
  262. """
  263. 批量执行命令
  264. :param ecs_client: 客户端连接
  265. :param instance_ids: 实例id列表, type-list, 最多能指定50台ECS实例ID
  266. :param command: 命令 type-string
  267. :return:
  268. """
  269. print(11111)
  270. print(ecs_client, instance_ids, command)
  271. for i in range(len(instance_ids) // 50 + 1):
  272. instance_id_list = instance_ids[i * 50:(i + 1) * 50]
  273. if len(instance_id_list) == 0:
  274. return
  275. request = RunCommandRequest()
  276. request.set_accept_format('json')
  277. request.set_Type("RunShellScript")
  278. request.set_CommandContent(command)
  279. request.set_InstanceIds(instance_id_list)
  280. response = send_request(ecs_client=ecs_client, request=request)
  281. logging.info(response)
  282. def send_file_to_ecs(ecs_client, instance_id_list, target_dir, name, content):
  283. """
  284. 发送文件到ecs;alb应用,区分上方clb
  285. :param ecs_client:
  286. :param instance_id_list: 最多能指定50台ECS实例ID
  287. :param target_dir: 文件存放目录 type-string
  288. :param name: 文件名 type-string
  289. :param content: 文件内容 type-string
  290. :return:
  291. """
  292. if not instance_id_list:
  293. logging.warning("实例ID列表为空,无法发送文件。")
  294. return
  295. for i in range(len(instance_id_list) // 50 + 1):
  296. instance_ids = instance_id_list[i * 50:(i + 1) * 50]
  297. if len(instance_ids) == 0:
  298. logging.info("没有更多的实例ID需要发送文件,退出。")
  299. return
  300. request = SendFileRequest()
  301. request.set_Content(content)
  302. request.set_TargetDir(target_dir)
  303. request.set_Name(name)
  304. request.set_Overwrite(True)
  305. request.set_InstanceIds(instance_ids)
  306. try:
  307. logging.info(f"正在向实例 {instance_ids} 发送文件 '{name}' 到目录 '{target_dir}'")
  308. response = send_request(ecs_client=ecs_client, request=request)
  309. logging.info(f"成功发送文件到实例 {instance_ids},响应: {response}")
  310. except Exception as e:
  311. logging.error(f"发送文件到实例 {instance_ids} 失败,错误: {str(e)}")
  312. def add_servers_to_server_group(alb_client, server_group_id, instance_id, weight, port):
  313. """
  314. 添加服务器到ALB服务器组
  315. :param alb_client: ALB客户端连接
  316. :param server_group_id: 服务器组ID
  317. :param instance_id: 实例ID
  318. :param weight: 权重
  319. :param port: 后端服务器使用的端口
  320. """
  321. server = alb_models.AddServersToServerGroupRequestServers(
  322. server_id=instance_id,
  323. server_type='ecs',
  324. weight=weight,
  325. port=port
  326. )
  327. request = alb_models.AddServersToServerGroupRequest(
  328. server_group_id=server_group_id,
  329. servers=[server]
  330. )
  331. runtime = util_models.RuntimeOptions()
  332. try:
  333. alb_client.add_servers_to_server_group_with_options(request, runtime)
  334. logging.info(f"Successfully added server {instance_id} to server group {server_group_id} with weight {weight}.")
  335. except Exception as e:
  336. logging.error(f"Failed to add server {instance_id} to server group {server_group_id}: {str(e)}")
  337. def remove_servers_from_server_group(alb_client, server_group_id, instance_ids, port):
  338. """
  339. 从ALB服务器组中移除服务器
  340. :param alb_client: ALB客户端连接
  341. :param server_group_id: 服务器组ID
  342. :param instance_ids: 实例ID
  343. :param port: 后端服务器使用的端口
  344. """
  345. for instance_id in instance_ids:
  346. server = alb_models.RemoveServersFromServerGroupRequestServers(
  347. port=port,
  348. server_id=instance_id,
  349. server_type='ecs'
  350. )
  351. request = alb_models.RemoveServersFromServerGroupRequest(
  352. server_group_id=server_group_id,
  353. servers=[server]
  354. )
  355. runtime = util_models.RuntimeOptions()
  356. try:
  357. alb_client.remove_servers_from_server_group_with_options(request, runtime)
  358. logging.info(f"Successfully removed server {instance_id} from server group {server_group_id}.")
  359. except Exception as e:
  360. logging.error(f"Failed to remove server {instance_id} from server group {server_group_id}: {str(e)}")
  361. def list_server_group_servers(alb_client, server_group_id):
  362. """
  363. 列出服务器组中的服务器并返回实例ID列表
  364. @param alb_client: ALB客户端
  365. @param server_group_id: 服务器组ID
  366. @return: 实例ID列表
  367. """
  368. list_server_group_servers_request = alb_20200616_models.ListServerGroupServersRequest(
  369. server_group_id=server_group_id
  370. )
  371. runtime = util_models.RuntimeOptions()
  372. try:
  373. response = alb_client.list_server_group_servers_with_options(list_server_group_servers_request, runtime)
  374. instance_ids = [server.server_id for server in response.body.servers]
  375. return instance_ids
  376. except Exception as error:
  377. print(str(error))
  378. UtilClient.assert_as_string(str(error))
  379. return []
  380. async def list_server_group_servers_async(alb_client, server_group_id):
  381. """
  382. 异步列出指定服务器组中的服务器并返回实例ID列表
  383. @param alb_client: ALB客户端
  384. @param server_group_id: 服务器组ID
  385. @return: 实例ID列表
  386. """
  387. list_server_group_servers_request = alb_20200616_models.ListServerGroupServersRequest(
  388. server_group_id=server_group_id
  389. )
  390. runtime = util_models.RuntimeOptions()
  391. try:
  392. response = await alb_client.list_server_group_servers_with_options_async(list_server_group_servers_request, runtime)
  393. instance_ids = [server.server_id for server in response.body.servers]
  394. return instance_ids
  395. except Exception as error:
  396. print(str(error))
  397. UtilClient.assert_as_string(str(error))
  398. return []
  399. def update_server_group_server_weight(alb_client, server_group_id, instance_id, weight, port):
  400. """
  401. 更指定服务器在服务器组中的权重
  402. :param alb_client: ALB客户端
  403. :param server_group_id: 服务器组ID
  404. :param instance_id: 实例ID
  405. :param weight: 权重值
  406. :param port: 后端服务器使用的端口
  407. """
  408. server = alb_20200616_models.UpdateServerGroupServersAttributeRequestServers(
  409. server_type='Ecs',
  410. server_id=instance_id,
  411. weight=weight,
  412. port=port
  413. )
  414. request = alb_20200616_models.UpdateServerGroupServersAttributeRequest(
  415. servers=[server],
  416. server_group_id=server_group_id
  417. )
  418. runtime = util_models.RuntimeOptions()
  419. try:
  420. alb_client.update_server_group_servers_attribute_with_options(request, runtime)
  421. print(f"Successfully updated server {instance_id} in group {server_group_id} to weight {weight}.")
  422. except Exception as error:
  423. print(str(error))
  424. UtilClient.assert_as_string(str(error))
  425. def update_server_group_servers_attribute(alb_client, server_group_id_list, instance_id_list, weight_list, port):
  426. """
  427. 更新服务器组中的服务器权重
  428. :param alb_client: ALB客户端
  429. :param server_group_id_list: 服务器组ID列表
  430. :param instance_id_list: 实例ID列表
  431. :param weight_list: 权重修改列表 type-list [(weight, sleep_time), ...]
  432. :param port: 后端服务器使用的端口
  433. """
  434. for server_group_id in server_group_id_list:
  435. for instance_id in instance_id_list:
  436. for weight, sleep_time in weight_list:
  437. update_server_group_server_weight(alb_client, server_group_id, instance_id, weight, port)
  438. time.sleep(sleep_time)
  439. async def update_server_group_server_weight_async(alb_client, server_group_id, instance_id, weight, port):
  440. """
  441. 异步更新特定服务器在服务器组中的权重
  442. :param alb_client: ALB客户端
  443. :param server_group_id: 服务器组ID
  444. :param instance_id: 实例ID
  445. :param weight: 权重值
  446. :param port: 后端服务器使用的端口
  447. """
  448. server = alb_20200616_models.UpdateServerGroupServersAttributeRequestServers(
  449. server_type='Ecs',
  450. server_id=instance_id,
  451. weight=weight,
  452. port=port
  453. )
  454. request = alb_20200616_models.UpdateServerGroupServersAttributeRequest(
  455. servers=[server],
  456. server_group_id=server_group_id
  457. )
  458. runtime = util_models.RuntimeOptions()
  459. try:
  460. await alb_client.update_server_group_servers_attribute_with_options_async(request, runtime)
  461. print(f"Successfully updated server {instance_id} in group {server_group_id} to weight {weight} asynchronously.")
  462. except Exception as error:
  463. print(str(error))
  464. UtilClient.assert_as_string(str(error))
  465. async def update_server_group_servers_attribute_async(alb_client, server_group_id_list, instance_ids, weight_list, port):
  466. """
  467. 异步更新服务器组中的服务器属性
  468. :param alb_client: ALB客户端
  469. :param server_group_id_list: 服务器组ID列表
  470. :param instance_ids: 实例ID列表
  471. :param weight_list: 权重修改列表 type-list [(weight, sleep_time), ...]
  472. """
  473. tasks = []
  474. for server_group_id in server_group_id_list:
  475. for instance_id in instance_ids:
  476. for weight, sleep_time in weight_list:
  477. tasks.append(update_server_group_server_weight_async(alb_client, server_group_id, instance_id, weight, port))
  478. await asyncio.sleep(sleep_time)
  479. await asyncio.gather(*tasks)