Search in sources :

Example 46 with Result

use of io.fabric8.insight.metrics.model.Result in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method checkPendingPatches.

@Override
public void checkPendingPatches() {
    File[] pendingPatches = patchesDir.listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.exists() && pathname.getName().endsWith(".pending");
        }
    });
    if (pendingPatches == null || pendingPatches.length == 0) {
        return;
    }
    final String dataCache = systemContext.getProperty("org.osgi.framework.storage");
    for (File pending : pendingPatches) {
        try {
            Pending what = Pending.valueOf(FileUtils.readFileToString(pending));
            final String prefix = what == Pending.ROLLUP_INSTALLATION ? "install" : "rollback";
            String name = pending.getName().replaceFirst("\\.pending$", "");
            if (isStandaloneChild()) {
                if (name.endsWith("." + System.getProperty("karaf.name") + ".patch")) {
                    name = name.replaceFirst("\\." + System.getProperty("karaf.name"), "");
                } else {
                    continue;
                }
            }
            File patchFile = new File(pending.getParentFile(), name);
            if (!patchFile.isFile()) {
                Activator.log(LogService.LOG_INFO, "Ignoring patch result file: " + patchFile.getName());
                continue;
            }
            PatchData patchData = PatchData.load(new FileInputStream(patchFile));
            Patch patch = loadPatch(new PatchDetailsRequest(patchData.getId()));
            String dataFilesName = patchData.getId() + ".datafiles";
            if (isStandaloneChild()) {
                dataFilesName = patchData.getId() + "." + System.getProperty("karaf.name") + ".datafiles";
            }
            final File dataFilesBackupDir = new File(pending.getParentFile(), dataFilesName);
            final Properties backupProperties = new Properties();
            FileInputStream inStream = new FileInputStream(new File(dataFilesBackupDir, "backup-" + prefix + ".properties"));
            backupProperties.load(inStream);
            IOUtils.closeQuietly(inStream);
            // maybe one of those bundles has data directory to restore?
            for (Bundle b : systemContext.getBundles()) {
                if (b.getSymbolicName() != null) {
                    String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
                    if (backupProperties.containsKey(key)) {
                        String backupDirName = backupProperties.getProperty(key);
                        File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
                        restoreDataDirectory(dataCache, b, backupDir);
                        // we no longer want to restore this dir
                        backupProperties.remove(key);
                    }
                }
            }
            // 2. We can however have more bundle data backups - we'll restore them after each bundle
            // is INSTALLED and we'll use listener for this
            BundleListener bundleListener = new SynchronousBundleListener() {

                @Override
                public void bundleChanged(BundleEvent event) {
                    Bundle b = event.getBundle();
                    if (event.getType() == BundleEvent.INSTALLED && b.getSymbolicName() != null) {
                        String key = String.format("%s$$%s", stripSymbolicName(b.getSymbolicName()), b.getVersion().toString());
                        if (backupProperties.containsKey(key)) {
                            String backupDirName = backupProperties.getProperty(key);
                            File backupDir = new File(dataFilesBackupDir, prefix + "/" + backupDirName + "/data");
                            restoreDataDirectory(dataCache, b, backupDir);
                        }
                    }
                }
            };
            systemContext.addBundleListener(bundleListener);
            pendingPatchesListeners.put(patchData.getId(), bundleListener);
        } catch (Exception e) {
            Activator.log(LogService.LOG_ERROR, null, e.getMessage(), e, true);
        }
    }
}
Also used : PatchData(io.fabric8.patch.management.PatchData) Bundle(org.osgi.framework.Bundle) Properties(java.util.Properties) PatchDetailsRequest(io.fabric8.patch.management.PatchDetailsRequest) FileInputStream(java.io.FileInputStream) PatchException(io.fabric8.patch.management.PatchException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BundleEvent(org.osgi.framework.BundleEvent) BundleListener(org.osgi.framework.BundleListener) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) FileFilter(java.io.FileFilter) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) ManagedPatch(io.fabric8.patch.management.ManagedPatch) Patch(io.fabric8.patch.management.Patch) Pending(io.fabric8.patch.management.Pending) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener)

Example 47 with Result

use of io.fabric8.insight.metrics.model.Result in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method loadPatch.

/**
 * Retrieves patch information from existing file
 * @param patchDescriptor existing file with patch descriptor (<code>*.patch</code> file)
 * @param details whether the returned {@link Patch} should contain {@link ManagedPatch} information
 * @return
 * @throws IOException
 */
private Patch loadPatch(File patchDescriptor, boolean details) throws IOException {
    Patch p = new Patch();
    if (!patchDescriptor.exists() || !patchDescriptor.isFile()) {
        return null;
    }
    PatchData data = PatchData.load(new FileInputStream(patchDescriptor));
    p.setPatchData(data);
    File patchDirectory = new File(patchesDir, FilenameUtils.getBaseName(patchDescriptor.getName()));
    if (patchDirectory.exists() && patchDirectory.isDirectory()) {
        // not every descriptor downloaded may be a ZIP file, not every patch has content
        data.setPatchDirectory(patchDirectory);
    }
    data.setPatchLocation(patchesDir);
    File resultFile = new File(patchesDir, FilenameUtils.getBaseName(patchDescriptor.getName()) + ".patch.result");
    if (resultFile.exists() && resultFile.isFile()) {
        PatchResult result = PatchResult.load(data, new FileInputStream(resultFile));
        p.setResult(result);
    }
    if (details) {
        ManagedPatch mp = gitPatchRepository.getManagedPatch(data.getId());
        p.setManagedPatch(mp);
    }
    return p;
}
Also used : ManagedPatch(io.fabric8.patch.management.ManagedPatch) PatchData(io.fabric8.patch.management.PatchData) PatchResult(io.fabric8.patch.management.PatchResult) ManagedPatch(io.fabric8.patch.management.ManagedPatch) Patch(io.fabric8.patch.management.Patch) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 48 with Result

use of io.fabric8.insight.metrics.model.Result in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method mergeProfileChanges.

@Override
public void mergeProfileChanges(Patch patch, File gitRepository, String versionBranch, String patchBranch) {
    Git git = null;
    try {
        git = Git.open(gitRepository);
        // merge version branch with patch branch - no fast forward, so we have nice named commit
        git.checkout().setCreateBranch(false).setName(versionBranch).call();
        MergeResult result = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(git.getRepository().resolve(patchBranch)).setCommit(false).call();
        boolean commit = true;
        if (result.getMergeStatus() == MergeResult.MergeStatus.CONFLICTING) {
            handleMergeConflict(git, result, versionBranch, patchBranch);
        } else if (result.getMergeStatus() != MergeResult.MergeStatus.MERGED_NOT_COMMITTED) {
            commit = false;
            Activator.log2(LogService.LOG_ERROR, "Can't merge version branch \"" + versionBranch + "\" with" + " patch branch \"" + patchBranch + "\". Resetting the branch.");
            git.reset().setMode(ResetCommand.ResetType.HARD).call();
        }
    // if (commit) {
    // git.commit()
    // .setMessage("Installing rollup patch \"" + patch.getPatchData().getId() + "\"")
    // .call();
    // }
    } catch (Exception e) {
        throw new PatchException(e.getMessage(), e);
    } finally {
        if (git != null) {
            try {
                git.branchDelete().setBranchNames(patchBranch).setForce(true).call();
            } catch (GitAPIException e) {
                Activator.log2(LogService.LOG_ERROR, e.getMessage());
            }
            gitPatchRepository.closeRepository(git, false);
        }
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) MergeResult(org.eclipse.jgit.api.MergeResult) PatchException(io.fabric8.patch.management.PatchException) PatchException(io.fabric8.patch.management.PatchException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 49 with Result

use of io.fabric8.insight.metrics.model.Result in project fabric8 by jboss-fuse.

the class SupportServiceImpl method collect.

@Override
public File collect() {
    String name = String.format("%s-%s", "SUPPORT", DATE_TIME_SUFFIX.format(new Date()));
    File result = null;
    ZipOutputStream file = null;
    try {
        result = File.createTempFile(name, ".zip");
        LOGGER.info("Collecting information for support in file {}", result.getAbsolutePath());
        file = new ZipOutputStream(new FileOutputStream(result));
        for (Collector collector : collectors) {
            for (Resource resource : collector.collect(resourceFactory)) {
                collectFromResource(file, resource);
            }
        }
    } catch (IOException e) {
        LOGGER.error("Exception occured while collecting support information - resulting support file may not be usable", e);
    } finally {
        IOHelpers.close(file);
    }
    return result;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) Collector(io.fabric8.support.api.Collector) Resource(io.fabric8.support.api.Resource) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date)

Example 50 with Result

use of io.fabric8.insight.metrics.model.Result in project fabric8 by jboss-fuse.

the class FileBackupTest method backupSomeDataFiles.

@Test
public void backupSomeDataFiles() throws IOException {
    PatchData patchData = new PatchData("my-patch");
    patchData.setPatchLocation(new File(karafHome, "patches"));
    PatchResult result = new PatchResult(patchData);
    // updates installed bundle, has data dir
    BundleUpdate b3 = new BundleUpdate("com.irrelevant.services", "1.2", "file:/dev/null", "1.1.0", "file:/dev/random");
    // updates installed bundle, has data dir, but special case
    BundleUpdate b4 = new BundleUpdate("org.apache.karaf.features.core", "1.3", "file:/dev/null", "1.2.1", "file:/dev/random");
    // reinstalled bundle, has data dir
    BundleUpdate b5 = new BundleUpdate("com.irrelevant.iot", null, null, "1.2.5", "file:/dev/random");
    // reinstalled bundle, no data dir
    BundleUpdate b6 = new BundleUpdate("com.irrelevant.space", null, null, "1.1.0", "file:/dev/random");
    // update, but not for installed bundle
    BundleUpdate b7 = new BundleUpdate("com.irrelevant.the.final.frontier", "1.5", "file:/dev/null", "1.1.3", "file:/dev/random");
    result.getBundleUpdates().add(b3);
    result.getBundleUpdates().add(b4);
    result.getBundleUpdates().add(b5);
    result.getBundleUpdates().add(b6);
    result.getBundleUpdates().add(b7);
    new FileBackupService(sys).backupDataFiles(result, Pending.ROLLUP_INSTALLATION);
    Properties props = new Properties();
    props.load(new FileInputStream(new File(karafHome, "patches/my-patch.datafiles/backup-install.properties")));
    assertThat(props.getProperty("com.irrelevant.services$$1.1.0"), equalTo("com.irrelevant.services$$1.1.0"));
    assertThat(props.getProperty("com.irrelevant.services$$1.2"), equalTo("com.irrelevant.services$$1.1.0"));
    assertThat(props.getProperty("com.irrelevant.iot$$1.2.5"), equalTo("com.irrelevant.iot$$1.2.5"));
    assertThat(props.stringPropertyNames().size(), equalTo(3));
    assertTrue(new File(karafHome, "patches/my-patch.datafiles/install/com.irrelevant.services$$1.1.0/data/x").isDirectory());
    assertTrue(new File(karafHome, "patches/my-patch.datafiles/install/com.irrelevant.iot$$1.2.5/data/z").isDirectory());
    assertFalse(new File(karafHome, "patches/my-patch.datafiles/install/com.irrelevant.the.final.frontier$$1.5").isDirectory());
}
Also used : PatchData(io.fabric8.patch.management.PatchData) PatchResult(io.fabric8.patch.management.PatchResult) Properties(java.util.Properties) File(java.io.File) BundleUpdate(io.fabric8.patch.management.BundleUpdate) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)104 Expectations (mockit.Expectations)49 HashMap (java.util.HashMap)32 Map (java.util.Map)24 ArrayList (java.util.ArrayList)23 File (java.io.File)21 GitContext (io.fabric8.api.GitContext)20 IOException (java.io.IOException)20 Exchange (org.apache.camel.Exchange)20 Processor (org.apache.camel.Processor)20 DefaultPullPushPolicy (io.fabric8.git.internal.DefaultPullPushPolicy)18 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)17 Probe (io.fabric8.kubernetes.api.model.Probe)16 PatchResult (io.fabric8.patch.management.PatchResult)13 Properties (java.util.Properties)13 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)11 Patch (io.fabric8.patch.management.Patch)11 PatchException (io.fabric8.patch.management.PatchException)11 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)10 TreeMap (java.util.TreeMap)10