private void doHandleNotification(Notification notif, Object o) { LOGGER.trace("handleNotification[{}]", notif); if (notif instanceof MBeanServerNotification) { MBeanServerNotification notification = (MBeanServerNotification) notif; String domain = notification.getMBeanName().getDomain(); String path = CONTAINER_DOMAIN.getPath((String) o, domain); try { if (MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(notification.getType())) { if (domains.add(domain) && exists(curator.get(), path) == null) { setData(curator.get(), path, ""); } } else if (MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals( notification.getType())) { domains.clear(); domains.addAll(Arrays.asList(mbeanServer.get().getDomains())); if (!domains.contains(domain)) { // domain is no present any more deleteSafe(curator.get(), path); } } } catch (Exception e) { LOGGER.warn( "Exception while jmx domain synchronization from event: " + notif + ". This exception will be ignored.", e); } } }
// FIXME public access on the impl public String getZookeeperInfo(String name) { assertValid(); String zooKeeperUrl = null; // We are looking directly for at the zookeeper for the url, since container might not even be // mananaged. // Also this is required for the integration with the IDE. try { if (curator.get().getZookeeperClient().isConnected()) { Version defaultVersion = getDefaultVersion(); if (defaultVersion != null) { Profile profile = defaultVersion.getProfile("default"); if (profile != null) { Map<String, String> zookeeperConfig = profile.getConfiguration(Constants.ZOOKEEPER_CLIENT_PID); if (zookeeperConfig != null) { zooKeeperUrl = getSubstitutedData(curator.get(), zookeeperConfig.get(name)); } } } } } catch (Exception e) { // Ignore it. } if (zooKeeperUrl == null) { try { Configuration config = configAdmin.get().getConfiguration(Constants.ZOOKEEPER_CLIENT_PID, null); zooKeeperUrl = (String) config.getProperties().get(name); } catch (Exception e) { // Ignore it. } } return zooKeeperUrl; }
@Activate void activate() { lock = lockService.get().getLock(PROFILE_WORKER_LOCK); RuntimeProperties props = runtimeProperties.get(); name = props.getProperty(SystemProperties.KARAF_NAME); activateComponent(); }
private String getWebUrl(String containerPath) throws Exception { if (curator.get().checkExists().forPath(containerPath) != null) { byte[] bytes = ZkPath.loadURL(curator.get(), containerPath); String text = new String(bytes); // NOTE this is a bit naughty, we should probably be doing // Jackson parsing here; but we only need 1 String and // this avoids the jackson runtime dependency - its just a bit brittle // only finding http endpoints and all String prefix = "\"services\":[\""; int idx = text.indexOf(prefix); String answer = text; if (idx > 0) { int startIndex = idx + prefix.length(); int endIdx = text.indexOf("\"]", startIndex); if (endIdx > 0) { answer = text.substring(startIndex, endIdx); if (answer.length() > 0) { // lets expand any variables answer = ZooKeeperUtils.getSubstitutedData(curator.get(), answer); return answer; } } } } return null; }
@Override public List<URI> getMavenRepoURIs() { assertValid(); try { List<URI> uris = new ArrayList<URI>(); if (exists(curator.get(), ZkPath.MAVEN_PROXY.getPath("download")) != null) { List<String> children = getChildren(curator.get(), ZkPath.MAVEN_PROXY.getPath("download")); if (children != null && !children.isEmpty()) { Collections.sort(children); } if (children != null) { for (String child : children) { String mavenRepo = getSubstitutedPath( curator.get(), ZkPath.MAVEN_PROXY.getPath("download") + "/" + child); if (mavenRepo != null && !mavenRepo.endsWith("/")) { mavenRepo += "/"; } uris.add(new URI(mavenRepo)); } } } return uris; } catch (Exception e) { throw FabricException.launderThrowable(e); } }
private void manageProfile(TaskContext context) { Container current = fabricService.get().getCurrentContainer(); ProfileData profileData = createProfileData(context); String profileId = context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME) + "-" + name; Version version = current.getVersion(); try { if (lock.acquire(60, TimeUnit.SECONDS)) { if (profileData.isEmpty()) { if (version.hasProfile(profileId)) { // Just delete the profile version.getProfile(profileId).delete(true); } return; } else if (!version.hasProfile(profileId)) { // Create the profile fabricService.get().getDataStore().createProfile(version.getId(), profileId); } Profile managedProfile = version.getProfile(profileId); // managedProfile.setConfigurations(profileData.getConfigs()); managedProfile.setFileConfigurations(profileData.getFiles()); current.addProfiles(managedProfile); } else { throw new TimeoutException("Timed out waiting for lock"); } } catch (Exception e) { LOGGER.error("Error managing work items.", e); } finally { releaseLock(); } }
private void registerDomains() throws Exception { String runtimeIdentity = runtimeProperties.get().getRuntimeIdentity(); synchronized (this) { domains.addAll(Arrays.asList(mbeanServer.get().getDomains())); } for (String domain : mbeanServer.get().getDomains()) { setData( curator.get(), CONTAINER_DOMAIN.getPath(runtimeIdentity, domain), "", CreateMode.EPHEMERAL); } }
protected String getFirstService(String containerPath) throws Exception { CuratorFramework curatorFramework = curator.get(); if (curatorFramework != null) { byte[] data = curatorFramework.getData().forPath(containerPath); if (data != null && data.length > 0) { String text = new String(data).trim(); if (!text.isEmpty()) { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> map = mapper.readValue(data, HashMap.class); Object serviceValue = map.get("services"); if (serviceValue instanceof List) { List services = (List) serviceValue; if (services != null) { if (!services.isEmpty()) { List<String> serviceTexts = new ArrayList<String>(); for (Object service : services) { String serviceText = getSubstitutedData(curatorFramework, service.toString()); if (io.fabric8.common.util.Strings.isNotBlank(serviceText)) { return serviceText; } } } } } } } } return null; }
@Override public String getRestAPI() { assertValid(); String restApiFolder = ZkPath.REST_API_CLUSTERS.getPath("FabricResource/fabric8"); try { CuratorFramework curatorFramework = curator.get(); if (curatorFramework != null) { List<String> versions = getChildrenSafe(curatorFramework, restApiFolder); for (String version : versions) { String versionPath = restApiFolder + "/" + version; List<String> containers = getChildrenSafe(curatorFramework, versionPath); for (String container : containers) { String containerPath = versionPath + "/" + container; String answer = getFirstService(containerPath); if (!Strings.isNullOrEmpty(answer)) { return answer; } } } } } catch (Exception e) { // On exception just return uri. LOGGER.warn("Failed to find API " + restApiFolder + ". " + e, e); } return null; }
public void destroyContainer(Container container, boolean force) { assertValid(); String containerId = container.getId(); LOGGER.info("Destroying container {}", containerId); boolean destroyed = false; try { ContainerProvider provider = getProvider(container, true); if (provider != null) { try { provider.stop(container); } catch (Exception ex) { // Ignore error while stopping and try to destroy. } provider.destroy(container); destroyed = true; } } finally { try { if (destroyed || force) { portService.get().unregisterPort(container); getDataStore().deleteContainer(container.getId()); } } catch (Exception e) { LOGGER.warn( "Failed to cleanup container {} entries due to: {}. This will be ignored.", containerId, e.getMessage()); } } }
private void registerMBeanServer() { try { String runtimeIdentity = runtimeProperties.get().getRuntimeIdentity(); mbeanServer .get() .addNotificationListener( new ObjectName("JMImplementation:type=MBeanServerDelegate"), this, null, runtimeIdentity); registerDomains(); registerFabricMBeans(); } catch (Exception e) { LOGGER.warn( "An error occurred during mbean server registration. This exception will be ignored.", e); } }
private void registerFabricMBeans() { this.healthCheck = new HealthCheck(fabricService.get()); this.managerMBean = new FabricManager((FabricServiceImpl) fabricService.get()); this.zooKeeperMBean = new ZooKeeperFacade((FabricServiceImpl) fabricService.get()); this.fileSystemMBean = new FileSystem(runtimeProperties.get()); healthCheck.registerMBeanServer(shutdownTracker, mbeanServer.get()); managerMBean.registerMBeanServer(shutdownTracker, mbeanServer.get()); fileSystemMBean.registerMBeanServer(shutdownTracker, mbeanServer.get()); zooKeeperMBean.registerMBeanServer(shutdownTracker, mbeanServer.get()); }
@Override @SuppressWarnings("unchecked") public <T> T adapt(Class<T> type) { assertValid(); if (type.isAssignableFrom(CuratorFramework.class)) { return (T) curator.get(); } return null; }
@Override public void stop(TaskContext context) { Container current = fabricService.get().getCurrentContainer(); Version version = current.getVersion(); String profileId = context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME) + "-" + name; if (version.hasProfile(profileId)) { // Just delete the profile version.getProfile(profileId).delete(true); } }
/** * Creates a representation of the profile based on the assigned item for the specified * {@linkTaskContext}. * * @param context * @return */ private ProfileData createProfileData(TaskContext context) { ProfileData profileData = new ProfileData(); Set<WorkItem> workItems = assignedWorkItems.get(context); if (workItems.isEmpty()) { return profileData; } Container current = fabricService.get().getCurrentContainer(); Version version = current.getVersion(); String templateProfileName = String.valueOf(context.getConfiguration().get(TEMPLATE_PROFILE_PROPERTY_NAME)); Profile templateProfile = version.getProfile(templateProfileName); Set<String> allFiles = templateProfile.getFileConfigurations().keySet(); Iterable<String> mvelFiles = Iterables.filter(allFiles, MvelPredicate.INSTANCE); Iterable<String> plainFiles = Iterables.filter(allFiles, Predicates.not(MvelPredicate.INSTANCE)); for (String mvelFile : mvelFiles) { Key key = new Key(templateProfile.getId(), mvelFile); synchronized (templates) { CompiledTemplate template = templates.get(key); if (template == null) { template = TemplateCompiler.compileTemplate( new String(templateProfile.getFileConfigurations().get(mvelFile)), parserContext); templates.put(key, template); } } } for (WorkItem workItem : workItems) { Map<String, WorkItem> data = new HashMap<String, WorkItem>(); data.put(WorkItem.ITEM, workItem); // Render templates for (String fileTemplate : mvelFiles) { String file = renderTemplateName(fileTemplate, workItem); Key key = new Key(templateProfile.getId(), fileTemplate); try { String renderedTemplate = TemplateRuntime.execute(templates.get(key), parserContext, data).toString(); updateProfileData(file, renderedTemplate, profileData); } catch (Exception ex) { LOGGER.warn("Failed to render {}. Ignoring.", fileTemplate); } } // Copy plain files. for (String file : plainFiles) { String content = new String(templateProfile.getFileConfigurations().get(file)); updateProfileData(file, content, profileData); } } return profileData; }
@Override public InterProcessLock getLock(String path) { synchronized (locks) { assertValid(); if (locks.containsKey(path)) { return locks.get(path); } else { locks.put(path, new InterProcessMutex(curator.get(), path)); return locks.get(path); } } }
private void updateProcessId() { try { // TODO: this is Sun JVM specific ... // String processName = (String) mbeanServer.get().getAttribute(new // ObjectName("java.lang:type=Runtime"), "Name"); String processName = ManagementFactory.getRuntimeMXBean().getName(); Long processId = Long.parseLong(processName.split("@")[0]); String runtimeIdentity = runtimeProperties.get().getRuntimeIdentity(); String path = ZkPath.CONTAINER_PROCESS_ID.getPath(runtimeIdentity); Stat stat = exists(curator.get(), path); if (stat != null) { if (stat.getEphemeralOwner() != curator.get().getZookeeperClient().getZooKeeper().getSessionId()) { delete(curator.get(), path); if (processId != null) { create(curator.get(), path, processId.toString(), CreateMode.EPHEMERAL); } } } else { if (processId != null) { create(curator.get(), path, processId.toString(), CreateMode.EPHEMERAL); } } } catch (Exception ex) { LOGGER.error("Error while updating the process id.", ex); } }
private void unregisterMBeanServer() { try { mbeanServer .get() .removeNotificationListener( new ObjectName("JMImplementation:type=MBeanServerDelegate"), this); unregisterFabricMBeans(); } catch (Exception e) { LOGGER.warn( "An error occurred during mbean server unregistration. This exception will be ignored.", e); } }
private String containerWebAppUrl(String versionsPath, String name) { try { if (exists(curator.get(), versionsPath) != null) { List<String> children = getChildren(curator.get(), versionsPath); if (children != null && !children.isEmpty()) { for (String child : children) { if (Strings.isNullOrEmpty(name)) { // lets just use the first container we find String parentPath = versionsPath + "/" + child; List<String> grandChildren = getChildren(curator.get(), parentPath); if (!grandChildren.isEmpty()) { String containerPath = parentPath + "/" + grandChildren.get(0); String answer = getWebUrl(containerPath); if (!Strings.isNullOrEmpty(answer)) { return answer; } } } else { String childPath = versionsPath + "/" + child; String containerPath = childPath + "/" + name; String answer = getWebUrl(containerPath); if (Strings.isNullOrEmpty(answer)) { // lets recurse into a child folder just in case // or in the case of servlet paths where there may be extra levels of depth answer = containerWebAppUrl(childPath, name); } if (!Strings.isNullOrEmpty(answer)) { return answer; } } } } } } catch (Exception e) { LOGGER.error("Failed to find container Jolokia URL " + e, e); } return null; }
@Override public URI getMavenRepoUploadURI() { assertValid(); URI uri = URI.create(getDefaultRepo()); try { if (exists(curator.get(), ZkPath.MAVEN_PROXY.getPath("upload")) != null) { List<String> children = getChildren(curator.get(), ZkPath.MAVEN_PROXY.getPath("upload")); if (children != null && !children.isEmpty()) { Collections.sort(children); String mavenRepo = getSubstitutedPath( curator.get(), ZkPath.MAVEN_PROXY.getPath("upload") + "/" + children.get(0)); if (mavenRepo != null && !mavenRepo.endsWith("/")) { mavenRepo += "/"; } uri = new URI(mavenRepo); } } } catch (Exception e) { // On exception just return uri. } return uri; }
public void createOrUpdateDataStoreConfig(CreateEnsembleOptions options) throws IOException { Configuration config = configAdmin.get().getConfiguration(Constants.DATASTORE_PID, null); Dictionary<String, Object> properties = config.getProperties(); if (properties == null || properties.isEmpty()) { boolean updateConfig = false; properties = new Hashtable<String, Object>(); Map<String, String> dataStoreProperties = options.getDataStoreProperties(); for (Map.Entry<String, String> entry : dataStoreProperties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); Object oldval = properties.put(key, value); updateConfig = updateConfig || !value.equals(oldval); } if (updateConfig) { config.update(properties); } } }
void bootIfNeeded() throws IOException { BundleContext bundleContext = componentContext.getBundleContext(); boolean isCreated = checkCreated(bundleContext); if (!Strings.isNotBlank(zookeeperUrl) && !isCreated && options.isEnsembleStart()) { String connectionUrl = getConnectionUrl(options); DataStoreOptions bootOptions = new DataStoreOptions(runtimeId, homeDir, connectionUrl, options); runtimeProperties .get() .putRuntimeAttribute( DataStoreTemplate.class, new DataStoreBootstrapTemplate(bootOptions)); createOrUpdateDataStoreConfig(options); createZooKeeeperServerConfig(options); createZooKeeeperClientConfig(connectionUrl, options); markCreated(bundleContext); } }
/** Creates ZooKeeper client configuration. */ public void createZooKeeeperClientConfig(String connectionUrl, CreateEnsembleOptions options) throws IOException { Dictionary<String, Object> properties = new Hashtable<String, Object>(); if (options.isAutoImportEnabled()) { loadPropertiesFrom( properties, options.getImportPath() + "/fabric/profiles/default.profile/io.fabric8.zookeeper.properties"); } properties.put("zookeeper.url", connectionUrl); properties.put( "zookeeper.timeout", System.getProperties().containsKey("zookeeper.timeout") ? System.getProperties().getProperty("zookeeper.timeout") : "30000"); properties.put("fabric.zookeeper.pid", Constants.ZOOKEEPER_CLIENT_PID); properties.put("zookeeper.password", PasswordEncoder.encode(options.getZookeeperPassword())); Configuration config = configAdmin.get().getConfiguration(Constants.ZOOKEEPER_CLIENT_PID, null); config.update(properties); }
@Override public String getGitUrl() { assertValid(); String restApiFolder = ZkPath.GIT.getPath(); try { CuratorFramework curatorFramework = curator.get(); if (curatorFramework != null) { List<String> versions = getChildrenSafe(curatorFramework, restApiFolder); for (String version : versions) { String versionPath = restApiFolder + "/" + version; String answer = getFirstService(versionPath); if (!Strings.isNullOrEmpty(answer)) { return answer; } } } } catch (Exception e) { // On exception just return uri. LOGGER.warn("Failed to find API " + restApiFolder + ". " + e, e); } return null; }
/** Creates ZooKeeper server configuration */ public void createZooKeeeperServerConfig(CreateEnsembleOptions options) throws IOException { int serverPort = Ports.mapPortToRange( options.getZooKeeperServerPort(), options.getMinimumPort(), options.getMaximumPort()); String serverHost = options.getBindAddress(); Dictionary<String, Object> properties = new Hashtable<String, Object>(); if (options.isAutoImportEnabled()) { loadPropertiesFrom( properties, options.getImportPath() + "/fabric/profiles/default.profile/io.fabric8.zookeeper.server.properties"); } properties.put("tickTime", String.valueOf(options.getZooKeeperServerTickTime())); properties.put("initLimit", String.valueOf(options.getZooKeeperServerInitLimit())); properties.put("syncLimit", String.valueOf(options.getZooKeeperServerSyncLimit())); properties.put("dataDir", options.getZooKeeperServerDataDir() + File.separator + "0000"); properties.put("clientPort", Integer.toString(serverPort)); properties.put("clientPortAddress", serverHost); properties.put("fabric.zookeeper.pid", "io.fabric8.zookeeper.server-0000"); Configuration config = configAdmin.get().createFactoryConfiguration(Constants.ZOOKEEPER_SERVER_PID, null); config.update(properties); }
FabricService getFabricService() { return fabricService.get(); }
@Override public Action createNewAction() { assertValid(); return new ArchetypeCreateAction(archetypeService.get()); }
@Override public Action createNewAction() { assertValid(); return new ContainerCreateSshAction(fabricService.get(), clusterService.get()); }
private void unregisterFabricMBeans() { zooKeeperMBean.unregisterMBeanServer(mbeanServer.get()); fileSystemMBean.unregisterMBeanServer(mbeanServer.get()); managerMBean.unregisterMBeanServer(mbeanServer.get()); healthCheck.unregisterMBeanServer(mbeanServer.get()); }
protected ProcessManager getProcessManager() { return processManager.get(); }