Skip to content

poc: support multiple ip#48

Draft
Sczlog wants to merge 1 commit into
masterfrom
multiple-ip
Draft

poc: support multiple ip#48
Sczlog wants to merge 1 commit into
masterfrom
multiple-ip

Conversation

@Sczlog
Copy link
Copy Markdown
Collaborator

@Sczlog Sczlog commented May 8, 2026

测试代码:

  from __future__ import absolute_import

  import logging
  import os
  import time

  import urllib3

  from cloudtower import ActivePassiveApiClient
  from cloudtower.api.vm_api import VmApi
  from cloudtower.configuration import Configuration
  from cloudtower.exceptions import ApiException
  from cloudtower.models import UserSource, VmUpdateParams,
  VmUpdateParamsData, VmWhereInput

  logging.basicConfig(level=logging.INFO)
  urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


  def main():
      endpoints = os.environ.get("CLOUDTOWER_ENDPOINTS")
      username = os.environ.get("CLOUDTOWER_USERNAME")
      password = os.environ.get("CLOUDTOWER_PASSWORD")

      if not endpoints or not username or not password:
          raise RuntimeError(
              "CLOUDTOWER_ENDPOINTS, CLOUDTOWER_USERNAME and "
              "CLOUDTOWER_PASSWORD are required"
          )

      configuration = Configuration()
      configuration.verify_ssl = False

      client = ActivePassiveApiClient(
          endpoints=[item.strip() for item in endpoints.split(",")],
          base_path="/v2/api",
          schemes=["https"],
          user_config={
              "name": username,
              "password": password,
              "source": UserSource.LOCAL,
          },
          configuration=configuration,
      )

      vm_api = VmApi(api_client=client)
      run_update_vm_no_match(client, vm_api)


  def run_update_vm_no_match(client, vm_api):
      active_ip_before_request = client.current_active_host

      try:
          body = VmUpdateParams(
              where=VmWhereInput(id=""),
              data=VmUpdateParamsData(
                  description="active-passive-304-check",
              ),
          )
          vm_api.update_vm(body)

          print("%s request success active_ip=%s" % (
              time.strftime("%Y-%m-%dT%H:%M:%S%z"),
              client.current_active_host,
          ))
      except ApiException as e:
          print("%s request failed status=%s active_ip_before=%s
  active_ip_after=%s error=%s" % (
              time.strftime("%Y-%m-%dT%H:%M:%S%z"),
              e.status,
              active_ip_before_request,
              client.current_active_host,
              e,
          ))
      except Exception as e:
          print("%s request failed active_ip_before=%s active_ip_after=%s
  error=%s" % (
              time.strftime("%Y-%m-%dT%H:%M:%S%z"),
              active_ip_before_request,
              client.current_active_host,
              e,
          ))


  if __name__ == "__main__":
      main()

自测:

2026-05-08T19:43:06+0800 request success active_ip=172.21.152.75
2026-05-08T19:43:16+0800 request success active_ip=172.21.152.75
2026-05-08T19:43:26+0800 request failed active_ip=None error=(0)
Reason: active-passive discover found no active host

2026-05-08T19:43:36+0800 request failed active_ip=None error=(0)
Reason: active-passive discover found no active host

2026-05-08T19:43:46+0800 request failed active_ip=None error=(0)
Reason: active-passive discover found no active host: 172.21.152.95: (502)
Reason: probe active-passive returned unexpected status


2026-05-08T19:44:01+0800 request failed active_ip=None error=(502)
Reason: Bad Gateway
HTTP response headers: HTTPHeaderDict({'Date': 'Fri, 08 May 2026 11:43:06 GMT', 'Content-Length': '154', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 'Content-Type': 'text/html', 'Connection': 'keep-alive'})
HTTP response body: <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>openresty</center>
</body>
</html>


2026-05-08T19:44:17+0800 request failed active_ip=None error=(502)
Reason: Bad Gateway
HTTP response headers: HTTPHeaderDict({'Date': 'Fri, 08 May 2026 11:43:22 GMT', 'Content-Length': '154', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 'Content-Type': 'text/html', 'Connection': 'keep-alive'})
HTTP response body: <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>openresty</center>
</body>
</html>


2026-05-08T19:44:27+0800 request failed active_ip=None error=(502)
Reason: Bad Gateway
HTTP response headers: HTTPHeaderDict({'Date': 'Fri, 08 May 2026 11:43:32 GMT', 'Content-Length': '154', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 'Content-Type': 'text/html', 'Connection': 'keep-alive'})
HTTP response body: <html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>openresty</center>
</body>
</html>


2026-05-08T19:44:37+0800 request failed active_ip=None error=(403)
Reason: Forbidden
HTTP response headers: HTTPHeaderDict({'Content-Length': '772', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 'Vary': 'Origin', 'x-tower-request-id': 'U1LdDK_dIbXQLQ1bEwzk0', 'Connection': 'keep-alive', 'Date': 'Fri, 08 May 2026 11:43:42 GMT', 'Content-Type': 'application/json; charset=utf-8'})
HTTP response body: {"path":"/get-vms","operationName":"vmsConnectionExternal","status":403,"message":"Unknown error on operation vmsConnectionExternal","stack":"Error: Unknown error on operation vmsConnectionExternal\n    at /app/lib/libs/graphql-client.js:25:19\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n    at async GeneratedQueryController.getVms (/app/lib/modules/generated/query-controller.js:8683:24)\n    at async runAuthenticationMiddleware (/app/lib/generated/routes.js:43678:17)\n    at async /app/lib/server.js:122:9\n    at async cors (/app/node_modules/@koa/cors/index.js:61:32)\n    at async bodyParser (/app/node_modules/koa-bodyparser/index.js:95:5)\n    at async errorHandler (/app/lib/middleware/error-handler.js:78:9)","props":{}}

2026-05-08T19:44:49+0800 request success active_ip=172.21.152.95

- add active_passive_client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant