Deprecating launch app and ansible support.
[snaps.git] / snaps / openstack / create_instance.py
index cdc778f..b158a25 100644 (file)
@@ -19,7 +19,8 @@ from novaclient.exceptions import NotFound
 
 from snaps.config.vm_inst import VmInstanceConfig, FloatingIpConfig
 from snaps.openstack.openstack_creator import OpenStackComputeObject
-from snaps.openstack.utils import glance_utils, cinder_utils, settings_utils
+from snaps.openstack.utils import (
+    glance_utils, cinder_utils, settings_utils, keystone_utils)
 from snaps.openstack.utils import neutron_utils
 from snaps.openstack.utils import nova_utils
 from snaps.openstack.utils.nova_utils import RebootType
@@ -72,7 +73,14 @@ class OpenStackVmInstance(OpenStackComputeObject):
         """
         super(self.__class__, self).initialize()
 
-        self.__neutron = neutron_utils.neutron_client(self._os_creds)
+        self.__neutron = neutron_utils.neutron_client(
+            self._os_creds, self._os_session)
+        self.__keystone = keystone_utils.keystone_client(
+            self._os_creds, self._os_session)
+        self.__cinder = cinder_utils.cinder_client(
+            self._os_creds, self._os_session)
+        self.__glance = glance_utils.glance_client(
+            self._os_creds, self._os_session)
 
         self.__ports = self.__query_ports(self.instance_settings.port_settings)
         self.__lookup_existing_vm_by_name()
@@ -88,7 +96,7 @@ class OpenStackVmInstance(OpenStackComputeObject):
         """
         self.initialize()
 
-        if len(self.__ports) == 0:
+        if len(self.__ports) != len(self.instance_settings.port_settings):
             self.__ports = self.__create_ports(
                 self.instance_settings.port_settings)
         if not self.__vm:
@@ -103,7 +111,7 @@ class OpenStackVmInstance(OpenStackComputeObject):
         within the project
         """
         server = nova_utils.get_server(
-            self._nova, self.__neutron,
+            self._nova, self.__neutron, self.__keystone,
             vm_inst_settings=self.instance_settings)
         if server:
             if server.name == self.instance_settings.name:
@@ -112,8 +120,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
                     'Found existing machine with name - %s',
                     self.instance_settings.name)
 
-                fips = neutron_utils.get_floating_ips(self.__neutron,
-                                                      self.__ports)
+                fips = neutron_utils.get_port_floating_ips(
+                    self.__neutron, self.__ports)
                 for port_id, fip in fips:
                     settings = self.instance_settings.floating_ip_settings
                     for fip_setting in settings:
@@ -132,10 +140,10 @@ class OpenStackVmInstance(OpenStackComputeObject):
                       active, error, or timeout waiting. Floating IPs will be
                       assigned after active when block=True
         """
-        glance = glance_utils.glance_client(self._os_creds)
         self.__vm = nova_utils.create_server(
-            self._nova, self.__neutron, glance, self.instance_settings,
-            self.image_settings, self.keypair_settings)
+            self._nova, self.__keystone, self.__neutron, self.__glance,
+            self.instance_settings, self.image_settings,
+            self._os_creds.project_name, self.keypair_settings)
         logger.info('Created instance with name - %s',
                     self.instance_settings.name)
 
@@ -159,20 +167,20 @@ class OpenStackVmInstance(OpenStackComputeObject):
 
         if self.instance_settings.volume_names:
             for volume_name in self.instance_settings.volume_names:
-                cinder = cinder_utils.cinder_client(self._os_creds)
                 volume = cinder_utils.get_volume(
-                    cinder, volume_name=volume_name)
+                    self.__cinder, self.__keystone, volume_name=volume_name,
+                    project_name=self._os_creds.project_name)
 
                 if volume and self.vm_active(block=True):
-                    timeout = 30
                     vm = nova_utils.attach_volume(
-                        self._nova, self.__neutron, self.__vm, volume, timeout)
+                        self._nova, self.__neutron, self.__keystone, self.__vm,
+                        volume, self._os_creds.project_name)
 
                     if vm:
                         self.__vm = vm
                     else:
-                        logger.warn('Volume [%s] not attached within timeout '
-                                    'of [%s]', volume.name, timeout)
+                        logger.warn(
+                            'Volume [%s] attachment timeout ', volume.name)
                 else:
                     logger.warn('Unable to attach volume named [%s]',
                                 volume_name)
@@ -215,7 +223,7 @@ class OpenStackVmInstance(OpenStackComputeObject):
             floating_ip_setting.router_name)
         if ext_gateway and self.vm_active(block=True):
             floating_ip = neutron_utils.create_floating_ip(
-                self.__neutron, ext_gateway, port.id)
+                self.__neutron, self.__keystone, ext_gateway, port.id)
             self.__floating_ip_dict[floating_ip_setting.name] = floating_ip
 
             logger.info(
@@ -236,7 +244,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
         :return: the external network name or None
         """
         router = neutron_utils.get_router(
-            self.__neutron, router_name=router_name)
+            self.__neutron, self.__keystone, router_name=router_name,
+            project_name=self._os_creds.project_name)
         if router and router.external_network_id:
             network = neutron_utils.get_network_by_id(
                 self.__neutron, router.external_network_id)
@@ -266,12 +275,12 @@ class OpenStackVmInstance(OpenStackComputeObject):
         if self.__vm:
             # Detach Volume
             for volume_rec in self.__vm.volume_ids:
-                cinder = cinder_utils.cinder_client(self._os_creds)
                 volume = cinder_utils.get_volume_by_id(
-                    cinder, volume_rec['id'])
+                    self.__cinder, volume_rec['id'])
                 if volume:
                     vm = nova_utils.detach_volume(
-                        self._nova, self.__neutron, self.__vm, volume, 30)
+                        self._nova, self.__neutron, self.__keystone, self.__vm,
+                        volume, self._os_creds.project_name)
                     if vm:
                         self.__vm = vm
                     else:
@@ -304,6 +313,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
                     'VM not deleted within the timeout period of %s '
                     'seconds', self.instance_settings.vm_delete_timeout)
 
+        super(self.__class__, self).clean()
+
     def __query_ports(self, port_settings):
         """
         Returns the previously configured ports or an empty list if none
@@ -316,7 +327,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
 
         for port_setting in port_settings:
             port = neutron_utils.get_port(
-                self.__neutron, port_settings=port_setting)
+                self.__neutron, self.__keystone, port_settings=port_setting,
+                project_name=self._os_creds.project_name)
             if port:
                 ports.append((port_setting.name, port))
 
@@ -334,12 +346,13 @@ class OpenStackVmInstance(OpenStackComputeObject):
 
         for port_setting in port_settings:
             port = neutron_utils.get_port(
-                self.__neutron, port_settings=port_setting)
+                self.__neutron, self.__keystone, port_settings=port_setting,
+                project_name=self._os_creds.project_name)
             if not port:
                 port = neutron_utils.create_port(
                     self.__neutron, self._os_creds, port_setting)
-                if port:
-                    ports.append((port_setting.name, port))
+            if port:
+                ports.append((port_setting.name, port))
 
         return ports
 
@@ -356,7 +369,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
         :return: Server object
         """
         return nova_utils.get_server_object_by_id(
-            self._nova, self.__neutron, self.__vm.id)
+            self._nova, self.__neutron, self.__keystone, self.__vm.id,
+            self._os_creds.project_name)
 
     def get_console_output(self):
         """
@@ -377,8 +391,10 @@ class OpenStackVmInstance(OpenStackComputeObject):
         port = self.get_port_by_name(port_name)
         if port:
             if subnet_name:
+                network = neutron_utils.get_network_by_id(
+                    self.__neutron, port.network_id)
                 subnet = neutron_utils.get_subnet(
-                    self.__neutron, subnet_name=subnet_name)
+                    self.__neutron, network, subnet_name=subnet_name)
                 if not subnet:
                     logger.warning('Cannot retrieve port IP as subnet could '
                                    'not be located with name - %s',
@@ -423,6 +439,10 @@ class OpenStackVmInstance(OpenStackComputeObject):
         Returns a dictionary of a VMs info as returned by OpenStack
         :return: a dict()
         """
+        from warnings import warn
+        warn('Do not use the returned dict() structure',
+             DeprecationWarning)
+
         return nova_utils.get_server_info(self._nova, self.__vm)
 
     def __get_first_provisioning_floating_ip(self):
@@ -454,9 +474,12 @@ class OpenStackVmInstance(OpenStackComputeObject):
                           playbook
         :param fip_name: the name of the floating IP to use for applying the
                          playbook (default - will take the first)
-        :return: the return value from ansible
         """
-        return ansible_utils.apply_playbook(
+        from warnings import warn
+        warn('This method will be removed in a subsequent release',
+             DeprecationWarning)
+
+        ansible_utils.apply_playbook(
             pb_file_loc, [self.get_floating_ip(fip_name=fip_name).ip],
             self.get_image_user(),
             ssh_priv_key_file_path=self.keypair_settings.private_filepath,
@@ -504,7 +527,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
                 STATUS_ACTIVE, block, self.instance_settings.vm_boot_timeout,
                 poll_interval):
             self.__vm = nova_utils.get_server_object_by_id(
-                self._nova, self.__neutron, self.__vm.id)
+                self._nova, self.__neutron, self.__keystone, self.__vm.id,
+                self._os_creds.project_name)
             return True
         return False
 
@@ -771,24 +795,32 @@ class OpenStackVmInstance(OpenStackComputeObject):
             self._nova, self.__vm, reboot_type=reboot_type)
 
 
-def generate_creator(os_creds, vm_inst, image_config, keypair_config=None):
+def generate_creator(os_creds, vm_inst, image_config, project_name,
+                     keypair_config=None):
     """
     Initializes an OpenStackVmInstance object
     :param os_creds: the OpenStack credentials
     :param vm_inst: the SNAPS-OO VmInst domain object
     :param image_config: the associated ImageConfig object
+    :param project_name: the associated project ID
     :param keypair_config: the associated KeypairConfig object (optional)
     :return: an initialized OpenStackVmInstance object
     """
-    nova = nova_utils.nova_client(os_creds)
-    neutron = neutron_utils.neutron_client(os_creds)
-    derived_inst_config = settings_utils.create_vm_inst_config(
-        nova, neutron, vm_inst)
-
-    derived_inst_creator = OpenStackVmInstance(
-        os_creds, derived_inst_config, image_config, keypair_config)
-    derived_inst_creator.initialize()
-    return derived_inst_creator
+    session = keystone_utils.keystone_session(os_creds)
+    nova = nova_utils.nova_client(os_creds, session)
+    keystone = keystone_utils.keystone_client(os_creds, session)
+    neutron = neutron_utils.neutron_client(os_creds, session)
+
+    try:
+        derived_inst_config = settings_utils.create_vm_inst_config(
+            nova, keystone, neutron, vm_inst, project_name)
+
+        derived_inst_creator = OpenStackVmInstance(
+            os_creds, derived_inst_config, image_config, keypair_config)
+        derived_inst_creator.initialize()
+        return derived_inst_creator
+    finally:
+        keystone_utils.close_session(session)
 
 
 class VmInstanceSettings(VmInstanceConfig):