Search in sources :

Example 46 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class ProfileWatcherImpl method run.

public void run() {
    assertValid();
    LOG.debug("Profile watcher thread started");
    int oldCounter = -1;
    SortedSet<String> oldActiveProfiles = null;
    Map<File, Long> localChecksums = new HashMap<File, Long>();
    Map<File, Long> localModified = new HashMap<File, Long>();
    Set<Profile> refreshProfiles = new HashSet<Profile>();
    ProfileService profileService = fabricService.get().adapt(ProfileService.class);
    while (running.get() && watchURLs.size() > 0) {
        SortedSet<String> currentActiveProfiles = getCurrentActiveProfileVersions();
        if (profileArtifacts == null || oldCounter != counter.get() || oldActiveProfiles == null || !oldActiveProfiles.equals(currentActiveProfiles)) {
            oldCounter = counter.get();
            oldActiveProfiles = currentActiveProfiles;
            try {
                LOG.debug("Reloading the currently active profile artifacts");
                profileArtifacts = findProfileArifacts();
            } catch (Exception e) {
                LOG.error("Failed to get profiles artifacts: " + e, e);
            }
        }
        // lets refresh profiles on the next loop; so we've time to finish uploading/modifying files
        for (Profile profile : refreshProfiles) {
            LOG.info("Refreshing profile: " + profile);
            Profiles.refreshProfile(fabricService.get(), profile);
        }
        refreshProfiles.clear();
        if (profileArtifacts != null) {
            List<File> localRepositories = new LinkedList<>();
            if (mavenResolver.get().getLocalRepository() != null) {
                localRepositories.add(mavenResolver.get().getLocalRepository());
            }
            if (mavenResolver.get().getDefaultRepositories() != null) {
                for (LocalRepository repository : mavenResolver.get().getDefaultRepositories()) {
                    localRepositories.add(repository.getBasedir());
                }
            }
            Set<Map.Entry<ProfileVersionKey, Map<String, Parser>>> entries = profileArtifacts.entrySet();
            for (Map.Entry<ProfileVersionKey, Map<String, Parser>> entry : entries) {
                ProfileVersionKey key = entry.getKey();
                Map<String, Parser> artifactMap = entry.getValue();
                // lets find a container for the profile
                Profile profile = key.getProfile();
                Properties checksums = findProfileChecksums(fabricService.get(), profile);
                if (checksums != null) {
                    Set<Map.Entry<String, Parser>> artifactMapEntries = artifactMap.entrySet();
                    for (Map.Entry<String, Parser> artifactMapEntry : artifactMapEntries) {
                        String location = artifactMapEntry.getKey();
                        Parser parser = artifactMapEntry.getValue();
                        if (isSnapshot(parser) || wildCardMatch(location)) {
                            Object value = checksums.get(location);
                            if (value == null) {
                                value = checksums.get(JavaContainers.removeUriPrefixBeforeMaven(location));
                            }
                            Long checksum = null;
                            if (value instanceof Number) {
                                checksum = ((Number) value).longValue();
                            } else if (value instanceof String) {
                                checksum = Long.parseLong((String) value);
                            }
                            if (checksum == null) {
                                if (missingChecksums.add(location)) {
                                    LOG.warn("Could not find checksum for location " + location);
                                }
                            } else {
                                File file = null;
                                for (File localRepository : localRepositories) {
                                    File _file = new File(localRepository.getPath() + File.separator + parser.getArtifactPath());
                                    if (_file.isFile()) {
                                        file = _file;
                                        break;
                                    }
                                }
                                if (!file.exists()) {
                                    LOG.info("Ignoring file " + file.getPath() + " as it does not exist");
                                } else {
                                    // lets use a cache of last modified times to avoid having to continuously
                                    // recalculate the checksum on each file
                                    Long oldModfied = localModified.get(file);
                                    long modified = file.lastModified();
                                    if (oldModfied == null || modified != oldModfied) {
                                        localModified.put(file, modified);
                                        Long fileChecksum = getFileChecksum(file);
                                        if (fileChecksum != null && !fileChecksum.equals(checksum)) {
                                            // lets keep track of local checksums in case we've already started the upload process
                                            // and it takes the profile a little while to respond to uploaded jars and to
                                            // refreshed profiles
                                            Long localChecksum = localChecksums.get(file);
                                            if (localChecksum == null || !localChecksum.equals(fileChecksum)) {
                                                localChecksums.put(file, fileChecksum);
                                                LOG.info("Checksums don't match for " + location + ", container: " + checksum + " and local file: " + fileChecksum);
                                                LOG.info("Updated version of " + location + " detected in " + file);
                                                if (isUpload()) {
                                                    uploadFile(location, parser, file);
                                                }
                                                refreshProfiles.add(profile);
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Ignoring " + location);
                            }
                        }
                    }
                }
            }
        }
        try {
            Thread.sleep(interval);
        } catch (InterruptedException ex) {
            running.set(false);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Profile watcher thread stopped");
    }
}
Also used : HashMap(java.util.HashMap) Properties(java.util.Properties) Profile(io.fabric8.api.Profile) HashSet(java.util.HashSet) LocalRepository(org.eclipse.aether.repository.LocalRepository) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Parser(io.fabric8.maven.util.Parser) ProfileService(io.fabric8.api.ProfileService) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 47 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class DeploymentAgent method doUpdate.

public boolean doUpdate(Dictionary<String, ?> props) throws Exception {
    if (props == null || Boolean.parseBoolean((String) props.get("disabled"))) {
        return false;
    }
    final Hashtable<String, String> properties = new Hashtable<>();
    for (Enumeration e = props.keys(); e.hasMoreElements(); ) {
        Object key = e.nextElement();
        Object val = props.get(key);
        if (!"service.pid".equals(key) && !FeatureConfigInstaller.FABRIC_ZOOKEEPER_PID.equals(key)) {
            properties.put(key.toString(), val.toString());
        }
    }
    updateStatus("analyzing", null);
    // Building configuration
    curatorCompleteService.waitForService(TimeUnit.SECONDS.toMillis(30));
    String httpUrl;
    List<URI> mavenRepoURIs;
    // force reading of updated informations from ZK
    if (!fabricService.isEmpty()) {
        updateMavenRepositoryConfiguration(fabricService.getService());
    }
    try {
        fabricServiceOperations.lock();
        // no one will change the members now
        httpUrl = this.httpUrl;
        mavenRepoURIs = this.mavenRepoURIs;
    } finally {
        fabricServiceOperations.unlock();
    }
    addMavenProxies(properties, httpUrl, mavenRepoURIs);
    final MavenResolver resolver = MavenResolvers.createMavenResolver(properties, "org.ops4j.pax.url.mvn");
    final DownloadManager manager = DownloadManagers.createDownloadManager(resolver, getDownloadExecutor());
    manager.addListener(new DownloadCallback() {

        @Override
        public void downloaded(StreamProvider provider) throws Exception {
            int pending = manager.pending();
            updateStatus(pending > 0 ? "downloading (" + pending + " pending)" : "downloading", null);
        }
    });
    // Update framework, libs, system and config props
    final Object lock = new Object();
    final AtomicBoolean restart = new AtomicBoolean();
    final Set<String> libsToRemove = new HashSet<>(managedLibs.keySet());
    final Set<String> endorsedLibsToRemove = new HashSet<>(managedEndorsedLibs.keySet());
    final Set<String> extensionLibsToRemove = new HashSet<>(managedExtensionLibs.keySet());
    final Set<String> sysPropsToRemove = new HashSet<>(managedSysProps.keySet());
    final Set<String> configPropsToRemove = new HashSet<>(managedConfigProps.keySet());
    final Set<String> etcsToRemove = new HashSet<>(managedEtcs.keySet());
    final Properties configProps = new Properties(new File(KARAF_BASE + File.separator + "etc" + File.separator + "config.properties"));
    final Properties systemProps = new Properties(new File(KARAF_BASE + File.separator + "etc" + File.separator + "system.properties"));
    Downloader downloader = manager.createDownloader();
    for (String key : properties.keySet()) {
        if (key.equals("framework")) {
            String url = properties.get(key);
            if (!url.startsWith("mvn:")) {
                throw new IllegalArgumentException("Framework url must use the mvn: protocol");
            }
            downloader.download(url, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File file = provider.getFile();
                    String path = file.getPath();
                    if (path.startsWith(KARAF_HOME)) {
                        path = path.substring(KARAF_HOME.length() + 1);
                    }
                    synchronized (lock) {
                        if (!path.equals(configProps.get("karaf.framework.felix"))) {
                            configProps.put("karaf.framework", "felix");
                            configProps.put("karaf.framework.felix", path);
                            restart.set(true);
                        }
                    }
                }
            });
        } else if (key.startsWith("config.")) {
            String k = key.substring("config.".length());
            String v = properties.get(key);
            synchronized (lock) {
                managedConfigProps.put(k, v);
                configPropsToRemove.remove(k);
                if (!v.equals(configProps.get(k))) {
                    configProps.put(k, v);
                    restart.set(true);
                }
            }
        } else if (key.startsWith("system.")) {
            String k = key.substring("system.".length());
            synchronized (lock) {
                String v = properties.get(key);
                managedSysProps.put(k, v);
                sysPropsToRemove.remove(k);
                if (!v.equals(systemProps.get(k))) {
                    systemProps.put(k, v);
                    restart.set(true);
                }
            }
        } else if (key.startsWith("lib.")) {
            String value = properties.get(key);
            downloader.download(value, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File libFile = provider.getFile();
                    String libName = libFile.getName();
                    Long checksum = ChecksumUtils.checksum(libFile);
                    boolean update;
                    synchronized (lock) {
                        managedLibs.put(libName, "true");
                        libsToRemove.remove(libName);
                        update = !Long.toString(checksum).equals(libChecksums.getProperty(libName));
                    }
                    if (update) {
                        Files.copy(libFile, new File(LIB_PATH, libName));
                        restart.set(true);
                    }
                }
            });
        } else if (key.startsWith("endorsed.")) {
            String value = properties.get(key);
            downloader.download(value, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File libFile = provider.getFile();
                    String libName = libFile.getName();
                    Long checksum = ChecksumUtils.checksum(new FileInputStream(libFile));
                    boolean update;
                    synchronized (lock) {
                        managedEndorsedLibs.put(libName, "true");
                        endorsedLibsToRemove.remove(libName);
                        update = !Long.toString(checksum).equals(endorsedChecksums.getProperty(libName));
                    }
                    if (update) {
                        Files.copy(libFile, new File(LIB_ENDORSED_PATH, libName));
                        restart.set(true);
                    }
                }
            });
        } else if (key.startsWith("extension.")) {
            String value = properties.get(key);
            downloader.download(value, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File libFile = provider.getFile();
                    String libName = libFile.getName();
                    Long checksum = ChecksumUtils.checksum(libFile);
                    boolean update;
                    synchronized (lock) {
                        managedExtensionLibs.put(libName, "true");
                        extensionLibsToRemove.remove(libName);
                        update = !Long.toString(checksum).equals(extensionChecksums.getProperty(libName));
                    }
                    if (update) {
                        Files.copy(libFile, new File(LIB_EXT_PATH, libName));
                        restart.set(true);
                    }
                }
            });
        } else if (key.startsWith("etc.")) {
            String value = properties.get(key);
            downloader.download(value, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File etcFile = provider.getFile();
                    String etcName = etcFile.getName();
                    Long checksum = ChecksumUtils.checksum(new FileInputStream(etcFile));
                    boolean update;
                    synchronized (lock) {
                        managedEtcs.put(etcName, "true");
                        etcsToRemove.remove(etcName);
                        update = !Long.toString(checksum).equals(etcChecksums.getProperty(etcName));
                    }
                    if (update) {
                        Files.copy(etcFile, new File(KARAF_ETC, etcName));
                    }
                }
            });
        }
    }
    downloader.await();
    // Remove unused libs, system & config properties
    for (String sysProp : sysPropsToRemove) {
        systemProps.remove(sysProp);
        managedSysProps.remove(sysProp);
        System.clearProperty(sysProp);
        restart.set(true);
    }
    for (String configProp : configPropsToRemove) {
        configProps.remove(configProp);
        managedConfigProps.remove(configProp);
        restart.set(true);
    }
    for (String lib : libsToRemove) {
        File libFile = new File(LIB_PATH, lib);
        libFile.delete();
        libChecksums.remove(lib);
        managedLibs.remove(lib);
        restart.set(true);
    }
    for (String lib : endorsedLibsToRemove) {
        File libFile = new File(LIB_ENDORSED_PATH, lib);
        libFile.delete();
        endorsedChecksums.remove(lib);
        managedEndorsedLibs.remove(lib);
        restart.set(true);
    }
    for (String lib : extensionLibsToRemove) {
        File libFile = new File(LIB_EXT_PATH, lib);
        libFile.delete();
        extensionChecksums.remove(lib);
        managedExtensionLibs.remove(lib);
        restart.set(true);
    }
    for (String etc : etcsToRemove) {
        File etcFile = new File(KARAF_ETC, etc);
        etcFile.delete();
        etcChecksums.remove(etc);
        managedEtcs.remove(etc);
    }
    libChecksums.save();
    endorsedChecksums.save();
    extensionChecksums.save();
    etcChecksums.save();
    managedLibs.save();
    managedEndorsedLibs.save();
    managedExtensionLibs.save();
    managedConfigProps.save();
    managedSysProps.save();
    managedEtcs.save();
    if (restart.get()) {
        updateStatus("restarting", null);
        configProps.save();
        systemProps.save();
        System.setProperty("karaf.restart", "true");
        bundleContext.getBundle(0).stop();
        return false;
    }
    FeatureConfigInstaller configInstaller = null;
    ServiceReference configAdminServiceReference = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
    if (configAdminServiceReference != null) {
        ConfigurationAdmin configAdmin = (ConfigurationAdmin) bundleContext.getService(configAdminServiceReference);
        configInstaller = new FeatureConfigInstaller(bundleContext, configAdmin, manager);
    }
    int bundleStartTimeout = Constants.BUNDLE_START_TIMEOUT;
    String overriddenTimeout = properties.get(Constants.BUNDLE_START_TIMEOUT_PID_KEY);
    try {
        if (overriddenTimeout != null)
            bundleStartTimeout = Integer.parseInt(overriddenTimeout);
    } catch (Exception e) {
        LOGGER.warn("Failed to set {} value: [{}], applying default value: {}", Constants.BUNDLE_START_TIMEOUT_PID_KEY, overriddenTimeout, Constants.BUNDLE_START_TIMEOUT);
    }
    Agent agent = new Agent(bundleContext.getBundle(), systemBundleContext, manager, configInstaller, null, DEFAULT_FEATURE_RESOLUTION_RANGE, DEFAULT_BUNDLE_UPDATE_RANGE, DEFAULT_UPDATE_SNAPSHOTS, bundleContext.getDataFile(STATE_FILE), bundleStartTimeout) {

        @Override
        public void updateStatus(String status) {
            DeploymentAgent.this.updateStatus(status, null, false);
        }

        @Override
        public void updateStatus(String status, boolean force) {
            DeploymentAgent.this.updateStatus(status, null, force);
        }

        @Override
        protected void saveState(State newState) throws IOException {
            super.saveState(newState);
            DeploymentAgent.this.state.replace(newState);
        }

        @Override
        protected void provisionList(Set<Resource> resources) {
            DeploymentAgent.this.provisionList = resources;
        }

        @Override
        protected boolean done(boolean agentStarted, List<String> urls) {
            if (agentStarted) {
                // let's do patch-management "last touch" only if new agent wasn't started.
                return true;
            }
            // agent finished provisioning, we can call back to low level patch management
            ServiceReference<PatchManagement> srPm = systemBundleContext.getServiceReference(PatchManagement.class);
            ServiceReference<FabricService> srFs = systemBundleContext.getServiceReference(FabricService.class);
            if (srPm != null && srFs != null) {
                PatchManagement pm = systemBundleContext.getService(srPm);
                FabricService fs = systemBundleContext.getService(srFs);
                if (pm != null && fs != null) {
                    LOGGER.info("Validating baseline information");
                    this.updateStatus("validating baseline information", true);
                    Profile profile = fs.getCurrentContainer().getOverlayProfile();
                    Map<String, String> versions = profile.getConfiguration("io.fabric8.version");
                    File localRepository = resolver.getLocalRepository();
                    if (pm.alignTo(versions, urls, localRepository, new PatchSynchronization())) {
                        this.updateStatus("requires full restart", true);
                        // let's reuse the same flag
                        restart.set(true);
                        return false;
                    }
                    if (handleRestartJvmFlag(profile, restart)) {
                        return false;
                    }
                }
            }
            return true;
        }
    };
    agent.setDeploymentAgentId(deploymentAgentId);
    agent.provision(getPrefixedProperties(properties, "repository."), getPrefixedProperties(properties, "feature."), getPrefixedProperties(properties, "bundle."), getPrefixedProperties(properties, "req."), getPrefixedProperties(properties, "override."), getPrefixedProperties(properties, "optional."), getMetadata(properties, "metadata#"));
    if (restart.get()) {
        // prevent updating status to "success"
        return false;
    }
    return true;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) DownloadCallback(io.fabric8.agent.download.DownloadCallback) Downloader(io.fabric8.agent.download.Downloader) Properties(org.apache.felix.utils.properties.Properties) URI(java.net.URI) DownloadManager(io.fabric8.agent.download.DownloadManager) Profile(io.fabric8.api.Profile) PatchManagement(io.fabric8.patch.management.PatchManagement) MavenResolver(io.fabric8.maven.MavenResolver) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Agent(io.fabric8.agent.service.Agent) StreamProvider(io.fabric8.agent.download.StreamProvider) Enumeration(java.util.Enumeration) Hashtable(java.util.Hashtable) ConfigurationException(org.osgi.service.cm.ConfigurationException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ServiceReference(org.osgi.framework.ServiceReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FeatureConfigInstaller(io.fabric8.agent.service.FeatureConfigInstaller) State(io.fabric8.agent.service.State) FabricService(io.fabric8.api.FabricService) File(java.io.File) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 48 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class VerifyFeatureResolutionMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        Field field = URL.class.getDeclaredField("factory");
        field.setAccessible(true);
        field.set(null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    URL.setURLStreamHandlerFactory(new CustomBundleURLStreamHandlerFactory());
    System.setProperty("karaf.home", "target/karaf");
    System.setProperty("karaf.data", "target/karaf/data");
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(8);
    Hashtable<String, String> properties = new Hashtable<>();
    if (additionalMetadata != null) {
        try (Reader reader = new FileReader(additionalMetadata)) {
            Properties metadata = new Properties();
            metadata.load(reader);
            for (Enumeration<?> e = metadata.propertyNames(); e.hasMoreElements(); ) {
                Object key = e.nextElement();
                Object val = metadata.get(key);
                properties.put(key.toString(), val.toString());
            }
        } catch (IOException e) {
            throw new MojoExecutionException("Unable to load additional metadata from " + additionalMetadata, e);
        }
    }
    DownloadManager manager;
    MavenResolver resolver;
    final Map<String, Repository> repositories;
    Map<String, Feature[]> repos = new HashMap<>();
    Map<String, Feature> allFeatures = new HashMap<>();
    try {
        resolver = MavenResolvers.createMavenResolver(null, properties, "org.ops4j.pax.url.mvn", repositorySystem);
        manager = DownloadManagers.createDownloadManager(resolver, executor);
        repositories = downloadRepositories(manager, descriptors).call();
        for (String repoUri : repositories.keySet()) {
            Feature[] features = repositories.get(repoUri).getFeatures();
            // Ack features to inline configuration files urls
            for (Feature feature : features) {
                for (BundleInfo bi : feature.getBundles()) {
                    String loc = bi.getLocation();
                    String nloc = null;
                    if (loc.contains("file:")) {
                        for (ConfigFile cfi : feature.getConfigurationFiles()) {
                            if (cfi.getFinalname().substring(1).equals(loc.substring(loc.indexOf("file:") + "file:".length()))) {
                                nloc = cfi.getLocation();
                            }
                        }
                    }
                    if (nloc != null) {
                        bi.setLocation(loc.substring(0, loc.indexOf("file:")) + nloc);
                    }
                }
                allFeatures.put(feature.getId(), feature);
            }
            repos.put(repoUri, features);
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Unable to load features descriptors", e);
    }
    List<Feature> featuresToTest = new ArrayList<>();
    if (verifyTransitive) {
        for (Feature[] features : repos.values()) {
            featuresToTest.addAll(Arrays.asList(features));
        }
    } else {
        for (String uri : descriptors) {
            featuresToTest.addAll(Arrays.asList(repos.get(uri)));
        }
    }
    if (features != null && !features.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        for (String feature : features) {
            if (sb.length() > 0) {
                sb.append("|");
            }
            String p = feature.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*");
            sb.append(p);
            if (!feature.contains("/")) {
                sb.append("/.*");
            }
        }
        Pattern pattern = Pattern.compile(sb.toString());
        for (Iterator<Feature> iterator = featuresToTest.iterator(); iterator.hasNext(); ) {
            Feature feature = iterator.next();
            String id = feature.getName() + "/" + feature.getVersion();
            if (!pattern.matcher(id).matches()) {
                iterator.remove();
            }
        }
    }
    for (String fmk : framework) {
        properties.put("feature.framework." + fmk, fmk);
    }
    List<Throwable> failures = new ArrayList<>();
    for (Feature feature : featuresToTest) {
        try {
            String id = feature.getName() + "/" + feature.getVersion();
            manager = DownloadManagers.createDownloadManager(resolver, executor);
            verifyResolution(manager, allFeatures, id, properties);
            getLog().info("Verification of feature " + id + " succeeded");
        } catch (Exception e) {
            getLog().warn(e.getMessage());
            failures.add(e);
            if ("first".equals(fail)) {
                throw e;
            }
        }
    }
    if ("end".equals(fail) && !failures.isEmpty()) {
        throw new MojoExecutionException("Verification failures", new MultiException("Verification failures", failures));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) FileReader(java.io.FileReader) DeploymentAgent.getPrefixedProperties(io.fabric8.agent.DeploymentAgent.getPrefixedProperties) Properties(java.util.Properties) DownloadManager(io.fabric8.agent.download.DownloadManager) Feature(io.fabric8.agent.model.Feature) Field(java.lang.reflect.Field) BundleInfo(io.fabric8.agent.model.BundleInfo) FileReader(java.io.FileReader) Pattern(java.util.regex.Pattern) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ConfigFile(io.fabric8.agent.model.ConfigFile) Hashtable(java.util.Hashtable) IOException(java.io.IOException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) MultiException(io.fabric8.common.util.MultiException) Repository(io.fabric8.agent.model.Repository) MultiException(io.fabric8.common.util.MultiException)

Example 49 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class ProfileDisplayAction method displayProfile.

private void displayProfile(Profile profile) {
    PrintStream output = System.out;
    output.println("Profile id: " + profile.getId());
    output.println("Version   : " + profile.getVersion());
    output.println("Attributes: ");
    Map<String, String> props = profile.getAttributes();
    for (String key : props.keySet()) {
        output.println("\t" + key + ": " + props.get(key));
    }
    String versionId = profile.getVersion();
    String profileId = profile.getId();
    output.printf("Containers: %s\n", toString(fabricService.getAssociatedContainers(versionId, profileId)));
    ProfileService profileService = fabricService.adapt(ProfileService.class);
    if (overlay) {
        profile = profileService.getOverlayProfile(profile);
    }
    Map<String, Map<String, String>> configuration = new HashMap<>(profile.getConfigurations());
    Map<String, byte[]> resources = profile.getFileConfigurations();
    Map<String, String> agentConfiguration = profile.getConfiguration(Constants.AGENT_PID);
    List<String> agentProperties = new ArrayList<String>();
    List<String> systemProperties = new ArrayList<String>();
    List<String> configProperties = new ArrayList<String>();
    List<String> otherResources = new ArrayList<String>();
    for (Map.Entry<String, String> entry : agentConfiguration.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (value.contains(",")) {
            value = "\t" + value.replace(",", ",\n\t\t");
        }
        if (key.startsWith("system.")) {
            systemProperties.add("  " + key.substring("system.".length()) + " = " + value);
        } else if (key.startsWith("config.")) {
            configProperties.add("  " + key.substring("config.".length()) + " = " + value);
        } else if (!key.startsWith("feature.") && !key.startsWith("repository") && !key.startsWith("bundle.") && !key.startsWith("fab.") && !key.startsWith("override.") && !key.startsWith("attribute.")) {
            agentProperties.add("  " + key + " = " + value);
        }
    }
    if (configuration.containsKey(Constants.AGENT_PID)) {
        output.println("\nContainer settings");
        output.println("----------------------------");
        if (profile.getLibraries().size() > 0) {
            printConfigList("Libraries : ", output, profile.getLibraries());
        }
        if (profile.getRepositories().size() > 0) {
            printConfigList("Repositories : ", output, profile.getRepositories());
        }
        if (profile.getFeatures().size() > 0) {
            printConfigList("Features : ", output, profile.getFeatures());
        }
        if (profile.getBundles().size() > 0) {
            printConfigList("Bundles : ", output, profile.getBundles());
        }
        if (profile.getFabs().size() > 0) {
            printConfigList("Fabs : ", output, profile.getFabs());
        }
        if (profile.getOverrides().size() > 0) {
            printConfigList("Overrides : ", output, profile.getOverrides());
        }
        if (agentProperties.size() > 0) {
            printConfigList("Agent Properties : ", output, agentProperties);
        }
        if (systemProperties.size() > 0) {
            printConfigList("System Properties : ", output, systemProperties);
        }
        if (configProperties.size() > 0) {
            printConfigList("Config Properties : ", output, configProperties);
        }
        configuration.remove(Constants.AGENT_PID);
    }
    output.println("\nConfiguration details");
    output.println("----------------------------");
    for (Map.Entry<String, Map<String, String>> cfg : configuration.entrySet()) {
        output.println("PID: " + cfg.getKey());
        for (Map.Entry<String, String> values : cfg.getValue().entrySet()) {
            output.println("  " + values.getKey() + " " + values.getValue());
        }
        output.println("\n");
    }
    output.println("\nOther resources");
    output.println("----------------------------");
    for (Map.Entry<String, byte[]> resource : resources.entrySet()) {
        String name = resource.getKey();
        if (!name.endsWith(".properties")) {
            output.println("Resource: " + resource.getKey());
            if (displayResources) {
                output.println(new String(resource.getValue(), Charsets.UTF_8));
                output.println("\n");
            }
        }
    }
}
Also used : PrintStream(java.io.PrintStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProfileService(io.fabric8.api.ProfileService) HashMap(java.util.HashMap) Map(java.util.Map)

Example 50 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class FabricGitSummaryAction method afterEachContainer.

@Override
protected void afterEachContainer(Collection<String> names) {
    System.out.printf("Scheduled git-summary command to %d containers. Awaiting response(s).\n", names.size());
    final CountDownLatch latch = new CountDownLatch(requests.size() + (masterRequest == null ? 0 : 1));
    Thread waiter = null;
    try {
        waiter = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    String currentContainer = null;
                    try {
                        // master request
                        if (masterRequest != null && masterResult == null) {
                            currentContainer = master;
                            List<JMXResult> results = fetchResponses(master);
                            for (JMXResult result : results) {
                                if (result.getCorrelationId().equals(masterRequest.getId())) {
                                    masterResult = result;
                                    latch.countDown();
                                    break;
                                }
                            }
                        }
                        // requests for containers
                        for (Map.Entry<String, JMXRequest> req : requests.entrySet()) {
                            currentContainer = req.getKey();
                            List<JMXResult> containerResults = fetchResponses(currentContainer);
                            for (JMXResult result : containerResults) {
                                if (result.getCorrelationId().equals(req.getValue().getId())) {
                                    results.put(currentContainer, result);
                                    latch.countDown();
                                    break;
                                }
                            }
                        }
                        if ((masterRequest == null || masterResult != null) && results.size() == requests.size()) {
                            break;
                        } else {
                            // active waiting - so no need for ZK watchers, etc...
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break;
                    } catch (Exception e) {
                        System.err.println("Problem occurred while fetching response from " + currentContainer + " container: " + e.getMessage());
                        // active waiting - so no need for ZK watchers, etc...
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                        }
                    }
                }
            }
        });
        waiter.start();
        boolean finished = latch.await(timeout, TimeUnit.MILLISECONDS);
        if (!finished) {
            waiter.interrupt();
            System.out.println("Timeout waiting for git-summary response");
            return;
        }
        // before printing summary, let's check if there are out of sync containers
        Map<String, String> centralGitRepo = new HashMap<>();
        Set<String> outOfSyncContainers = new TreeSet<>();
        if (masterResult != null) {
            for (GitVersion version : ((GitVersions) masterResult.getResponse()).getVersions()) {
                centralGitRepo.put(version.getVersion(), version.getSha1());
            }
            for (String containerName : results.keySet()) {
                List<GitVersion> localRepo = ((GitVersions) results.get(containerName).getResponse()).getVersions();
                for (GitVersion version : localRepo) {
                    String ref = centralGitRepo.get(version.getVersion());
                    if (ref == null) {
                        // local container knows about version, which is not tracked in central repo
                        outOfSyncContainers.add(containerName);
                    } else if (!ref.equals(version.getSha1())) {
                        // version not in sync
                        outOfSyncContainers.add(containerName);
                    }
                }
                if (localRepo.size() != centralGitRepo.size()) {
                    // extra or not-in-sync versions handled, so this must mean some central version is not
                    // available locally
                    outOfSyncContainers.add(containerName);
                }
            }
            if (outOfSyncContainers.size() > 0) {
                System.out.println();
                System.out.println("Containers that require synchronization: " + outOfSyncContainers);
                System.out.println("Please use \"fabric:git-synchronize\" command");
            }
        } else {
            System.out.println();
            System.out.println("Can't determine synchronization status of containers - no master Git repository detected");
        }
        // now summary time
        if (masterResult != null) {
            System.out.println();
            printVersions("=== Summary for master Git repository (container: " + master + ") ===", ((GitVersions) masterResult.getResponse()).getVersions());
        }
        System.out.println();
        for (String containerName : results.keySet()) {
            printVersions("=== Summary for local Git repository (container: " + containerName + ") ===", ((GitVersions) results.get(containerName).getResponse()).getVersions());
            System.out.println();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        waiter.interrupt();
        System.out.println("Interrupted waiting for git-summary response");
    }
}
Also used : GitVersion(io.fabric8.api.commands.GitVersion) JMXRequest(io.fabric8.api.commands.JMXRequest) HashMap(java.util.HashMap) JMXResult(io.fabric8.api.commands.JMXResult) CountDownLatch(java.util.concurrent.CountDownLatch) GitVersions(io.fabric8.api.commands.GitVersions) TreeSet(java.util.TreeSet) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

File (java.io.File)49 Test (org.junit.Test)32 Git (org.eclipse.jgit.api.Git)30 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)27 IOException (java.io.IOException)24 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)20 HashMap (java.util.HashMap)15 ObjectId (org.eclipse.jgit.lib.ObjectId)13 MalformedURLException (java.net.MalformedURLException)11 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 PatchException (io.fabric8.patch.management.PatchException)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 Bundle (org.osgi.framework.Bundle)8 Version (org.osgi.framework.Version)8 MavenResolver (io.fabric8.maven.MavenResolver)7 HashSet (java.util.HashSet)7 Repository (io.fabric8.agent.model.Repository)6 BundleContext (org.osgi.framework.BundleContext)6 Feature (io.fabric8.agent.model.Feature)5