1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| import json
import flask from flask import request, jsonify, Response from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkecs.v2 import *
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkecs.v2 import *
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkecs.v2 import *
import requests
import threading import yaml
PORT = 8888 app = flask.Flask(__name__)
@app.route('/ecs/server', methods=['POST']) def create_server(): args = request.args print(request.method) content = request.get_data(as_text=True) print(content)
return Response(yaml.dump(content), mimetype='application/yaml')
def show_server(id): ak = "DQVNQ7NHZCRS9XFCJAME" sk = "jGYmdH66dfittUCEnlr4s9gJjb6xxN4P3tuyPl4j" credentials = BasicCredentials(ak, sk)
client = EcsClient.new_builder().with_credentials(credentials)\ .with_region(EcsRegion.value_of("cn-north-4")).build() try: request = ShowServerRequest() request.server_id = id response = client.show_server(request) return response
except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
def get_ipaddress(id): ak = "DQVNQ7NHZCRS9XFCJAME" sk = "jGYmdH66dfittUCEnlr4s9gJjb6xxN4P3tuyPl4j"
credentials = BasicCredentials(ak, sk) \
client = EcsClient.new_builder() \ .with_credentials(credentials) \ .with_region(EcsRegion.value_of("cn-north-4")) \ .build()
try: request = ListServerInterfacesRequest() request.server_id = id response = client.list_server_interfaces(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
def get_server_id(name): ak = "DQVNQ7NHZCRS9XFCJAME" sk = "jGYmdH66dfittUCEnlr4s9gJjb6xxN4P3tuyPl4j"
credentials = BasicCredentials(ak, sk) \
client = EcsClient.new_builder() \ .with_credentials(credentials) \ .with_region(EcsRegion.value_of("cn-north-4")) \ .build()
try: request = ListServersDetailsRequest() response = client.list_servers_details(request)
print(response.servers) for server in response.servers: if server.name == name: return server.id except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
@app.route('/ecs/server/<name>', methods=['GET']) def get_server(name): print(name) id = get_server_id(name) print(id) if id: data = show_server(id) return json.loads(str(data)) else: return "Not Exist!"
@app.route('/ecs/server/<name>', methods=['DELETE']) def delete_server(name): if name: id = get_server_id(name)
def delete_server(id_info): ak = "DQVNQ7NHZCRS9XFCJAME" sk = "jGYmdH66dfittUCEnlr4s9gJjb6xxN4P3tuyPl4j"
credentials = BasicCredentials(ak, sk) \
client = EcsClient.new_builder() \ .with_credentials(credentials) \ .with_region(EcsRegion.value_of("cn-north-4")) \ .build()
try: request = DeleteServersRequest() listServersbody = [ ServerId( id=id_info ) ] request.body = DeleteServersRequestBody( servers=listServersbody ) response = client.delete_servers(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
import os_remote_monitor
@app.route('/ecs/server/<name>/os', methods=['GET']) def get_server_os(name): if name: id = get_server_id(name) if id: ip = get_server_ip(id) return os_remote_monitor.get_remote_server_os_info(ip,22,"root","Cloud2022") else: return {}
def get_server_ip(id):
ak = "DQVNQ7NHZCRS9XFCJAME" sk = "jGYmdH66dfittUCEnlr4s9gJjb6xxN4P3tuyPl4j"
credentials = BasicCredentials(ak, sk) \
client = EcsClient.new_builder() \ .with_credentials(credentials) \ .with_region(EcsRegion.value_of("cn-north-4")) \ .build()
try: request = ShowServerRequest() request.server_id = id response = client.show_server(request) ips = list(response.server.addresses.values())[0] for ip in ips: if ip.os_ext_ip_stype == "floating": return ip.addr
except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
if __name__ == "__main__":
app.run(host='localhost', port='8888' , debug=True, use_reloader=True)
---------------------------------执行结果----------------------------------------- [{"status": "ACTIVE", "updated": "2022-10-11T07:22:43Z", "auto_terminate_time": "", "hostId": "02165bdab1b973b122477b3a2312960a69ec9c2a8705f0537fbd581b", "OS-EXT-SRV-ATTR:host": "2165bdab1b973b122477b3a2312960a69ec9c2a8705f0537fbd581b", "addresses": {"600b8e93-fbaa-4119-87c2-87f0678dd189": [{"version": "4", "addr": "192.168.0.121", "OS-EXT-IPS:type": "fixed", "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:39:90:0a", "OS-EXT-IPS:port_id": "094b325b-6e0a-4161-937f-f6bb3f60bee3"}, {"version": "4", "addr": "124.71.205.227", "OS-EXT-IPS:type": "floating", "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:39:90:0a", "OS-EXT-IPS:port_id": "094b325b-6e0a-4161-937f-f6bb3f60bee3"}]}, "image": {"id": "f025bf3e-3173-4f2a-8f59-34aca5bd853d"}, "OS-EXT-STS:vm_state": "active", "OS-EXT-SRV-ATTR:instance_name": "instance-00da7649", "OS-EXT-SRV-ATTR:hypervisor_hostname": "2165bdab1b973b122477b3a2312960a69ec9c2a8705f0537fbd581b", "flavor": {"id": "c7.large.2", "name": "c7.large.2", "disk": "0", "vcpus": "2", "ram": "4096"}, "id": "d7747398-377b-4292-8529-20f62df56d82", "security_groups": [{"name": "Sys-FullAccess", "id": "b46f7f8c-5e1d-4e85-91c6-1ee43ce7c9a5"}], "OS-EXT-AZ:availability_zone": "cn-east-3a", "user_id": "e93823c6ea3d45429b3173aea1acdc8b", "name": "ecs-e364", "created": "2022-10-11T07:22:04Z", "tenant_id": "1ac80630a2494ca7bb05a7e0ebd82862", "OS-DCF:diskConfig": "MANUAL", "accessIPv4": "", "accessIPv6": "", "progress": 0, "OS-EXT-STS:power_state": 1, "config_drive": "", "metadata": {"image_name": "CentOS 7.9 64bit", "metering.resourcespeccode": "c7.large.2.linux", "charging_mode": "0", "vpc_id": "600b8e93-fbaa-4119-87c2-87f0678dd189", "os_type": "Linux", "metering.resourcetype": "1", "metering.image_id": "f025bf3e-3173-4f2a-8f59-34aca5bd853d", "os_bit": "64", "__support_agent_list": "hss,ces", "cascaded.instance_extrainfo": "pcibridge:1", "metering.imagetype": "gold"}, "OS-SRV-USG:launched_at": "2022-10-11T07:22:20.000000", "os-extended-volumes:volumes_attached": [{"id": "a2dc099c-1bc9-44bf-9496-6027a75f8091", "delete_on_termination": "true", "bootIndex": "0", "device": "/dev/sda"}], "description": "", "host_status": "UP", "OS-EXT-SRV-ATTR:hostname": "ecs-e364", "OS-EXT-SRV-ATTR:reservation_id": "r-flhxwpqc", "OS-EXT-SRV-ATTR:launch_index": 0, "OS-EXT-SRV-ATTR:kernel_id": "", "OS-EXT-SRV-ATTR:ramdisk_id": "", "OS-EXT-SRV-ATTR:root_device_name": "/dev/sda", "OS-EXT-SRV-ATTR:user_data": "IyEvYmluL2Jhc2gKZWNobyAncm9vdDokNiRQQ3pDUUVaNSRJUzFIbE5JbFBJN2VEUGg2WEpNNjJKTHcuclFpOG0uL1EvWUdNQUdKVjhsR2c3VmZ5VWhKTTRpcXpXVXI1MTBUVzROSnZoT2ZnRHlva0suMTdQZTNlLycgfCBjaHBhc3N3ZCAtZTs=", "locked": false, "tags": [], "os:scheduler_hints": {}, "enterprise_project_id": "0", "sys_tags": [{"key": "_sys_enterprise_project_id", "value": "0"}], "cpu_options": {}}] d7747398-377b-4292-8529-20f62df56d82 {"server": {"status": "ACTIVE", "updated": "2022-10-11T07:22:43Z", "auto_terminate_time": "", "hostId": "02165bdab1b973b122477b3a2312960a69ec9c2a8705f0537fbd581b", "OS-EXT-SRV-ATTR:host": "2165bdab1b973b122477b3a2312960a69ec9c2a8705f0537fbd581b", "addresses": {"600b8e93-fbaa-4119-87c2-87f0678dd189": [{"version": "4", "addr": "192.168.0.121", "OS-EXT-IPS:type": "fixed", "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:39:90:0a", "OS-EXT-IPS:port_id": "094b325b-6e0a-4161-937f-f6bb3f60bee3"}, {"version": "4", "addr": "124.71.205.227", "OS-EXT-IPS:type": "floating", "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:39:90:0a", "OS-EXT-IPS:port_id": "094b325b-6e0a-4161-937f-f6bb3f60bee3"}]}, "image": {"id": "f025bf3e-3173-4f2a-8f59-34aca5bd853d"}, "OS-EXT-STS:vm_state": "active", "OS-EXT-SRV-ATTR:instance_name": "instance-00da7649", "OS-EXT-SRV-ATTR:hypervisor_hostname": "2165bdab1b973b122477b3a2312960a69ec9c2a8705f0537fbd581b", "flavor": {"id": "c7.large.2", "name": "c7.large.2", "disk": "0", "vcpus": "2", "ram": "4096"}, "id": "d7747398-377b-4292-8529-20f62df56d82", "security_groups": [{"name": "Sys-FullAccess", "id": "b46f7f8c-5e1d-4e85-91c6-1ee43ce7c9a5"}], "OS-EXT-AZ:availability_zone": "cn-east-3a", "user_id": "e93823c6ea3d45429b3173aea1acdc8b", "name": "ecs-e364", "created": "2022-10-11T07:22:04Z", "tenant_id": "1ac80630a2494ca7bb05a7e0ebd82862", "OS-DCF:diskConfig": "MANUAL", "accessIPv4": "", "accessIPv6": "", "progress": 0, "OS-EXT-STS:power_state": 1, "config_drive": "", "metadata": {"image_name": "CentOS 7.9 64bit", "metering.resourcespeccode": "c7.large.2.linux", "charging_mode": "0", "vpc_id": "600b8e93-fbaa-4119-87c2-87f0678dd189", "os_type": "Linux", "metering.resourcetype": "1", "metering.image_id": "f025bf3e-3173-4f2a-8f59-34aca5bd853d", "os_bit": "64", "__support_agent_list": "hss,ces", "cascaded.instance_extrainfo": "pcibridge:1", "metering.imagetype": "gold"}, "OS-SRV-USG:launched_at": "2022-10-11T07:22:20.000000", "os-extended-volumes:volumes_attached": [{"id": "a2dc099c-1bc9-44bf-9496-6027a75f8091", "delete_on_termination": "true", "bootIndex": "0", "device": "/dev/sda"}], "description": "", "host_status": "UP", "OS-EXT-SRV-ATTR:hostname": "ecs-e364", "OS-EXT-SRV-ATTR:reservation_id": "r-flhxwpqc", "OS-EXT-SRV-ATTR:launch_index": 0, "OS-EXT-SRV-ATTR:kernel_id": "", "OS-EXT-SRV-ATTR:ramdisk_id": "", "OS-EXT-SRV-ATTR:root_device_name": "/dev/sda", "OS-EXT-SRV-ATTR:user_data": "IyEvYmluL2Jhc2gKZWNobyAncm9vdDokNiRQQ3pDUUVaNSRJUzFIbE5JbFBJN2VEUGg2WEpNNjJKTHcuclFpOG0uL1EvWUdNQUdKVjhsR2c3VmZ5VWhKTTRpcXpXVXI1MTBUVzROSnZoT2ZnRHlva0suMTdQZTNlLycgfCBjaHBhc3N3ZCAtZTs=", "locked": false, "tags": [], "os:scheduler_hints": {}, "enterprise_project_id": "0", "sys_tags": [{"key": "_sys_enterprise_project_id", "value": "0"}], "cpu_options": {}}}
|