Search in sources :

Example 61 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method trackBaselinesForSSHContainers.

/**
 * SSH containers are created from io.fabric8:fabric8-karaf distros. These however may be official, foundation
 * or random (ZIPped from what currently was in FUSE_HOME of the container that created SSH container.
 * Track all new baselines for SSH containers. These are looked up inside
 * <code>io/fabric8/fabric8-karaf/**</code>
 * @param fork
 */
public void trackBaselinesForSSHContainers(Git fork) throws IOException, GitAPIException {
    // two separate branches for two kinds of baselines for SSH containers
    if (fork.getRepository().getRef("refs/heads/" + gitPatchRepository.getFuseSSHContainerPatchBranchName()) == null) {
        String startPoint = "patch-management^{commit}";
        if (fork.getRepository().getRef("refs/remotes/origin/" + gitPatchRepository.getFuseSSHContainerPatchBranchName()) != null) {
            startPoint = "refs/remotes/origin/" + gitPatchRepository.getFuseSSHContainerPatchBranchName();
        }
        gitPatchRepository.checkout(fork).setName(gitPatchRepository.getFuseSSHContainerPatchBranchName()).setStartPoint(startPoint).setCreateBranch(true).setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call();
    }
    if (fork.getRepository().getRef("refs/heads/" + gitPatchRepository.getFabric8SSHContainerPatchBranchName()) == null) {
        String startPoint = "patch-management^{commit}";
        if (fork.getRepository().getRef("refs/remotes/origin/" + gitPatchRepository.getFabric8SSHContainerPatchBranchName()) != null) {
            startPoint = "refs/remotes/origin/" + gitPatchRepository.getFabric8SSHContainerPatchBranchName();
        }
        gitPatchRepository.checkout(fork).setName(gitPatchRepository.getFabric8SSHContainerPatchBranchName()).setStartPoint(startPoint).setCreateBranch(true).setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call();
    }
    File systemRepo = getSystemRepository(karafHome, systemContext);
    File[] versionDirs = new File(systemRepo, "io/fabric8/fabric8-karaf").listFiles();
    Set<Version> versions = new TreeSet<>();
    if (versionDirs != null) {
        for (File version : versionDirs) {
            if (version.isDirectory()) {
                versions.add(Utils.getOsgiVersion(version.getName()));
            }
        }
    }
    for (Version v : versions) {
        String fabric8Version = v.toString();
        // each version may have two (maybe more?) baseline distros.
        // we may have "official" fabric8-karaf and the one created from FUSE_HOME (zipped on-fly).
        String[] artifactPatterns = new String[] { "fabric8-karaf-%1$s.zip", "fabric8-karaf-%1$s-custom.zip" };
        for (String artifactPattern : artifactPatterns) {
            File baselineDistribution = null;
            String location = String.format(systemRepo.getCanonicalPath() + "/io/fabric8/fabric8-karaf/%1$s/" + artifactPattern, fabric8Version);
            if (new File(location).isFile()) {
                baselineDistribution = new File(location);
                Activator.log(LogService.LOG_INFO, "Found SSH baseline distribution: " + baselineDistribution.getCanonicalPath());
            }
            if (baselineDistribution != null) {
                try {
                    // we don't know yet which branch we have to checkout, this method will do 2 pass unzipping
                    // and leave the fork checked out to correct branch - its name will be returned
                    String rootDir = String.format("fabric8-karaf-%s", fabric8Version);
                    String branchName = unzipFabric8Distro(rootDir, baselineDistribution, fabric8Version, fork);
                    if (branchName == null) {
                        continue;
                    }
                    // remove the deletes
                    for (String missing : fork.status().call().getMissing()) {
                        fork.rm().addFilepattern(missing).call();
                    }
                    // and we'll tag the child baseline
                    String tagName = branchName.replace("patches-", "");
                    fork.add().addFilepattern(".").call();
                    RevCommit commit = gitPatchRepository.prepareCommit(fork, String.format(MARKER_BASELINE_SSH_COMMIT_PATTERN, tagName, fabric8Version)).call();
                    fork.tag().setName(String.format("baseline-%s-%s", tagName, fabric8Version)).setObjectId(commit).call();
                } catch (Exception e) {
                    Activator.log(LogService.LOG_ERROR, null, e.getMessage(), e, true);
                }
            }
        }
    }
    gitPatchRepository.push(fork, gitPatchRepository.getFuseSSHContainerPatchBranchName());
    gitPatchRepository.push(fork, gitPatchRepository.getFabric8SSHContainerPatchBranchName());
}
Also used : Version(org.osgi.framework.Version) Artifact.isSameButVersion(io.fabric8.patch.management.Artifact.isSameButVersion) TreeSet(java.util.TreeSet) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) PatchException(io.fabric8.patch.management.PatchException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 62 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.

the class ServiceImpl method load.

/**
 * Loads available patches without caching
 * @param details whether to load {@link io.fabric8.patch.management.ManagedPatch} details too
 * @return
 */
private Map<String, Patch> load(boolean details) {
    List<Patch> patchesList = patchManagement.listPatches(details);
    Map<String, Patch> patches = new HashMap<String, Patch>();
    for (Patch patch : patchesList) {
        patches.put(patch.getPatchData().getId(), patch);
    }
    return patches;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Patch(io.fabric8.patch.management.Patch)

Example 63 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.

the class ServiceImpl method checkStandaloneChild.

/**
 * Check if this is installation in @{link {@link io.fabric8.patch.management.EnvType#STANDALONE_CHILD}}
 * - in this case the patch has to be installed in root first
 * @param patches
 */
private void checkStandaloneChild(Collection<Patch> patches) {
    if (patchManagement.isStandaloneChild()) {
        for (Patch patch : patches) {
            if (patch.getResult() == null) {
                throw new PatchException(String.format("Patch '%s' should be installed in parent container first", patch.getPatchData().getId()));
            } else {
                List<String> bases = patch.getResult().getKarafBases();
                boolean isInstalledInRoot = false;
                for (String base : bases) {
                    String[] coords = base.split("\\s*\\|\\s*");
                    if (coords.length == 2 && coords[1].trim().equals(System.getProperty("karaf.home"))) {
                        isInstalledInRoot = true;
                    }
                }
                if (!isInstalledInRoot) {
                    throw new PatchException(String.format("Patch '%s' should be installed in parent container first", patch.getPatchData().getId()));
                }
            }
        }
    }
}
Also used : PatchException(io.fabric8.patch.management.PatchException) Patch(io.fabric8.patch.management.Patch)

Example 64 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model 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 65 with io.fabric8.kubernetes.api.model

use of io.fabric8.kubernetes.api.model in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceIT method addPatch4.

/**
 * Patch 4 is rollup patch (doesn't contain descriptor, contains default.profile/io.fabric8.version.properties)
 * Adding it is not different that adding non-rollup patch. Installation is different
 * @throws IOException
 * @throws GitAPIException
 */
@Test
public void addPatch4() throws IOException, GitAPIException {
    initializationPerformedBaselineDistributionFoundInSystem();
    // prepare some ZIP patches
    preparePatchZip("src/test/resources/content/patch4", "target/karaf/patches/source/patch-4.zip", false);
    PatchManagement service = (PatchManagement) pm;
    PatchData patchData = service.fetchPatches(new File("target/karaf/patches/source/patch-4.zip").toURI().toURL()).get(0);
    assertThat(patchData.getId(), equalTo("patch-4"));
    Patch patch = service.trackPatch(patchData);
    GitPatchRepository repository = ((GitPatchManagementServiceImpl) pm).getGitPatchRepository();
    Git fork = repository.cloneRepository(repository.findOrCreateMainGitRepository(), true);
    // we should see remote branch for the patch, but without checking it out, it won't be available in the clone's local branches
    List<Ref> branches = fork.branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call();
    Ref patchBranch = null;
    for (Ref remoteBranch : branches) {
        if (String.format("refs/remotes/origin/patch-%s", patchData.getId()).equals(remoteBranch.getName())) {
            patchBranch = remoteBranch;
            break;
        }
    }
    assertNotNull("Should find remote branch for the added patch", patchBranch);
    assertThat(patch.getManagedPatch().getCommitId(), equalTo(patchBranch.getObjectId().getName()));
    RevCommit patchCommit = new RevWalk(fork.getRepository()).parseCommit(patchBranch.getObjectId());
    // patch commit should be child of baseline commit
    RevCommit baselineCommit = new RevWalk(fork.getRepository()).parseCommit(patchCommit.getParent(0));
    // this baseline commit should be tagged "baseline-VERSION"
    Ref tag = fork.tagList().call().get(0);
    assertThat(tag.getName(), equalTo("refs/tags/baseline-6.2.0"));
    RevCommit baselineCommitFromTag = new RevWalk(fork.getRepository()).parseCommit(tag.getTarget().getObjectId());
    assertThat(baselineCommit.getId(), equalTo(baselineCommitFromTag.getId()));
    List<DiffEntry> patchDiff = repository.diff(fork, baselineCommit, patchCommit);
    int changes = SystemUtils.IS_OS_WINDOWS ? 8 : 9;
    assertThat("patch-4 should lead to " + changes + " changes", patchDiff.size(), equalTo(changes));
    for (Iterator<DiffEntry> iterator = patchDiff.iterator(); iterator.hasNext(); ) {
        DiffEntry de = iterator.next();
        if ("bin/start".equals(de.getNewPath()) && de.getChangeType() == DiffEntry.ChangeType.MODIFY) {
            iterator.remove();
        }
        if ("bin/stop".equals(de.getNewPath()) && de.getChangeType() == DiffEntry.ChangeType.MODIFY) {
            iterator.remove();
        }
        if (!SystemUtils.IS_OS_WINDOWS && "bin/setenv".equals(de.getNewPath()) && de.getChangeType() == DiffEntry.ChangeType.MODIFY) {
            iterator.remove();
        }
        if ("etc/startup.properties".equals(de.getNewPath()) && de.getChangeType() == DiffEntry.ChangeType.MODIFY) {
            iterator.remove();
        }
        if ("etc/my.properties".equals(de.getNewPath()) && de.getChangeType() == DiffEntry.ChangeType.ADD) {
            iterator.remove();
        }
        if ("etc/system.properties".equals(de.getNewPath()) && de.getChangeType() == DiffEntry.ChangeType.MODIFY) {
            iterator.remove();
        }
        if ("fabric/import/fabric/profiles/default.profile/io.fabric8.agent.properties".equals(de.getNewPath()) && de.getChangeType() == DiffEntry.ChangeType.MODIFY) {
            iterator.remove();
        }
        if ("fabric/import/fabric/profiles/default.profile/io.fabric8.version.properties".equals(de.getNewPath()) && de.getChangeType() == DiffEntry.ChangeType.MODIFY) {
            iterator.remove();
        }
        if ("patch-info.txt".equals(de.getNewPath()) && de.getChangeType() == DiffEntry.ChangeType.ADD) {
            iterator.remove();
        }
    }
    assertThat("Unknown changes in patch-4", patchDiff.size(), equalTo(0));
    // let's see the patch applied to baseline-6.2.0
    fork.checkout().setName("patch-4").setStartPoint("origin/patch-patch-4").setCreateBranch(true).call();
    String startupProperties = FileUtils.readFileToString(new File(fork.getRepository().getWorkTree(), "etc/startup.properties"));
    assertTrue(startupProperties.contains("org/ops4j/pax/url/pax-url-gopher/2.4.0/pax-url-gopher-2.4.0.jar=5"));
    repository.closeRepository(fork, true);
}
Also used : GitPatchRepository(io.fabric8.patch.management.impl.GitPatchRepository) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Ref(org.eclipse.jgit.lib.Ref) GitPatchManagementServiceImpl(io.fabric8.patch.management.impl.GitPatchManagementServiceImpl) Git(org.eclipse.jgit.api.Git) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)29 FabricService (io.fabric8.api.FabricService)26 File (java.io.File)22 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)18 InputStream (java.io.InputStream)16 Logger (org.slf4j.Logger)16 ServiceLocator (io.fabric8.api.gravia.ServiceLocator)15 CommandSupport (io.fabric8.itests.support.CommandSupport)15 Deployment (org.jboss.arquillian.container.test.api.Deployment)15 StartLevelAware (org.jboss.arquillian.osgi.StartLevelAware)15 OSGiManifestBuilder (org.jboss.osgi.metadata.OSGiManifestBuilder)15 Asset (org.jboss.shrinkwrap.api.asset.Asset)15 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)15 ServiceTracker (org.osgi.util.tracker.ServiceTracker)15 IOException (java.io.IOException)14 Action (org.apache.felix.gogo.commands.Action)14 AbstractCommand (org.apache.felix.gogo.commands.basic.AbstractCommand)14 MavenResolver (io.fabric8.maven.MavenResolver)11 Container (io.fabric8.api.Container)10 BundleContext (org.osgi.framework.BundleContext)9