コード例 #1
0
 def get_network_handler(self):
     if not self.quantum_h:
         self.quantum_h = QuantumHelper(username=self.username,
                                        password=self.password,
                                        project_id=self.project_id,
                                        inputs=self.inputs)
         self.quantum_h.setUp()
     return self.quantum_h
コード例 #2
0
 def get_network_handler(self):
     if not self.quantum_h: 
         self.quantum_h = QuantumHelper(username=self.username,
                                        password=self.password,
                                        project_id=self.project_id,
                                        auth_server_ip=self.auth_server_ip,
                                        logger=self.logger)
         self.quantum_h.setUp()
     return self.quantum_h
コード例 #3
0
 def __init__(self, inputs, username, password, project_name, logger):
     self.inputs = inputs
     self.logger = logger
     self.quantum_h = QuantumHelper(username=username,
                                    password=password,
                                    inputs=inputs,
                                    project_name=project_name,
                                    openstack_ip=inputs.openstack_ip)
     self.nova_h = NovaHelper(inputs=inputs,
                              project_name=project_name,
                              username=username,
                              password=password,
                              update_ssh_key=False)
コード例 #4
0
 def __init__(self, inputs, username, password, project_name, project_id,
              vnclib, logger):
     self.inputs = inputs
     self.logger = logger
     self.quantum_h = QuantumHelper(username=username,
                                    password=password,
                                    inputs=inputs,
                                    project_id=project_id,
                                    cfgm_ip=inputs.cfgm_ip,
                                    openstack_ip=inputs.openstack_ip)
     self.nova_h = NovaHelper(inputs=inputs,
                              project_name=project_name,
                              username=username,
                              password=password)
コード例 #5
0
 def get_network_handler(self):
     if not self.quantum_h: 
         self.quantum_h = QuantumHelper(username=self.username,
                                        password=self.password,
                                        project_id=self.project_id,
                                        inputs=self.inputs)
         self.quantum_h.setUp()
     return self.quantum_h
コード例 #6
0
 def __init__(self, inputs, username, password, project_name, logger):
     self.inputs = inputs
     self.logger = logger
     self.quantum_h = QuantumHelper(username=username, password=password,
                                  inputs=inputs, project_name=project_name,
                                  openstack_ip=inputs.openstack_ip)
     self.nova_h = NovaHelper(inputs=inputs, project_name=project_name,
                            username=username, password=password,update_ssh_key=False)
コード例 #7
0
ファイル: openstack.py プロジェクト: hkumarmk/aaa
 def get_network_handler(self):
     if not self.quantum_h: 
         self.quantum_h = QuantumHelper(username=self.username,
                                        password=self.password,
                                        project_id=self.project_id,
                                        auth_server_ip=self.auth_server_ip,
                                        logger=self.logger)
         self.quantum_h.setUp()
     return self.quantum_h
コード例 #8
0
ファイル: openstack.py プロジェクト: smurugap/contrail-test
 def __init__(self, inputs, username, password, project_name, project_id,
               vnclib, logger):
     self.inputs = inputs
     self.logger = logger
     self.quantum_h = QuantumHelper(username=username, password=password,
                                  inputs=inputs, project_id=project_id,
                                  cfgm_ip=inputs.cfgm_ip,
                                  openstack_ip=inputs.openstack_ip)
     self.nova_h = NovaHelper(inputs=inputs, project_name=project_name,
                            username=username, password=password)
コード例 #9
0
ファイル: openstack.py プロジェクト: hkumarmk/aaa
class OpenstackOrchestrator(ContrailApi):

   def __init__(self, inputs, username, password, project_name, project_id,
                 vnclib=None, logger=None, auth_server_ip=None):
       self.logger = logger or logging.getLogger(__name__)
       super(OpenstackOrchestrator, self).__init__(inputs, vnclib, self.logger)
       self.inputs = inputs
       self.quantum_h = None
       self.nova_h = None
       self.username = username
       self.password = password
       self.project_name = project_name
       self.project_id = project_id 
       self.vnc_lib = vnclib
       self.auth_server_ip = auth_server_ip
       if not auth_server_ip:
           self.auth_server_ip = self.inputs.auth_ip

   def get_network_handler(self):
       if not self.quantum_h: 
           self.quantum_h = QuantumHelper(username=self.username,
                                          password=self.password,
                                          project_id=self.project_id,
                                          auth_server_ip=self.auth_server_ip,
                                          logger=self.logger)
           self.quantum_h.setUp()
       return self.quantum_h

   def get_compute_handler(self):
       if not self.nova_h:
          self.nova_h = NovaHelper(inputs=self.inputs,
                                   project_name=self.project_name,
                                   username=self.username,
                                   password=self.password)
       return self.nova_h

   def get_image_account(self, image_name):
       return self.nova_h.get_image_account(image_name)

   def get_image_name_for_zone(self, image_name='ubuntu', zone='nova'):
       return self.nova_h.get_image_name_for_zone(image_name, zone)

   def get_hosts(self, zone=None):
       if not zone:
          return self.nova_h.get_hosts()
       else:
          return self.nova_h.get_hosts(zone)

   def get_zones(self):
       return self.nova_h.get_zones()

   def create_vm(self, vm_name, image_name, vn_objs, **kwargs):
       vn_ids = [vn['network']['id'] for vn in vn_objs]
       return self.nova_h.create_vm(vm_name=vm_name, image_name=image_name, vn_ids=vn_ids, **kwargs)

   def delete_vm(self, vm_obj, **kwargs):
       return self.nova_h.delete_vm(vm_obj)

   def is_vm_deleted(self, vm_obj, **kwargs):
       return self.nova_h.is_vm_deleted_in_nova_db(vm_obj, self._inputs.openstack_ip)

   def get_host_of_vm(self, vm_obj, **kwargs):
       return self.nova_h.get_nova_host_of_vm(vm_obj)

   def get_networks_of_vm(self, vm_obj, **kwargs):
       vm_obj.get()
       return vm_obj.networks.keys()

   def wait_till_vm_is_active(self, vm_obj, **kwargs):
       return self.nova_h.wait_till_vm_is_active(vm_obj)

   def wait_till_vm_status(self, vm_obj, status, **kwargs):
       return self.nova_h.wait_till_vm_status(vm_obj, status)

   def get_console_output(self, vm_obj, **kwargs):
       return self.nova_h.get_vm_console_output(vm_obj)

   def get_vm_by_id(self, vm_id, **kwargs):
       return self.nova_h.get_vm_by_id(vm_id)

   def get_vm_if_present(self, vm_name=None, **kwargs):
       return  self.nova_h.get_vm_if_present(vm_name=vm_name, **kwargs)

   def get_vm_list(self, name_pattern='', **kwargs):
       return self.nova_h.get_vm_list(name_pattern=name_pattern, **kwargs)

   def get_vm_detail(self, vm_obj, **kwargs):
       return self.nova_h.get_vm_detail(vm_obj)

   def get_vm_ip(self, vm_obj, vn_name=None, **kwargs):
       return self.nova_h.get_vm_ip(vm_obj, vn_name)

   def get_key_file(self):
       return self.nova_h.get_key_file()

   def put_key_file_to_host(self, host_ip):
       self.nova_h.put_key_file_to_host(host_ip)

   def create_vn(self, name, subnets, option='orch', **kwargs):
       return self.quantum_h.create_network(name, subnets, **kwargs)

   def delete_vn(self, vn_obj, option='orch', **kwargs):
       return self.quantum_h.delete_vn(vn_obj['network']['id'])

   def get_vn_id(self, vn_obj, option='orch', **kwargs):
       return vn_obj['network']['id']

   def get_vn_name(self, vn_obj, option='orch', **kwargs):
       return vn_obj['network']['name']

   def get_vn_obj_if_present(self, vn_name, option='orch', **kwargs):
       return self.quantum_h.get_vn_obj_if_present(vn_name, **kwargs)

   def get_vn_list(self, **kwargs):
       return super(OpenstackOrchestrator, self).get_vn_list(**kwargs)

   def get_policy(self, fq_name, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).get_policy(fq_name=fq_name, **kwargs)
       return self.quantum_h.get_policy_if_present(fq_name[1], fq_name[2])

   def get_floating_ip(self, fip_id, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).get_floating_ip(fip_id=fip_id, **kwargs)
       fip = self.quantum_h.get_floatingip(fip_id)
       return fip['floating_ip_address']

   def create_floating_ip(self, pool_vn_id, project_obj, option='orch', **kwargs):
       fip_resp = self.quantum_h.create_floatingip(
                        pool_vn_id, project_obj.uuid)
       return (fip_resp['floatingip']['floating_ip_address'],
                        fip_resp['floatingip']['id'])

   def delete_floating_ip(self, fip_id, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).delete_floating_ip(fip_id=fip_id, **kwargs)
       self.quantum_h.delete_floatingip(fip_id)

   def assoc_floating_ip(self, fip_id, vm_id, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).assoc_floating_ip(fip_id=fip_id, vm_id=vm_id, **kwargs)
       update_dict = {}
       update_dict['port_id'] = self.quantum_h.get_port_id(vm_id)
       self.logger.debug('Associating FIP ID %s with Port ID %s' %(fip_id,
                          update_dict['port_id']))
       if update_dict['port_id']:
           fip_resp = self.quantum_h.update_floatingip(fip_id,
                            {'floatingip': update_dict})
           return fip_resp
       else:
           return None

   def disassoc_floating_ip(self, fip_id, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).disassoc_floating_ip(fip_id=fip_id, **kwargs)
       update_dict = {}
       update_dict['port_id'] = None
       self.logger.debug('Disassociating port from FIP ID : %s' %(fip_id))
       fip_resp = self.quantum_h.update_floatingip(fip_id,
                       {'floatingip': update_dict})
       return fip_resp

   def get_image_name_for_zone(self, image_name='ubuntu', zone='nova'):
       return self.nova_h.get_image_name_for_zone(image_name, zone)

   def get_vm_tap_interface(self,obj):
       return obj['name']

   def add_security_group(self, vm_id, sg_id, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).add_security_group(vm_id=vm_id, sg_id=sg_id, **kwargs)
       return self.nova_h.add_security_group(vm_id, sg_id)

   def remove_security_group(self, vm_id, sg_id, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).remove_security_group(vm_id=vm_id, sg_id=sg_id, **kwargs)
       return self.nova_h.remove_security_group(vm_id, sg_id)

   def create_security_group(self, sg_name, parent_fqname, sg_entries, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).create_security_group(sg_name=sg_name,
                                parent_fqname=parent_fqname, sg_entries=sg_entries, **kwargs)
       sg = self.quantum_h.create_security_group(sg_name)
       if not sg:
           self.logger.error("security group creation failed through quantum")
           return False
       self.quantum_h.delete_default_egress_rule(sg['id'])
       self._create_rules_in_quantum(sg['id'],secgrp_rules=sg_entries)
       return sg['id']

   def delete_security_group(self, sg_id, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).delete_security_group(sg_id=sg_id, **kwargs)
       self.quantum_h.delete_security_group(sg_id)

   def get_security_group_rules(self, sg_id, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).get_security_group_rules(sg_id=sg_id, **kwargs)
       sg_info = self.quantum_h.show_security_group(sg_id)
       return sg_info['security_group']['security_group_rules']

   def delete_security_group_rules(self, sg_id, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).delete_security_group_rules(sg_id=sg_id, **kwargs)
       rules = self.quantum_h.list_security_group_rules(tenant_id=self.quantum_h.project_id)
       for rule in rules['security_group_rules']:
           if rule['security_group_id'] == sg_id:
               self.quantum_h.delete_security_group_rule(rule['id'])

   def set_security_group_rules(self, sg_id, sg_entries, option='orch', **kwargs):
       if option == 'contrail':
           return super(OpenstackOrchestrator, self).set_security_group_rules(sg_id=sg_id, sg_entries=sg_entries, **kwargs)
       self.delete_security_group_rules(sg_id, option=option, **kwargs)
       return self._create_rules_in_quantum(sg_id, sg_entries)

   def _create_rules_in_quantum(self, sg_id, secgrp_rules):
       ret = False
       for rule in secgrp_rules:
           remote_group_id=None;remote_ip_prefix=None
           if rule['protocol'] == 'any':
               proto = None
           else:
               proto = rule['protocol']
           if rule['src_addresses'][0].has_key('security_group'):
               if rule['src_addresses'][0]['security_group'] == 'local':
                   direction = 'egress'
                   port_range_min = rule['src_ports'][0]['start_port']
                   port_range_max = rule['src_ports'][0]['end_port']
               else:
                   if rule['dst_addresses'][0]['security_group'] != None:
                       remote_group_id = self.get_security_group(sg_id=rule['src_addresses'][0]['security_group'].split(':')).uuid
           if rule['dst_addresses'][0].has_key('security_group'):
               if rule['dst_addresses'][0]['security_group'] == 'local':
                   direction = 'ingress'
                   port_range_min = rule['dst_ports'][0]['start_port']
                   port_range_max = rule['dst_ports'][0]['end_port']
               else:
                   if rule['dst_addresses'][0]['security_group'] != None:
                      remote_group_id = self.get_security_group(sg_id=rule['dst_addresses'][0]['security_group'].split(':')).uuid
           if (port_range_min == 0 and port_range_max == -1) \
                    or (port_range_min == 0 and port_range_max == 65535):
               port_range_min = None;port_range_max = None
           if direction == 'ingress':
               try:
                   for addr in rule['src_addresses']:
                       if addr.has_key('subnet') and  addr['subnet'] != None:
                           remote_ip_prefix = addr['subnet']['ip_prefix'] + '/' + str(addr['subnet']['ip_prefix_len'])
                           ret = self.quantum_h.create_security_group_rule(
                                                sg_id,direction=direction,
                                                port_range_min=port_range_min,
                                                port_range_max=port_range_max,
                                                protocol=proto,
                                                remote_group_id=remote_group_id,
                                                remote_ip_prefix=remote_ip_prefix)
               except:
                   self.logger.error("error while creating sg rule through quantum")
                   return False
           if direction == 'egress':
               try:
                   for addr in rule['dst_addresses']:
                       if addr.has_key('subnet') and addr['subnet'] != None:
                           remote_ip_prefix = addr['subnet']['ip_prefix'] + '/' + str(addr['subnet']['ip_prefix_len'])
                           ret = self.quantum_h.create_security_group_rule(
                                                sg_id,direction=direction,
                                                port_range_min=port_range_min,
                                                port_range_max=port_range_max,
                                                protocol=proto,
                                                remote_group_id=remote_group_id,
                                                remote_ip_prefix=remote_ip_prefix)
               except:
                   self.logger.error("error while creating sg rule through quantum")
                   return False
           #when remote is security group
           if remote_group_id:
               if not self.quantum_h.create_security_group_rule(
                                           sg_id,direction=direction,
                                           port_range_min=port_range_min,
                                           port_range_max=port_range_max,
                                           protocol=proto,
                                           remote_group_id=remote_group_id,
                                           remote_ip_prefix=remote_ip_prefix):
                  return False
       return ret
コード例 #10
0
class OpenstackOrchestrator(Orchestrator):

   def __init__(self, inputs, username, password, project_name, logger):
       self.inputs = inputs
       self.logger = logger
       self.quantum_h = QuantumHelper(username=username, password=password,
                                    inputs=inputs, project_name=project_name,
                                    openstack_ip=inputs.openstack_ip)
       self.nova_h = NovaHelper(inputs=inputs, project_name=project_name,
                              username=username, password=password,update_ssh_key=False)

   def get_compute_h(self):
       return self.nova_h

   def get_network_h(self):
       return self.quantum_h

   def get_image_account(self, image_name):
       return self.nova_h.get_image_account(image_name)
        
   def get_hosts(self, zone=None):
       if not zone:
          return self.nova_h.get_hosts()
       else:
          return self.nova_h.get_hosts(zone)

   def get_zones(self):
       return self.nova_h.get_zones()

   def host_aggregates(self):
       #import pdb;pdb.set_trace()
       self.nova_h.obj.aggregates.AggregateManager.create('AG1','AZ')

   def create_vm(self, vm_name, image_name, vn_objs, count=1, zone=None, node_name=None, **kwargs):
       vn_ids = kwargs.pop('vn_ids',[])
       if not vn_ids:
          vn_ids = [vn['network']['id'] for vn in vn_objs]
       return self.nova_h.create_vm(vm_name=vm_name, image_name=image_name, vn_ids=vn_ids,
                                   zone=zone, node_name=node_name, count=count, **kwargs)

   def delete_vm(self, vm_obj):
       return self.nova_h.delete_vm(vm_obj)
 
   def is_vm_deleted(self, vm_obj):
       return self.nova_h.is_vm_deleted_in_nova_db(vm_obj, self.inputs.openstack_ip)

   def get_host_of_vm(self, vm_obj):
       return self.nova_h.get_nova_host_of_vm(vm_obj)

   def get_networks_of_vm(self, vm_obj):
       return vm_obj.networks.keys()

   def wait_till_vm_is_active(self, vm_obj):
       return self.nova_h.wait_till_vm_is_active(vm_obj)

   def get_vm_if_present(self, vm_name=None, **kwargs):
       return  self.nova_h.get_vm_if_present(vm_name=vm_name, **kwargs)

   def get_vm_list(self, name_pattern='', **kwargs):
       return self.nova_h.get_vm_list(name_pattern=name_pattern, **kwargs)

   def get_vm_detail(self, vm_obj):
       return self.nova_h.get_vm_detail(vm_obj)

   def get_vm_by_id(self, vm_id):
       return self.nova_h.get_vm_by_id(vm_id)

   def get_vm_ip(self, vm_obj, vn_name):
       return self.nova_h.get_vm_ip(vm_obj, vn_name)

   def get_key_file(self):
       return self.nova_h.get_key_file()

   def put_key_file_to_host(self, host_ip):
       self.nova_h.put_key_file_to_host(host_ip)

   def get_tmp_key_file(self):
       return self.nova_h.tmp_key_file

   def create_vn(self, name, subnets, **kwargs):
       return self.quantum_h.create_network(name, subnets, **kwargs)

   def delete_vn(self, vn_obj):
       return self.quantum_h.delete_vn(vn_obj['network']['id'])

   def get_vn_id(self, vn_obj):
       return vn_obj['network']['id']

   def get_vn_name(self, vn_obj):
       return vn_obj['network']['name']

   def get_vn_obj_if_present(self, vn_name, **kwargs):
       return self.quantum_h.get_vn_obj_if_present(vn_name, **kwargs)

   def add_security_group(self, vm_obj, secgrp):
       return self.nova_h.add_security_group(vm_obj.id, secgrp)

   def remove_security_group(self, vm_obj, secgrp):
       return self.nova_h.remove_security_group(vm_obj.id, secgrp)

   def get_console_output(self, vm_obj):
       return self.nova_h.get_vm_console_output(vm_obj)

   def wait_till_vm_status(self, vm_obj, status):
       return self.nova_h.wait_till_vm_status(vm_obj, status)

   def get_policy(self, fq_name):
       return self.quantum_h.get_policy_if_present(fq_name[1], fq_name[2])

   def get_floating_ip(self, fip_id):
       fip = self.quantum_h.get_floatingip(fip_id)
       return fip['floatingip']['floating_ip_address']

   def create_floating_ip(self, pool_vn_id, project_obj, **kwargs):
       fip_resp = self.quantum_h.create_floatingip(
                        pool_vn_id, project_obj.uuid)
       return (fip_resp['floatingip']['floating_ip_address'],
                        fip_resp['floatingip']['id'])

   def delete_floating_ip(self, fip_id):
       self.quantum_h.delete_floatingip(fip_id)

   def assoc_floating_ip(self, fip_id, vm_id,vn_id=None):
       update_dict = {}
       update_dict['port_id'] = self.quantum_h.get_port_id(vm_id,vn_id)
       self.logger.debug('Associating FIP ID %s with Port ID %s' %(fip_id,
                          update_dict['port_id']))
       if update_dict['port_id']:
           fip_resp = self.quantum_h.update_floatingip(fip_id,
                            {'floatingip': update_dict})
           return fip_resp
       else:
           return None

   def disassoc_floating_ip(self, fip_id):
       update_dict = {}
       update_dict['port_id'] = None
       self.logger.debug('Disassociating port from FIP ID : %s' %(fip_id))
       fip_resp = self.quantum_h.update_floatingip(fip_id,
                       {'floatingip': update_dict})
       return fip_resp
コード例 #11
0
class OpenstackOrchestrator(Orchestrator):
    def __init__(self,
                 inputs,
                 username,
                 password,
                 project_name,
                 project_id,
                 vnclib=None,
                 logger=None,
                 auth_server_ip=None):
        self.logger = logger or contrail_logging.getLogger(__name__)
        super(OpenstackOrchestrator, self).__init__(inputs, vnclib,
                                                    self.logger)
        self.inputs = inputs
        self.quantum_h = None
        self.nova_h = None
        self.username = username
        self.password = password
        self.project_name = project_name
        self.project_id = project_id
        self.vnc_lib = vnclib
        self.auth_server_ip = auth_server_ip
        self.region_name = inputs.region_name
        if not auth_server_ip:
            self.auth_server_ip = self.inputs.auth_ip
        #for vcenter as compute
        self.vcntr_handle = self.get_vcenter_handle()

    def get_vcenter_handle(self):
        if self.inputs.vcenter_dc:
            vcntr = VcenterOrchestrator(user=self.inputs.vcenter_username,
                                        pwd=self.inputs.vcenter_password,
                                        host=self.inputs.vcenter_server,
                                        port=self.inputs.vcenter_port,
                                        dc_name=self.inputs.vcenter_dc,
                                        vnc=self.vnc_lib,
                                        inputs=self.inputs,
                                        logger=self.logger)
        else:
            vcntr = None
        return vcntr

    def get_network_handler(self):
        if not self.quantum_h:
            self.quantum_h = QuantumHelper(username=self.username,
                                           password=self.password,
                                           project_id=self.project_id,
                                           inputs=self.inputs)
            self.quantum_h.setUp()
        return self.quantum_h

    def get_compute_handler(self):
        if not self.nova_h:
            self.nova_h = NovaHelper(inputs=self.inputs,
                                     project_name=self.project_name,
                                     username=self.username,
                                     password=self.password)
        return self.nova_h

    def get_image_account(self, image_name):
        return self.nova_h.get_image_account(image_name)

    def get_image_name_for_zone(self, image_name='ubuntu', zone='nova'):
        return self.nova_h.get_image_name_for_zone(image_name, zone)

    def get_image(self, image_name):
        return self.nova_h.get_image(image_name)

    def get_default_image_flavor(self, image_name):
        return self.nova_h.get_default_image_flavor(image_name)

    def get_flavor(self, flavor):
        return self.nova_h.get_flavor(flavor)

    def get_hosts(self, zone=None):
        if not zone:
            return self.nova_h.get_hosts()
        else:
            return self.nova_h.get_hosts(zone)

    def get_zones(self):
        return self.nova_h.get_zones()

    def create_vm(self, vm_name, image_name, vn_objs, **kwargs):
        vn_ids = [vn['network']['id'] for vn in vn_objs]
        return self.nova_h.create_vm(vm_name=vm_name,
                                     image_name=image_name,
                                     vn_ids=vn_ids,
                                     **kwargs)

    def delete_vm(self, vm_obj, **kwargs):
        return self.nova_h.delete_vm(vm_obj)

    def is_vm_deleted(self, vm_obj, **kwargs):
        return self.nova_h.is_vm_deleted_in_nova_db(vm_obj,
                                                    self.inputs.openstack_ip)

    def get_host_of_vm(self, vm_obj, **kwargs):
        return self.nova_h.get_nova_host_of_vm(vm_obj)

    def get_networks_of_vm(self, vm_obj, **kwargs):
        vm_obj.get()
        return vm_obj.networks.keys()

    def wait_till_vm_is_active(self, vm_obj, **kwargs):
        return self.nova_h.wait_till_vm_is_active(vm_obj)

    def wait_till_vm_status(self, vm_obj, status, **kwargs):
        return self.nova_h.wait_till_vm_status(vm_obj, status)

    def get_console_output(self, vm_obj, **kwargs):
        return self.nova_h.get_vm_console_output(vm_obj)

    def get_vm_by_id(self, vm_id, **kwargs):
        return self.nova_h.get_vm_by_id(vm_id)

    def get_vm_if_present(self, vm_name=None, **kwargs):
        return self.nova_h.get_vm_if_present(vm_name=vm_name, **kwargs)

    def get_vm_list(self, name_pattern='', **kwargs):
        return self.nova_h.get_vm_list(name_pattern=name_pattern, **kwargs)

    def get_vm_detail(self, vm_obj, **kwargs):
        return self.nova_h.get_vm_detail(vm_obj)

    def get_vm_ip(self, vm_obj, vn_name=None, **kwargs):
        return self.nova_h.get_vm_ip(vm_obj, vn_name)

    def get_key_file(self):
        return self.nova_h.get_key_file()

    def put_key_file_to_host(self, host_ip):
        self.nova_h.put_key_file_to_host(host_ip)

    def create_vn(self, name, subnets, option='orch', **kwargs):
        return self.quantum_h.create_network(name, subnets, **kwargs)

    def delete_vn(self, vn_obj, option='orch', **kwargs):
        return self.quantum_h.delete_vn(vn_obj['network']['id'])

    def get_vn_id(self, vn_obj, option='orch', **kwargs):
        return vn_obj['network']['id']

    def get_vn_name(self, vn_obj, option='orch', **kwargs):
        return vn_obj['network']['name']

    def get_vn_obj_if_present(self, vn_name, option='orch', **kwargs):
        return self.quantum_h.get_vn_obj_if_present(vn_name, **kwargs)

    def get_vn_obj_from_id(self, vn_id):
        return self.quantum_h.get_vn_obj_from_id(vn_id)

    def get_vn_list(self, **kwargs):
        return super(OpenstackOrchestrator, self).get_vn_list(**kwargs)

    def get_policy(self, fq_name, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).get_policy(fq_name=fq_name, **kwargs)
        return self.quantum_h.get_policy_if_present(fq_name[1], fq_name[2])

    def get_floating_ip(self, fip_id, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).get_floating_ip(fip_id=fip_id, **kwargs)
        fip = self.quantum_h.get_floatingip(fip_id)
        return fip['floating_ip_address']

    def create_floating_ip(self,
                           pool_vn_id,
                           project_obj,
                           option='orch',
                           **kwargs):
        fip_resp = self.quantum_h.create_floatingip(pool_vn_id,
                                                    project_obj.uuid)
        return (fip_resp['floatingip']['floating_ip_address'],
                fip_resp['floatingip']['id'])

    def delete_floating_ip(self, fip_id, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).delete_floating_ip(fip_id=fip_id, **kwargs)
        self.quantum_h.delete_floatingip(fip_id)

    def assoc_floating_ip(self, fip_id, vm_id, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).assoc_floating_ip(fip_id=fip_id,
                                                 vm_id=vm_id,
                                                 **kwargs)
        update_dict = {}
        update_dict['port_id'] = self.quantum_h.get_port_id(vm_id)
        self.logger.debug('Associating FIP ID %s with Port ID %s' %
                          (fip_id, update_dict['port_id']))
        if update_dict['port_id']:
            fip_resp = self.quantum_h.update_floatingip(
                fip_id, {'floatingip': update_dict})
            return fip_resp
        else:
            return None

    def disassoc_floating_ip(self, fip_id, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).disassoc_floating_ip(fip_id=fip_id, **kwargs)
        update_dict = {}
        update_dict['port_id'] = None
        self.logger.debug('Disassociating port from FIP ID : %s' % (fip_id))
        fip_resp = self.quantum_h.update_floatingip(
            fip_id, {'floatingip': update_dict})
        return fip_resp

    def get_image_name_for_zone(self, image_name='ubuntu', zone='nova'):
        return self.nova_h.get_image_name_for_zone(image_name, zone)

    def get_vm_tap_interface(self, obj):
        return obj['name']

    def add_security_group(self, vm_id, sg_id, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).add_security_group(vm_id=vm_id,
                                                  sg_id=sg_id,
                                                  **kwargs)
        return self.nova_h.add_security_group(vm_id, sg_id)

    def remove_security_group(self, vm_id, sg_id, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).remove_security_group(vm_id=vm_id,
                                                     sg_id=sg_id,
                                                     **kwargs)
        return self.nova_h.remove_security_group(vm_id, sg_id)

    def create_security_group(self,
                              sg_name,
                              parent_fqname,
                              sg_entries,
                              option='orch',
                              **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator, self).create_security_group(
                sg_name=sg_name,
                parent_fqname=parent_fqname,
                sg_entries=sg_entries,
                **kwargs)
        sg = self.quantum_h.create_security_group(sg_name)
        if not sg:
            self.logger.error("security group creation failed through quantum")
            return False
        self.quantum_h.delete_default_egress_rule(sg['id'])
        self._create_rules_in_quantum(sg['id'], secgrp_rules=sg_entries)
        return sg['id']

    def delete_security_group(self, sg_id, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).delete_security_group(sg_id=sg_id, **kwargs)
        self.quantum_h.delete_security_group(sg_id)

    def get_security_group_rules(self, sg_id, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).get_security_group_rules(sg_id=sg_id, **kwargs)
        sg_info = self.quantum_h.show_security_group(sg_id)
        return sg_info['security_group']['security_group_rules']

    def delete_security_group_rules(self, sg_id, option='orch', **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).delete_security_group_rules(sg_id=sg_id,
                                                           **kwargs)
        rules = self.quantum_h.list_security_group_rules(
            tenant_id=self.quantum_h.project_id)
        for rule in rules['security_group_rules']:
            if rule['security_group_id'] == sg_id:
                self.quantum_h.delete_security_group_rule(rule['id'])

    def set_security_group_rules(self,
                                 sg_id,
                                 sg_entries,
                                 option='orch',
                                 **kwargs):
        if option == 'contrail':
            return super(OpenstackOrchestrator,
                         self).set_security_group_rules(sg_id=sg_id,
                                                        sg_entries=sg_entries,
                                                        **kwargs)
        self.delete_security_group_rules(sg_id, option=option, **kwargs)
        return self._create_rules_in_quantum(sg_id, sg_entries)

    def _create_rules_in_quantum(self, sg_id, secgrp_rules):
        ret = False
        for rule in secgrp_rules:
            remote_group_id = None
            remote_ip_prefix = None
            if rule['protocol'] == 'any':
                proto = None
            else:
                proto = rule['protocol']
            if rule['src_addresses'][0].has_key('security_group'):
                if rule['src_addresses'][0]['security_group'] == 'local':
                    direction = 'egress'
                    port_range_min = rule['src_ports'][0]['start_port']
                    port_range_max = rule['src_ports'][0]['end_port']
                else:
                    if rule['dst_addresses'][0]['security_group'] != None:
                        remote_group_id = self.get_security_group(
                            sg_id=rule['src_addresses'][0]
                            ['security_group'].split(':')).uuid
            if rule['dst_addresses'][0].has_key('security_group'):
                if rule['dst_addresses'][0]['security_group'] == 'local':
                    direction = 'ingress'
                    port_range_min = rule['dst_ports'][0]['start_port']
                    port_range_max = rule['dst_ports'][0]['end_port']
                else:
                    if rule['dst_addresses'][0]['security_group'] != None:
                        remote_group_id = self.get_security_group(
                            sg_id=rule['dst_addresses'][0]
                            ['security_group'].split(':')).uuid
            if (port_range_min == 0 and port_range_max == -1) \
                     or (port_range_min == 0 and port_range_max == 65535):
                port_range_min = None
                port_range_max = None
            if direction == 'ingress':
                try:
                    for addr in rule['src_addresses']:
                        if addr.has_key('subnet') and addr['subnet'] != None:
                            remote_ip_prefix = addr['subnet'][
                                'ip_prefix'] + '/' + str(
                                    addr['subnet']['ip_prefix_len'])
                            ret = self.quantum_h.create_security_group_rule(
                                sg_id,
                                direction=direction,
                                port_range_min=port_range_min,
                                port_range_max=port_range_max,
                                protocol=proto,
                                remote_group_id=remote_group_id,
                                remote_ip_prefix=remote_ip_prefix)
                except:
                    self.logger.error(
                        "error while creating sg rule through quantum")
                    return False
            if direction == 'egress':
                try:
                    for addr in rule['dst_addresses']:
                        if addr.has_key('subnet') and addr['subnet'] != None:
                            remote_ip_prefix = addr['subnet'][
                                'ip_prefix'] + '/' + str(
                                    addr['subnet']['ip_prefix_len'])
                            ret = self.quantum_h.create_security_group_rule(
                                sg_id,
                                direction=direction,
                                port_range_min=port_range_min,
                                port_range_max=port_range_max,
                                protocol=proto,
                                remote_group_id=remote_group_id,
                                remote_ip_prefix=remote_ip_prefix)
                except:
                    self.logger.error(
                        "error while creating sg rule through quantum")
                    return False
            #when remote is security group
            if remote_group_id:
                if not self.quantum_h.create_security_group_rule(
                        sg_id,
                        direction=direction,
                        port_range_min=port_range_min,
                        port_range_max=port_range_max,
                        protocol=proto,
                        remote_group_id=remote_group_id,
                        remote_ip_prefix=remote_ip_prefix):
                    return False
        return ret
コード例 #12
0
class OpenstackOrchestrator(Orchestrator):
    def __init__(self, inputs, username, password, project_name, logger):
        self.inputs = inputs
        self.logger = logger
        self.quantum_h = QuantumHelper(username=username,
                                       password=password,
                                       inputs=inputs,
                                       project_name=project_name,
                                       openstack_ip=inputs.openstack_ip)
        self.nova_h = NovaHelper(inputs=inputs,
                                 project_name=project_name,
                                 username=username,
                                 password=password,
                                 update_ssh_key=False)

    def get_compute_h(self):
        return self.nova_h

    def get_network_h(self):
        return self.quantum_h

    def get_image_account(self, image_name):
        return self.nova_h.get_image_account(image_name)

    def get_hosts(self, zone=None):
        if not zone:
            return self.nova_h.get_hosts()
        else:
            return self.nova_h.get_hosts(zone)

    def get_zones(self):
        return self.nova_h.get_zones()

    def host_aggregates(self):
        #import pdb;pdb.set_trace()
        self.nova_h.obj.aggregates.AggregateManager.create('AG1', 'AZ')

    def create_vm(self,
                  vm_name,
                  image_name,
                  vn_objs,
                  count=1,
                  zone=None,
                  node_name=None,
                  **kwargs):
        vn_ids = kwargs.pop('vn_ids', [])
        if not vn_ids:
            vn_ids = [vn['network']['id'] for vn in vn_objs]
        return self.nova_h.create_vm(vm_name=vm_name,
                                     image_name=image_name,
                                     vn_ids=vn_ids,
                                     zone=zone,
                                     node_name=node_name,
                                     count=count,
                                     **kwargs)

    def delete_vm(self, vm_obj):
        return self.nova_h.delete_vm(vm_obj)

    def is_vm_deleted(self, vm_obj):
        return self.nova_h.is_vm_deleted_in_nova_db(vm_obj,
                                                    self.inputs.openstack_ip)

    def get_host_of_vm(self, vm_obj):
        return self.nova_h.get_nova_host_of_vm(vm_obj)

    def get_networks_of_vm(self, vm_obj):
        return vm_obj.networks.keys()

    def wait_till_vm_is_active(self, vm_obj):
        return self.nova_h.wait_till_vm_is_active(vm_obj)

    def get_vm_if_present(self, vm_name=None, **kwargs):
        return self.nova_h.get_vm_if_present(vm_name=vm_name, **kwargs)

    def get_vm_list(self, name_pattern='', **kwargs):
        return self.nova_h.get_vm_list(name_pattern=name_pattern, **kwargs)

    def get_vm_detail(self, vm_obj):
        return self.nova_h.get_vm_detail(vm_obj)

    def get_vm_by_id(self, vm_id):
        return self.nova_h.get_vm_by_id(vm_id)

    def get_vm_ip(self, vm_obj, vn_name):
        return self.nova_h.get_vm_ip(vm_obj, vn_name)

    def get_key_file(self):
        return self.nova_h.get_key_file()

    def put_key_file_to_host(self, host_ip):
        self.nova_h.put_key_file_to_host(host_ip)

    def get_tmp_key_file(self):
        return self.nova_h.tmp_key_file

    def create_vn(self, name, subnets, **kwargs):
        return self.quantum_h.create_network(name, subnets, **kwargs)

    def delete_vn(self, vn_obj):
        return self.quantum_h.delete_vn(vn_obj['network']['id'])

    def get_vn_id(self, vn_obj):
        return vn_obj['network']['id']

    def get_vn_name(self, vn_obj):
        return vn_obj['network']['name']

    def get_vn_obj_if_present(self, vn_name, **kwargs):
        return self.quantum_h.get_vn_obj_if_present(vn_name, **kwargs)

    def add_security_group(self, vm_obj, secgrp):
        return self.nova_h.add_security_group(vm_obj.id, secgrp)

    def remove_security_group(self, vm_obj, secgrp):
        return self.nova_h.remove_security_group(vm_obj.id, secgrp)

    def get_console_output(self, vm_obj):
        return self.nova_h.get_vm_console_output(vm_obj)

    def wait_till_vm_status(self, vm_obj, status):
        return self.nova_h.wait_till_vm_status(vm_obj, status)

    def get_policy(self, fq_name):
        return self.quantum_h.get_policy_if_present(fq_name[1], fq_name[2])

    def get_floating_ip(self, fip_id):
        fip = self.quantum_h.get_floatingip(fip_id)
        return fip['floatingip']['floating_ip_address']

    def create_floating_ip(self, pool_vn_id, project_obj, **kwargs):
        fip_resp = self.quantum_h.create_floatingip(pool_vn_id,
                                                    project_obj.uuid)
        return (fip_resp['floatingip']['floating_ip_address'],
                fip_resp['floatingip']['id'])

    def delete_floating_ip(self, fip_id):
        self.quantum_h.delete_floatingip(fip_id)

    def assoc_floating_ip(self, fip_id, vm_id, vn_id=None):
        update_dict = {}
        update_dict['port_id'] = self.quantum_h.get_port_id(vm_id, vn_id)
        self.logger.debug('Associating FIP ID %s with Port ID %s' %
                          (fip_id, update_dict['port_id']))
        if update_dict['port_id']:
            fip_resp = self.quantum_h.update_floatingip(
                fip_id, {'floatingip': update_dict})
            return fip_resp
        else:
            return None

    def disassoc_floating_ip(self, fip_id):
        update_dict = {}
        update_dict['port_id'] = None
        self.logger.debug('Disassociating port from FIP ID : %s' % (fip_id))
        fip_resp = self.quantum_h.update_floatingip(
            fip_id, {'floatingip': update_dict})
        return fip_resp