1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from novaclient.exceptions import NotFound
20 from snaps.openstack.utils import nova_utils
22 __author__ = 'spisarski'
24 logger = logging.getLogger('OpenStackKeypair')
27 class OpenStackKeypair:
29 Class responsible for creating a keypair in OpenStack
32 def __init__(self, os_creds, keypair_settings):
34 Constructor - all parameters are required
35 :param os_creds: The credentials to connect with OpenStack
36 :param keypair_settings: The settings used to create a keypair
39 self.__os_creds = os_creds
40 self.keypair_settings = keypair_settings
41 self.__nova = nova_utils.nova_client(os_creds)
43 # Attributes instantiated on create()
46 def create(self, cleanup=False):
48 Responsible for creating the keypair object.
49 :param cleanup: Denotes whether or not this is being called for cleanup or not
51 self.__nova = nova_utils.nova_client(self.__os_creds)
53 logger.info('Creating keypair %s...' % self.keypair_settings.name)
55 self.__keypair = nova_utils.get_keypair_by_name(self.__nova, self.keypair_settings.name)
57 if not self.__keypair and not cleanup:
58 if self.keypair_settings.public_filepath and os.path.isfile(self.keypair_settings.public_filepath):
59 logger.info("Uploading existing keypair")
60 self.__keypair = nova_utils.upload_keypair_file(self.__nova, self.keypair_settings.name,
61 self.keypair_settings.public_filepath)
63 logger.info("Creating new keypair")
64 # TODO - Make this value configurable
65 keys = nova_utils.create_keys(1024)
66 self.__keypair = nova_utils.upload_keypair(self.__nova, self.keypair_settings.name,
67 nova_utils.public_key_openssh(keys))
68 nova_utils.save_keys_to_files(keys, self.keypair_settings.public_filepath,
69 self.keypair_settings.private_filepath)
75 Removes and deletes the keypair.
79 nova_utils.delete_keypair(self.__nova, self.__keypair)
84 def get_keypair(self):
86 Returns the OpenStack keypair object
92 class KeypairSettings:
94 Class representing a keypair configuration
97 def __init__(self, config=None, name=None, public_filepath=None, private_filepath=None):
99 Constructor - all parameters are optional
100 :param config: Should be a dict object containing the configuration settings using the attribute names below
101 as each member's the key and overrides any of the other parameters.
102 :param name: The keypair name.
103 :param public_filepath: The path to/from the filesystem where the public key file is or will be stored
104 :param private_filepath: The path where the generated private key file will be stored
109 self.name = config.get('name')
110 self.public_filepath = config.get('public_filepath')
111 self.private_filepath = config.get('private_filepath')
114 self.public_filepath = public_filepath
115 self.private_filepath = private_filepath
118 raise Exception('The attributes name, public_filepath, and private_filepath are required')