diff --git a/examples/compute/create.py b/examples/compute/create.py index 847eba7ba..90d018974 100644 --- a/examples/compute/create.py +++ b/examples/compute/create.py @@ -10,6 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. +import errno import os from examples.connect import FLAVOR_NAME @@ -17,6 +18,7 @@ from examples.connect import IMAGE_NAME from examples.connect import KEYPAIR_NAME from examples.connect import NETWORK_NAME from examples.connect import PRIVATE_KEYPAIR_FILE +from examples.connect import SSH_DIR """ Create resources with the Compute service. @@ -35,6 +37,12 @@ def create_keypair(conn): print(keypair) + try: + os.mkdir(SSH_DIR) + except OSError as e: + if e.errno != errno.EEXIST: + raise e + with open(PRIVATE_KEYPAIR_FILE, 'w') as f: f.write("%s" % keypair.private_key) diff --git a/examples/connect.py b/examples/connect.py index 03e5f93b6..a2d678942 100644 --- a/examples/connect.py +++ b/examples/connect.py @@ -56,11 +56,11 @@ IMAGE_NAME = _get_resource_value('image_name', 'fedora-20.x86_64') FLAVOR_NAME = _get_resource_value('flavor_name', 'm1.small') NETWORK_NAME = _get_resource_value('network_name', 'private') KEYPAIR_NAME = _get_resource_value('keypair_name', 'openstacksdk-example') -PRIVATE_KEYPAIR_FILE = _get_resource_value('private_keypair_file', - "{home}/{ssh}/id_rsa.{key}".format( - home=os.getenv("HOME"), - ssh='.ssh', - key=KEYPAIR_NAME)) +SSH_DIR = _get_resource_value( + 'ssh_dir', '{home}/.ssh'.format(home=os.path.expanduser("~"))) +PRIVATE_KEYPAIR_FILE = _get_resource_value( + 'private_keypair_file', '{ssh_dir}/id_rsa.{key}'.format( + ssh_dir=SSH_DIR, key=KEYPAIR_NAME)) def create_connection_from_config():