@Override protected Long getId(Object obj) { if (obj instanceof Instance) { return ((Instance) obj).getId(); } return null; } }
private void createLabels(Instance instance) { @SuppressWarnings("unchecked") Map<String, String> labels = DataAccessor.fields(instance).withKey(InstanceConstants.FIELD_LABELS).as(Map.class); if (labels == null) { return; } for (Map.Entry<String, String> labelEntry : labels.entrySet()) { String labelKey = labelEntry.getKey(); String labelValue = labelEntry.getValue(); labelsService.createContainerLabel(instance.getAccountId(), instance.getId(), labelKey, labelValue); } }
@SuppressWarnings("unchecked") public void setInstanceAndHostMetadata(Instance instance, HostMetaData host, List<String> healthcheckHosts, Account account, InstanceHealthCheck healthCheck) { this.name = instance.getName(); this.uuid = instance.getUuid(); this.external_id = instance.getExternalId(); this.system = instance.getSystem(); Map<String, String> labels = DataAccessor.fields(instance).withKey(InstanceConstants.FIELD_LABELS) .withDefault(Collections.EMPTY_MAP).as(Map.class); this.labels = labels; this.hostname = instance.getHostname(); this.ports = DataAccessor.fields(instance) .withKey(InstanceConstants.FIELD_PORTS).withDefault(Collections.EMPTY_LIST) this.host_ip = host.agent_ip; this.create_index = instance.getCreateIndex(); this.health_state = instance.getHealthState(); this.start_count = instance.getStartCount(); this.state = instance.getState(); this.memory_reservation = instance.getMemoryReservation(); this.milli_cpu_reservation = instance.getMilliCpuReservation(); if (instance.getDnsInternal() != null) { this.dns = IteratorUtils.toList(Splitter.on(",").omitEmptyStrings().trimResults() .split(instance.getDnsInternal()).iterator()); if (instance.getDnsSearchInternal() != null) { this.dns_search = IteratorUtils.toList(Splitter.on(",").omitEmptyStrings().trimResults() .split(instance.getDnsSearchInternal()).iterator());
public static String getInstanceName(Instance instance) { if (instance != null && instance.getRemoved() == null) { return instance.getUuid(); } else { return null; } }
@Override public boolean isError() { return this.instance != null && (ERROR_STATES.contains(this.instance.getState()) || this.instance.getRemoved() != null); }
/** * The docker API allows for identification of a container by name or id. * This method will return the docker container id (which is stored in the * externalId field of the instance) if it is available. If it isn't * available, the instance uuid will be returned, since that is currently * used as the name for docker containers that do not have the docker id * set. * * @param instance * @return A string suitable for identifying a container in the docker API. */ public static String getDockerIdentifier(Instance instance) { return !StringUtils.isEmpty(instance.getExternalId()) ? instance.getExternalId() : instance.getUuid(); } }
setId(from.getId()); setName(from.getName()); setAccountId(from.getAccountId()); setKind(from.getKind()); setUuid(from.getUuid()); setDescription(from.getDescription()); setState(from.getState()); setCreated(from.getCreated()); setRemoved(from.getRemoved()); setRemoveTime(from.getRemoveTime()); setData(from.getData()); setAllocationState(from.getAllocationState()); setCompute(from.getCompute()); setMemoryMb(from.getMemoryMb()); setImageId(from.getImageId()); setHostname(from.getHostname()); setZoneId(from.getZoneId()); setInstanceTriggeredStop(from.getInstanceTriggeredStop()); setAgentId(from.getAgentId()); setDomain(from.getDomain()); setFirstRunning(from.getFirstRunning()); setToken(from.getToken()); setUserdata(from.getUserdata()); setRegistryCredentialId(from.getRegistryCredentialId()); setExternalId(from.getExternalId()); setNativeContainer(from.getNativeContainer()); setNetworkContainerId(from.getNetworkContainerId()); setHealthState(from.getHealthState()); setStartCount(from.getStartCount());
@Override public boolean evaluate(Instance obj) { if ((startOnce && ERROR_STATES.contains(obj.getState())) || obj.getRemoved() != null || (!startOnce && BAD_ALLOCATING_STATES.contains(obj.getState()))) { String error = TransitioningUtils.getTransitioningError(obj); String message = "Bad instance [" + key(instance) + "] in state [" + obj.getState() + "]"; if (StringUtils.isNotBlank(error)) { message = message + ": " + error; } throw new RuntimeException(message); } return context.objectManager.find(InstanceHostMap.class, INSTANCE_HOST_MAP.INSTANCE_ID, instance.getId()).size() > 0; }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { Instance instance = (Instance)state.getResource(); if (!InstanceConstants.KIND_VIRTUAL_MACHINE.equals(instance.getKind())) { return null; labels.put(SystemLabels.LABEL_VM, "true"); long mem = 512L; if (instance.getMemoryMb() == null) { labels.put(SystemLabels.LABEL_VM_MEMORY, "512"); } else { mem = instance.getMemoryMb(); labels.put(SystemLabels.LABEL_VM_MEMORY, instance.getMemoryMb().toString()); labels.put(SystemLabels.LABEL_VM_USERDATA, instance.getUserdata()); labels.put(SystemLabels.LABEL_VM_VCPU, DataAccessor.fieldString(instance, InstanceConstants.FIELD_VCPU)); Object objectDisks = DataAccessor.field(instance, InstanceConstants.FIELD_DISKS, Object.class); if (objectDisks instanceof List<?>) { String namePrefix = instance.getName(); Long svcIndexId = instance.getServiceIndexId(); uuidPart = svc.getUuid().substring(0, 7); } else { uuidPart = instance.getUuid().substring(0, 7); List<? extends Volume> volumes = volumeDao.findSharedOrUnmappedVolumes(instance.getAccountId(), name); if (volumes.size() == 0 && !assignedName) { name = String.format("%s-%s", namePrefix, name); volumes = volumeDao.findSharedOrUnmappedVolumes(instance.getAccountId(), name);
protected void waitForDeploymentUnitCreate(Instance instance) { if(StringUtils.isEmpty(instance.getDeploymentUnitUuid())) { return; instanceDao.findUnallocatedInstanceByDeploymentUnitUuid(instance.getAccountId(), instance.getDeploymentUnitUuid()); if (UNALLOCATED_WAIT_STATES.contains(i.getState())) { waitList.add(i); throw new ExecutionException("Dependencies readiness error", "instance hasn't gotten passed creating state", instance.getId());
protected Volume getRoot(Instance instance, List<Volume> volumes) { if (instance.getImageId() == null) { return null; } for (Volume volume : volumes) { if (volume.getDeviceNumber() != null && volume.getDeviceNumber().intValue() == 0) { return volume; } } return objectManager.create(Volume.class, VOLUME.ACCOUNT_ID, instance.getAccountId(), VOLUME.INSTANCE_ID, instance.getId(), VOLUME.IMAGE_ID, instance .getImageId(), VOLUME.DEVICE_NUMBER, 0, VOLUME.ATTACHED_STATE, CommonStatesConstants.ACTIVE); }
public AgentInstanceBuilderImpl(AgentInstanceFactoryImpl factory, Instance instance, Set<String> roles) { this(factory); this.accountId = instance.getAccountId(); this.zoneId = instance.getZoneId(); String uriPrefix = "event"; Map<String, Object> labels = DataAccessor.fieldMap(instance, InstanceConstants.FIELD_LABELS); Object prefix = labels.get(SystemLabels.LABEL_AGENT_URI_PREFIX); if (prefix != null) { uriPrefix = prefix.toString(); } this.uri = uriPrefix + ":///instanceId=" + instance.getId(); this.resourceAccountId = instance.getAccountId(); this.requestedRoles = roles; }
@SuppressWarnings("unchecked") void processLabels(Instance instance) { Map<String, String> labels = CollectionUtils.toMap(CollectionUtils.getNestedValue(instance.getData(), FIELD_DOCKER_INSPECT, "Config", "Labels")); for (Map.Entry<String, String>label : labels.entrySet()) { labelsService.createContainerLabel(instance.getAccountId(), instance.getId(), label.getKey(), label.getValue()); } Map<String, Object> inspect = (Map<String, Object>)instance.getData().get(FIELD_DOCKER_INSPECT); if (inspect == null) { return; } transformer.setLabels(instance, inspect); objectManager.persist(instance); }
private Long getAccountId(HealthcheckInstanceType type, long instanceId) { if (type == HealthcheckInstanceType.INSTANCE) { Instance instance = objectManager.loadResource(Instance.class, instanceId); if (instance != null) { return instance.getAccountId(); } } return null; } }
private String checkContainerNetwork(String inspectNetMode, Instance instance, Map<String, Object> data) { if (!StringUtils.startsWith(inspectNetMode, NetworkConstants.NETWORK_MODE_CONTAINER)) return null; String[] parts = StringUtils.split(inspectNetMode, ":", 2); String targetContainer = null; if (parts.length == 2) { targetContainer = parts[1]; Instance netFromInstance = instanceDao.getInstanceByUuidOrExternalId(instance.getAccountId(), targetContainer, targetContainer); if (netFromInstance == null) { Event inspectEvent = newInspectEvent(targetContainer, targetContainer); Object inspectObj = callAgentForInspect(data, inspectEvent); Map<String, Object> inspect = CollectionUtils.toMap(inspectObj); String uuid = getRancherUuidLabel(inspect, null); String externalId = inspect.get("Id") != null ? inspect.get("Id").toString() : null; netFromInstance = instanceDao.getInstanceByUuidOrExternalId(instance.getAccountId(), uuid, externalId); } if (netFromInstance != null) { DataAccessor.fields(instance).withKey(FIELD_NETWORK_CONTAINER_ID).set(netFromInstance.getId()); return NetworkConstants.NETWORK_MODE_CONTAINER; } } log.warn("Problem configuring container networking for container [externalId: {}]. Could not find target container: [{}].", instance.getExternalId(), targetContainer); return NetworkConstants.NETWORK_MODE_NONE; }
request.setInstanceId(instance.getId().toString()); request.setResourceUuid(instance.getUuid()); List<PortSpec> portReservation = new ArrayList<>(); for(Port port: objectManager.children(instance, Port.class)) { return new ComputeResourceRequest(INSTANCE_RESERVATION, 1l, poolType); case MEMORY_RESERVATION: if (instance.getMemoryReservation() != null && instance.getMemoryReservation() > 0) { ResourceRequest rr = new ComputeResourceRequest(MEMORY_RESERVATION, instance.getMemoryReservation(), poolType); return rr; if (instance.getMilliCpuReservation() != null && instance.getMilliCpuReservation() > 0) { ResourceRequest rr = new ComputeResourceRequest(CPU_RESERVATION, instance.getMilliCpuReservation(), poolType); return rr;
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { Instance instance = (Instance)state.getResource(); if (!InstanceConstants.CONTAINER_LIKE.contains(instance.getKind())) { return null; Image image = objectManager.loadResource(Image.class, instance.getImageId()); if (image == null) { dockerStorageDao.createImageForInstance(instance); && needToRemoveLabel(instance.getNativeContainer(), ObjectUtils.toString(labels.get(DockerInstanceConstants.FIELD_NETWORK_MODE)))) { labels.remove(SystemLabels.LABEL_RANCHER_NETWORK); data.put(DockerInstanceConstants.FIELD_NETWORK_MODE, mode); Network network = networkService.resolveNetwork(instance.getAccountId(), mode); if (network == null && StringUtils.isNotBlank(mode) && !instance.getNativeContainer()) { objectProcessManager.scheduleProcessInstance(InstanceConstants.PROCESS_REMOVE, instance, null); throw new ExecutionException(String.format("Failed to find network for networkMode %s", mode),
protected boolean isStartedOnce() { List<String> validStates = Arrays.asList(InstanceConstants.STATE_STOPPED, InstanceConstants.STATE_STOPPING, InstanceConstants.STATE_RUNNING); return validStates.contains(context.objectManager.reload(this.instance).getState()) && context.objectManager.find(InstanceHostMap.class, INSTANCE_HOST_MAP.INSTANCE_ID, instance.getId()).size() > 0; }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { Instance instance = (Instance) state.getResource(); if (instance.getDeploymentUnitUuid() == null) { return null; } List<Instance> dependants = objectManager.find(Instance.class, INSTANCE.REMOVED, null, INSTANCE.DEPLOYMENT_UNIT_UUID, instance.getDeploymentUnitUuid(), INSTANCE.NETWORK_CONTAINER_ID, instance.getId()); if (dependants.isEmpty()) { return null; } List<String> invalidStates = Arrays.asList(CommonStatesConstants.REMOVING, InstanceConstants.STATE_ERROR, InstanceConstants.STATE_ERRORING, InstanceConstants.STATE_STOPPING, InstanceConstants.STATE_STOPPED); for (Instance dependant : dependants) { if (!invalidStates.contains(dependant.getState())) { objectProcessManager.scheduleProcessInstance(InstanceConstants.PROCESS_STOP, dependant, null); } } return null; } }