Search in sources :

Example 21 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class PatchManagementIT method init.

@Before
public void init() throws IOException, GitAPIException {
    super.init(true, true);
    pm = new GitPatchManagementServiceImpl(bundleContext);
    ((GitPatchManagementServiceImpl) pm).start();
    // prepare some ZIP patches
    preparePatchZip("src/test/resources/content/patch1", "target/karaf/patches/source/patch-1.zip", false);
    preparePatchZip("src/test/resources/content/patch3", "target/karaf/patches/source/patch-3.zip", false);
}
Also used : GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) Before(org.junit.Before)

Example 22 with Patch

use of io.fabric8.patch.management.Patch 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)

Example 23 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class ServiceImpl method activate.

@Activate
void activate(ComponentContext componentContext) throws IOException {
    // Use system bundle' bundle context to avoid running into
    // "Invalid BundleContext" exceptions when updating bundles
    this.bundleContext = componentContext.getBundleContext().getBundle(0).getBundleContext();
    String dir = this.bundleContext.getProperty(NEW_PATCH_LOCATION);
    if (dir != null) {
        patchDir = new File(dir);
    } else {
        dir = this.bundleContext.getProperty(PATCH_LOCATION);
        if (dir != null) {
            patchDir = new File(dir);
        } else {
            if (patchManagement.isStandaloneChild()) {
                patchDir = new File(System.getProperty("karaf.home"), "patches");
            }
            if (patchDir == null) {
                // only now fallback to datafile of system bundle
                patchDir = this.bundleContext.getDataFile("patches");
            }
        }
    }
    if (!patchDir.isDirectory()) {
        patchDir.mkdirs();
        if (!patchDir.isDirectory()) {
            throw new PatchException("Unable to create patch folder");
        }
    }
    this.karafHome = new File(bundleContext.getProperty("karaf.home"));
    this.repository = new File(bundleContext.getProperty("karaf.default.repository"));
    helper = new OSGiPatchHelper(karafHome, bundleContext);
    load(true);
    resumePendingPatchTasks();
}
Also used : PatchException(io.fabric8.patch.management.PatchException) File(java.io.File) Activate(org.apache.felix.scr.annotations.Activate)

Example 24 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class ServiceImpl method installMigratorBundle.

/**
 * If patch contains migrator bundle, install it by dropping to <code>deploy</code> directory.
 * @param patch
 * @throws IOException
 */
private void installMigratorBundle(Patch patch) throws IOException {
    if (patch.getPatchData().getMigratorBundle() != null) {
        Artifact artifact = mvnurlToArtifact(patch.getPatchData().getMigratorBundle(), true);
        if (artifact != null) {
            // Copy it to the deploy dir
            File src = new File(repository, artifact.getPath());
            File target = new File(Utils.getDeployDir(karafHome), artifact.getArtifactId() + ".jar");
            FileUtils.copyFile(src, target);
        }
    }
}
Also used : File(java.io.File) Utils.mvnurlToArtifact(io.fabric8.patch.management.Utils.mvnurlToArtifact) Artifact(io.fabric8.patch.management.Artifact)

Example 25 with Patch

use of io.fabric8.patch.management.Patch in project fabric8 by jboss-fuse.

the class ServiceImpl method rollback.

@Override
public void rollback(final Patch patch, boolean simulate, boolean force) throws PatchException {
    final PatchResult result = !patchManagement.isStandaloneChild() ? patch.getResult() : patch.getResult().getChildPatches().get(System.getProperty("karaf.name"));
    if (result == null) {
        throw new PatchException("Patch " + patch.getPatchData().getId() + " is not installed");
    }
    if (patch.getPatchData().isRollupPatch()) {
        // we already have the "state" (feature repositories, features, bundles and their states, datafiles
        // and start-level info) stored in *.result file
        Presentation.displayFeatureUpdates(result.getFeatureUpdates(), false);
        Presentation.displayBundleUpdates(result.getBundleUpdates(), false);
        try {
            if (!simulate) {
                // let's backup data files before configadmin detects changes to etc/* files.
                backupService.backupDataFiles(result, Pending.ROLLUP_ROLLBACK);
                for (Bundle b : this.bundleContext.getBundles()) {
                    if (b.getSymbolicName() != null && Utils.stripSymbolicName(b.getSymbolicName()).equals("org.apache.felix.fileinstall")) {
                        b.stop(Bundle.STOP_TRANSIENT);
                        break;
                    }
                }
                patchManagement.rollback(patch.getPatchData());
                result.setPending(Pending.ROLLUP_ROLLBACK);
                if (patchManagement.isStandaloneChild()) {
                    result.getParent().store();
                } else {
                    result.store();
                }
                if (isJvmRestartNeeded(result)) {
                    boolean handlesFullRestart = Boolean.getBoolean("karaf.restart.jvm.supported");
                    if (handlesFullRestart) {
                        System.out.println("Rollup patch " + patch.getPatchData().getId() + " rolled back. Restarting Karaf..");
                        System.setProperty("karaf.restart.jvm", "true");
                    } else {
                        System.out.println("Rollup patch " + patch.getPatchData().getId() + " rolled back. Shutting down Karaf, please restart...");
                    }
                } else {
                    // We don't need a JVM restart, so lets just do a OSGi framework restart
                    System.setProperty("karaf.restart", "true");
                }
                File karafData = new File(bundleContext.getProperty("karaf.data"));
                File cleanCache = new File(karafData, "clean_cache");
                cleanCache.createNewFile();
                bundleContext.getBundle(0l).stop();
                // stop/shutdown occurs on another thread
                return;
            } else {
                System.out.println("Simulation only - no files and runtime data will be modified.");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.err.flush();
            throw new PatchException(e.getMessage(), e);
        }
    }
    // continue with NON_ROLLUP patch
    // current state of the framework
    Bundle[] allBundles = bundleContext.getBundles();
    // check if all the bundles that were updated in patch are available (installed)
    List<BundleUpdate> badUpdates = new ArrayList<BundleUpdate>();
    for (BundleUpdate update : result.getBundleUpdates()) {
        boolean found = false;
        Version v = Version.parseVersion(update.getNewVersion() == null ? update.getPreviousVersion() : update.getNewVersion());
        for (Bundle bundle : allBundles) {
            if (bundle.getSymbolicName() == null || update.getSymbolicName() == null) {
                continue;
            }
            if (stripSymbolicName(bundle.getSymbolicName()).equals(stripSymbolicName(update.getSymbolicName())) && bundle.getVersion().equals(v)) {
                found = true;
                break;
            }
        }
        if (!found) {
            badUpdates.add(update);
        }
    }
    if (!badUpdates.isEmpty() && !force) {
        StringBuilder sb = new StringBuilder();
        sb.append("Unable to rollback patch ").append(patch.getPatchData().getId()).append(" because of the following missing bundles:\n");
        for (BundleUpdate up : badUpdates) {
            String version = up.getNewVersion() == null ? up.getPreviousVersion() : up.getNewVersion();
            sb.append(" - ").append(up.getSymbolicName()).append("/").append(version).append("\n");
        }
        throw new PatchException(sb.toString());
    }
    if (!simulate) {
        // bundle -> old location of the bundle to downgrade from
        final Map<Bundle, String> toUpdate = new HashMap<Bundle, String>();
        for (BundleUpdate update : result.getBundleUpdates()) {
            Version v = Version.parseVersion(update.getNewVersion() == null ? update.getPreviousVersion() : update.getNewVersion());
            for (Bundle bundle : allBundles) {
                if (bundle.getSymbolicName() == null || update.getSymbolicName() == null) {
                    continue;
                }
                if (stripSymbolicName(bundle.getSymbolicName()).equals(stripSymbolicName(update.getSymbolicName())) && bundle.getVersion().equals(v)) {
                    toUpdate.put(bundle, update.getPreviousLocation());
                }
            }
        }
        final boolean isStandaloneChild = patchManagement.isStandaloneChild();
        patchManagement.rollback(patch.getPatchData());
        Executors.newSingleThreadExecutor().execute(new Runnable() {

            @Override
            public void run() {
                try {
                    applyChanges(toUpdate);
                } catch (Exception e) {
                    throw new PatchException("Unable to rollback patch " + patch.getPatchData().getId() + ": " + e.getMessage(), e);
                }
                patch.setResult(null);
                File file = new File(patchDir, result.getPatchData().getId() + ".patch.result");
                if (isStandaloneChild) {
                    file = new File(patchDir, result.getPatchData().getId() + "." + System.getProperty("karaf.name") + ".patch.result");
                }
                file.delete();
            }
        });
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) PatchException(io.fabric8.patch.management.PatchException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) Version(org.osgi.framework.Version) PatchResult(io.fabric8.patch.management.PatchResult) PatchException(io.fabric8.patch.management.PatchException) File(java.io.File) BundleUpdate(io.fabric8.patch.management.BundleUpdate)

Aggregations

File (java.io.File)54 Test (org.junit.Test)43 Git (org.eclipse.jgit.api.Git)35 PatchException (io.fabric8.patch.management.PatchException)34 Patch (io.fabric8.patch.management.Patch)30 IOException (java.io.IOException)28 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)27 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)26 HashMap (java.util.HashMap)18 RevCommit (org.eclipse.jgit.revwalk.RevCommit)18 LinkedList (java.util.LinkedList)17 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)17 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)17 ObjectId (org.eclipse.jgit.lib.ObjectId)17 Bundle (org.osgi.framework.Bundle)17 Version (org.osgi.framework.Version)15 PatchResult (io.fabric8.patch.management.PatchResult)13 BundleUpdate (io.fabric8.patch.management.BundleUpdate)11 PatchData (io.fabric8.patch.management.PatchData)11 ArrayList (java.util.ArrayList)11