예제 #1
0
 protected Instance getInstanceForPrimaryIp(IpAddress ipAddress) {
   if (IpAddressConstants.ROLE_PRIMARY.equals(ipAddress.getRole())) {
     for (Nic nic : getObjectManager().mappedChildren(ipAddress, Nic.class)) {
       if (nic.getDeviceNumber() != null && nic.getDeviceNumber() == 0) {
         Instance instance = getObjectManager().loadResource(Instance.class, nic.getInstanceId());
         if (instance != null) {
           return instance;
         }
       }
     }
   }
   return null;
 }
예제 #2
0
  protected String getIpAddress(
      ServiceInstanceData serviceInstanceData,
      boolean isSource,
      Map<Long, IpAddress> instanceIdToHostIpMap) {
    Nic nic = serviceInstanceData.getNic();
    ServiceExposeMap exposeMap = serviceInstanceData.getExposeMap();
    IpAddress ipAddr = serviceInstanceData.getIpAddress();
    String ip = null;

    if (isSource
        && serviceInstanceData
            .getService()
            .getKind()
            .equalsIgnoreCase(ServiceDiscoveryConstants.KIND.SERVICE.name())) {
      if (ipAddr != null) {
        ip = ipAddr.getAddress();
      }
    } else {
      if (ipAddr != null) {
        ip = ipAddr.getAddress();
      } else {
        ip = exposeMap.getIpAddress();
      }
    }

    if (nic == null || nic.getDeviceNumber().equals(0)) {
      return ip;
    } else {
      IpAddress hostIp = instanceIdToHostIpMap.get(nic.getInstanceId());
      if (hostIp != null) {
        if (isSource) {
          return "default";
        }
        return hostIp.getAddress();
      }
    }
    return null;
  }
예제 #3
0
  @Override
  public boolean recordCandidate(AllocationAttempt attempt, AllocationCandidate candidate) {
    Long newHost = candidate.getHost();
    if (newHost != null) {
      for (Instance instance : attempt.getInstances()) {
        log.info("Associating instance [{}] to host [{}]", instance.getId(), newHost);
        objectManager.create(
            InstanceHostMap.class,
            INSTANCE_HOST_MAP.HOST_ID,
            newHost,
            INSTANCE_HOST_MAP.INSTANCE_ID,
            instance.getId());

        modifyDisk(newHost, instance, true);

        List<Volume> vols = InstanceHelpers.extractVolumesFromMounts(instance, objectManager);
        for (Volume v : vols) {
          if (VolumeConstants.ACCESS_MODE_SINGLE_HOST_RW.equals(v.getAccessMode())) {
            objectManager.setFields(v, VOLUME.HOST_ID, newHost);
          }
        }
      }
    }

    Map<Long, Set<Long>> existingPools = attempt.getPoolIds();
    Map<Long, Set<Long>> newPools = candidate.getPools();

    if (!existingPools.keySet().equals(newPools.keySet())) {
      throw new IllegalStateException(
          String.format(
              "Volumes don't match. currently %s, new %s",
              existingPools.keySet(), newPools.keySet()));
    }

    for (Map.Entry<Long, Set<Long>> entry : newPools.entrySet()) {
      long volumeId = entry.getKey();
      Set<Long> existingPoolsForVol = existingPools.get(entry.getKey());
      Set<Long> newPoolsForVol = entry.getValue();
      if (existingPoolsForVol == null || existingPoolsForVol.size() == 0) {
        for (long poolId : newPoolsForVol) {
          log.info("Associating volume [{}] to storage pool [{}]", volumeId, poolId);
          objectManager.create(
              VolumeStoragePoolMap.class,
              VOLUME_STORAGE_POOL_MAP.VOLUME_ID,
              volumeId,
              VOLUME_STORAGE_POOL_MAP.STORAGE_POOL_ID,
              poolId);
        }
      } else if (!existingPoolsForVol.equals(newPoolsForVol)) {
        throw new IllegalStateException(
            String.format(
                "Can not move volume %s, currently: %s, new: %s",
                volumeId, existingPools, newPools));
      }
    }

    for (Nic nic : attempt.getNics()) {
      Long subnetId = candidate.getSubnetIds().get(nic.getId());

      if (subnetId == null
          || (nic.getSubnetId() != null && subnetId.longValue() == nic.getSubnetId())) {
        continue;
      }

      log.info("Associating nic [{}] to subnet [{}]", nic.getId(), subnetId);
      int i =
          create().update(NIC).set(NIC.SUBNET_ID, subnetId).where(NIC.ID.eq(nic.getId())).execute();

      if (i != 1) {
        throw new IllegalStateException(
            "Expected to update nic id ["
                + nic.getId()
                + "] with subnet ["
                + subnetId
                + "] but update ["
                + i
                + "] rows");
      }
    }

    return true;
  }
예제 #4
0
  @Override
  protected void populateContext(
      Agent agent, Instance instance, ConfigItem item, ArchiveContext context) {
    context.getData().put("hostnameGenerator", new HostnameGenerator());
    context.getData().put("instance", instance);
    context.getData().put("agent", agent);

    List<? extends NetworkService> services = networkInfo.networkServices(instance);
    Map<String, NetworkServiceInfo> servicesMap = new HashMap<String, NetworkServiceInfo>();
    Map<String, Subnet> subnetMap = new HashMap<String, Subnet>();
    List<Nic> nics = new ArrayList<Nic>();
    List<Subnet> subnets = new ArrayList<Subnet>();
    Set<String> serviceSet = new HashSet<String>();

    Map<Nic, Subnet> nicToSubnet = networkInfo.getNicsAndSubnet(instance);
    Network primaryNetwork = null;
    Map<Long, Network> networks = networkInfo.networks(instance);
    Map<String, IpAddress> ipAddresses = new HashMap<String, IpAddress>();

    for (NetworkService service : services) {
      serviceSet.add(service.getKind());

      NetworkServiceInfo info = servicesMap.get(service.getKind());
      if (info == null) {
        info = new NetworkServiceInfo(service);
        servicesMap.put(service.getKind(), info);
      }

      for (Map.Entry<Nic, Subnet> entry : nicToSubnet.entrySet()) {
        Nic nic = entry.getKey();
        Subnet subnet = entry.getValue();

        if (subnetMap.put(nic.getId().toString(), subnet) == null) {
          nics.add(nic);
          if (subnet != null) {
            subnets.add(subnet);
          }
        }

        if (!ipAddresses.containsKey(nic.getUuid())) {
          ipAddresses.put(nic.getUuid(), ipAddressDao.getPrimaryIpAddress(nic));
        }

        if (primaryNetwork == null && nic.getDeviceNumber() != null && nic.getDeviceNumber() == 0) {
          primaryNetwork = networks.get(nic.getNetworkId());
        }

        if (service.getNetworkId().equals(nic.getNetworkId())) {
          if (!info.getNicIds().contains(nic.getId())) {
            info.getNicIds().add(nic.getId());
            info.getNics().add(nic);
            info.getNicNames().add("eth" + nic.getDeviceNumber());
          }
        }
      }

      if (!info.getNetworkIds().contains(service.getNetworkId())) {
        info.getNetworkIds().add(service.getNetworkId());
        info.getNetworks().add(networks.get(service.getNetworkId()));
      }
    }

    context.getData().put("nics", nics);
    context.getData().put("nicToSubnet", subnetMap);
    context.getData().put("subnets", subnets);
    context.getData().put("primaryIpAddresses", ipAddresses);
    context.getData().put("services", servicesMap);
    context.getData().put("serviceSet", serviceSet);
    context.getData().put("primaryNetwork", primaryNetwork);
  }
예제 #5
0
  @Override
  public List<DnsEntryData> getServiceDnsData(
      final Instance instance, final boolean isVIPProvider) {
    MultiRecordMapper<ServiceDnsEntryData> mapper =
        new MultiRecordMapper<ServiceDnsEntryData>() {
          @Override
          protected ServiceDnsEntryData map(List<Object> input) {
            Service clientService = (Service) input.get(0);
            Service targetService = (Service) input.get(1);
            ServiceConsumeMap consumeMap = (ServiceConsumeMap) input.get(2);
            ServiceDnsEntryData data =
                new ServiceDnsEntryData(clientService, targetService, consumeMap);
            return data;
          }
        };

    ServiceTable clientService = mapper.add(SERVICE);
    ServiceTable targetService = mapper.add(SERVICE);
    ServiceConsumeMapTable serviceConsumeMap = mapper.add(SERVICE_CONSUME_MAP);

    // there are 2 conditions below linked with OR clause
    // first condition - means to return all non-dns clientService + target service map within the
    // same stack
    // that are not linked
    // second condition - return only clientService + targetService with explicit links
    Condition condition =
        (clientService
                .KIND
                .ne(ServiceDiscoveryConstants.KIND.DNSSERVICE.name())
                .and(targetService.ENVIRONMENT_ID.eq(clientService.ENVIRONMENT_ID))
                .and(serviceConsumeMap.ID.isNull()))
            .or(
                serviceConsumeMap
                    .ID
                    .isNotNull()
                    .and(serviceConsumeMap.REMOVED.isNull())
                    .and(
                        serviceConsumeMap.STATE.in(
                            CommonStatesConstants.ACTIVATING, CommonStatesConstants.ACTIVE)));

    List<ServiceDnsEntryData> serviceDnsEntries =
        create()
            .select(mapper.fields())
            .from(clientService)
            .join(targetService)
            .on(targetService.ACCOUNT_ID.eq(clientService.ACCOUNT_ID))
            .leftOuterJoin(serviceConsumeMap)
            .on(
                serviceConsumeMap
                    .SERVICE_ID
                    .eq(clientService.ID)
                    .and(serviceConsumeMap.CONSUMED_SERVICE_ID.eq(targetService.ID))
                    .and(serviceConsumeMap.REMOVED.isNull()))
            .where(targetService.REMOVED.isNull())
            .and(clientService.REMOVED.isNull())
            .and(condition)
            .fetch()
            .map(mapper);

    Nic nic = ntwkDao.getPrimaryNic(instance.getId());
    long vnetId = nic.getVnetId();
    return convertToDnsEntryData(isVIPProvider, serviceDnsEntries, instance.getAccountId(), vnetId);
  }