Create Key Pair

Factor out key pair creation into its own method to make key pair
creation clearer.

Change-Id: I871c95df12cf5d00484775389816900849789a80
This commit is contained in:
Everett Toews 2015-11-10 20:24:18 -06:00
parent fed616b849
commit 405dd19bea
2 changed files with 33 additions and 9 deletions

View File

@ -52,6 +52,18 @@ A **network** provides connectivity to servers.
Full example: `network resource list`_
Create Key Pair
---------------
A **key pair** is the public key and private key of
`publickey cryptography`_. They are used to encrypt and decrypt login
information when connecting to your server.
.. literalinclude:: ../examples/compute/create.py
:pyobject: create_keypair
Full example: `compute resource create`_
Create Server
-------------
@ -60,8 +72,8 @@ creation. You can discover the names and IDs of these attributes by listing
them as above and then using the find methods to get the appropriate
resources.
Ideally you'll also create a server using a public/private keypair so you can
login to that server with the private key.
Ideally you'll also create a server using a keypair so you can login to that
server with the private key.
Servers take time to boot so we call ``wait_for_server`` to wait
for it to become active.
@ -74,3 +86,4 @@ Full example: `compute resource create`_
.. _compute resource list: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/compute/list.py
.. _network resource list: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/network/list.py
.. _compute resource create: http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/examples/compute/create.py
.. _publickey cryptography: https://en.wikipedia.org/wiki/Public-key_cryptography

View File

@ -25,24 +25,35 @@ For a full guide see TODO(etoews):link to docs on developer.openstack.org
"""
def create_server(conn):
print("Create Server:")
def create_keypair(conn):
keypair = conn.compute.find_keypair(KEYPAIR_NAME)
image = conn.compute.find_image(IMAGE_NAME)
flavor = conn.compute.find_flavor(FLAVOR_NAME)
network = conn.network.find_network(NETWORK_NAME)
if not keypair:
print("Create Key Pair:")
if not conn.compute.find_keypair(KEYPAIR_NAME):
keypair = conn.compute.create_keypair(name=KEYPAIR_NAME)
print(keypair)
with open(PRIVATE_KEYPAIR_FILE, 'w') as f:
f.write("%s" % keypair.private_key)
os.chmod(PRIVATE_KEYPAIR_FILE, 0o400)
return keypair
def create_server(conn):
print("Create Server:")
image = conn.compute.find_image(IMAGE_NAME)
flavor = conn.compute.find_flavor(FLAVOR_NAME)
network = conn.network.find_network(NETWORK_NAME)
keypair = create_keypair(conn)
server = conn.compute.create_server(
name='openstacksdk-example', image=image, flavor=flavor,
networks=[{"uuid": network.id}], key_name=KEYPAIR_NAME)
networks=[{"uuid": network.id}], key_name=keypair.name)
server = conn.compute.wait_for_server(server)