/** * {@inheritDoc} */ @Override public void from(io.cattle.platform.core.model.PhysicalHost 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()); setAgentId(from.getAgentId()); setExternalId(from.getExternalId()); setDriver(from.getDriver()); }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { PhysicalHost physHost = (PhysicalHost) state.getResource(); if (!StringUtils.equalsIgnoreCase(physHost.getKind(), KIND_MACHINE)) { return null; } Map<Object, Object> newFields = new HashMap<Object, Object>(); if (StringUtils.isEmpty(physHost.getExternalId())) { String externalId = UUID.randomUUID().toString(); newFields.put(PHYSICAL_HOST.EXTERNAL_ID, externalId); } else { newFields.put(PHYSICAL_HOST.EXTERNAL_ID, physHost.getExternalId()); } String driver = HostCreateToProvision.getDriver(physHost); if (driver != null) { newFields.put(FIELD_DRIVER, driver); } return new HandlerResult(newFields); }
if (StringUtils.isNotBlank(extractedConfig) && extractedConfig.startsWith("{")) { try { extractedConfig = secretsService.decrypt(host.getAccountId(), extractedConfig); } catch (Exception e) { throw new IOException(e); response.setContentLength(content.length); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + host.getName() + ".tar.gz"); response.setHeader("Cache-Control", "private"); response.setHeader("Pragma", "private");
return host.getId(); if (host != null && host.getRemoved() == null) { Long agentId = DataAccessor.fields(host).withKey(AgentConstants.ID_REF).as(Long.class); host.setAgentId(agent.getId()); DataAccessor.fields(host).withKey(AgentConstants.ID_REF).remove(); objectManager.persist(host); return host.getId(); if (host != null && host.getAgentId() == null && host.getRemoved() == null) { host.setAgentId(agent.getId()); DataAccessor.fields(host).withKey(AgentConstants.ID_REF).remove(); objectManager.persist(host); return host.getId(); host = resourceDao.createAndSchedule(PhysicalHost.class, data); return host.getId();
if (!CommonStatesConstants.ACTIVE.equals(physicalHost.getState())) { objectProcessManager.scheduleStandardProcess(StandardProcess.ERROR, host, null); String message = TransitioningUtils.getTransitioningMessage(physicalHost); MachineConstants.FIELD_DRIVER, physicalHost.getDriver() ).withChainProcessName(objectProcessManager.getProcessName(host, StandardProcess.ACTIVATE));
protected void upgradeHosts(Account account) { List<PhysicalHost> phyHosts = objectManager.find(PhysicalHost.class, PHYSICAL_HOST.DRIVER, new Condition(ConditionType.NOTNULL), PHYSICAL_HOST.REMOVED, null, PHYSICAL_HOST.ACCOUNT_ID, account.getId()); for (PhysicalHost physicalHost : phyHosts) { for (Host host : objectManager.children(physicalHost, Host.class)) { String driver = DataAccessor.fieldString(host, MachineConstants.FIELD_DRIVER); if (!physicalHost.getDriver().equals(driver)) { objectManager.setFields(host, MachineConstants.FIELD_DRIVER, physicalHost.getDriver()); } } } }
@Override public Map<String, PhysicalHost> getPhysicalHosts(long agentId) { Map<String,PhysicalHost> hosts = new HashMap<>(); List<? extends PhysicalHost> hostList = create() .select(PHYSICAL_HOST.fields()) .from(PHYSICAL_HOST) .where( PHYSICAL_HOST.AGENT_ID.eq(agentId) .and(PHYSICAL_HOST.REMOVED.isNull())) .fetchInto(PhysicalHostRecord.class); for ( PhysicalHost host : hostList ) { String uuid = host.getExternalId(); if (StringUtils.isEmpty(uuid)) { uuid = DataAccessor.fields(host).withKey(HostConstants.FIELD_REPORTED_UUID).as(String.class); } if (StringUtils.isEmpty(uuid)) { uuid = host.getUuid(); } hosts.put(uuid, host); } return hosts; }
@Override public PhysicalHost createMachineForHost(final Host host, String driver) { String uuid = UUID.randomUUID().toString(); final Map<Object, Object> data = new HashMap<Object, Object>(DataUtils.getFields(host)); data.put(PHYSICAL_HOST.KIND, MachineConstants.KIND_MACHINE); data.put(PHYSICAL_HOST.NAME, DataAccessor.fieldString(host, HostConstants.FIELD_HOSTNAME)); data.put(PHYSICAL_HOST.DESCRIPTION, host.getDescription()); data.put(PHYSICAL_HOST.ACCOUNT_ID, host.getAccountId()); data.put(PHYSICAL_HOST.EXTERNAL_ID, uuid); data.put(PHYSICAL_HOST.DRIVER, driver); PhysicalHost phyHost = DeferredUtils.nest(new Callable<PhysicalHost>() { @Override public PhysicalHost call() throws Exception { return genericResourceDao.createAndSchedule(PhysicalHost.class, objectManager.convertToPropertiesFor(PhysicalHost.class, data)); } }); objectManager.setFields(host, HOST.PHYSICAL_HOST_ID, phyHost.getId(), HostConstants.FIELD_REPORTED_UUID, uuid); return phyHost; }
@Override public Resource filter(ApiRequest request, Object original, Resource converted) { if (!(original instanceof PhysicalHost)) { return converted; } Object extracted = converted.getFields().get(MachineConstants.EXTRACTED_CONFIG_FIELD); if (extracted instanceof String) { try { if (((String) extracted).startsWith("{")) { extracted = serviceService.decrypt(((PhysicalHost)original).getAccountId(), (String)extracted); } } catch (Exception e) { log.error("Failed to decrypt machine extracted config", e); } converted.getFields().put(MachineConstants.EXTRACTED_CONFIG_FIELD, extracted); } return converted; }
@Override public boolean evaluate(final PhysicalHost obj) { boolean transitioning = objectMetaDataManager.isTransitioningState(obj.getClass(), obj.getState()); if (!transitioning) { return true; } objectManager.reload(host); if (!host.getState().equals(HostConstants.STATE_PROVISIONING)) { throw new ResourceTimeoutException(host, "provisioning canceled"); } String message = TransitioningUtils.getTransitioningMessage(obj); objectManager.setFields(host, ObjectMetaDataManager.TRANSITIONING_MESSAGE_FIELD, message); DeferredUtils.nest(new Runnable() { @Override public void run() { ObjectUtils.publishChanged(eventService, objectManager, host); } }); return false; }
updates.put(HostConstants.FIELD_AGENT_ID, physicalHost.getAgentId());
@Override public Map<Long, PhysicalHost> getPhysicalHostsForHosts(List<Long> hostIds) { Map<Long, PhysicalHost> hosts = new HashMap<>(); List<? extends PhysicalHost> hostList = create().select(PHYSICAL_HOST.fields()) .from(PHYSICAL_HOST) .join(HOST) .on(HOST.PHYSICAL_HOST_ID.eq(PHYSICAL_HOST.ID)) .where(HOST.ID.in(hostIds)) .fetchInto(PhysicalHostRecord.class); for (PhysicalHost host : hostList) { hosts.put(host.getId(), host); } return hosts; }