Skip to content

Commit

Permalink
Fix: Replace shade library with openstacksdk
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Anil Belur <[email protected]>
Change-Id: Ie45f6ba7862a1bd3f471d469286b042512604c54
  • Loading branch information
Bengt Thuree authored and askb committed Mar 17, 2023
1 parent 3aead71 commit 4457964
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 31 deletions.
5 changes: 3 additions & 2 deletions lftools/openstack/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

__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:
Expand Down
12 changes: 7 additions & 5 deletions lftools/openstack/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
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):
Expand All @@ -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)
Expand All @@ -51,7 +53,7 @@ def _remove_servers_from_cloud(servers, cloud):
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
Expand All @@ -68,7 +70,7 @@ def _remove_servers_from_cloud(servers, cloud):
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)
Expand All @@ -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:
Expand Down
39 changes: 20 additions & 19 deletions lftools/openstack/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from datetime import datetime

import openstack
import shade
import openstack.config
from openstack.cloud.exc import OpenStackCloudHTTPError

from lftools.jenkins import Jenkins

Expand All @@ -29,7 +30,7 @@

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))
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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.")
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 7 additions & 5 deletions lftools/openstack/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
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):
Expand All @@ -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)
Expand All @@ -53,7 +55,7 @@ def _remove_volumes_from_cloud(volumes, cloud):
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
Expand All @@ -70,7 +72,7 @@ def _remove_volumes_from_cloud(volumes, cloud):
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)
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 4457964

Please sign in to comment.