Skip to content

Instantly share code, notes, and snippets.

@zacharygraber
Created October 29, 2024 19:10
Show Gist options
  • Save zacharygraber/ec88cd0b47c0753b3f0f7ded11cfa780 to your computer and use it in GitHub Desktop.
Save zacharygraber/ec88cd0b47c0753b3f0f7ded11cfa780 to your computer and use it in GitHub Desktop.

Revisions

  1. zacharygraber created this gist Oct 29, 2024.
    20 changes: 20 additions & 0 deletions shelve_current_instance.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/bin/python3

    # Requires the openstacksdk pip package - openstacksdk==4.0.0

    import openstack
    from urllib.request import urlopen
    import json

    # Initiate connection to the OpenStack API (get a token)
    # Expects credentials to be available in "OS_"-prefixed env vars
    conn = openstack.connect()

    # Query the OpenStack metadata service to find the UUID of the instance we're currently running on
    # see https://docs.openstack.org/nova/latest/user/metadata.html
    response = urlopen("http://169.254.169.254/openstack/latest/meta_data.json")
    json_data = json.loads(response.read())

    # Here's where you could put other fun stuff, like an email/Slack notification to yourself

    conn.compute.shelve_server(json_data["uuid"])