protected EventService getWrappedEventService(long agentId) { Agent agent = objectManager.loadResource(Agent.class, agentId); if (agent == null) { return null; } String uri = agent.getUri(); if (uri != null && !uri.startsWith(EVENTING)) { return null; } return new WrappedEventService(agentId, false, eventService, null, jsonMapper, delegateDao); }
protected EventService buildDelegate(long agentId) { Map<String, Object> instanceData = new LinkedHashMap<>(); Agent hostAgent = getAgentForDelegate(agentId, instanceData); if (hostAgent == null) { return null; } String hostAgentUri = hostAgent.getUri(); if (hostAgentUri != null && !hostAgentUri.startsWith(EVENTING)) { return null; } return new WrappedEventService(hostAgent.getId(), true, eventService, instanceData, jsonMapper, delegateDao); }
private void cleanAgents(HashSet<String> existingLinks) { List<Agent> agents = objectManager.find(Agent.class, AGENT.REMOVED, new Condition(ConditionType.NULL), AGENT.EXTERNAL_ID, new Condition(ConditionType.NOTNULL)); if(agents.size() > 0) { for(Agent agent : agents) { try { String[] uri = agent.getUri().substring(RegionUtil.EXTERNAL_AGENT_URI_PREFIX.length()).split("_"); String regionEnv = String.format("%s:%s", uri[0], uri[1]); if (existingLinks.contains(regionEnv)) { continue; } cleanupAgent(agent); } catch (Exception ex) { log.warn(String.format("Fail to remove agent ", agent.getId(), ex)); } } } }
protected Instance createInstance(final Agent agent, final Map<String, Object> properties, final AgentInstanceBuilderImpl builder) { return lockManager.lock(new AgentInstanceAgentCreateLock(agent.getUri()), new LockCallback<Instance>() { @Override public Instance doWithLock() { Instance instance = factoryDao.getInstanceByAgent(agent.getId()); if (instance == null) { instance = DeferredUtils.nest(new Callable<Instance>() { @Override public Instance call() throws Exception { return resourceDao.createAndSchedule(Instance.class, properties); } }); } return instance; } }); }
protected Simulator makeSimulator(String agentId) { Agent agent = objectManager.loadResource(Agent.class, agentId); if (agent == null) { return NULL_SIMULATOR; } String uri = agent.getUri(); if (uri == null) { return NULL_SIMULATOR; } if (uri.startsWith("sim://")) { return new AgentConnectionSimulator(objectManager, agent, processors); } Map<String, Object> instanceData = new LinkedHashMap<>(); Agent hostAgent = agentLocator.getAgentForDelegate(Long.parseLong(agentId), instanceData); if (hostAgent == null) { return NULL_SIMULATOR; } Simulator target = cache.getUnchecked(hostAgent.getId().toString()); if (target == null) { return NULL_SIMULATOR; } return new DelegateSimulator(target, instanceData); }
String uri = agent.getUri(); if (uri != null && !uri.startsWith(DELEGATE)) { return null; log.error("Failed to find instance to delegate to for agent [{}] uri [{}]", agent.getId(), agent.getUri()); throw new IllegalStateException("Delegate [" + agent.getUri() + "] has no instance associated"); log.error("Failed to find host to delegate to for agent [{}] uri [{}]", agent.getId(), agent.getUri()); return null;
@Override public Event handle(AgentConnectionSimulator simulator, Event event) throws Exception { if (!FrameworkEvents.PING.equals(event.getName())) return null; Agent agent = simulator.getAgent(); Ping ping = jsonMapper.convertValue(event, Ping.class); Ping pong = jsonMapper.convertValue(EventVO.reply(event).withData(ping.getData()), Ping.class); if (ping.getOption(Ping.RESOURCES) && !agent.getUri().startsWith("delegate://")) { addResources(pong, agent); } if (ping.getOption(Ping.INSTANCES) && !agent.getUri().startsWith("delegate://")) { addInstances(simulator, pong, agent); } return pong; }
@Override public Object create(String type, ApiRequest request, ResourceManager next) { String ip = request.getClientIp(); Agent agent = request.proxyRequestObject(Agent.class); /* * This ensures that the accountId is always set from the request and * never overwritten by the default accountId setting logic. In the * situation in which the client doesn't have access to the accountId * field, the result will be null, which is correct. You want it to be * null so that the AgentCreate logic will create an account for this * Agent */ agent.setAccountId(agent.getAccountId()); String uri = agent.getUri(); String user = DataUtils.getFieldFromRequest(request, AgentConstants.USER, String.class); if (uri == null) { uri = getUri(user, ip); if (uri != null) { isUnique(uri); agent.setUri(uri); } } return super.create(type, request, next); }
/** * {@inheritDoc} */ @Override public void from(io.cattle.platform.core.model.Agent from) { 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()); setUri(from.getUri()); setManagedConfig(from.getManagedConfig()); setZoneId(from.getZoneId()); setExternalId(from.getExternalId()); }