Created
January 6, 2016 15:42
-
-
Save therve/9c7c762d3eb81b076be4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Example script creating a Heat stack and waiting for its completion by | |
receiving events over Zaqar. | |
Usage: | |
$ IDENTITY_API_VERSION=3 source devstack/openrc | |
$ create_stack_complete.py template.yaml stack_name | |
""" | |
import json | |
import os | |
import sys | |
import uuid | |
import websocket | |
from keystoneclient.auth.identity import v3 | |
from keystoneclient import session | |
from heatclient import client as heatclient | |
client_id = str(uuid.uuid4()) | |
queue_name = str(uuid.uuid4()) | |
environment_template = """ | |
event_sinks: | |
- type: zaqar-queue | |
target: %s | |
ttl: 3600 | |
""" | |
def authenticate(ws, token, project): | |
ws.send(json.dumps({'action': 'authenticate', | |
'headers': {'X-Auth-Token': token, | |
'Client-ID': client_id, | |
'X-Project-ID': project}})) | |
data = json.loads(ws.recv()) | |
if not data['headers']['status'] == 200: | |
raise RuntimeError(data) | |
def send_message(ws, project, action, body=None): | |
msg = {'action': action, | |
'headers': {'Client-ID': client_id, 'X-Project-ID': project}} | |
if body: | |
msg['body'] = body | |
ws.send(json.dumps(msg)) | |
data = json.loads(ws.recv()) | |
if data['headers']['status'] not in (200, 201): | |
raise RuntimeError(data) | |
def main(template, stack): | |
auth_url = os.environ.get('OS_AUTH_URL') | |
user = os.environ.get('OS_USERNAME') | |
password = os.environ.get('OS_PASSWORD') | |
project = os.environ.get('OS_TENANT_NAME') | |
auth = v3.Password(auth_url=auth_url, | |
username=user, | |
user_domain_name='default', | |
password=password, | |
project_name=project, | |
project_domain_name='default') | |
sess = session.Session(auth=auth) | |
token = auth.get_token(sess) | |
project = auth.auth_ref['project']['id'] | |
ws_url = auth.get_endpoint(sess, service_type='messaging-websocket') | |
ws = websocket.create_connection(ws_url.replace('http', 'ws')) | |
authenticate(ws, token, project) | |
send_message(ws, project, 'queue_create', {'queue_name': queue_name}) | |
send_message(ws, project, 'subscription_create', | |
{'queue_name': queue_name, 'ttl': 3000}) | |
environment = environment_template % queue_name | |
heat_url = auth.get_endpoint(sess, service_type='orchestration') | |
heat = heatclient.Client('1', endpoint=heat_url, token=token) | |
heat.stacks.create(template=open(template).read(), stack_name=stack, | |
environment=environment) | |
while True: | |
data = json.loads(ws.recv())['body']['payload'] | |
if data['resource_type'] == 'OS::Heat::Stack': | |
if data['resource_status'] != 'IN_PROGRESS': | |
print data['resource_status_reason'] | |
break | |
ws.close() | |
if __name__ == '__main__': | |
main(*sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment