diff options
author | spisarski <s.pisarski@cablelabs.com> | 2018-01-22 19:27:31 -0700 |
---|---|---|
committer | spisarski <s.pisarski@cablelabs.com> | 2018-01-23 00:33:39 -0700 |
commit | 7ea11eada552627b5385a6a347f23fccee484e67 (patch) | |
tree | 701e7a2bb4d0e247868c9ae205766e6ac0530155 /snaps/openstack | |
parent | e2cbd241617b3eeaa62ab9f47ed4dff75f68b101 (diff) |
Added password support for SSH and Ansible
Additional protections when initializing network resources
Enhanced playbook runner variable support
Change-Id: Id897c4ece1de706afbc52d0a034ca3bfb68fdce3
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/openstack')
-rw-r--r-- | snaps/openstack/create_instance.py | 40 | ||||
-rw-r--r-- | snaps/openstack/create_network.py | 13 | ||||
-rw-r--r-- | snaps/openstack/create_router.py | 10 | ||||
-rw-r--r-- | snaps/openstack/utils/launch_utils.py | 2 | ||||
-rw-r--r-- | snaps/openstack/utils/nova_utils.py | 7 |
5 files changed, 51 insertions, 21 deletions
diff --git a/snaps/openstack/create_instance.py b/snaps/openstack/create_instance.py index d91e360..631ac6b 100644 --- a/snaps/openstack/create_instance.py +++ b/snaps/openstack/create_instance.py @@ -380,7 +380,7 @@ class OpenStackVmInstance(OpenStackComputeObject): logger.error('Cannot add floating IP [%s]', bre) raise except Exception as e: - logger.debug( + logger.warn( 'Retry adding floating IP to instance. Last attempt ' 'failed with - %s', e) time.sleep(poll_interval) @@ -509,8 +509,9 @@ class OpenStackVmInstance(OpenStackComputeObject): """ return ansible_utils.apply_playbook( pb_file_loc, [self.get_floating_ip(fip_name=fip_name).ip], - self.get_image_user(), self.keypair_settings.private_filepath, - variables, self._os_creds.proxy_settings) + self.get_image_user(), + ssh_priv_key_file_path=self.keypair_settings.private_filepath, + variables=variables, proxy_setting=self._os_creds.proxy_settings) def get_image_user(self): """ @@ -619,7 +620,8 @@ class OpenStackVmInstance(OpenStackComputeObject): status) return status == expected_status_code - def vm_ssh_active(self, block=False, poll_interval=POLL_INTERVAL): + def vm_ssh_active(self, user_override=None, password=None, block=False, + timeout=None, poll_interval=POLL_INTERVAL): """ Returns true when the VM can be accessed via SSH :param block: When true, thread will block until active or timeout @@ -630,7 +632,8 @@ class OpenStackVmInstance(OpenStackComputeObject): # sleep and wait for VM status change logger.info('Checking if VM is active') - timeout = self.instance_settings.ssh_connect_timeout + if not timeout: + timeout = self.instance_settings.ssh_connect_timeout if self.vm_active(block=True): if block: @@ -639,7 +642,8 @@ class OpenStackVmInstance(OpenStackComputeObject): start = time.time() - timeout while timeout > time.time() - start: - status = self.__ssh_active() + status = self.__ssh_active( + user_override=user_override, password=password) if status: logger.info('SSH is active for VM instance') return True @@ -653,13 +657,14 @@ class OpenStackVmInstance(OpenStackComputeObject): logger.error('Timeout attempting to connect with VM via SSH') return False - def __ssh_active(self): + def __ssh_active(self, user_override=None, password=None): """ Returns True when can create a SSH session else False :return: T/F """ if len(self.__floating_ip_dict) > 0: - ssh = self.ssh_client() + ssh = self.ssh_client( + user_override=user_override, password=password) if ssh: ssh.close() return True @@ -727,19 +732,32 @@ class OpenStackVmInstance(OpenStackComputeObject): else: return self.__get_first_provisioning_floating_ip() - def ssh_client(self, fip_name=None): + def ssh_client(self, fip_name=None, user_override=None, password=None): """ Returns an SSH client using the name or the first known floating IP if exists, else None :param fip_name: the name of the floating IP to return + :param user_override: the username to use instead of the default + :param password: the password to use instead of the private key :return: the SSH client or None """ fip = self.get_floating_ip(fip_name) + + ansible_user = self.get_image_user() + if user_override: + ansible_user = user_override + + if password: + private_key = None + else: + private_key = self.keypair_settings.private_filepath + if fip: return ansible_utils.ssh_client( self.__get_first_provisioning_floating_ip().ip, - self.get_image_user(), - self.keypair_settings.private_filepath, + ansible_user, + private_key_filepath=private_key, + password=password, proxy_settings=self._os_creds.proxy_settings) else: FloatingIPAllocationError( diff --git a/snaps/openstack/create_network.py b/snaps/openstack/create_network.py index c9c58e8..984eedd 100644 --- a/snaps/openstack/create_network.py +++ b/snaps/openstack/create_network.py @@ -15,7 +15,7 @@ import logging import enum -from neutronclient.common.exceptions import NetworkNotFoundClient +from neutronclient.common.exceptions import NetworkNotFoundClient, Unauthorized from snaps.config.network import NetworkConfig, SubnetConfig, PortConfig from snaps.openstack.openstack_creator import OpenStackNetworkObject @@ -51,9 +51,14 @@ class OpenStackNetwork(OpenStackNetworkObject): """ super(self.__class__, self).initialize() - self.__network = neutron_utils.get_network( - self._neutron, network_settings=self.network_settings, - project_id=self.network_settings.get_project_id(self._os_creds)) + try: + self.__network = neutron_utils.get_network( + self._neutron, network_settings=self.network_settings, + project_id=self.network_settings.get_project_id( + self._os_creds)) + except Unauthorized as e: + logger.warn('Unable to lookup network with name %s - %s', + self.network_settings.name, e) return self.__network diff --git a/snaps/openstack/create_router.py b/snaps/openstack/create_router.py index 4f95c3b..c9ccdd6 100644 --- a/snaps/openstack/create_router.py +++ b/snaps/openstack/create_router.py @@ -14,7 +14,7 @@ # limitations under the License. import logging -from neutronclient.common.exceptions import NotFound +from neutronclient.common.exceptions import NotFound, Unauthorized from snaps.config.router import RouterConfig from snaps.openstack.openstack_creator import OpenStackNetworkObject @@ -61,8 +61,12 @@ class OpenStackRouter(OpenStackNetworkObject): """ super(self.__class__, self).initialize() - self.__router = neutron_utils.get_router( - self._neutron, router_settings=self.router_settings) + try: + self.__router = neutron_utils.get_router( + self._neutron, router_settings=self.router_settings) + except Unauthorized as e: + logger.warn('Unable to lookup router with name %s - %s', + self.router_settings.name, e) if self.__router: for internal_subnet_name in self.router_settings.internal_subnets: diff --git a/snaps/openstack/utils/launch_utils.py b/snaps/openstack/utils/launch_utils.py index e10cf48..05d4cb5 100644 --- a/snaps/openstack/utils/launch_utils.py +++ b/snaps/openstack/utils/launch_utils.py @@ -462,7 +462,7 @@ def __apply_ansible_playbook(ansible_config, os_creds, vm_dict, image_dict, retval = ansible_utils.apply_playbook( ansible_config['playbook_location'], floating_ips, remote_user, - private_key_filepath, + ssh_priv_key_file_path=private_key_filepath, variables=variables, proxy_setting=proxy_settings) if retval != 0: diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py index e15484c..279e2ec 100644 --- a/snaps/openstack/utils/nova_utils.py +++ b/snaps/openstack/utils/nova_utils.py @@ -69,8 +69,11 @@ def create_server(nova, neutron, glance, instance_config, image_config, ports = list() for port_setting in instance_config.port_settings: - ports.append(neutron_utils.get_port( - neutron, port_settings=port_setting)) + port = neutron_utils.get_port(neutron, port_settings=port_setting) + if port: + ports.append(port) + else: + raise Exception('Cannot find port named - ' + port_setting.name) nics = [] for port in ports: kv = dict() |