How to deploy an app in an OpenStack instance with Python and Shade

Now that you know how to create and destroy instances, you can deploy the sample application. The instance that you create for the application is similar to the first instance that you created, but this time, we’ll briefly introduce a few extra concepts.

Note

Internet connectivity from your cloud instance is required to download the application.

When you create an instance for the application, you’ll want to give it a bit more information than you supplied to the bare instance that you just created and destroyed. We’ll go into more detail in later sections, but for now, simply create the following resources so that you can feed them to the instance:

  • A key pair. To access your instance, you must import an SSH public key into OpenStack to create a key pair. OpenStack installs this key pair on the new instance. Typically, your public key is written to .ssh/id_rsa.pub. If you do not have an SSH public key file, follow these instructions first.

In the following example, pub_key_file should be set to the location of your public SSH key file.

print('Checking for existing SSH keypair...')
keypair_name = 'demokey'
pub_key_file = '~/.ssh/demo_key.pub'

if conn.search_keypairs(keypair_name):
    print('Keypair already exists. Skipping import.')
else:
    print('Adding keypair...')
    conn.create_keypair(keypair_name, open(pub_key_file, 'r').read().strip())

for keypair in conn.list_keypairs():
    print(keypair)

  • Network access. DreamCompute has a default security group that will work for our purposes. If you want, you can create another security group by following along below. This security group allows HTTP and SSH access.
print('Checking for existing security groups...')
sec_group_name = 'all-in-one'
if conn.search_security_groups(sec_group_name):
    print('Security group already exists. Skipping creation.')
else:
    print('Creating security group.')
    conn.create_security_group(sec_group_name, 'network access for all-in-one application.')
    conn.create_security_group_rule(sec_group_name, 80, 80, 'TCP')
    conn.create_security_group_rule(sec_group_name, 22, 22, 'TCP')

conn.search_security_groups(sec_group_name)

  • User data. During instance creation, you can provide user data to OpenStack to configure instances after they boot. The cloud-init service applies the user data to an instance. You must pre-install the cloud-init service on your chosen image.
ex_userdata = '''#!/usr/bin/env bash

curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
-i faafo -i messaging -r api -r worker -r demo
'''

Now, you can boot and configure the instance.

Boot and configure an instance

Use the image, flavor, key pair, and userdata to create an instance. After you request the instance, wait for it to build.

The shade framework can select and assign a free floating IP quickly

instance_name = 'all-in-one'
testing_instance = conn.create_server(wait=True, auto_ip=False,
    name=instance_name,
    image=image['id'],
    flavor=flavor['id'],
    key_name=keypair_name,
    security_groups=[sec_group_name],
    userdata=ex_userdata,
    network='public')

ip_address = testing_instance['addresses']['public'][0]['addr']
#step-15
print('The Fractals app will be deployed to http://%s' % ip_address )

When the instance boots, the ex_userdata variable value instructs the instance to deploy the Fractals application.

Run the script to start the deployment.

Access the application

Deploying application data and configuration to the instance can take some time. Consider enjoying a cup of coffee while you wait. After the application deploys, you can visit the awesome graphic interface at the following link by using your preferred browser.

print('The Fractals app will be deployed to http://%s' % ip_address )
screenshot of the webinterface

Complete code sample

The following file contains all of the code from this section of the tutorial. This comprehensive code sample lets you view and run the code as a single script.

Before you run this script, confirm that you have set your authentication information, the flavor ID, and image ID.

#step-1
from shade import *

simple_logging(debug=True)
conn = openstack_cloud(cloud='dreamcompute')

#step-2
images = conn.list_images()
for image in images:
    print(image)

#step-3
flavors =  conn.list_flavors()
for flavor in flavors:
    print(flavor)

#step-4
image_name = 'Ubuntu-14.04'
image = conn.get_image(image_name)
print(image)

#step-5
flavor_name = 'gp1.semisonic'
flavor = conn.get_flavor(flavor_name)
print(flavor)

#step-6
instance_name = 'testing'
testing_instance = conn.create_server(wait=True, auto_ip=True,
    name=instance_name,
    image=image['id'],
    flavor=flavor['id'],
    network='public'
    )
print(testing_instance)

#step-7
instances = conn.list_servers()
for instance in instances:
    print(instance)

#step-8
conn.delete_server(name_or_id=instance_name)

#step-9
print('Checking for existing SSH keypair...')
keypair_name = 'demokey'
pub_key_file = '~/.ssh/demo_key.pub'

if conn.search_keypairs(keypair_name):
    print('Keypair already exists. Skipping import.')
else:
    print('Adding keypair...')
    conn.create_keypair(keypair_name, open(pub_key_file, 'r').read().strip())

for keypair in conn.list_keypairs():
    print(keypair)

#step-10
print('Checking for existing security groups...')
sec_group_name = 'all-in-one'
if conn.search_security_groups(sec_group_name):
    print('Security group already exists. Skipping creation.')
else:
    print('Creating security group.')
    conn.create_security_group(sec_group_name, 'network access for all-in-one application.')
    conn.create_security_group_rule(sec_group_name, 80, 80, 'TCP')
    conn.create_security_group_rule(sec_group_name, 22, 22, 'TCP')

conn.search_security_groups(sec_group_name)

#step-11
ex_userdata = '''#!/usr/bin/env bash

curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
-i faafo -i messaging -r api -r worker -r demo
'''

#step-12
instance_name = 'all-in-one'
testing_instance = conn.create_server(wait=True, auto_ip=False,
    name=instance_name,
    image=image['id'],
    flavor=flavor['id'],
    key_name=keypair_name,
    security_groups=[sec_group_name],
    userdata=ex_userdata,
    network='public')

ip_address = testing_instance['addresses']['public'][0]['addr']
#step-15
print('The Fractals app will be deployed to http://%s' % ip_address )

Did this article answer your questions?

Article last updated .