diff options
Diffstat (limited to 'snaps/openstack/utils')
-rw-r--r-- | snaps/openstack/utils/glance_utils.py | 4 | ||||
-rw-r--r-- | snaps/openstack/utils/keystone_utils.py | 7 | ||||
-rw-r--r-- | snaps/openstack/utils/nova_utils.py | 67 |
3 files changed, 75 insertions, 3 deletions
diff --git a/snaps/openstack/utils/glance_utils.py b/snaps/openstack/utils/glance_utils.py index 2606e32..a127ad3 100644 --- a/snaps/openstack/utils/glance_utils.py +++ b/snaps/openstack/utils/glance_utils.py @@ -168,7 +168,7 @@ def __create_image_v2(glance, image_settings): """ cleanup_temp_file = False image_file = None - if image_settings.image_file: + if image_settings.image_file is not None: image_filename = image_settings.image_file elif image_settings.url: file_name = str(uuid.uuid4()) @@ -199,7 +199,7 @@ def __create_image_v2(glance, image_settings): kwargs.update(image_settings.extra_properties) os_image = glance.images.create(**kwargs) - image_file = open(image_filename, 'rb') + image_file = open(os.path.expanduser(image_filename), 'rb') glance.images.upload(os_image['id'], image_file) except: logger.error('Unexpected exception creating image. Rolling back') diff --git a/snaps/openstack/utils/keystone_utils.py b/snaps/openstack/utils/keystone_utils.py index b36c19f..46f6fb8 100644 --- a/snaps/openstack/utils/keystone_utils.py +++ b/snaps/openstack/utils/keystone_utils.py @@ -176,6 +176,7 @@ def create_project(keystone, project_settings): enabled=project_settings.enabled) domain_id = os_project.domain_id + logger.info('Created project with name - %s', project_settings.name) return Project( name=os_project.name, project_id=os_project.id, domain_id=domain_id) @@ -186,6 +187,7 @@ def delete_project(keystone, project): :param keystone: the Keystone clien :param project: the SNAPS-OO Project domain object """ + logger.info('Deleting project with name - %s', project.name) if keystone.version == V2_VERSION_STR: keystone.tenants.delete(project.id) else: @@ -273,6 +275,7 @@ def create_user(keystone, user_settings): project=os_project) if os_user: + logger.info('Created user with name - %s', os_user.name) return User(name=os_user.name, user_id=os_user.id) @@ -282,6 +285,7 @@ def delete_user(keystone, user): :param keystone: the Keystone client :param user: the SNAPS-OO User domain object """ + logger.info('Deleting user with name - %s', user.name) keystone.users.delete(user.id) @@ -337,6 +341,7 @@ def create_role(keystone, name): :return: a SNAPS-OO Role domain object """ role = keystone.roles.create(name) + logger.info('Created role with name - %s', role.name) return Role(name=role.name, role_id=role.id) @@ -347,6 +352,7 @@ def delete_role(keystone, role): :param role: the SNAPS-OO Role domain object to delete :return: """ + logger.info('Deleting role with name - %s', role.name) keystone.roles.delete(role.id) @@ -361,6 +367,7 @@ def grant_user_role_to_project(keystone, role, user, project): """ os_role = get_role_by_id(keystone, role.id) + logger.info('Granting role %s to project %s', role.name, project) if keystone.version == V2_VERSION_STR: keystone.roles.add_user_role(user, os_role, tenant=project) else: 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 |