mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
[cloud/rax] support wait on delete
Add support to existing rax module to honor the wait (and wait_timeout) parameters on delete operations. This patch removes existing logic in favor of the built-in pyrax.utils.wait_until method.
This commit is contained in:
parent
a5ee6ff1e5
commit
03252e1d30
1 changed files with 33 additions and 19 deletions
|
@ -119,6 +119,7 @@ import os
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import pyrax
|
import pyrax
|
||||||
|
import pyrax.utils
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print("failed=True msg='pyrax required for this module'")
|
print("failed=True msg='pyrax required for this module'")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -174,29 +175,27 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files,
|
||||||
module.fail_json(msg = '%s' % e.message)
|
module.fail_json(msg = '%s' % e.message)
|
||||||
|
|
||||||
for server in servers:
|
for server in servers:
|
||||||
# wait here until the instances are up
|
# If requested, wait for server activation
|
||||||
wait_timeout = time.time() + wait_timeout
|
if wait:
|
||||||
while wait and wait_timeout > time.time():
|
pyrax.utils.wait_until(server, 'status', ('ACTIVE', 'ERROR'),
|
||||||
# refresh the server details
|
interval=5, attempts=wait_timeout/5)
|
||||||
server.get()
|
|
||||||
if server.status in ('ACTIVE', 'ERROR'):
|
|
||||||
break
|
|
||||||
time.sleep(5)
|
|
||||||
if wait and wait_timeout <= time.time():
|
|
||||||
# waiting took too long
|
|
||||||
module.fail_json(msg = 'Timeout waiting on %s' % server.id)
|
|
||||||
# Get a fresh copy of the server details
|
# Get a fresh copy of the server details
|
||||||
server.get()
|
server.get()
|
||||||
if server.status == 'ERROR':
|
if server.status == 'ACTIVE':
|
||||||
module.fail_json(msg = '%s failed to build' % server.id)
|
|
||||||
instance = {'id': server.id,
|
instance = {'id': server.id,
|
||||||
'accessIPv4': server.accessIPv4,
|
'accessIPv4': server.accessIPv4,
|
||||||
'name': server.name,
|
'name': server.name,
|
||||||
'status': server.status}
|
'status': server.status}
|
||||||
instances.append(instance)
|
instances.append(instance)
|
||||||
|
elif server.status == 'ERROR':
|
||||||
|
module.fail_json(msg = '%s failed to build' % server.id)
|
||||||
|
elif wait:
|
||||||
|
# waiting took too long
|
||||||
|
module.fail_json(msg = 'Timeout waiting on %s' % server.id)
|
||||||
|
|
||||||
|
|
||||||
elif state in ('absent', 'deleted'):
|
elif state in ('absent', 'deleted'):
|
||||||
deleted = []
|
|
||||||
# See if we can find a server that matches our credentials
|
# See if we can find a server that matches our credentials
|
||||||
for server in servers:
|
for server in servers:
|
||||||
if server.name == name:
|
if server.name == name:
|
||||||
|
@ -205,13 +204,28 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files,
|
||||||
server.metadata == meta:
|
server.metadata == meta:
|
||||||
try:
|
try:
|
||||||
server.delete()
|
server.delete()
|
||||||
deleted.append(server)
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
module.fail_json(msg = e.message)
|
module.fail_json(msg = e.message)
|
||||||
|
|
||||||
instance = {'id': server.id,
|
instance = {'id': server.id,
|
||||||
'accessIPv4': server.accessIPv4,
|
'accessIPv4': server.accessIPv4,
|
||||||
'name': server.name,
|
'name': server.name,
|
||||||
'status': 'DELETING'}
|
'status': 'DELETING'}
|
||||||
|
|
||||||
|
# If requested, wait for server deletion
|
||||||
|
if wait:
|
||||||
|
try:
|
||||||
|
pyrax.utils.wait_until(server, 'status', '', interval=5,
|
||||||
|
attempts=wait_timeout/5)
|
||||||
|
# Get a fresh copy of the server details
|
||||||
|
server.get()
|
||||||
|
except Exception, e:
|
||||||
|
# In this case, an exception means the server is NotFound
|
||||||
|
instance['status'] = 'DELETED'
|
||||||
|
else:
|
||||||
|
# waiting took too long
|
||||||
|
module.fail_json(msg = 'Timeout waiting on delete %s (%s)' % (server.id, server.status))
|
||||||
|
|
||||||
instances.append(instance)
|
instances.append(instance)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue