Esempio n. 1
0
    def create_ports(request, instance_name):
        nics = []
        pts = []

        for policy_target_id in request.DATA["group_policy_targets"]:
            policy_target = client.policy_target_get(request,
                                                     policy_target_id['id'])

            args = {
                'policy_target_group_id': policy_target.id,
                'name': instance_name[:41] + "_gbpui"
            }

            for subnet_id in policy_target.subnets:
                subnet = api.neutron.subnet_get(request, subnet_id)

                if 'fixed_ip' in policy_target_id and IPAddress(
                        policy_target_id['fixed_ip']) in \
                        IPNetwork(subnet['cidr']):
                    args['fixed_ips'] = [{
                        'subnet_id': subnet['id'],
                        'ip_address': policy_target_id['fixed_ip']
                    }]

            port = client.pt_create(request, **args)

            nics.append({
                'port-id': port.port_id
            })
            pts.append(port.id)

        meta_data = {'pts': ','.join(pts)}

        return meta_data, nics
Esempio n. 2
0
    def create_ports(request, instance_name):
        nics = []
        pts = []

        for policy_target_id in request.DATA["group_policy_targets"]:
            policy_target = client.policy_target_get(request,
                                                     policy_target_id['id'])

            args = {
                'policy_target_group_id': policy_target.id,
                'name': instance_name[:41] + "_gbpui"
            }

            for subnet_id in policy_target.subnets:
                subnet = api.neutron.subnet_get(request, subnet_id)

                if 'fixed_ip' in policy_target_id and IPAddress(
                        policy_target_id['fixed_ip']) in \
                        IPNetwork(subnet['cidr']):
                    args['fixed_ips'] = [{
                        'subnet_id':
                        subnet['id'],
                        'ip_address':
                        policy_target_id['fixed_ip']
                    }]

            port = client.pt_create(request, **args)

            nics.append({'port-id': port.port_id})
            pts.append(port.id)

        meta_data = {'pts': ','.join(pts)}

        return meta_data, nics
 def handle(self, request, context):
     policy_target_id = self.request.path.split("/")[-2]
     try:
         msg = _('Member was successfully created.')
         ep = client.pt_create(
             request, policy_target_group_id=policy_target_id)
         api.nova.server_create(request,
                                context['name'],
                                context['image'],
                                context['flavor'],
                                key_name=None,
                                user_data=None,
                                security_groups=None,
                                instance_count=context['count'],
                                nics=[{'port-id': ep.port_id}])
         LOG.debug(msg)
         messages.success(request, msg)
     except Exception:
         msg = _('Failed to launch VM')
         LOG.error(msg)
         u = "horizon:project:policytargets:policy_targetdetails"
         redirect = reverse(u, kwargs={'policy_target_id':
             policy_target_id})
         exceptions.handle(request, msg, redirect=redirect)
 def handle(self, request, context):
     policy_target_id = self.request.path.split("/")[-2]
     try:
         msg = _('Member was successfully created.')
         ep = client.pt_create(
             request, policy_target_group_id=policy_target_id)
         api.nova.server_create(request,
                                context['name'],
                                context['image'],
                                context['flavor'],
                                key_name=None,
                                user_data=None,
                                security_groups=None,
                                instance_count=context['count'],
                                nics=[{'port-id': ep.port_id}])
         LOG.debug(msg)
         messages.success(request, msg)
     except Exception:
         msg = _('Failed to launch VM')
         LOG.error(msg)
         u = "horizon:project:policytargets:policy_targetdetails"
         redirect = reverse(u, kwargs={'policy_target_id':
             policy_target_id})
         exceptions.handle(request, msg, redirect=redirect)
    def handle(self, request, context):
        custom_script = context.get('script_data', '')
        dev_mapping_1 = None
        dev_mapping_2 = None

        image_id = ''

        # Determine volume mapping options
        source_type = context.get('source_type', None)
        if source_type in ['image_id', 'instance_snapshot_id']:
            image_id = context['source_id']
        elif source_type in ['volume_id', 'volume_snapshot_id']:
            try:
                if api.nova.extension_supported("BlockDeviceMappingV2Boot",
                                                request):
                    # Volume source id is extracted from the source
                    volume_source_id = context['source_id'].split(':')[0]
                    device_name = context.get('device_name', '') \
                        .strip() or None
                    dev_source_type_mapping = {
                        'volume_id': 'volume',
                        'volume_snapshot_id': 'snapshot'
                    }
                    dev_mapping_2 = [
                        {'device_name': device_name,
                         'source_type': dev_source_type_mapping[source_type],
                         'destination_type': 'volume',
                         'delete_on_termination':
                             int(bool(context['delete_on_terminate'])),
                         'uuid': volume_source_id,
                         'boot_index': '0',
                         'volume_size': context['volume_size']
                         }
                    ]
                else:
                    dev_mapping_1 = {context['device_name']: '%s::%s' %
                                     (context['source_id'],
                                     int(bool(context['delete_on_terminate'])))
                                     }
            except Exception:
                msg = _('Unable to retrieve extensions information')
                exceptions.handle(request, msg)

        elif source_type == 'volume_image_id':
            device_name = context.get('device_name', '').strip() or None
            dev_mapping_2 = [
                {'device_name': device_name,  # None auto-selects device
                 'source_type': 'image',
                 'destination_type': 'volume',
                 'delete_on_termination':
                     int(bool(context['delete_on_terminate'])),
                 'uuid': context['source_id'],
                 'boot_index': '0',
                 'volume_size': context['volume_size']
                 }
            ]
        avail_zone = context.get('availability_zone', None)
        try:
            instance_count = int(context['count'])
            count = 1
            while count <= instance_count:
                if instance_count == 1:
                    instance_name = context['name']
                else:
                    instance_name = context['name'] + str(count)
                nics = []
                pts = []
                for ptg_id in context['group_id']:
                    values = ptg_id.split(":")
                    ptg_id = values[0]
                    args = {'policy_target_group_id': ptg_id,
                            'name': instance_name[:41] + "_gbpui"}
                    if len(values) == 3:
                        ptg = client.policy_target_get(request, ptg_id)
                        fixed_ip = values[2]
                        for subnet_id in ptg.subnets:
                            try:
                                subnet = api.neutron.subnet_get(
                                    request, subnet_id)
                            except Exception:
                                continue
                            if IPAddress(fixed_ip) in \
                                    IPNetwork(subnet['cidr']):
                                args['fixed_ips'] = [
                                    {'subnet_id': subnet['id'],
                                     'ip_address': fixed_ip}]
                                break
                    ep = client.pt_create(request, **args)
                    nics.append({'port-id': ep.port_id})
                    pts.append(ep.id)
                meta_data = {'pts': ','.join(pts)}
                api.nova.server_create(request,
                                   instance_name,
                                   image_id,
                                   context['flavor'],
                                   context['keypair_id'],
                                   normalize_newlines(custom_script),
                                   security_groups=None,
                                   block_device_mapping=dev_mapping_1,
                                   block_device_mapping_v2=dev_mapping_2,
                                   nics=nics,
                                   availability_zone=avail_zone,
                                   instance_count=1,
                                   admin_pass=context['admin_pass'],
                                   disk_config=context.get('disk_config'),
                                   config_drive=context.get('config_drive'),
                                   meta=meta_data)
                count += 1
            return True
        except Exception as e:
            error = _("Unable to launch member %(count)s with name %(name)s")
            msg = error % {'count': count, 'name': instance_name}
            LOG.error(str(e))
            u = "horizon:project:policytargets:policy_targetdetails"
            policy_target_id = self.request.path.split("/")[-2]
            redirect = reverse(u, kwargs={'policy_target_id':
                policy_target_id})
            exceptions.handle(request, msg, redirect=redirect)
            return False
    def handle(self, request, context):
        custom_script = context.get('script_data', '')

        dev_mapping_1 = None
        dev_mapping_2 = None

        image_id = ''

        # Determine volume mapping options
        source_type = context.get('source_type', None)
        if source_type in ['image_id', 'instance_snapshot_id']:
            image_id = context['source_id']
        elif source_type in ['volume_id', 'volume_snapshot_id']:
            try:
                if api.nova.extension_supported("BlockDeviceMappingV2Boot",
                                                request):
                    # Volume source id is extracted from the source
                    volume_source_id = context['source_id'].split(':')[0]
                    device_name = context.get('device_name', '') \
                        .strip() or None
                    dev_mapping_2 = [
                        {'device_name': device_name,
                         'source_type': 'volume',
                         'destination_type': 'volume',
                         'delete_on_termination':
                             int(bool(context['delete_on_terminate'])),
                         'uuid': volume_source_id,
                         'boot_index': '0',
                         'volume_size': context['volume_size']
                         }
                    ]
                else:
                    dev_mapping_1 = {context['device_name']: '%s::%s' %
                                     (context['source_id'],
                                     int(bool(context['delete_on_terminate'])))
                                     }
            except Exception:
                msg = _('Unable to retrieve extensions information')
                exceptions.handle(request, msg)

        elif source_type == 'volume_image_id':
            device_name = context.get('device_name', '').strip() or None
            dev_mapping_2 = [
                {'device_name': device_name,  # None auto-selects device
                 'source_type': 'image',
                 'destination_type': 'volume',
                 'delete_on_termination':
                     int(bool(context['delete_on_terminate'])),
                 'uuid': context['source_id'],
                 'boot_index': '0',
                 'volume_size': context['volume_size']
                 }
            ]
        avail_zone = context.get('availability_zone', None)
        try:
            policy_target_id = self.request.path.split("/")[-2]
            instance_count = int(context['count'])
            count = 1
            while count <= instance_count:
                if instance_count == 1:
                    instance_name = context['name']
                else:
                    instance_name = context['name'] + str(count)
                ep = client.pt_create(
                    request, policy_target_group_id=policy_target_id,
                    name=instance_name[:41] + "_gbpui")
                api.nova.server_create(request,
                                   instance_name,
                                   image_id,
                                   context['flavor'],
                                   context['keypair_id'],
                                   normalize_newlines(custom_script),
                                   security_groups=None,
                                   block_device_mapping=dev_mapping_1,
                                   block_device_mapping_v2=dev_mapping_2,
                                   nics=[{'port-id': ep.port_id}],
                                   availability_zone=avail_zone,
                                   instance_count=1,
                                   admin_pass=context['admin_pass'],
                                   disk_config=context.get('disk_config'),
                                   config_drive=context.get('config_drive'))
                count += 1
            return True
        except Exception:
            error = _("Unable to launch member %(count)s with name %(name)s")
            msg = error % {'count': count, 'name': instance_name}
            LOG.error(msg)
            u = "horizon:project:policytargets:policy_targetdetails"
            redirect = reverse(u, kwargs={'policy_target_id':
                policy_target_id})
            exceptions.handle(request, msg, redirect=redirect)
            return False
Esempio n. 7
0
    def handle(self, request, context):
        custom_script = context.get('script_data', '')
        dev_mapping_1 = None
        dev_mapping_2 = None

        image_id = ''

        # Determine volume mapping options
        source_type = context.get('source_type', None)
        if source_type in ['image_id', 'instance_snapshot_id']:
            image_id = context['source_id']
        elif source_type in ['volume_id', 'volume_snapshot_id']:
            try:
                if api.nova.extension_supported("BlockDeviceMappingV2Boot",
                                                request):
                    # Volume source id is extracted from the source
                    volume_source_id = context['source_id'].split(':')[0]
                    device_name = context.get('device_name', '') \
                        .strip() or None
                    dev_source_type_mapping = {
                        'volume_id': 'volume',
                        'volume_snapshot_id': 'snapshot'
                    }
                    dev_mapping_2 = [
                        {'device_name': device_name,
                         'source_type': dev_source_type_mapping[source_type],
                         'destination_type': 'volume',
                         'delete_on_termination':
                             int(bool(context['delete_on_terminate'])),
                         'uuid': volume_source_id,
                         'boot_index': '0',
                         'volume_size': context['volume_size']
                         }
                    ]
                else:
                    dev_mapping_1 = {context['device_name']: '%s::%s' %
                                     (context['source_id'],
                                     int(bool(context['delete_on_terminate'])))
                                     }
            except Exception:
                msg = _('Unable to retrieve extensions information')
                exceptions.handle(request, msg)

        elif source_type == 'volume_image_id':
            device_name = context.get('device_name', '').strip() or None
            dev_mapping_2 = [
                {'device_name': device_name,  # None auto-selects device
                 'source_type': 'image',
                 'destination_type': 'volume',
                 'delete_on_termination':
                     int(bool(context['delete_on_terminate'])),
                 'uuid': context['source_id'],
                 'boot_index': '0',
                 'volume_size': context['volume_size']
                 }
            ]
        avail_zone = context.get('availability_zone', None)
        try:
            instance_count = int(context['count'])
            count = 1
            while count <= instance_count:
                if instance_count == 1:
                    instance_name = context['name']
                else:
                    instance_name = context['name'] + str(count)
                nics = []
                for ptg_id in context['group_id']:
                    values = ptg_id.split(":")
                    ptg_id = values[0]
                    args = {'policy_target_group_id': ptg_id,
                            'name': instance_name[:41] + "_gbpui"}
                    if len(values) == 3:
                        ptg = client.policy_target_get(request, ptg_id)
                        fixed_ip = values[2]
                        for subnet_id in ptg.subnets:
                            subnet = api.neutron.subnet_get(request, subnet_id)
                            if IPAddress(fixed_ip) in \
                                    IPNetwork(subnet['cidr']):
                                args['fixed_ips'] = [
                                    {'subnet_id': subnet['id'],
                                     'ip_address': fixed_ip}]
                                break
                    ep = client.pt_create(request, **args)
                    nics.append({'port-id': ep.port_id})
                api.nova.server_create(request,
                                   instance_name,
                                   image_id,
                                   context['flavor'],
                                   context['keypair_id'],
                                   normalize_newlines(custom_script),
                                   security_groups=None,
                                   block_device_mapping=dev_mapping_1,
                                   block_device_mapping_v2=dev_mapping_2,
                                   nics=nics,
                                   availability_zone=avail_zone,
                                   instance_count=1,
                                   admin_pass=context['admin_pass'],
                                   disk_config=context.get('disk_config'),
                                   config_drive=context.get('config_drive'))
                count += 1
            return True
        except Exception as e:
            error = _("Unable to launch member %(count)s with name %(name)s")
            msg = error % {'count': count, 'name': instance_name}
            LOG.error(str(e))
            u = "horizon:project:policytargets:policy_targetdetails"
            policy_target_id = self.request.path.split("/")[-2]
            redirect = reverse(u, kwargs={'policy_target_id':
                policy_target_id})
            exceptions.handle(request, msg, redirect=redirect)
            return False