/** * {@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()); }
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); }
protected Map<String, Object> createData(Agent agent, String uuid, Map<String, Object> data) { Map<String, Object> properties = new HashMap<>(data); properties.put(HostConstants.FIELD_REPORTED_UUID, uuid); properties.remove(ObjectMetaDataManager.UUID_FIELD); Long accountId = DataAccessor.fromDataFieldOf(agent).withKey(AgentConstants.DATA_AGENT_RESOURCES_ACCOUNT_ID).as(Long.class); if (accountId == null) { accountId = agent.getAccountId(); } properties.put(ObjectMetaDataManager.ACCOUNT_FIELD, accountId); properties.put(AgentConstants.ID_REF, agent.getId()); return properties; }
protected String getAccountUuid(Agent agent, String role) { return "agentAccount" + agent.getId() + "-" + role; }
protected void pingFailure(Agent agent) { long count = status.getUnchecked(agent.getId()).failed(); if (count < 3) { log.info("Missed ping from agent [{}] count [{}]", agent.getId(), count); } else { log.error("Failed to get ping from agent [{}] count [{}]", agent.getId(), count); } if (count >= BAD_PINGS.get()) { try { agent = objectManager.reload(agent); if (CommonStatesConstants.ACTIVE.equals(agent.getState())) { Host host = objectManager.findAny(Host.class, HOST.AGENT_ID, agent.getId()); if (host != null) { log.error("Scheduling reconnect for agent [{}] host [{}] count [{}]", agent.getId(), host.getId(), count); } else { log.error("Scheduling reconnect for agent [{}] count [{}]", agent.getId(), count); } processManager.scheduleProcessInstance(AgentConstants.PROCESS_RECONNECT, agent, null); } } catch (ProcessInstanceException e) { if (e.getExitReason() != ExitReason.CANCELED) { throw e; } } } }
public static Map<String, Object> getAgentAuth(Agent agent, ObjectManager objectManager) { Account account = objectManager.loadResource(Account.class, agent.getAccountId()); if (account == null) { return null; } return AgentUtils.getAgentAuth(account, objectManager, null); }
@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); }
private Map<String, Object> getInstanceProperties(Agent agent, AgentInstanceBuilderImpl builder) { Map<Object, Object> properties = new HashMap<Object, Object>(); properties.put(INSTANCE.ACCOUNT_ID, getAccountId(builder)); properties.put(INSTANCE.AGENT_ID, agent.getId()); properties.put(InstanceConstants.FIELD_IMAGE_UUID, builder.getImageUuid()); properties.put(INSTANCE.NAME, builder.getName()); properties.put(INSTANCE.ZONE_ID, agent.getZoneId()); properties.put(INSTANCE.KIND, builder.getInstanceKind()); properties.put(InstanceConstants.FIELD_PRIVILEGED, builder.isPrivileged()); properties.put(InstanceConstants.FIELD_NETWORK_IDS, getNetworkIds(agent, builder)); properties.putAll(builder.getParams()); addAdditionalProperties(properties, agent, builder); return objectManager.convertToPropertiesFor(Instance.class, properties); }
private boolean isAgentDisconnected(Host host) { if (host == null) { return false; } Agent agent = context.objectManager.loadResource(Agent.class, host.getAgentId()); if (agent != null && (AgentConstants.STATE_RECONNECTING.equals(agent.getState()) || AgentConstants.STATE_DISCONNECTED.equals(agent.getState()) || AgentConstants.STATE_DISCONNECTING .equals(agent.getState()))) { return true; } return false; }
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); }
@Override public ExternalAgent handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= 300) { throw new IOException(String.format("Failed to delete external agent externalId=%s, response error code %s", agent.getUuid(), response.getStatusLine().getReasonPhrase())); } return null; } });
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { Agent agent = (Agent) state.getResource(); if (agent.getExternalId() != null) { return null; } regionService.deactivateAndRemoveExternalAgent(agent); return null; }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { Object resource = state.getResource(); Long agentId = AgentLocatorImpl.getAgentId(resource); Agent agent = objectManager.loadResource(Agent.class, agentId); if (agent == null || agent.getRemoved() != null) { return null; } try { objectProcessManager.scheduleStandardProcess(StandardProcess.DEACTIVATE, agent, ProcessUtils.chainInData(state.getData(), AgentConstants.PROCESS_DEACTIVATE, AgentConstants.PROCESS_REMOVE)); } catch (ProcessCancelException e) { objectProcessManager.scheduleStandardProcess(StandardProcess.REMOVE, agent, state.getData()); } return null; } }
public AgentResourceCreateLock(Agent agent) { super("AGENT.RESOUCE.CREATE." + agent.getId()); }
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 AgentAndHost loadAgentAndHostData(ImmutablePair<Long, String> agentIdAndHostUuid) { Long agentId = agentIdAndHostUuid.left; String hostUuid = agentIdAndHostUuid.right; Agent agent = objectManager.loadResource(Agent.class, agentId); Host host = null; Map<String, Host> hosts = null; if (agent != null) { hosts = agentDao.getHosts(agent.getId()); host = hosts.get(hostUuid); } if (agent == null || agent.getAccountId() == null || host == null) throw new CantFindAgentAndHostException(); return new AgentAndHost(agent.getAccountId(), host.getId()); }
protected Long getAgent() { ApiRequest request = ApiContext.getContext().getApiRequest(); Long agentId = getRawAgentId(); if (agentId != null) { return agentId; } String type = request.getSchemaFactory().getSchemaName(Agent.class); ResourceManager rm = getLocator().getResourceManagerByType(type); Long id = null; /* This really isn't the best logic. Basically we are looking for agents with state in STATES */ for (Object obj : rm.list(type, null, Pagination.limit(2))) { if (!(obj instanceof Agent)) { continue; } Agent agent = (Agent)obj; if (STATES.contains(agent.getState())) { if (id != null) { throw new ValidationErrorException(ValidationErrorCodes.MISSING_REQUIRED, "agentId"); } else { if (DISCONNECTED.contains(agent.getState())) { processManager.scheduleProcessInstance(AgentConstants.PROCESS_RECONNECT, agent, null); } id = agent.getId(); } } } return id; }
protected Credential getCredential(Agent agent) { Account account = loadResource(Account.class, agent.getAccountId()); for (Credential cred : children(account, Credential.class)) { if (cred.getKind().equals(CredentialConstants.KIND_AGENT_API_KEY) && CommonStatesConstants.ACTIVE.equals(cred.getState())) { return cred; } } return null; }
@Override protected Event callSync(RemoteAgent remoteAgent, Event event, EventCallOptions options) { Agent agent = loadResource(Agent.class, remoteAgent.getAgentId()); if (agent != null && (AgentConstants.STATE_RECONNECTING.equals(agent.getState()) || AgentConstants.STATE_DISCONNECTED.equals(agent.getState()))) { return null; } return super.callSync(remoteAgent, event, options); }
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; } }); }