#!/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"])