Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use openstacksdk rather than shade for resources deletion #319

Merged
merged 2 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions scripts/os-idr-delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import argparse
from builtins import input
import shade
import openstack
import sys


Expand All @@ -28,18 +28,18 @@ def is_in_idrenv(idrenv, obj):


class DeleteResource(object):
def __init__(self, cloud, wait):
self.cloud = cloud
def __init__(self, conn, wait):
self.conn = conn
self.wait = wait

def __str__(self):
return '\n'.join(self.description)


class DeleteServers(DeleteResource):
def __init__(self, cloud, idrenv, wait=True):
super(DeleteServers, self).__init__(cloud, wait)
self.servers = [s for s in cloud.list_servers()
def __init__(self, conn, idrenv, wait=True):
super(DeleteServers, self).__init__(conn, wait)
self.servers = [s for s in conn.list_servers()
if is_in_idrenv(idrenv, s)]

self.description = (
Expand All @@ -50,16 +50,16 @@ def __init__(self, cloud, idrenv, wait=True):

def __call__(self):
for s in self.servers:
self.cloud.delete_server(s.id, wait=self.wait)
self.conn.delete_server(s.id, wait=self.wait)


class DeleteVolumes(DeleteResource):
def __init__(self, cloud, idrenv, wait=True):
super(DeleteVolumes, self).__init__(cloud, wait)
self.volumes = [v for v in cloud.list_volumes()
def __init__(self, conn, idrenv, wait=True):
super(DeleteVolumes, self).__init__(conn, wait)
self.volumes = [v for v in conn.list_volumes()
if is_in_idrenv(idrenv, v)]
volume_ids = set(v.id for v in self.volumes)
self.volume_snapshots = [s for s in cloud.list_volume_snapshots()
self.volume_snapshots = [s for s in conn.list_volume_snapshots()
if s.volume_id in volume_ids]

self.description = (
Expand All @@ -72,17 +72,17 @@ def __init__(self, cloud, idrenv, wait=True):

def __call__(self):
for s in self.volume_snapshots:
cloud.delete_volume_snapshot(s.id, wait=self.wait)
conn.delete_volume_snapshot(s.id, wait=self.wait)
for v in self.volumes:
self.cloud.delete_volume(v.id, wait=self.wait)
self.conn.delete_volume(v.id, wait=self.wait)


class DeleteNetworks(DeleteResource):
def __init__(self, cloud, idrenv, wait=True):
super(DeleteNetworks, self).__init__(cloud, wait)
self.routers = [r for r in cloud.list_routers()
def __init__(self, conn, idrenv, wait=True):
super(DeleteNetworks, self).__init__(conn, wait)
self.routers = [r for r in conn.list_routers()
if is_in_idrenv(idrenv, r)]
self.networks = [n for n in cloud.list_networks()
self.networks = [n for n in conn.list_networks()
if is_in_idrenv(idrenv, n)]
self.subnet_ids = []
for n in self.networks:
Expand All @@ -91,7 +91,7 @@ def __init__(self, cloud, idrenv, wait=True):
self.ports = {}
self.router_map = dict((r.id, r) for r in self.routers)
for n in self.networks:
for p in cloud.list_ports({
for p in conn.list_ports({
'device_owner': 'network:router_interface',
'network_id': n.id
}):
Expand All @@ -102,7 +102,7 @@ def __init__(self, cloud, idrenv, wait=True):
self.ports[p.device_id] = set([p.id])

# Fetch subnet metadata for display
subnet_dict = dict((s.id, s) for s in cloud.list_subnets())
subnet_dict = dict((s.id, s) for s in conn.list_subnets())

self.description = (
['Deleting Networks'] +
Expand All @@ -119,20 +119,20 @@ def __init__(self, cloud, idrenv, wait=True):
def __call__(self):
for (router_id, port_ids) in self.ports.items():
for p in port_ids:
self.cloud.remove_router_interface(
self.conn.remove_router_interface(
self.router_map[router_id], port_id=p)
for r in self.routers:
self.cloud.delete_router(r.id)
self.conn.delete_router(r.id)
for s in self.subnet_ids:
self.cloud.delete_subnet(s)
self.conn.delete_subnet(s)
for n in self.networks:
self.cloud.delete_network(n.id)
self.conn.delete_network(n.id)


class DeleteSecurityGroups(DeleteResource):
def __init__(self, cloud, idrenv, wait=True):
super(DeleteSecurityGroups, self).__init__(cloud, wait)
self.security_groups = [g for g in cloud.list_security_groups()
def __init__(self, conn, idrenv, wait=True):
super(DeleteSecurityGroups, self).__init__(conn, wait)
self.security_groups = [g for g in conn.list_security_groups()
if is_in_idrenv(idrenv, g)]

self.description = (
Expand All @@ -143,21 +143,21 @@ def __init__(self, cloud, idrenv, wait=True):

def __call__(self):
for g in self.security_groups:
self.cloud.delete_security_group(g.id)
self.conn.delete_security_group(g.id)


def delete(cloud, idrenv, resource_types, wait):
def delete(conn, idrenv, resource_types, wait):
commands = []
delete_all = 'all' in resource_types

if delete_all or 'server' in resource_types:
commands.append(DeleteServers(cloud, idrenv, wait))
commands.append(DeleteServers(conn, idrenv, wait))
if delete_all or 'volume' in resource_types:
commands.append(DeleteVolumes(cloud, idrenv, wait))
commands.append(DeleteVolumes(conn, idrenv, wait))
if delete_all or 'network' in resource_types:
commands.append(DeleteNetworks(cloud, idrenv, wait))
commands.append(DeleteNetworks(conn, idrenv, wait))
if delete_all or 'secgroup' in resource_types:
commands.append(DeleteSecurityGroups(cloud, idrenv, wait))
commands.append(DeleteSecurityGroups(conn, idrenv, wait))

return commands

Expand Down Expand Up @@ -198,8 +198,8 @@ def idrenv_validate(s):
print('Environment does not match, aborting')
sys.exit(2)

cloud = shade.openstack_cloud()
commands = delete(cloud, args.idrenv, args.type, not args.nowait)
conn = openstack.connection.from_config()
commands = delete(conn, args.idrenv, args.type, not args.nowait)
for c in commands:
print(c)

Expand Down
32 changes: 16 additions & 16 deletions scripts/os-idr-swapip.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@
# target's previous IP to that server (i.e. swap the IPs)

from builtins import input
import shade
import openstack
import socket
import sys


class DetachFloatingIP(object):
def __init__(self, cloud, floatingips, server):
self.cloud = cloud
def __init__(self, conn, floatingips, server):
self.conn = conn
fip = floatingips[server.accessIPv4]
self.kwargs = dict(server_id=server.id, floating_ip_id=fip.id)
self.description = 'Detaching IP {0} from {1} ({2})'.format(
fip.floating_ip_address, server.name, fip.fixed_ip_address)

def __call__(self):
cloud.detach_ip_from_server(**self.kwargs)
conn.detach_ip_from_server(**self.kwargs)

def __str__(self):
return self.description


class AttachFloatingIP(object):
def __init__(self, cloud, floatingips, server, targetip):
self.cloud = cloud
def __init__(self, conn, floatingips, server, targetip):
self.conn = conn
fip = floatingips[server.accessIPv4]
if targetip not in floatingips:
raise ValueError(
Expand All @@ -52,17 +52,17 @@ def __init__(self, cloud, floatingips, server, targetip):
targetip, server.name, fip.fixed_ip_address)

def __call__(self):
cloud.add_ip_list(**self.kwargs)
conn.add_ip_list(**self.kwargs)

def __str__(self):
return self.description


def swapip(cloud, targetname, targetdns):
def swapip(conn, targetname, targetdns):
targetip = socket.gethostbyname(targetdns)
servers = cloud.list_servers()
servers = conn.list_servers()
floatingips = dict((fip.floating_ip_address, fip)
for fip in cloud.list_floating_ips())
for fip in conn.list_floating_ips())

target = None
current = None
Expand Down Expand Up @@ -90,14 +90,14 @@ def swapip(cloud, targetname, targetdns):
break

commands = []
commands.append(DetachFloatingIP(cloud, floatingips, target))
commands.append(DetachFloatingIP(conn, floatingips, target))
if current:
commands.append(DetachFloatingIP(cloud, floatingips, current))
commands.append(DetachFloatingIP(conn, floatingips, current))

commands.append(AttachFloatingIP(cloud, floatingips, target, targetip))
commands.append(AttachFloatingIP(conn, floatingips, target, targetip))
if current:
commands.append(AttachFloatingIP(
cloud, floatingips, current, oldfloatingip))
conn, floatingips, current, oldfloatingip))

return commands

Expand All @@ -106,8 +106,8 @@ def swapip(cloud, targetname, targetdns):
args = sys.argv[1:]
if len(args) != 2:
raise ValueError('Required parameters: server-name target-ip-or-dns')
cloud = shade.openstack_cloud()
commands = swapip(cloud, args[0], args[1])
conn = openstack.connection.from_config()
commands = swapip(conn, args[0], args[1])
for c in commands:
print(c)
r = input('Enter "yes" to continue with these changes\n'
Expand Down