summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils/nova_utils.py
diff options
context:
space:
mode:
authorspisarski <s.pisarski@cablelabs.com>2017-08-23 13:26:36 -0600
committerspisarski <s.pisarski@cablelabs.com>2017-09-08 09:27:24 -0600
commitc021ff613049c36943916296d736a3388238705a (patch)
tree3fa92a59107737eff8952b283916dbe4c8ec9143 /snaps/openstack/utils/nova_utils.py
parent23b3a1f209ee613982de9e759b1879d771b91f5c (diff)
Enhanced launch.py app to support all types.
Added application support for users, projects, and security groups. In addition, added support for multiple credential sets so one can manage multiple projects potentially on multiple clouds. Added Jinja2 template support for substituting values in the template file with values contained in the environment file. Added ansible substituion value for a VM's floating IP. Changed credentials interface attribute's default value from 'admin' to 'public'. Added optional pre_sleep_time attribute to ansible execution that will wait for a given number of seconds prior to attempting to apply a playbook to a set of VMs. JIRA: SNAPS-4, SNAPS-5, SNAPS-6, SNAPS-26 Change-Id: I67b8d69a3b06a43631d80e8fe0c56e02773dbfbe Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/openstack/utils/nova_utils.py')
-rw-r--r--snaps/openstack/utils/nova_utils.py67
1 files changed, 66 insertions, 1 deletions
diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py
index fe53211..1665fd0 100644
--- a/snaps/openstack/utils/nova_utils.py
+++ b/snaps/openstack/utils/nova_utils.py
@@ -22,6 +22,7 @@ from cryptography.hazmat.primitives.asymmetric import rsa
from novaclient.client import Client
from novaclient.exceptions import NotFound
+from snaps import file_utils
from snaps.domain.flavor import Flavor
from snaps.domain.keypair import Keypair
from snaps.domain.project import ComputeQuotas
@@ -86,6 +87,18 @@ def create_server(nova, neutron, glance, instance_settings, image_settings,
image = glance_utils.get_image(glance, image_settings=image_settings)
if image:
+ userdata = None
+ if instance_settings.userdata:
+ if isinstance(instance_settings.userdata, str):
+ userdata = instance_settings.userdata + '\n'
+ elif (isinstance(instance_settings.userdata, dict) and
+ 'script_file' in instance_settings.userdata):
+ try:
+ userdata = file_utils.read_file(
+ instance_settings.userdata['script_file'])
+ except Exception as e:
+ logger.warn('error reading userdata file %s - %s',
+ instance_settings.userdata, e)
args = {'name': instance_settings.name,
'flavor': flavor,
'image': image,
@@ -93,7 +106,7 @@ def create_server(nova, neutron, glance, instance_settings, image_settings,
'key_name': keypair_name,
'security_groups':
instance_settings.security_group_names,
- 'userdata': instance_settings.userdata}
+ 'userdata': userdata}
if instance_settings.availability_zone:
args['availability_zone'] = instance_settings.availability_zone
@@ -264,6 +277,58 @@ def public_key_openssh(keys):
serialization.PublicFormat.OpenSSH)
+def save_keys_to_files(keys=None, pub_file_path=None, priv_file_path=None):
+ """
+ Saves the generated RSA generated keys to the filesystem
+ :param keys: the keys to save generated by cryptography
+ :param pub_file_path: the path to the public keys
+ :param priv_file_path: the path to the private keys
+ """
+ if keys:
+ if pub_file_path:
+ # To support '~'
+ pub_expand_file = os.path.expanduser(pub_file_path)
+ pub_dir = os.path.dirname(pub_expand_file)
+
+ if not os.path.isdir(pub_dir):
+ os.mkdir(pub_dir)
+
+ public_handle = None
+ try:
+ public_handle = open(pub_expand_file, 'wb')
+ public_bytes = keys.public_key().public_bytes(
+ serialization.Encoding.OpenSSH,
+ serialization.PublicFormat.OpenSSH)
+ public_handle.write(public_bytes)
+ finally:
+ if public_handle:
+ public_handle.close()
+
+ os.chmod(pub_expand_file, 0o600)
+ logger.info("Saved public key to - " + pub_expand_file)
+ if priv_file_path:
+ # To support '~'
+ priv_expand_file = os.path.expanduser(priv_file_path)
+ priv_dir = os.path.dirname(priv_expand_file)
+ if not os.path.isdir(priv_dir):
+ os.mkdir(priv_dir)
+
+ private_handle = None
+ try:
+ private_handle = open(priv_expand_file, 'wb')
+ private_handle.write(
+ keys.private_bytes(
+ encoding=serialization.Encoding.PEM,
+ format=serialization.PrivateFormat.TraditionalOpenSSL,
+ encryption_algorithm=serialization.NoEncryption()))
+ finally:
+ if private_handle:
+ private_handle.close()
+
+ os.chmod(priv_expand_file, 0o600)
+ logger.info("Saved private key to - " + priv_expand_file)
+
+
def upload_keypair_file(nova, name, file_path):
"""
Uploads a public key from a file