Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save julianpistorius/8c680fb876a50981bf41a4504af76636 to your computer and use it in GitHub Desktop.
Save julianpistorius/8c680fb876a50981bf41a4504af76636 to your computer and use it in GitHub Desktop.
This quick and dirty Python script runs on a Jetstream2 instance, causing it to shelve itself (as long as OpenStack credentials are present in the environment). Lacks any error handling!
#!/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"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment