Search in sources :

Example 11 with PatchResult

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

the class ServiceImplTest method testPatchWithVersionRanges.

@Test
public void testPatchWithVersionRanges() throws Exception {
    ComponentContext componentContext = createMock(ComponentContext.class);
    BundleContext bundleContext = createMock(BundleContext.class);
    Bundle sysBundle = createMock(Bundle.class);
    BundleContext sysBundleContext = createMock(BundleContext.class);
    Bundle bundle = createMock(Bundle.class);
    Bundle bundle2 = createMock(Bundle.class);
    FrameworkWiring wiring = createMock(FrameworkWiring.class);
    GitPatchRepository repository = createMock(GitPatchRepository.class);
    // 
    // Create a new service, download a patch
    // 
    expect(componentContext.getBundleContext()).andReturn(bundleContext);
    expect(bundleContext.getBundle(0)).andReturn(sysBundle).anyTimes();
    expect(sysBundle.getBundleContext()).andReturn(sysBundleContext).anyTimes();
    expect(sysBundleContext.getProperty(Service.NEW_PATCH_LOCATION)).andReturn(storage.toString()).anyTimes();
    expect(repository.getManagedPatch(anyString())).andReturn(null).anyTimes();
    expect(repository.findOrCreateMainGitRepository()).andReturn(null).anyTimes();
    expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
    expect(sysBundleContext.getProperty("karaf.home")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.base")).andReturn(karaf.getCanonicalPath()).anyTimes();
    expect(sysBundleContext.getProperty("karaf.name")).andReturn("root").anyTimes();
    expect(sysBundleContext.getProperty("karaf.instances")).andReturn(karaf.getCanonicalPath() + "/instances").anyTimes();
    expect(sysBundleContext.getProperty("karaf.data")).andReturn(karaf.getCanonicalPath() + "/data").anyTimes();
    replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, repository);
    PatchManagement pm = mockManagementService(bundleContext);
    ((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
    ServiceImpl service = new ServiceImpl();
    setField(service, "patchManagement", pm);
    service.activate(componentContext);
    Iterable<Patch> patches = service.download(patch140.toURI().toURL());
    assertNotNull(patches);
    Iterator<Patch> it = patches.iterator();
    assertTrue(it.hasNext());
    Patch patch = it.next();
    assertNotNull(patch);
    assertEquals("patch-1.4.0", patch.getPatchData().getId());
    assertNotNull(patch.getPatchData().getBundles());
    assertEquals(1, patch.getPatchData().getBundles().size());
    Iterator<String> itb = patch.getPatchData().getBundles().iterator();
    assertEquals("mvn:foo/my-bsn/1.4.0", itb.next());
    assertNull(patch.getResult());
    verify(componentContext, sysBundleContext, sysBundle, bundleContext, bundle);
    // 
    // Simulate the patch
    // 
    reset(componentContext, sysBundleContext, sysBundle, bundleContext, bundle);
    expect(sysBundleContext.getBundles()).andReturn(new Bundle[] { bundle });
    expect(sysBundleContext.getServiceReference("io.fabric8.api.FabricService")).andReturn(null);
    expect(bundle.getSymbolicName()).andReturn("my-bsn").anyTimes();
    expect(bundle.getVersion()).andReturn(new Version("1.3.1")).anyTimes();
    expect(bundle.getLocation()).andReturn("location").anyTimes();
    expect(bundle.getBundleId()).andReturn(123L);
    BundleStartLevel bsl = createMock(BundleStartLevel.class);
    expect(bsl.getStartLevel()).andReturn(30).anyTimes();
    expect(bundle.adapt(BundleStartLevel.class)).andReturn(bsl).anyTimes();
    expect(bundle.getState()).andReturn(1);
    expect(sysBundleContext.getProperty("karaf.default.repository")).andReturn("system").anyTimes();
    replay(componentContext, sysBundleContext, sysBundle, bundleContext, bundle, bsl);
    PatchResult result = service.install(patch, true);
    assertNotNull(result);
    assertNull(patch.getResult());
    assertEquals(1, result.getBundleUpdates().size());
    assertTrue(result.isSimulation());
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) ComponentContext(org.osgi.service.component.ComponentContext) Bundle(org.osgi.framework.Bundle) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) GitPatchRepository(io.fabric8.patch.management.impl.GitPatchRepository) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) Version(org.osgi.framework.Version) PatchManagement(io.fabric8.patch.management.PatchManagement) PatchResult(io.fabric8.patch.management.PatchResult) Patch(io.fabric8.patch.management.Patch) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 12 with PatchResult

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

the class FabricPatchServiceImpl method install.

@Override
public PatchResult install(final Patch patch, boolean simulation, final String versionId, boolean upload, final String username, final String password, final ProfileUpdateStrategy strategy) throws IOException {
    // we start from the same state as in standalone mode - after successful patch:add
    // we have other things to do in fabric env however:
    // 1. check prerequisites
    // 2. we don't care about current state of framework - it'll be managed by fabric-agent and we don't
    // necessary install a patch for this container we're in
    // 3. we don't do patchManagement.beginInstallation / patchManagement.commitInstallation here
    // this will be done later - after updated fabric-agent is started
    // 4. we don't have to analyze bundles/features/repositories updates - these will be handled simply by
    // updating profiles in specified version
    PatchKind kind = patch.getPatchData().isRollupPatch() ? PatchKind.ROLLUP : PatchKind.NON_ROLLUP;
    if (kind == PatchKind.NON_ROLLUP) {
        throw new UnsupportedOperationException("patch:fabric-install should be used for Rollup patches only");
    }
    String currentContainersVersionId = fabricService.getCurrentContainer().getVersionId();
    if (!simulation && versionId.equals(currentContainersVersionId)) {
        throw new UnsupportedOperationException("Can't install Rollup patch in current version. Please install" + " this patch in new version and then upgrade existing container(s)");
    }
    fabricService.adapt(ProfileService.class).getRequiredVersion(versionId);
    // just a list of new bundle locations - in fabric the updatable version depends on the moment we
    // apply the new version to existing containers.
    List<BundleUpdate> bundleUpdatesInThisPatch = bundleUpdatesInPatch(patch);
    Presentation.displayBundleUpdates(bundleUpdatesInThisPatch, true);
    PatchResult result = new PatchResult(patch.getPatchData(), simulation, System.currentTimeMillis(), bundleUpdatesInThisPatch, null);
    if (!simulation) {
        // update profile definitions stored in Git. We don't update ${karaf.home}/fabric, becuase it is used
        // only once - when importing profiles during fabric:create.
        // when fabric is already available, we have to update (Git) repository information
        GitOperation operation = new GitOperation() {

            @Override
            public Object call(Git git, GitContext context) throws Exception {
                // we can't pass git reference to patch-management
                // because patch-management private-packages git library
                // but we can leverage the write lock we have
                GitHelpers.checkoutBranch(git, versionId);
                // let's get back in history to the point before user changes (profile-edits), but not earlier
                // than last R patch
                String patchBranch = patchManagement.findLatestPatchRevision(git.getRepository().getDirectory(), versionId);
                // now install profiles from patch just like there were no user changes
                patchManagement.installProfiles(git.getRepository().getDirectory(), versionId, patch, strategy);
                // and finally we have to merge user and patch changes to profiles.
                patchManagement.mergeProfileChanges(patch, git.getRepository().getDirectory(), versionId, patchBranch);
                context.commitMessage("Installing rollup patch \"" + patch.getPatchData().getId() + "\"");
                return null;
            }
        };
        gitDataStore.gitOperation(new GitContext().requireCommit().setRequirePush(true), operation, null);
        if (upload) {
            PatchManagement.UploadCallback callback = new PatchManagement.UploadCallback() {

                @Override
                public void doWithUrlConnection(URLConnection connection) throws ProtocolException {
                    if (connection instanceof HttpURLConnection) {
                        ((HttpURLConnection) connection).setRequestMethod("PUT");
                    }
                    if (username != null && password != null) {
                        connection.setRequestProperty("Authorization", "Basic " + Base64Encoder.encode(username + ":" + password));
                    }
                }
            };
            patchManagement.uploadPatchArtifacts(patch.getPatchData(), fabricService.getMavenRepoUploadURI(), callback);
        }
    }
    return result;
}
Also used : PatchKind(io.fabric8.patch.management.PatchKind) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) ProfileService(io.fabric8.api.ProfileService) GitOperation(io.fabric8.git.internal.GitOperation) Git(org.eclipse.jgit.api.Git) HttpURLConnection(java.net.HttpURLConnection) GitContext(io.fabric8.api.GitContext) PatchManagement(io.fabric8.patch.management.PatchManagement) PatchResult(io.fabric8.patch.management.PatchResult) BundleUpdate(io.fabric8.patch.management.BundleUpdate)

Example 13 with PatchResult

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

the class FabricInstallAction method doExecute.

@Override
protected void doExecute(Service service) throws Exception {
    Patch patch = service.getPatch(patchId);
    if (patch == null) {
        throw new PatchException("Patch '" + patchId + "' not found");
    }
    ProfileUpdateStrategy strategy = ProfileUpdateStrategy.GIT;
    if ((mergeOverwrite && (mergeSelectNew || mergeKeepExisting)) || (mergeSelectNew && (mergeOverwrite || mergeKeepExisting)) || (mergeKeepExisting && (mergeOverwrite || mergeSelectNew))) {
        throw new UnsupportedOperationException("Please select single merge strategy");
    }
    if (mergeKeepExisting) {
        strategy = ProfileUpdateStrategy.PROPERTIES_PREFER_EXISTING;
    } else if (mergeSelectNew) {
        strategy = ProfileUpdateStrategy.PROPERTIES_PREFER_NEW;
    }
    PatchResult result = fabricPatchService.install(patch, simulation, versionId, upload, username, password, strategy);
}
Also used : PatchResult(io.fabric8.patch.management.PatchResult) PatchException(io.fabric8.patch.management.PatchException) ProfileUpdateStrategy(io.fabric8.patch.management.ProfileUpdateStrategy) Patch(io.fabric8.patch.management.Patch)

Example 14 with PatchResult

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

the class InstallAction method doExecute.

@Override
protected void doExecute(Service service) throws Exception {
    Patch patch = super.getPatch(patchId);
    if (patch.getPatchData().getMigratorBundle() != null) {
        System.out.println("This patch cannot be rolled back.  Are you sure you want to install?");
        while (true) {
            String response = ShellUtils.readLine(session, "[y/n]: ", false);
            if (response == null) {
                return;
            }
            response = response.trim().toLowerCase();
            if (response.equals("y") || response.equals("yes")) {
                break;
            }
            if (response.equals("n") || response.equals("no")) {
                return;
            }
        }
    }
    PatchResult result = service.install(patch, simulation, synchronous);
}
Also used : PatchResult(io.fabric8.patch.management.PatchResult) Patch(io.fabric8.patch.management.Patch)

Example 15 with PatchResult

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

the class PatchActionSupport method display.

protected void display(PatchResult result) {
    int l1 = "[name]".length(), l2 = "[old]".length(), l3 = "[new]".length();
    for (BundleUpdate update : result.getBundleUpdates()) {
        if (update.getSymbolicName() != null && stripSymbolicName(update.getSymbolicName()).length() > l1) {
            l1 = stripSymbolicName(update.getSymbolicName()).length();
        }
        if (update.getPreviousVersion().length() > l2) {
            l2 = update.getPreviousVersion().length();
        }
        if (update.getNewVersion().length() > l3) {
            l3 = update.getNewVersion().length();
        }
    }
    System.out.println(String.format("%-" + l1 + "s   %-" + l2 + "s   %-" + l3 + "s", "[name]", "[old]", "[new]"));
    java.util.List<BundleUpdate> updates = new ArrayList<>(result.getBundleUpdates());
    Collections.sort(updates, new Comparator<BundleUpdate>() {

        @Override
        public int compare(BundleUpdate o1, BundleUpdate o2) {
            return o1.getSymbolicName().compareTo(o2.getSymbolicName());
        }
    });
    for (BundleUpdate update : updates) {
        System.out.println(String.format("%-" + l1 + "s | %-" + l2 + "s | %-" + l3 + "s", update.getSymbolicName() == null ? "" : stripSymbolicName(update.getSymbolicName()), update.getPreviousVersion(), update.getNewVersion()));
    }
}
Also used : java.util(java.util) BundleUpdate(io.fabric8.patch.management.BundleUpdate)

Aggregations

PatchResult (io.fabric8.patch.management.PatchResult)13 Patch (io.fabric8.patch.management.Patch)9 BundleUpdate (io.fabric8.patch.management.BundleUpdate)8 PatchException (io.fabric8.patch.management.PatchException)7 File (java.io.File)7 Bundle (org.osgi.framework.Bundle)6 IOException (java.io.IOException)5 Test (org.junit.Test)5 PatchData (io.fabric8.patch.management.PatchData)4 HashMap (java.util.HashMap)4 PatchManagement (io.fabric8.patch.management.PatchManagement)3 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)3 FileInputStream (java.io.FileInputStream)3 URISyntaxException (java.net.URISyntaxException)3 BundleException (org.osgi.framework.BundleException)3 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)3 FeatureUpdate (io.fabric8.patch.management.FeatureUpdate)2 PatchKind (io.fabric8.patch.management.PatchKind)2 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)2 FileFilter (java.io.FileFilter)2