Skip to content

Commit

Permalink
Add delete confirmation + created at examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Powers committed Dec 19, 2024
1 parent 8115459 commit 78eae95
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Python-chi Examples
Cleanup Non-leased Resources
============================


Opt-in Cleanup using tags
-------------------------

While baremetal nodes on Chameleon require leases, other resources like networks, and KVM instances
are persistent, and require manual cleanup. For large projects (e.g. for educational courses), it
may be useful to have some automatic process to clean up resources after a certain time. Below is
Expand All @@ -22,9 +26,10 @@ you can set a "delete_at" tag. This is simply an date string in the future, whic
network.set_floating_ip_tag(MY_FIP_ADDRESS, f"delete_at={util.date_string_in_future(days=3)}")
network.set_network_tag(MY_NETWORK_ID, f"delete_at={util.date_string_in_future(days=3)}")
Then, set up the following script to run periodically using something like cron. You'll need
to set authentication variables (e.g. source an openrc file), and set the site/project. This script
iterates over the resources in your project, and deletes them if the "delete_at" tag is in the past.
Then run the following script. You'll need to set authentication variables (e.g. source an openrc file),
and set the site/project at the beginning. This script iterates over the resources in your project, and
deletes them if the "delete_at" tag is in the past, if the user manually enters "yes" to confirm.
You can remove this confirmation in order to automatically run periodically with e.g. cron.

.. code-block:: python
Expand All @@ -33,21 +38,55 @@ iterates over the resources in your project, and deletes them if the "delete_at"
# Find servers with "delete_at" metadata
for s in server.list_servers():
if util.should_delete(my_servers.get_metadata().get("delete_at", "")):
s.delete()
confirm = input(f"Delete server {s.name}? (yes/no): ").strip().lower()
if confirm == "yes":
s.delete()
# Networks, FIP tags set via tags as a string
for net in network.list_networks():
try:
delete_tag = next(s for s in net["tags"] if s.startswith("delete_at="))
if util.should_delete(delete_tag.split("=")[1]):
network.nuke_network(net_id)
confirm = input(f"Delete the network {net['name']}? (yes/no): ").strip().lower()
if confirm == "yes":
# nuke_network deletes subnets, routers, and the network itself
network.nuke_network(net_id)
except StopIteration:
pass
for fip in network.list_floating_ips():
try:
delete_tag = next(s for s in fip["tags"] if s.startswith("delete_at="))
if util.should_delete(delete_tag.split("=")[1]):
network.deallocate_floating_ip(fip["floating_ip_address"])
confirm = input(f"Delete the floating ip {fip['floating_ip_address']}? (yes/no): ").strip().lower()
if confirm == "yes":
network.deallocate_floating_ip(fip["floating_ip_address"])
except StopIteration:
pass
pass
Opt-in Cleanup using `created_at`
---------------------------------

The above examples are explicitly opt-in, requring the user to set the "delete_at" tag to ensure that
nothing is deleted unknowlingly. Alternatively, you can monitor the "created_at" field of resources to
check for long-running resources such as in the following example.

.. code-block:: python
from datetime import datetime, timedelta
from chi import util, server, network
MAX_AGE = 3 # days
for s in server.list_servers():
age = util.utcnow() - datetime.fromisoformat(s.created_at)
if age > timedelta(days=MAX_AGE):
confirm = input(f"Delete server {s.name}? Age is {age}. (yes/no): ").strip().lower()
if confirm == "yes":
s.delete()
for net in network.list_networks():
age = util.utcnow() - datetime.fromisoformat(net["created_at"])
if age > timedelta(days=MAX_AGE):
confirm = input(f"Delete the network {net['name']}? Age is {age}. (yes/no): ").strip().lower()
if confirm == "yes":
# nuke_network deletes subnets, routers, and the network itself
network.nuke_network(net_id)

0 comments on commit 78eae95

Please sign in to comment.