Skip to content

Instantly share code, notes, and snippets.

@spjmurray
Created August 9, 2017 08:40
Show Gist options
  • Save spjmurray/60b14e06363d9c4c20213b79758e1504 to your computer and use it in GitHub Desktop.
Save spjmurray/60b14e06363d9c4c20213b79758e1504 to your computer and use it in GitHub Desktop.
Stopping an Instance in OpenStack
#!/usr/bin/python
"""
Demo to showdown an instance
"""
import time
from keystoneauth1 import session
from keystoneauth1.identity import v3
from novaclient import client as compute_client
def main():
"""
Get an instance by ID and shut it down
"""
# Setup my credentials ...
auth = v3.Password(auth_url='https://compute.datacentred.io:5000/v3',
username='USERNAME',
password='PASSWORD',
user_domain_name='default',
project_name='PROJECT',
project_domain_name='default')
# ... associate them with a session ...
password_session = session.Session(auth=auth)
# ... and create a client
compute = compute_client.Client(2, session=password_session)
# From the severs collection grab the server by ID ...
server = compute.servers.get('be2b932b-6388-43f4-af08-f0ff30519be3')
# ... and stop the resource
server.stop()
# You can even poll for when the operation has completed ...
while True:
temp = compute.servers.get(server.id)
if temp.status == 'SHUTOFF':
break
time.sleep(5)
if __name__ == '__main__':
main()
# vi: ts=4 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment