[Git][qa/jenkins.debian.net][master] Refactor powercycle_x86_nodes.py to use the new IONOS API & its new python bindings

Mattia Rizzolo (@mattia) gitlab at salsa.debian.org
Sat Mar 9 12:55:12 GMT 2024



Mattia Rizzolo pushed to branch master at Debian QA / jenkins.debian.net


Commits:
e7532623 by Mattia Rizzolo at 2024-03-09T13:54:41+01:00
Refactor powercycle_x86_nodes.py to use the new IONOS API & its new python bindings

Signed-off-by: Mattia Rizzolo <mattia at debian.org>

- - - - -


1 changed file:

- bin/powercycle_x86_nodes.py


Changes:

=====================================
bin/powercycle_x86_nodes.py
=====================================
@@ -1,24 +1,24 @@
 #!/usr/bin/python3
 
 # Copyright 2019-2020 Holger Levsen <holger at layer-acht.org>
+# Copyright 2024 Mattia Rizzolo <mattia at debian.org>
 # released under the GPLv2
 
 #
-# This script helps powercycling x86 nodes at IONOS (formerly known as Profitbricks).
+# This script helps powercycling x86 nodes at IONOS (previously Profitbricks).
 #
 # It needs an account and expects the environment variables
 # IONOS_USERNAME and IONOS_PASSWORD to be set accordingly
-# or have this information stored in ~/.config/profitbricks-sdk-python.ini
+# or have a token stored in ~/.config/ionos_token
 #
 # Then it expects node numbers or 'jenkins' as params.
 #
 
 import os
 import sys
-import json
 import logging
 
-from profitbricks.client import ProfitBricksService
+import ionoscloud
 
 
 log = logging.getLogger(__name__)
@@ -41,7 +41,11 @@ for argument in sys.argv[1:]:
             print("This script helps powercycling x86 nodes at IONOS.")
 
             print()
-            print("It needs an account and expects the environment variables IONOS_USERNAME and IONOS_PASSWORD to be set accordingly.")
+            print("It needs an account and expects the environment variables")
+            print("IONOS_USERNAME and IONOS_PASSWORD to be set accordingly.")
+            print()
+            print("Alternatively, you can save a token in a file named")
+            print("~/.config/ionos_token.  Be mindful of security")
             print()
             print("Then it expects node numbers or 'jenkins' as params.")
             sys.exit(1)
@@ -51,23 +55,35 @@ for argument in sys.argv[1:]:
 
 log.debug("Acting on nodes: %s", nodes)
 
-client = ProfitBricksService(
-    username=os.getenv("IONOS_USERNAME"), password=os.getenv("IONOS_PASSWORD")
-)
+try:
+    with open(os.path.expanduser("~/.config/ionos_token")) as f:
+        configuration = ionoscloud.Configuration(token=f.read().strip())
+except FileNotFoundError:
+    configuration = ionoscloud.Configuration(
+        username=os.getenv("IONOS_USERNAME"),
+        password=os.getenv("IONOS_PASSWORD")
+    )
+client = ionoscloud.ApiClient(configuration)
 
-response = client.list_datacenters(0)
-log.debug(json.dumps(response, indent=4))
+dcs = ionoscloud.DataCentersApi(client).datacenters_get()
+log.debug(dcs)
 
-for item in response["items"]:
-    datacenter = item["id"]
+for dc in dcs.items:
+    datacenter = dc.id
     log.debug(datacenter)
-    response = client.list_servers(datacenter_id=datacenter)
-    log.debug(json.dumps(response, indent=4))
-    for item in response["items"]:
-        log.debug(item['id'])
-        log.debug(item['type'])
-        for server in nodes:
-            if item["type"] == "server" and item["properties"]["name"] == server:
-                log.info("Rebooting " + item["properties"]["name"] + " aka " + server)
-                result = client.reboot_server(datacenter, item["id"])
+    servers = ionoscloud.ServersApi(client).datacenters_servers_get(
+        datacenter_id=datacenter, depth=1
+    )
+    log.debug(servers)
+    for server in servers.items:
+        log.debug(server.id)
+        log.debug(server.type)
+        server_name = server.properties.name
+        log.debug(server_name)
+        for node in nodes:
+            if server.type == "server" and server_name == node:
+                log.info(f"Rebooting {server_name} aka {node}")
+                result = ionoscloud.ServersApi(client).\
+                    datacenters_servers_reboot_post(
+                         datacenter_id=datacenter, server_id=server.id)
                 log.info(result)



View it on GitLab: https://salsa.debian.org/qa/jenkins.debian.net/-/commit/e75326235abdf9c72d240d31032699cc7c072769

-- 
View it on GitLab: https://salsa.debian.org/qa/jenkins.debian.net/-/commit/e75326235abdf9c72d240d31032699cc7c072769
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/qa-jenkins-scm/attachments/20240309/b925d900/attachment-0001.htm>


More information about the Qa-jenkins-scm mailing list