@Override
 public void updateLoadBalancerService(
     Service service, List<? extends Long> certIds, Long defaultCertId) {
   LoadBalancer lb =
       objectManager.findOne(
           LoadBalancer.class,
           LOAD_BALANCER.SERVICE_ID,
           service.getId(),
           LOAD_BALANCER.REMOVED,
           null);
   if (lb != null) {
     Map<String, Object> data = new HashMap<>();
     if (certIds == null) {
       certIds =
           DataAccessor.fields(service)
               .withKey(LoadBalancerConstants.FIELD_LB_CERTIFICATE_IDS)
               .asList(jsonMapper, Long.class);
     }
     if (defaultCertId == null) {
       defaultCertId =
           DataAccessor.fieldLong(service, LoadBalancerConstants.FIELD_LB_DEFAULT_CERTIFICATE_ID);
     }
     data.put(LoadBalancerConstants.FIELD_LB_CERTIFICATE_IDS, certIds);
     data.put(LoadBalancerConstants.FIELD_LB_DEFAULT_CERTIFICATE_ID, defaultCertId);
     DataUtils.getWritableFields(lb).putAll(data);
     objectManager.persist(lb);
     objectProcessManager.scheduleStandardProcess(StandardProcess.UPDATE, lb, data);
   }
 }
Example #2
0
  protected IPAssignment allocateIp(IpAddress ipAddress, Network network) {
    Instance instance = getInstanceForPrimaryIp(ipAddress);
    IPAssignment ip = null;
    String requestedIp = null;
    if (instance != null) {
      String allocatedIpAddress =
          DataAccessor.fieldString(instance, InstanceConstants.FIELD_ALLOCATED_IP_ADDRESS);
      if (allocatedIpAddress != null) {
        ip = new IPAssignment(allocatedIpAddress, null);
      }
      requestedIp =
          DataAccessor.fieldString(instance, InstanceConstants.FIELD_REQUESTED_IP_ADDRESS);
    }

    if (ip == null) {
      ip = networkService.assignIpAddress(network, ipAddress, requestedIp);
      if (ip == null) {
        objectProcessManager.scheduleStandardProcess(StandardProcess.DEACTIVATE, ipAddress, null);
        throw new ResourceExhaustionException(
            "IP allocation error", "Failed to allocate IP from subnet", ipAddress);
      }
    }

    return ip;
  }
  @SuppressWarnings("unchecked")
  protected ExternalDeploymentUnitInstance(
      DeploymentServiceContext context,
      String uuid,
      Service service,
      String launchConfigName,
      String ipAddress,
      String hostName) {
    super(context, uuid, service, launchConfigName);
    if (ipAddress != null) {
      this.ipAddress = ipAddress;
      this.exposeMap = context.exposeMapDao.getServiceIpExposeMap(service, ipAddress);
      this.serviceExternalIps =
          DataAccessor.fields(service)
              .withKey(ServiceConstants.FIELD_EXTERNALIPS)
              .withDefault(Collections.EMPTY_LIST)
              .as(List.class);

    } else {
      this.hostName = hostName;
      this.exposeMap = context.exposeMapDao.getServiceHostnameExposeMap(service, hostName);
      this.serviceHostName =
          DataAccessor.fields(service).withKey(ServiceConstants.FIELD_HOSTNAME).as(String.class);
    }
  }
  @SuppressWarnings("unchecked")
  protected Map<Long, String> populateNewServiceLinks(ApiRequest request) {
    final Map<Long, String> newServiceLinks = new HashMap<>();
    final Map<String, Long> newServiceLinksMaps =
        DataAccessor.fromMap(request.getRequestObject())
            .withKey(ServiceDiscoveryConstants.FIELD_SERVICE_LINKS)
            .withDefault(Collections.EMPTY_MAP)
            .as(Map.class);

    if (newServiceLinksMaps != null) {
      for (String linkName : newServiceLinksMaps.keySet()) {
        newServiceLinks.put(newServiceLinksMaps.get(linkName), linkName);
      }
    }

    final List<? extends Long> newConsumedServicesIds =
        DataAccessor.fromMap(request.getRequestObject())
            .withKey(ServiceDiscoveryConstants.FIELD_SERVICE_IDS)
            .asList(jsonMapper, Long.class);

    if (newConsumedServicesIds != null) {
      for (Long consumedServiceId : newConsumedServicesIds) {
        newServiceLinks.put(consumedServiceId, null);
      }
    }
    return newServiceLinks;
  }
Example #5
0
 @Override
 protected void postAuthModification(Account account) {
   if (account != null) {
     ApiRequest request = ApiContext.getContext().getApiRequest();
     String accessToken = (String) request.getAttribute(AzureConstants.AZURE_ACCESS_TOKEN);
     String refreshToken = (String) request.getAttribute(AzureConstants.AZURE_REFRESH_TOKEN);
     DataAccessor.fields(account).withKey(AzureConstants.AZURE_ACCESS_TOKEN).set(accessToken);
     DataAccessor.fields(account).withKey(AzureConstants.AZURE_REFRESH_TOKEN).set(refreshToken);
     getObjectManager().persist(account);
   }
 }
Example #6
0
  @Override
  @SuppressWarnings({"unchecked", "rawtypes"})
  public List<LockDefinition> extractAllocationLockDefinitions(Instance instance) {
    Map env =
        DataAccessor.fields(instance)
            .withKey(InstanceConstants.FIELD_ENVIRONMENT)
            .as(jsonMapper, Map.class);
    List<LockDefinition> lockDefs = extractAllocationLockDefinitionsFromEnv(env);

    Map labels =
        DataAccessor.fields(instance)
            .withKey(InstanceConstants.FIELD_LABELS)
            .as(jsonMapper, Map.class);
    if (labels == null) {
      return lockDefs;
    }

    Iterator<Map.Entry> iter = labels.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry affinityDef = iter.next();
      String key = ((String) affinityDef.getKey()).toLowerCase();
      String valueStr = (String) affinityDef.getValue();
      valueStr = valueStr == null ? "" : valueStr.toLowerCase();

      if (instance != null) {
        valueStr = evaluateMacros(valueStr, instance);
      }

      String opStr = "";
      if (key.startsWith(ContainerLabelAffinityConstraint.LABEL_HEADER_AFFINITY_CONTAINER_LABEL)) {
        opStr =
            key.substring(
                ContainerLabelAffinityConstraint.LABEL_HEADER_AFFINITY_CONTAINER_LABEL.length());
        List<AffinityConstraintDefinition> defs =
            extractAffinityConstraintDefinitionFromLabel(opStr, valueStr, true);
        for (AffinityConstraintDefinition def : defs) {
          lockDefs.add(
              new AllocateConstraintLock(AllocateConstraintLock.Type.AFFINITY, def.getValue()));
        }

      } else if (key.startsWith(ContainerAffinityConstraint.LABEL_HEADER_AFFINITY_CONTAINER)) {
        opStr = key.substring(ContainerAffinityConstraint.LABEL_HEADER_AFFINITY_CONTAINER.length());
        List<AffinityConstraintDefinition> defs =
            extractAffinityConstraintDefinitionFromLabel(opStr, valueStr, false);
        for (AffinityConstraintDefinition def : defs) {
          lockDefs.add(
              new AllocateConstraintLock(AllocateConstraintLock.Type.AFFINITY, def.getValue()));
        }
      }
    }
    return lockDefs;
  }
Example #7
0
  @Override
  public void releaseAllocation(Instance instance, InstanceHostMap map) {
    // Reload for persisting
    map = objectManager.loadResource(InstanceHostMap.class, map.getId());

    DataAccessor data =
        DataAccessor.fromDataFieldOf(map).withScope(AllocatorDao.class).withKey("deallocated");

    Boolean done = data.as(Boolean.class);
    if (done == null || !done.booleanValue()) {
      modifyDisk(map.getHostId(), instance, false);
      data.set(true);
      objectManager.persist(map);
    }
  }
 private void populateNetworkForService(
     Service service, String launchConfigName, Map<String, Object> composeServiceData) {
   Object networkMode =
       composeServiceData.get(ServiceDiscoveryConfigItem.NETWORKMODE.getDockerName());
   if (networkMode != null) {
     if (networkMode.equals(DockerNetworkConstants.NETWORK_MODE_CONTAINER)) {
       Map<String, Object> serviceData =
           ServiceDiscoveryUtil.getLaunchConfigDataAsMap(service, launchConfigName);
       // network mode can be passed by container, or by service name, so check both
       // networkFromContainerId wins
       Integer targetContainerId =
           DataAccessor.fieldInteger(service, DockerInstanceConstants.DOCKER_CONTAINER);
       if (targetContainerId != null) {
         Instance instance =
             objectManager.loadResource(Instance.class, targetContainerId.longValue());
         String instanceName = ServiceDiscoveryUtil.getInstanceName(instance);
         composeServiceData.put(
             ServiceDiscoveryConfigItem.NETWORKMODE.getDockerName(),
             DockerNetworkConstants.NETWORK_MODE_CONTAINER + ":" + instanceName);
       } else {
         Object networkLaunchConfig =
             serviceData.get(ServiceDiscoveryConstants.FIELD_NETWORK_LAUNCH_CONFIG);
         if (networkLaunchConfig != null) {
           composeServiceData.put(
               ServiceDiscoveryConfigItem.NETWORKMODE.getDockerName(),
               DockerNetworkConstants.NETWORK_MODE_CONTAINER + ":" + networkLaunchConfig);
         }
       }
     } else if (networkMode.equals(DockerNetworkConstants.NETWORK_MODE_MANAGED)) {
       composeServiceData.remove(ServiceDiscoveryConfigItem.NETWORKMODE.getDockerName());
     }
   }
 }
  protected Map<String, Object> populateLBData(
      Service service,
      String lbName,
      LoadBalancerConfig lbConfig,
      Map<String, Object> launchConfigData,
      List<? extends Long> certIds,
      Long defaultCertId) {
    Map<String, Object> data = new HashMap<>();
    data.put("name", lbName);
    data.put(LoadBalancerConstants.FIELD_LB_CONFIG_ID, lbConfig.getId());
    data.put(LoadBalancerConstants.FIELD_LB_SERVICE_ID, service.getId());
    data.put(LoadBalancerConstants.FIELD_LB_NETWORK_ID, getServiceNetworkId(service));
    data.put(
        LoadBalancerConstants.FIELD_LB_INSTANCE_IMAGE_UUID,
        launchConfigData.get(InstanceConstants.FIELD_IMAGE_UUID));
    data.put(
        LoadBalancerConstants.FIELD_LB_INSTANCE_URI_PREDICATE,
        DataAccessor.fields(service)
            .withKey(LoadBalancerConstants.FIELD_LB_INSTANCE_URI_PREDICATE)
            .withDefault("delegate:///")
            .as(String.class));
    data.put("accountId", service.getAccountId());
    if (defaultCertId != null) {
      data.put(LoadBalancerConstants.FIELD_LB_DEFAULT_CERTIFICATE_ID, defaultCertId);
    }

    if (certIds != null) {
      data.put(LoadBalancerConstants.FIELD_LB_CERTIFICATE_IDS, certIds);
    }
    return data;
  }
Example #10
0
  @Override
  public HandlerResult handle(ProcessState state, ProcessInstance process) {
    final Instance instance = (Instance) state.getResource();

    List<Volume> volumes = InstanceHelpers.extractVolumesFromMounts(instance, objectManager);
    for (final Volume v : volumes) {
      String driver = DataAccessor.fieldString(v, VolumeConstants.FIELD_VOLUME_DRIVER);
      if (StringUtils.isNotEmpty(driver) && !VolumeConstants.LOCAL_DRIVER.equals(driver)) {
        StoragePool sp = storagePoolDao.findStoragePoolByDriverName(v.getAccountId(), driver);
        if (sp == null) {
          continue;
        }
        final String accessMode = sp.getVolumeAccessMode();
        if (StringUtils.isNotEmpty(accessMode)
            && StringUtils.isEmpty(v.getAccessMode())
            && !accessMode.equals(v.getAccessMode())) {
          lockManager.lock(
              new InstanceVolumeAccessModeLock(v.getId()),
              new LockCallbackNoReturn() {
                @Override
                public void doWithLockNoResult() {
                  objectManager.setFields(v, VOLUME.ACCESS_MODE, accessMode);
                }
              });
        }
      }
    }

    return null;
  }
  @Override
  @SuppressWarnings("unchecked")
  public void createLoadBalancerService(
      Service service, List<? extends Long> certIds, Long defaultCertId) {
    String lbName = getLoadBalancerName(service);
    // 1. create load balancer config
    Map<String, Object> lbConfigData =
        (Map<String, Object>)
            DataAccessor.field(
                service,
                ServiceDiscoveryConstants.FIELD_LOAD_BALANCER_CONFIG,
                jsonMapper,
                Map.class);

    if (lbConfigData == null) {
      lbConfigData = new HashMap<String, Object>();
    }

    LoadBalancerConfig lbConfig = createDefaultLoadBalancerConfig(lbName, lbConfigData, service);

    // 2. add listeners to the config based on the ports info
    Map<String, Object> launchConfigData =
        ServiceDiscoveryUtil.getLaunchConfigDataAsMap(service, null);
    createListeners(service, lbConfig, launchConfigData);

    // 3. create a load balancer
    createLoadBalancer(service, lbName, lbConfig, launchConfigData, certIds, defaultCertId);
  }
  protected void activateDeploymentUnits(ServiceDeploymentPlanner planner) {
    /*
     * Delete invalid units
     */
    planner.cleanupBadUnits();

    /*
     * Cleanup incomplete units
     */
    planner.cleanupIncompleteUnits();

    /*
     * Stop unhealthy units for service with retainIp=true
     */
    if (DataAccessor.fieldBool(
        planner.getServices().get(0), ServiceDiscoveryConstants.FIELD_SERVICE_RETAIN_IP)) {
      planner.scheduleUnhealthyUnitsStop();
    }

    /*
     * Activate all the units
     */
    startUnits(planner);

    /*
     * Delete the units that have a bad health
     */
    planner.cleanupUnhealthyUnits();

    /*
     * Cleanup unused service indexes
     */
    planner.cleanupUnusedAndDuplicatedServiceIndexes();
  }
 public DefaultServiceDeploymentPlanner(
     List<Service> services, List<DeploymentUnit> units, DeploymentServiceContext context) {
   super(services, units, context);
   for (Service service : services) {
     int scale = DataAccessor.fieldInteger(service, ServiceDiscoveryConstants.FIELD_SCALE);
     if (scale > this.requestedScale) {
       this.requestedScale = scale;
     }
   }
 }
  protected boolean isPod(Instance instance) {
    Map<String, Object> labels = DataAccessor.fieldMap(instance, InstanceConstants.FIELD_LABELS);
    Object namespace = labels.get(K8sPreInstanceCreate.POD_NAMESPACE);
    Object name = labels.get(K8sPreInstanceCreate.POD_NAME);
    Object containerName = labels.get(K8sPreInstanceCreate.CONTAINER_NAME);
    if (namespace == null || name == null) {
      return false;
    }

    return K8sPreInstanceCreate.POD.equals(containerName);
  }
Example #15
0
  protected PooledResourceOptions getPoolOptions(IpAddress ipAddress, Instance instance) {
    PooledResourceOptions options = new PooledResourceOptions();
    if (instance != null) {
      String ip = DataAccessor.fieldString(instance, InstanceConstants.FIELD_REQUESTED_IP_ADDRESS);
      if (ip != null) {
        options.setRequestedItem(ip);
      }
    }

    return options;
  }
  protected Map<String, ServiceLink> populateNewServiceLinks(ApiRequest request) {
    Map<String, ServiceLink> newServiceLinks = new HashMap<>();
    List<? extends ServiceLink> serviceLinks =
        DataAccessor.fromMap(request.getRequestObject())
            .withKey(ServiceConstants.FIELD_SERVICE_LINKS)
            .withDefault(Collections.EMPTY_LIST)
            .asList(jsonMapper, ServiceLink.class);

    if (serviceLinks != null) {
      for (ServiceLink serviceLink : serviceLinks) {
        newServiceLinks.put(serviceLink.getUuid(), serviceLink);
      }
    }

    return newServiceLinks;
  }
Example #17
0
  @Override
  public Object create(String type, ApiRequest request, ResourceManager next) {
    ServiceEvent event = request.proxyRequestObject(ServiceEvent.class);

    /* Will never return null, MissingRequired will be thrown if missing */
    Agent agent = getAgent();
    if (agent == null) {
      throw new ClientVisibleException(ResponseCodes.FORBIDDEN, VERIFY_AGENT);
    }

    HealthcheckInstanceHostMap healthcheckInstanceHostMap = null;
    String[] splitted = event.getHealthcheckUuid().split("_");
    if (splitted.length > 2) {
      healthcheckInstanceHostMap = serviceDao.getHealthCheckInstanceUUID(splitted[0], splitted[1]);
    } else {
      healthcheckInstanceHostMap =
          objectManager.findOne(
              HealthcheckInstanceHostMap.class, ObjectMetaDataManager.UUID_FIELD, splitted[0]);
    }

    if (healthcheckInstanceHostMap == null) {
      throw new ClientVisibleException(ResponseCodes.FORBIDDEN, VERIFY_AGENT);
    }

    HealthcheckInstance healthcheckInstance =
        objectManager.loadResource(
            HealthcheckInstance.class, healthcheckInstanceHostMap.getHealthcheckInstanceId());

    if (healthcheckInstance == null) {
      return null;
    }
    Long resourceAccId =
        DataAccessor.fromDataFieldOf(agent)
            .withKey(AgentConstants.DATA_AGENT_RESOURCES_ACCOUNT_ID)
            .as(Long.class);

    if (!healthcheckInstanceHostMap.getAccountId().equals(resourceAccId)) {
      throw new ClientVisibleException(ResponseCodes.FORBIDDEN, VERIFY_AGENT);
    }

    event.setInstanceId(healthcheckInstance.getInstanceId());
    event.setHealthcheckInstanceId(healthcheckInstance.getId());
    event.setHostId(healthcheckInstanceHostMap.getHostId());

    return super.create(type, request, next);
  }
  @SuppressWarnings("unchecked")
  protected void populateLoadBalancerServiceLabels(
      Service service, String launchConfigName, Map<String, Object> composeServiceData) {
    if (!service
        .getKind()
        .equalsIgnoreCase(ServiceDiscoveryConstants.KIND.LOADBALANCERSERVICE.name())) {
      return;
    }

    Map<String, String> labels = new HashMap<>();
    if (composeServiceData.get(InstanceConstants.FIELD_LABELS) != null) {
      labels.putAll(
          (HashMap<String, String>) composeServiceData.get(InstanceConstants.FIELD_LABELS));
    }
    // get all consumed services maps
    List<? extends ServiceConsumeMap> consumedServiceMaps =
        consumeMapDao.findConsumedServices(service.getId());
    // for each port, populate the label
    for (ServiceConsumeMap map : consumedServiceMaps) {
      Service consumedService =
          objectManager.loadResource(Service.class, map.getConsumedServiceId());
      List<String> ports =
          DataAccessor.fieldStringList(map, LoadBalancerConstants.FIELD_LB_TARGET_PORTS);
      String consumedServiceName = consumedService.getName();
      if (!service.getEnvironmentId().equals(consumedService.getEnvironmentId())) {
        Environment env =
            objectManager.loadResource(Environment.class, consumedService.getEnvironmentId());
        consumedServiceName = env.getName() + "/" + consumedServiceName;
      }
      String labelName = ServiceDiscoveryConstants.LABEL_LB_TARGET + consumedServiceName;
      StringBuilder bldr = new StringBuilder();
      for (String port : ports) {
        bldr.append(port).append(",");
      }
      if (bldr.length() > 0) {
        labels.put(labelName, bldr.toString().substring(0, bldr.length() - 1));
      }
    }

    if (!labels.isEmpty()) {
      composeServiceData.put(InstanceConstants.FIELD_LABELS, labels);
    }
  }
Example #19
0
 protected List<? extends Instance> getServiceInstancesToRestart(Service service) {
   // get all instances of the service
   List<? extends Instance> instances = exposeMapDao.listServiceManagedInstances(service.getId());
   List<Instance> toRestart = new ArrayList<>();
   ServiceRestart svcRestart =
       DataAccessor.field(
           service, ServiceDiscoveryConstants.FIELD_RESTART, jsonMapper, ServiceRestart.class);
   RollingRestartStrategy strategy = svcRestart.getRollingRestartStrategy();
   Map<Long, Long> instanceToStartCount = strategy.getInstanceToStartCount();
   // compare its start_count with one set on the service restart field
   for (Instance instance : instances) {
     if (instanceToStartCount.containsKey(instance.getId())) {
       Long previousStartCount = instanceToStartCount.get(instance.getId());
       if (previousStartCount == instance.getStartCount()) {
         toRestart.add(instance);
       }
     }
   }
   return toRestart;
 }
  @Override
  public void cloneConsumingServices(Service fromService, Service toService) {
    List<ServiceLink> linksToCreate = new ArrayList<>();

    for (ServiceConsumeMap map : consumeMapDao.findConsumingServices(fromService.getId())) {
      ServiceLink link;
      List<String> ports =
          DataAccessor.fieldStringList(map, LoadBalancerConstants.FIELD_LB_TARGET_PORTS);
      if (ports == null) {
        link = new ServiceLink(toService.getId(), map.getName());
      } else {
        link = new LoadBalancerServiceLink(toService.getId(), map.getName(), ports);
      }

      link.setConsumingServiceId(map.getServiceId());
      linksToCreate.add(link);
    }

    consumeMapDao.createServiceLinks(linksToCreate);
  }
Example #21
0
  /**
   * Supported macros ${service_name} ${stack_name} LEGACY: ${project_name}
   *
   * @param valueStr
   * @param instance
   * @return
   */
  @SuppressWarnings("unchecked")
  private String evaluateMacros(String valueStr, Instance instance) {
    if (valueStr.indexOf(SERVICE_NAME_MACRO) != -1
        || valueStr.indexOf(STACK_NAME_MACRO) != -1
        || valueStr.indexOf(PROJECT_NAME_MACRO) != -1) {

      Map<String, String> labels =
          DataAccessor.fields(instance).withKey(InstanceConstants.FIELD_LABELS).as(Map.class);
      String serviceLaunchConfigName = "";
      String stackName = "";
      if (labels != null && !labels.isEmpty()) {
        for (Map.Entry<String, String> label : labels.entrySet()) {
          if (LABEL_STACK_NAME.equals(label.getKey())) {
            stackName = label.getValue();
          } else if (LABEL_STACK_SERVICE_NAME.equals(label.getKey())) {
            if (label.getValue() != null) {
              int i = label.getValue().indexOf('/');
              if (i != -1) {
                serviceLaunchConfigName = label.getValue().substring(i + 1);
              }
            }
          }
        }
      }
      if (!StringUtils.isBlank(stackName)) {
        valueStr = valueStr.replace(STACK_NAME_MACRO, stackName);

        // LEGACY: ${project_name} rename ${stack_name}
        valueStr = valueStr.replace(PROJECT_NAME_MACRO, stackName);
      }

      if (!StringUtils.isBlank(serviceLaunchConfigName)) {
        valueStr = valueStr.replace(SERVICE_NAME_MACRO, serviceLaunchConfigName);
      }
    }

    return valueStr;
  }
Example #22
0
 protected int getScale(Service service) {
   Integer i = DataAccessor.fieldInteger(service, ServiceDiscoveryConstants.FIELD_SCALE);
   return i == null ? 0 : i;
 }