Example #1
0
def network_setup():
    """Creates VPC if it doesn't already exists, configures it for public
  internet access, returns vpc, subnet, security_group"""

    # from https://gist.github.com/nguyendv/8cfd92fc8ed32ebb78e366f44c2daea6

    ec2 = u.create_ec2_resource()
    existing_vpcs = u.get_vpc_dict()
    zones = u.get_available_zones()
    if VPC_NAME in existing_vpcs:
        print("Reusing VPC " + VPC_NAME)
        vpc = existing_vpcs[VPC_NAME]
        subnets = list(vpc.subnets.all())
        assert len(subnets) == len(
            zones
        ), "Has %s subnets, but %s zones, something went wrong during resource creation, try delete_resources.py/create_resources.py" % (
            len(subnets), len(zones))

    else:
        print("Creating VPC " + VPC_NAME)
        vpc = ec2.create_vpc(CidrBlock='192.168.0.0/16')

        # enable DNS on the VPC
        response = vpc.modify_attribute(EnableDnsHostnames={"Value": True})
        assert u.is_good_response(response)
        response = vpc.modify_attribute(EnableDnsSupport={"Value": True})
        assert u.is_good_response(response)

        vpc.create_tags(Tags=u.make_name(VPC_NAME))
        vpc.wait_until_available()

    gateways = u.get_gateway_dict(vpc)
    if DEFAULT_NAME in gateways:
        print("Reusing gateways " + DEFAULT_NAME)
    else:
        print("Creating gateway " + DEFAULT_NAME)
        ig = ec2.create_internet_gateway()
        ig.attach_to_vpc(VpcId=vpc.id)
        ig.create_tags(Tags=u.make_name(DEFAULT_NAME))

        # check that attachment succeeded
        # TODO: sometimes get
        # AssertionError: vpc vpc-33d0804b is in state None

        attach_state = u.get1(ig.attachments, State=-1, VpcId=vpc.id)
        assert attach_state == 'available', "vpc %s is in state %s" % (
            vpc.id, attach_state)

        route_table = vpc.create_route_table()
        route_table.create_tags(Tags=u.make_name(ROUTE_TABLE_NAME))

        dest_cidr = '0.0.0.0/0'
        route = route_table.create_route(DestinationCidrBlock=dest_cidr,
                                         GatewayId=ig.id)
        # check success
        for route in route_table.routes:
            # result looks like this
            # ec2.Route(route_table_id='rtb-a8b438cf',
            #    destination_cidr_block='0.0.0.0/0')
            if route.destination_cidr_block == dest_cidr:
                break
        else:
            # sometimes get
            #      AssertionError: Route for 0.0.0.0/0 not found in [ec2.Route(route_table_id='rtb-cd9153b0', destination_cidr_block='192.168.0.0/16')]
            # TODO: add a wait/retry?
            assert False, "Route for %s not found in %s" % (dest_cidr,
                                                            route_table.routes)

        assert len(zones) <= 16  # for cidr/20 to fit into cidr/16
        ip = 0
        for zone in zones:
            cidr_block = '192.168.%d.0/20' % (ip, )
            ip += 16
            print("Creating subnet %s in zone %s" % (cidr_block, zone))
            subnet = vpc.create_subnet(CidrBlock=cidr_block,
                                       AvailabilityZone=zone)
            subnet.create_tags(Tags=[{
                'Key': 'Name',
                'Value': f'{VPC_NAME}-subnet'
            }, {
                'Key': 'Region',
                'Value': zone
            }])
            u.wait_until_available(subnet)
            route_table.associate_with_subnet(SubnetId=subnet.id)

    # Creates security group if necessary
    existing_security_groups = u.get_security_group_dict()
    if SECURITY_GROUP_NAME in existing_security_groups:
        print("Reusing security group " + SECURITY_GROUP_NAME)
        security_group = existing_security_groups[SECURITY_GROUP_NAME]
    else:
        print("Creating security group " + SECURITY_GROUP_NAME)
        security_group = ec2.create_security_group(
            GroupName=SECURITY_GROUP_NAME,
            Description=SECURITY_GROUP_NAME,
            VpcId=vpc.id)

        security_group.create_tags(Tags=[{
            "Key": "Name",
            "Value": SECURITY_GROUP_NAME
        }])

        # allow ICMP access for public ping
        security_group.authorize_ingress(CidrIp='0.0.0.0/0',
                                         IpProtocol='icmp',
                                         FromPort=-1,
                                         ToPort=-1)

        # open public ports
        # always include SSH port which is required for basic functionality
        assert 22 in PUBLIC_TCP_RANGES, "Must enable SSH access"
        for port in PUBLIC_TCP_RANGES:
            if u.is_list_or_tuple(port):
                assert len(port) == 2
                from_port, to_port = port
            else:
                from_port, to_port = port, port

            response = security_group.authorize_ingress(IpProtocol="tcp",
                                                        CidrIp="0.0.0.0/0",
                                                        FromPort=from_port,
                                                        ToPort=to_port)
            assert u.is_good_response(response)

        for port in PUBLIC_UDP_RANGES:
            if u.is_list_or_tuple(port):
                assert len(port) == 2
                from_port, to_port = port
            else:
                from_port, to_port = port, port

            response = security_group.authorize_ingress(IpProtocol="udp",
                                                        CidrIp="0.0.0.0/0",
                                                        FromPort=from_port,
                                                        ToPort=to_port)
            assert u.is_good_response(response)

        # allow ingress within security group
        # Authorizing ingress doesn't work with names in a non-default VPC,
        # so must use more complicated syntax
        # https://github.com/boto/boto3/issues/158

        for protocol in ['icmp']:
            try:
                rule = {
                    'FromPort': -1,
                    'IpProtocol': protocol,
                    'IpRanges': [],
                    'PrefixListIds': [],
                    'ToPort': -1,
                    'UserIdGroupPairs': [{
                        'GroupId': security_group.id
                    }]
                }
                security_group.authorize_ingress(IpPermissions=[rule])
            except Exception as e:
                if e.response['Error'][
                        'Code'] == 'InvalidPermission.Duplicate':
                    print("Warning, got " + str(e))
                else:
                    assert False, "Failed while authorizing ingress with " + str(
                        e)

        for protocol in ['tcp', 'udp']:
            try:
                rule = {
                    'FromPort': 0,
                    'IpProtocol': protocol,
                    'IpRanges': [],
                    'PrefixListIds': [],
                    'ToPort': 65535,
                    'UserIdGroupPairs': [{
                        'GroupId': security_group.id
                    }]
                }
                security_group.authorize_ingress(IpPermissions=[rule])
            except Exception as e:
                if e.response['Error'][
                        'Code'] == 'InvalidPermission.Duplicate':
                    print("Warning, got " + str(e))
                else:
                    assert False, "Failed while authorizing ingress with " + str(
                        e)

    return vpc, security_group
    def read(self):
        vehiculo = self.next()
        if vehiculo == None:
            return None

        values = []
        vehiculo_id = None
        try:
            vehiculo_id = get1(vehiculo, "@ID_ACCIDENTE") + "/" + get1(
                vehiculo, "@ID_VEHICULO")

            values.append(vehiculo_id)
            values.append(get1(vehiculo, "@ID_ACCIDENTE"))

            values.append(get1(vehiculo, "@ID_VEHICULO"))
            values.append(parseToBool(get1(vehiculo, "@SIN_CONDUCTOR"), False))
            values.append(
                get1(vehiculo, "@ID_ACCIDENTE") + "/" +
                get1(vehiculo, "@ID_VEHICULO"))

            values.append(get1(vehiculo, "FECHA_MATRICULACION"))
            values.append(get1(vehiculo, "NACIONALIDAD"))
            values.append(parseToNull(get1(vehiculo, "TIPO_VEHICULO")))
            values.append(parseToNull(get1(vehiculo, "MMA")))
            values.append(get1(vehiculo, "MARCA_NOMBRE"))
            values.append(get1(vehiculo, "MODELO"))
            values.append(parseToNull(get1(vehiculo, "ITV")))
            values.append(parseToNull(get1(vehiculo, "SEGURO")))
            values.append(parseToNumber(get1(vehiculo, "NUM_OCUPANTES")))
            values.append(parseToBool(get1(vehiculo, "VEHICULO_ADAPTADO")))
            values.append(parseToBool(get1(vehiculo, "TRANSPORTE_ESPECIAL")))

            values.append(parseToBool(get1(vehiculo, "FUGADO")))
            values.append(parseToBool(get1(vehiculo, "INCENDIADO")))

            values.append(
                parseToBool(get2(vehiculo, "MERCANCIAS_PELIGROSAS", "MP")))
            values.append(
                parseToNull(
                    get2(vehiculo, "MERCANCIAS_PELIGROSAS", "MP_NUMERO_ONU")))

            values.append(parseToNull(get1(vehiculo, "DANYOS")))

            values.append(parseToBool(get1(vehiculo, "REMOLQUE")))
            values.append(parseToBool(get1(vehiculo, "SEMIREMOLQUE")))
            values.append(parseToBool(get1(vehiculo, "CARAVANA")))
            values.append(parseToBool(get1(vehiculo, "REMOLQUE_OTROS")))

            values.append(parseToNull(get1(vehiculo, "POS_VIA")))
            values.append(parseToNull(get1(vehiculo, "APROXIMACION_NUDO")))
            values.append(parseToNull(get1(vehiculo, "SENTIDO_CIRCULACION")))
            values.append(
                parseToNull(get2(vehiculo, "LUGAR_CIRCULABA", "#text")))
            values.append(
                parseToBool(
                    get2(vehiculo, "LUGAR_CIRCULABA", "@FACT_LUGAR_CIRCULA")))

            values.append(parseToBool(get1(vehiculo, "TACOGRAFO_DISCO")))
            values.append(parseToBool(get1(vehiculo, "TACOGRAFO_LECTURA")))
            values.append(parseToBool(get1(vehiculo, "DESCANSO_DIARIO")))
            values.append(parseToBool(get1(vehiculo,
                                           "HORAS_COND_CONTINU_SUP")))
            values.append(parseToBool(get1(vehiculo, "HORAS_COND_DIARIA_SUP")))
            values.append(
                parseToNumber(get1(vehiculo, "HORAS_COND_CONTINUADAS_H")))
            values.append(
                parseToNumber(get1(vehiculo, "HORAS_COND_CONTINUADAS_MIN")))

            values.append(
                parseToBool(
                    get2(vehiculo, "ANOMALIAS_PREVIAS",
                         "@FACT_ANOMALIAS_PREVIAS")))
            values.append(
                parseToBool(
                    get2(vehiculo, "ANOMALIAS_PREVIAS", "ANOMALIAS_NINGUNA")))
            values.append(
                parseToBool(
                    get2(vehiculo, "ANOMALIAS_PREVIAS",
                         "ANOMALIAS_NEUMATICOS")))
            values.append(
                parseToBool(
                    get2(vehiculo, "ANOMALIAS_PREVIAS", "ANOMALIAS_REVENTON")))
            values.append(
                parseToBool(
                    get2(vehiculo, "ANOMALIAS_PREVIAS",
                         "ANOMALIAS_DIRECCION")))
            values.append(
                parseToBool(
                    get2(vehiculo, "ANOMALIAS_PREVIAS", "ANOMALIAS_FRENOS")))
            values.append(
                parseToBool(
                    get2(vehiculo, "ANOMALIAS_PREVIAS", "ANOMALIAS_OTRAS")))
            values.append(get2(vehiculo, "ANOMALIAS_PREVIAS",
                               "ANOMALIAS_OTRA"))

            values.append(parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_COND")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_PAS_DEL")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_ROD_IZDA")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_ROD_DCHA")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_LAT_DEL_IZDA")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_LAT_DEL_DCHA")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_CORT_DEL_IZDA")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_CORT_DEL_DCHA")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_LAT_TRAS_IZDA")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_LAT_TRAS_DCHA")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_CORT_TRAS_IZDA")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_CORT_TRAS_DCHA")))
            values.append(parseToBool(get2(vehiculo, "AIRBAG",
                                           "AIRBAG_OTROS")))
            values.append(
                parseToBool(get2(vehiculo, "AIRBAG", "AIRBAG_DESCONOCIDO")))
            values.append(None)  # Pasajeros

        except:
            ex = sys.exc_info()[1]
            gvsig.logger(
                "No se puede leer el vehiculo %s. %s" % (vehiculo_id, str(ex)),
                gvsig.LOGGER_WARN, ex)
            raise

        return values
Example #3
0
    def read(self):
        pasajero = self.next()
        if pasajero == None:
            return None

        values = []
        pasajero_id = None
        try:
            pasajero_id = get1(pasajero, "@ID_ACCIDENTE") + "/" + get1(
                pasajero, "@ID_VEHICULO") + "/" + get1(pasajero,
                                                       "@ID_PASAJERO")

            values.append(pasajero_id)

            values.append(get1(pasajero, "@ID_ACCIDENTE"))

            values.append(
                get1(pasajero, "@ID_ACCIDENTE") + "/" +
                get1(pasajero, "@ID_VEHICULO"))

            values.append(get1(pasajero, "@ID_VEHICULO"))

            values.append(get1(pasajero, "@ID_PASAJERO"))

            values.append(get1(pasajero, "FECHA_NACIMIENTO"))
            values.append(parseToNull(get1(pasajero, "SEXO")))
            values.append(get1(pasajero, "PAIS_RESIDENCIA"))
            values.append(get1(pasajero, "PROVINCIA_RESIDENCIA"))
            values.append(get1(pasajero, "MUNICIPIO_RESIDENCIA"))
            values.append(parseToNull(get1(pasajero, "ASISTENCIA_SANITARIA")))

            values.append(parseToNull(get1(pasajero, "POSICION_VEHI")))
            values.append(parseToBool(get1(pasajero, "NINYO_EN_BRAZO")))

            values.append(
                parseToBool(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD",
                         "ACC_SEG_CINTURON")))
            values.append(
                parseToNull(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD", "ACC_SEG_CASCO")))
            values.append(
                parseToBool(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD",
                         "ACC_SEG_SIS_RETEN_INFANTIL")))
            values.append(
                parseToBool(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD_OPCIONALES",
                         "ACC_SEG_BRAZOS")))
            values.append(
                parseToBool(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD_OPCIONALES",
                         "ACC_SEG_ESPALDA")))
            values.append(
                parseToBool(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD_OPCIONALES",
                         "ACC_SEG_TORSO")))
            values.append(
                parseToBool(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD_OPCIONALES",
                         "ACC_SEG_MANOS")))
            values.append(
                parseToBool(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD_OPCIONALES",
                         "ACC_SEG_PIERNAS")))
            values.append(
                parseToBool(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD_OPCIONALES",
                         "ACC_SEG_PIES")))
            values.append(
                parseToBool(
                    get2(pasajero, "ACCESORIOS_SEGURIDAD_OPCIONALES",
                         "ACC_SEG_PRENDA_REF")))

        except:
            ex = sys.exc_info()[1]
            gvsig.logger(
                "No se puede leer el pasajero %s. %s" % (pasajero_id, str(ex)),
                gvsig.LOGGER_WARN, ex)
            raise

        return values
Example #4
0
  def read(self):
    peaton = self.next()
    if peaton == None:
      return None

    values = []
    peaton_id = None
    try:
      peaton_id = get1(peaton,"@ID_ACCIDENTE") +"/"+ get1(peaton,"@ID_PEATON")
    
      # LID_PEATON
      values.append(peaton_id)
      
      values.append(get1(peaton,"@ID_ACCIDENTE"))
      values.append(get1(peaton,"@ID_PEATON"))
      
      values.append(parseToBool(get1(peaton,"POSIBLE_RESPONSABLE")))
      
      values.append(get1(peaton,"FECHA_NACIMIENTO"))
      values.append(parseToNull(get1(peaton,"SEXO")))
      values.append(get1(peaton,"NACIONALIDAD"))
      values.append(get1(peaton,"PAIS_RESIDENCIA"))
      values.append(get1(peaton,"PROVINCIA_RESIDENCIA"))
      values.append(get1(peaton,"MUNICIPIO_RESIDENCIA"))
      values.append(parseToNull(get1(peaton,"ASISTENCIA_SANITARIA")))

      values.append(parseToBool(get2(peaton,"FACTORES_ATENCION","@INFLU_FACT_ATENCION")))
      values.append(parseToNull(get2(peaton,"FACTORES_ATENCION","#text")))
      values.append(parseToBool(get2(peaton,"PRESUNTOS_ERRORES","@INFLU_PRES_ERROR")))
      values.append(parseToNull(get2(peaton,"PRESUNTOS_ERRORES","#text")))
      values.append(parseToBool(get2(peaton,"PRES_INFRAC_PEA","@INFLU_PRES_INFRAC")))
      values.append(parseToNull(get2(peaton,"PRES_INFRAC_PEA","#text")))

      values.append(parseToNull(get1(peaton,"MOTIVO_DESPLAZAMIENTO")))
      values.append(parseToNull(get1(peaton,"ACCION_PEA")))
      
      values.append(parseToBool(get2(peaton,"ALCOHOL","@INFLU_ALCOHOL")))
      values.append(parseToNull(get2(peaton,"ALCOHOL","PRUEBA_ALCOHOLEMIA")))
      values.append(parseToNumber(get2(peaton,"ALCOHOL","TASA_ALCOHOLEMIA1")))
      values.append(parseToNumber(get2(peaton,"ALCOHOL","TASA_ALCOHOLEMIA2")))
      values.append(parseToBool(get2(peaton,"ALCOHOL","PRUEBA_ALC_SANGRE")))
      values.append(parseToBool(get2(peaton,"ALCOHOL","SIGNOS_INFLU_ALCOHOL")))

      values.append(parseToBool(get2(peaton,"DROGAS","@INFLU_DROGAS")))
      values.append(parseToNull(get2(peaton,"DROGAS","PRUEBA_DROGAS")))
      values.append(parseToBool(get2(peaton,"DROGAS","AMP")))
      values.append(parseToBool(get2(peaton,"DROGAS","CONFIRMADO_AMP")))
      values.append(parseToBool(get2(peaton,"DROGAS","BDZ")))
      values.append(parseToBool(get2(peaton,"DROGAS","CONFIRMADO_BDZ")))
      values.append(parseToBool(get2(peaton,"DROGAS","COC")))
      values.append(parseToBool(get2(peaton,"DROGAS","CONFIRMADO_COC")))
      values.append(parseToBool(get2(peaton,"DROGAS","THC")))
      values.append(parseToBool(get2(peaton,"DROGAS","CONFIRMADO_THC")))
      values.append(parseToBool(get2(peaton,"DROGAS","METH")))
      values.append(parseToBool(get2(peaton,"DROGAS","CONFIRMADO_METH")))
      values.append(parseToBool(get2(peaton,"DROGAS","OPI")))
      values.append(parseToBool(get2(peaton,"DROGAS","CONFIRMADO_OPI")))
      values.append(parseToBool(get2(peaton,"DROGAS","OTRAS")))
      values.append(parseToBool(get2(peaton,"DROGAS","CONFIRMADO_OTRAS")))
      values.append(parseToBool(get2(peaton,"DROGAS","SIGNOS_INFLU_DROGAS")))
      

    except:
      ex = sys.exc_info()[1]
      gvsig.logger("No se puede leer el peaton %s. %s" % (peaton_id,str(ex)), gvsig.LOGGER_WARN, ex)
      raise

    return values