Fix: Replace shade library with openstacksdk 23/71423/6
authorBengt Thuree <bthuree@linuxfoundation.org>
Fri, 17 Mar 2023 12:25:44 +0000 (23:25 +1100)
committerAnil Belur <abelur@linuxfoundation.org>
Fri, 17 Mar 2023 16:13:46 +0000 (21:43 +0530)
The shade library has been depreciated. Replace shade library
with openstacksdk for remaining openstack commands.

Issue-ID: RELENG-4644
Ref: https://docs.openstack.org/releasenotes/shade/stein.html
Signed-off-by: Bengt Thuree <bthuree@linuxfoundation.org>
Signed-off-by: Anil Belur <abelur@linuxfoundation.org>
Change-Id: Ie45f6ba7862a1bd3f471d469286b042512604c54

lftools/openstack/object.py
lftools/openstack/server.py
lftools/openstack/stack.py
lftools/openstack/volume.py
releasenotes/notes/migrate-away-from-depreciated-shade-library-516eaec59f1922dd.yaml [new file with mode: 0644]

index 7895866..bb0cf4c 100644 (file)
 
 __author__ = "Thanh Ha"
 
-import shade
+import openstack
+import openstack.config
 
 
 def list_containers(os_cloud):
     """List volumes found according to parameters."""
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)
     containers = cloud.list_containers()
 
     for container in containers:
index b0bdda3..4f6922f 100644 (file)
@@ -15,7 +15,9 @@ __author__ = "Anil Belur"
 import sys
 from datetime import datetime, timedelta
 
-import shade
+import openstack
+import openstack.config
+from openstack.cloud.exc import OpenStackCloudException
 
 
 def _filter_servers(servers, days=0):
@@ -31,7 +33,7 @@ def _filter_servers(servers, days=0):
 
 def list(os_cloud, days=0):
     """List servers found according to parameters."""
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)
     servers = cloud.list_servers()
 
     filtered_servers = _filter_servers(servers, days)
@@ -51,7 +53,7 @@ def cleanup(os_cloud, days=0):
         for server in servers:
             try:
                 result = cloud.delete_server(server.name)
-            except shade.exc.OpenStackCloudException as e:
+            except OpenStackCloudException as e:
                 if str(e).startswith("Multiple matches found for"):
                     print("WARNING: {}. Skipping server...".format(str(e)))
                     continue
@@ -68,7 +70,7 @@ def cleanup(os_cloud, days=0):
             else:
                 print('Removed "{}" from {}.'.format(server.name, cloud.cloud_config.name))
 
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)
     servers = cloud.list_servers()
     filtered_servers = _filter_servers(servers, days)
     _remove_servers_from_cloud(filtered_servers, cloud)
@@ -80,7 +82,7 @@ def remove(os_cloud, server_name, minutes=0):
     :arg str os_cloud: Cloud name as defined in OpenStack clouds.yaml.
     :arg int minutes: Only delete server if it is older than number of minutes.
     """
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)
     server = cloud.get_server(server_name)
 
     if not server:
index 3e58c46..d7b5acb 100644 (file)
@@ -20,7 +20,8 @@ import urllib.request
 from datetime import datetime
 
 import openstack
-import shade
+import openstack.config
+from openstack.cloud.exc import OpenStackCloudHTTPError
 
 from lftools.jenkins import Jenkins
 
@@ -29,7 +30,7 @@ log = logging.getLogger(__name__)
 
 def create(os_cloud, name, template_file, parameter_file, timeout=900, tries=2):
     """Create a heat stack from a template_file and a parameter_file."""
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)
     stack_success = False
 
     print("Creating stack {}".format(name))
@@ -38,7 +39,7 @@ def create(os_cloud, name, template_file, parameter_file, timeout=900, tries=2):
             stack = cloud.create_stack(
                 name, template_file=template_file, environment_files=[parameter_file], timeout=timeout, rollback=False
             )
-        except shade.exc.OpenStackCloudHTTPError as e:
+        except OpenStackCloudHTTPError as e:
             if cloud.search_stacks(name):
                 print("Stack with name {} already exists.".format(name))
             else:
@@ -51,18 +52,18 @@ def create(os_cloud, name, template_file, parameter_file, timeout=900, tries=2):
             time.sleep(10)
             stack = cloud.get_stack(stack_id)
 
-            if stack.stack_status == "CREATE_IN_PROGRESS":
+            if stack.status == "CREATE_IN_PROGRESS":
                 print("Waiting to initialize infrastructure...")
-            elif stack.stack_status == "CREATE_COMPLETE":
+            elif stack.status == "CREATE_COMPLETE":
                 print("Stack initialization successful.")
                 stack_success = True
                 break
-            elif stack.stack_status == "CREATE_FAILED":
-                print("WARN: Failed to initialize stack. Reason: {}".format(stack.stack_status_reason))
+            elif stack.status == "CREATE_FAILED":
+                print("WARN: Failed to initialize stack. Reason: {}".format(stack.status_reason))
                 if delete(os_cloud, stack_id):
                     break
             else:
-                print("Unexpected status: {}".format(stack.stack_status))
+                print("Unexpected status: {}".format(stack.status))
 
         if stack_success:
             break
@@ -132,7 +133,7 @@ def delete(os_cloud, name_or_id, force, timeout=900):
 
     Return True if delete was successful.
     """
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)
     print("Deleting stack {}".format(name_or_id))
     cloud.delete_stack(name_or_id)
 
@@ -141,17 +142,17 @@ def delete(os_cloud, name_or_id, force, timeout=900):
         time.sleep(10)
         stack = cloud.get_stack(name_or_id)
 
-        if not stack or stack.stack_status == "DELETE_COMPLETE":
+        if not stack or stack.status == "DELETE_COMPLETE":
             print("Successfully deleted stack {}".format(name_or_id))
             return True
-        elif stack.stack_status == "DELETE_IN_PROGRESS":
+        elif stack.status == "DELETE_IN_PROGRESS":
             print("Waiting for stack to delete...")
-        elif stack.stack_status == "DELETE_FAILED":
-            print("WARN: Failed to delete $STACK_NAME. Reason: {}".format(stack.stack_status_reason))
+        elif stack.status == "DELETE_FAILED":
+            print("WARN: Failed to delete $STACK_NAME. Reason: {}".format(stack.status_reason))
             print("Retrying delete...")
             cloud.delete_stack(name_or_id)
         else:
-            print("WARN: Unexpected delete status: {}".format(stack.stack_status))
+            print("WARN: Unexpected delete status: {}".format(stack.status))
             print("Retrying delete...")
             cloud.delete_stack(name_or_id)
 
@@ -166,7 +167,7 @@ def delete_stale(os_cloud, jenkins_servers):
     An orphaned stack is a stack that is not known in any of the Jenkins
     servers passed into this function.
     """
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)
     stacks = cloud.search_stacks()
     if not stacks:
         log.debug("No stacks to delete.")
@@ -196,13 +197,13 @@ def delete_stale(os_cloud, jenkins_servers):
     log.debug("Active stacks")
     for stack in stacks:
         if (
-            stack.stack_status == "CREATE_COMPLETE"
-            or stack.stack_status == "CREATE_FAILED"
-            or stack.stack_status == "DELETE_FAILED"
+            stack.status == "CREATE_COMPLETE"
+            or stack.status == "CREATE_FAILED"
+            or stack.status == "DELETE_FAILED"
         ):
             log.debug("    {}".format(stack.stack_name))
 
-            if stack.stack_status == "DELETE_FAILED":
+            if stack.status == "DELETE_FAILED":
                 cloud.pprint(stack)
 
             if stack.stack_name not in builds:
index e612b48..8838aa7 100644 (file)
@@ -15,7 +15,9 @@ __author__ = "Thanh Ha"
 import sys
 from datetime import datetime, timedelta
 
-import shade
+import openstack
+import openstack.config
+from openstack.cloud.exc import OpenStackCloudException
 
 
 def _filter_volumes(volumes, days=0):
@@ -33,7 +35,7 @@ def _filter_volumes(volumes, days=0):
 
 def list(os_cloud, days=0):
     """List volumes found according to parameters."""
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)    
     volumes = cloud.list_volumes()
 
     filtered_volumes = _filter_volumes(volumes, days)
@@ -53,7 +55,7 @@ def cleanup(os_cloud, days=0):
         for volume in volumes:
             try:
                 result = cloud.delete_volume(volume.name)
-            except shade.exc.OpenStackCloudException as e:
+            except OpenStackCloudException as e:
                 if str(e).startswith("Multiple matches found for"):
                     print("WARNING: {}. Skipping volume...".format(str(e)))
                     continue
@@ -70,7 +72,7 @@ def cleanup(os_cloud, days=0):
             else:
                 print('Removed "{}" from {}.'.format(volume.name, cloud.cloud_config.name))
 
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)
     volumes = cloud.list_volumes()
     filtered_volumes = _filter_volumes(volumes, days)
     _remove_volumes_from_cloud(filtered_volumes, cloud)
@@ -83,7 +85,7 @@ def remove(os_cloud, volume_id, minutes=0):
     :arg str volume_id: Volume ID to delete
     :arg int minutes: Only delete volume if it is older than number of minutes.
     """
-    cloud = shade.openstack_cloud(cloud=os_cloud)
+    cloud = openstack.connection.from_config(cloud=os_cloud)
     volume = cloud.get_volume_by_id(volume_id)
 
     if not volume:
diff --git a/releasenotes/notes/migrate-away-from-depreciated-shade-library-516eaec59f1922dd.yaml b/releasenotes/notes/migrate-away-from-depreciated-shade-library-516eaec59f1922dd.yaml
new file mode 100644 (file)
index 0000000..21b1b94
--- /dev/null
@@ -0,0 +1,7 @@
+---
+Fix:
+  - |
+    The shade library for openstacksdk is deprecated.
+    https://docs.openstack.org/releasenotes/shade/stein.html
+
+    Switch to the openstacksdk to replace shade.