Skip to content
Snippets Groups Projects
Commit fbb8357f authored by Arie Peterson's avatar Arie Peterson
Browse files

Wait for shutdown actions to complete using action object

parent 3aee708c
No related branches found
No related tags found
1 merge request!8Resolve "Use action objects to check status of stopping instance"
......@@ -258,12 +258,14 @@ def list_droplets():
headers=['ID', 'Name', 'IPv4', 'Status']))
def shutdown_droplet(droplet_id: int):
"""Shut down specified droplet (through a power_off call)."""
def shutdown_droplet(droplet_id: int, wait: bool = False):
"""Forcibly shut down specified droplet (through a power_off call)."""
log.info('Shutting down %s', droplet_id)
data = {"type": "power_off"}
response = request_api('droplets/{0}/actions'.format(droplet_id), 'POST', data)
return response
if wait:
action_id = response['action']['id']
wait_for_action_completed(action_id)
def status_droplet(droplet_id: int):
......@@ -274,8 +276,7 @@ def status_droplet(droplet_id: int):
def terminate_droplet(droplet_id: int):
"""Terminate a droplet by powering it down and deleting it."""
shutdown_droplet(droplet_id)
wait_for_state(droplet_id, 'off')
shutdown_droplet(droplet_id, True)
delete_droplet(droplet_id)
......@@ -311,7 +312,7 @@ def terminate_droplets_by_name(name_regex: str, ndays: int = 0,
# First stop all droplets
for droplet in terminate_droplets:
shutdown_droplet(droplet['id'])
shutdown_droplet(droplet['id'], True)
# Then delete all domain names associated with the droplets
for droplet in terminate_droplets:
......@@ -321,7 +322,6 @@ def terminate_droplets_by_name(name_regex: str, ndays: int = 0,
# Lastly delete all droplets:
for droplet in terminate_droplets:
wait_for_state(droplet['id'], 'off')
delete_droplet(droplet['id'])
......@@ -348,6 +348,20 @@ def wait_for_state(droplet_id: int, state):
status = status_droplet(droplet_id)
def wait_for_action_completed(action_id: int):
"""Wait for an action to be completed, by polling the action object."""
log.info('Waiting for action %s to complete...', action_id)
not_completed = "0000-00-00 00:00:00"
state = not_completed
while True:
response = request_api('actions/{0}'.format(action_id))
state = response['action']['completed_at']
log.debug(state)
if state != not_completed:
break
sleep(1)
# When called from from ipython, setup
# logging to console
try:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment