Exemple #1
0
def handler(request):
    match = url_regex.match(request.uri.lower())
    if not match:
        return Response(400, data='Invalid config uri', content_type='text')

    (request_type, _, config_uuid, structure_id, foundation_locator,
     ip_address, _, _, _) = match.groups()

    target = None

    if config_uuid is not None:
        try:
            target = Structure.objects.get(config_uuid=config_uuid[2:])
        except Structure.DoesNotExist:
            return Response(404,
                            data='Config UUID Not Found',
                            content_type='text')

    elif structure_id is not None:
        try:
            target = Structure.objects.get(pk=int(structure_id[2:]))
        except Structure.DoesNotExist:
            return Response(404,
                            data='Structure Not Found',
                            content_type='text')

    elif foundation_locator is not None:
        try:
            target = Foundation.objects.get(pk=foundation_locator[2:]).subclass
        except Foundation.DoesNotExist:
            return Response(404,
                            data='Foundation Not Found',
                            content_type='text')

    else:
        if ip_address is not None:
            address = BaseAddress.lookup(ip_address[2:])
        else:
            address = BaseAddress.lookup(request.remote_addr)

        if address is None:
            return Response(404, data='Address Not Found', content_type='text')

        address = address.subclass
        if address.type == 'Address':
            target = address.networked.subclass
        else:
            target = address

    if target is None:
        return Response(500, data='Target is missing', content_type='text')

    data = None
    config = getConfig(target)

    if request_type in ('boot_script', 'pxe_template'):
        pxe = None
        if isinstance(target, Structure):
            pxe = target.provisioning_interface.pxe
        elif isinstance(target, Foundation):
            pxe = target.structure.provisioning_interface.pxe
        elif isinstance(target, DynamicAddress):
            pxe = target.pxe

        if pxe is None:
            return Response(200, data='', content_type='text')

        if request_type == 'boot_script':
            template = '#!ipxe\n\n' + pxe.boot_script

        elif request_type == 'pxe_template':
            template = pxe.template

        data = renderTemplate(template, config)
        return Response(200, data=data, content_type='text')

    elif request_type == 'config':
        _fromPythonMap(
            config
        )  # this does not go out CInP's converter, we need to make the python dict JSON encodable our selves
        return Response(200, data=mergeValues(config))

    return Response(400, data='Invalid request type', content_type='text')
Exemple #2
0
def handler( request ):
  if not re.match( '^/config/[a-z_]+/([sf][0-9]+)?$', request.uri ):
    return Response( 400, data='Invalid config uri', content_type='text' )

  ( _, _, request_type, target_id ) = request.uri.split( '/' )

  target = None
  interface = None

  if len( target_id ) > 0:
    if target_id[0] == 's':
      try:
        target = Structure.objects.get( pk=int( target_id[ 1: ] ) )
      except Structure.DoesNotExist:
        return Response( 404, data='Structure Not Found', content_type='text' )

    elif target_id[0] == 'f':
      try:
        target = Foundation.objects.get( pk=int( target_id[ 1: ] ) )
      except Foundation.DoesNotExist:
        return Response( 404, data='Foundation Not Found', content_type='text' )

    else:
      return Response( 500, data='Target Confusion', content_type='text' )

    interface = target.provisioning_interface

  else:
    print( 'Getting config goodies for "{0}"'.format( request.remote_addr ) )
    address = BaseAddress.lookup( request.remote_addr )
    if address is None:
      return Response( 404, data='Address Not Found', content_type='text' )

    address = address.subclass
    if address.type != 'Address':
      return Response( 404, data='Address Not Valid', content_type='text' )

    target = address.networked.subclass
    try:
      if isinstance( target, Structure ):
        interface = target.foundation.interfaces.get( name=address.interface_name )
      else:
        interface = target.interfaces.get( name=address.interface_name )

    except NetworkInterface.DoesNotExist:
      return Response( 404, data='Interface Not Found', content_type='text' )

  if target is None or interface is None:
    return Response( 500, data='Target/Interface is missing', content_type='text' )

  data = None
  config = getConfig( target )

  if request_type in ( 'boot_script', 'pxe_template' ):
    pxe = interface.pxe
    if pxe is None:
      return Response( 200, data='', content_type='text' )

    if request_type == 'boot_script':
      template = Template( '#!ipxe\n\n' + pxe.boot_script )

    elif request_type == 'pxe_template':
      template = Template( pxe.template )

    data = template.render( Context( config ) )
    print( 'config_handler sending "{0}" to "{1}"\n    -----------    '.format( request_type, request.remote_addr ) )
    print( data )
    print( '    -----------    ')
    return Response( 200, data=data, content_type='text' )

  elif request_type == 'config':
    _fromPythonMap( config )
    return Response( 200, data=json.dumps( config ) )

  return Response( 400, data='Invalid request type', content_type='text' )