Skip to content

Instantly share code, notes, and snippets.

@cleverdevil
Created May 23, 2014 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cleverdevil/6a5817d8c3b053603b7e to your computer and use it in GitHub Desktop.
Save cleverdevil/6a5817d8c3b053603b7e to your computer and use it in GitHub Desktop.
A little script I am using to time ephemeral boots in DreamCompute (currently trending between 42-60 seconds!).
#!/usr/bin/env python
import os
import sys
import time
import socket
from paramiko import SSHClient, AutoAddPolicy
from novaclient.v1_1 import client as compute_client
# create our credentials
credentials = dict(
username = os.environ['OS_USERNAME'],
api_key = os.environ['OS_PASSWORD'],
auth_url = os.environ['OS_AUTH_URL'],
project_id = os.environ['OS_TENANT_NAME']
)
# create a compute client
compute = compute_client.Client(**credentials)
# lookup details for flavor and image
flavor = compute.flavors.find(name='subsonic')
image = compute.images.find(name='Trusty')
# create the instance
instance = compute.servers.create(name='emphemeral-test',
image=image,
flavor=flavor,
key_name='cleverair')
# poll it until its ready
t_start = time.time()
status = instance.status
print 'BOOTING',
sys.stdout.flush()
while status != u'ACTIVE':
time.sleep(0.5)
instance = compute.servers.get(instance.id)
status = instance.status
print '.',
sys.stdout.flush()
t_active = time.time()
print
print 'ACTIVE'
sys.stdout.flush()
# poll for SSH
ipv6_address = [address['addr']
for address in instance.addresses['private-network']
if address['version'] == 6][0]
waiting = True
print 'WAITING FOR SSH',
sys.stdout.flush()
while waiting:
try:
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(ipv6_address, 22, 'dhc-user')
client.exec_command('ls')
client.close()
waiting = False
except socket.error:
print '.',
sys.stdout.flush()
print
t_ssh = time.time()
# summarize
print 'Active:', t_active - t_start
print ' SSH:', t_ssh - t_start
# clean up
instance.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment