Search in sources :

Example 16 with PatchException

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

the class GitPatchManagementServiceImpl method trackBaselineRepository.

/**
 * Adds baseline distribution to the repository. It serves a different purpose in fabric and standalone scenarios.
 * In fabric mode, we put another baseline tag for root container when it is upgraded to specific version. The
 * versioning of root container is performed on another branch.
 * In standalone mode mode, the same branch is used to track baselines and for versioning of the container itself.
 * In standalone mode, patches are added to separate branch each.
 * In fabric mode, patches are added to baseline branches and each container has it's own versioning branch
 * (private, not pushed to main repository).
 * @param git non-bare repository to perform the operation with correct branch checked out already
 */
private RevCommit trackBaselineRepository(Git git) throws IOException, GitAPIException {
    // initialize repo with baseline version and push to reference repo
    String currentFuseVersion = determineVersion(karafHome, "fuse");
    String currentFabricVersion = determineVersion(karafHome, "fabric");
    // check what product are we in
    File systemRepo = getSystemRepository(karafHome, systemContext);
    File baselineDistribution = null;
    String baselineLocation = Utils.getBaselineLocationForProduct(karafHome, systemContext, currentFuseVersion);
    if (baselineLocation != null) {
        baselineDistribution = new File(patchesDir, baselineLocation);
    } else {
        // do some guessing - first JBoss Fuse, then JBoss A-MQ
        String[] locations = new String[] { systemRepo.getCanonicalPath() + "/org/jboss/fuse/jboss-fuse-karaf/%1$s/jboss-fuse-karaf-%1$s-baseline.zip", patchesDir.getCanonicalPath() + "/jboss-fuse-karaf-%1$s-baseline.zip", systemRepo.getCanonicalPath() + "/org/jboss/amq/jboss-a-mq/%s/jboss-a-mq-%1$s-baseline.zip", patchesDir.getCanonicalPath() + "/jboss-a-mq-%1$s-baseline.zip" };
        for (String location : locations) {
            location = String.format(location, currentFuseVersion);
            if (new File(location).isFile()) {
                baselineDistribution = new File(location);
                Activator.log(LogService.LOG_INFO, "Found baseline distribution: " + baselineDistribution.getCanonicalPath());
                break;
            }
        }
    }
    if (baselineDistribution != null) {
        return trackBaselineRepository(git, baselineDistribution, currentFuseVersion);
    } else {
        String message = "Can't find baseline distribution in patches dir or inside system repository.";
        Activator.log2(LogService.LOG_WARNING, message);
        throw new PatchException(message);
    }
}
Also used : PatchException(io.fabric8.patch.management.PatchException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 17 with PatchException

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

the class GitPatchManagementServiceImpl method install.

@Override
public void install(String transaction, Patch patch, List<BundleUpdate> bundleUpdatesInThisPatch) {
    transactionIsValid(transaction, patch);
    Git fork = pendingTransactions.get(transaction);
    try {
        switch(pendingTransactionsTypes.get(transaction)) {
            case ROLLUP:
                {
                    Activator.log2(LogService.LOG_INFO, String.format("Installing rollup patch \"%s\"", patch.getPatchData().getId()));
                    // we can install only one rollup patch within single transaction
                    // and it is equal to cherry-picking all user changes on top of transaction branch
                    // after cherry-picking the commit from the rollup patch branch
                    // rollup patches do their own update to startup.properties
                    // we're operating on patch branch, HEAD of the patch branch points to the baseline
                    ObjectId since = fork.getRepository().resolve("HEAD^{commit}");
                    // we'll pick all user changes between baseline and main patch branch without P installations
                    ObjectId to = fork.getRepository().resolve(gitPatchRepository.getMainBranchName() + "^{commit}");
                    Iterable<RevCommit> mainChanges = fork.log().addRange(since, to).call();
                    List<RevCommit> userChanges = new LinkedList<>();
                    // gather lines of HF patches - patches that have *only* bundle updates
                    // if any of HF patches provide newer version of artifact than currently installed R patch,
                    // we will leave the relevant line in etc/overrides.properties
                    List<String> hfChanges = new LinkedList<>();
                    for (RevCommit rc : mainChanges) {
                        if (isUserChangeCommit(rc)) {
                            userChanges.add(rc);
                        } else {
                            String hfPatchId = isHfChangeCommit(rc);
                            if (hfPatchId != null) {
                                hfChanges.addAll(gatherOverrides(hfPatchId, patch));
                            }
                        }
                    }
                    String patchRef = patch.getManagedPatch().getCommitId();
                    if (env == EnvType.STANDALONE_CHILD) {
                        // we're in a slightly different situation:
                        // - patch was patch:added in root container
                        // - its main commit should be used when patching full Fuse/AMQ container
                        // - it created "side" commits (with tags) for this case of patching admin:create based containers
                        // - those tags are stored in special patch-info.txt file within patch' commit
                        String patchInfo = gitPatchRepository.getFileContent(fork, patchRef, "patch-info.txt");
                        if (patchInfo != null) {
                            BufferedReader reader = new BufferedReader(new StringReader(patchInfo));
                            String line = null;
                            while ((line = reader.readLine()) != null) {
                                if (line.startsWith("#")) {
                                    continue;
                                }
                                Pattern p = Pattern.compile(env.getBaselineTagFormat().replace("%s", "(.*)"));
                                if (p.matcher(line).matches()) {
                                    // this means we have another commit/tag that we should chery-pick as a patch
                                    // for this standalone child container
                                    patchRef = line.trim();
                                }
                            }
                        } else {
                            // hmm, we actually can't patch standalone child container then...
                            Activator.log2(LogService.LOG_WARNING, String.format("Can't install rollup patch \"%s\" in admin container - no information about admin container patch", patch.getPatchData().getId()));
                            return;
                        }
                    }
                    if (env == EnvType.STANDALONE) {
                        // pick the rollup patch
                        fork.cherryPick().include(fork.getRepository().resolve(patchRef)).setNoCommit(true).call();
                        gitPatchRepository.prepareCommit(fork, String.format(MARKER_R_PATCH_INSTALLATION_PATTERN, patch.getPatchData().getId())).call();
                    } else if (env == EnvType.STANDALONE_CHILD) {
                        // rebase on top of rollup patch
                        fork.reset().setMode(ResetCommand.ResetType.HARD).setRef("refs/tags/" + patchRef + "^{commit}").call();
                    }
                    // next commit - reset overrides.properties - this is 2nd step of installing rollup patch
                    // we are doing it even if the commit is going to be empty - this is the same step as after
                    // creating initial baseline
                    resetOverrides(fork.getRepository().getWorkTree(), hfChanges);
                    fork.add().addFilepattern("etc/overrides.properties").call();
                    RevCommit c = gitPatchRepository.prepareCommit(fork, String.format(MARKER_R_PATCH_RESET_OVERRIDES_PATTERN, patch.getPatchData().getId())).call();
                    if (env == EnvType.STANDALONE) {
                        // tag the new rollup patch as new baseline
                        String newFuseVersion = determineVersion(fork.getRepository().getWorkTree(), "fuse");
                        fork.tag().setName(String.format(EnvType.STANDALONE.getBaselineTagFormat(), newFuseVersion)).setObjectId(c).call();
                    }
                    // reapply those user changes that are not conflicting
                    // for each conflicting cherry-pick we do a backup of user files, to be able to restore them
                    // when rollup patch is rolled back
                    ListIterator<RevCommit> it = userChanges.listIterator(userChanges.size());
                    int prefixSize = Integer.toString(userChanges.size()).length();
                    int count = 1;
                    while (it.hasPrevious()) {
                        RevCommit userChange = it.previous();
                        String prefix = String.format("%0" + prefixSize + "d-%s", count++, userChange.getName());
                        CherryPickResult result = fork.cherryPick().include(userChange).setNoCommit(true).call();
                        // ENTESB-5492: remove etc/overrides.properties if there is such file left from old patch
                        // mechanism
                        File overrides = new File(fork.getRepository().getWorkTree(), "etc/overrides.properties");
                        if (overrides.isFile()) {
                            // version of some bundles, overrides.properties should be kept
                            if (!(hfChanges.size() > 0 && overrides.length() > 0)) {
                                overrides.delete();
                                fork.rm().addFilepattern("etc/overrides.properties").call();
                            }
                        }
                        // if there's conflict here, prefer patch version (which is "ours" (first) in this case)
                        handleCherryPickConflict(patch.getPatchData().getPatchDirectory(), fork, result, userChange, false, PatchKind.ROLLUP, prefix, true, false);
                        // always commit even empty changes - to be able to restore user changes when rolling back
                        // rollup patch.
                        // commit has the original commit id appended to the message.
                        // when we rebase on OLDER baseline (rollback) we restore backed up files based on this
                        // commit id (from patches/patch-id.backup/number-commit directory)
                        String newMessage = userChange.getFullMessage() + "\n\n";
                        newMessage += prefix;
                        gitPatchRepository.prepareCommit(fork, newMessage).call();
                        // we may have unadded changes - when file mode is changed
                        fork.reset().setMode(ResetCommand.ResetType.HARD).call();
                    }
                    // finally - let's get rid of all the tags related to non-rollup patches installed between
                    // previous baseline and previous HEAD, because installing rollup patch makes all previous P
                    // patches obsolete
                    RevWalk walk = new RevWalk(fork.getRepository());
                    RevCommit c1 = walk.parseCommit(since);
                    RevCommit c2 = walk.parseCommit(to);
                    Map<String, RevTag> tags = gitPatchRepository.findTagsBetween(fork, c1, c2);
                    for (Map.Entry<String, RevTag> entry : tags.entrySet()) {
                        if (entry.getKey().startsWith("patch-")) {
                            fork.tagDelete().setTags(entry.getKey()).call();
                            fork.push().setRefSpecs(new RefSpec().setSource(null).setDestination("refs/tags/" + entry.getKey())).call();
                        }
                    }
                    break;
                }
            case NON_ROLLUP:
                {
                    Activator.log2(LogService.LOG_INFO, String.format("Installing non-rollup patch \"%s\"", patch.getPatchData().getId()));
                    // simply cherry-pick patch commit to transaction branch
                    // non-rollup patches require manual change to artifact references in all files
                    // pick the non-rollup patch
                    RevCommit commit = new RevWalk(fork.getRepository()).parseCommit(fork.getRepository().resolve(patch.getManagedPatch().getCommitId()));
                    CherryPickResult result = fork.cherryPick().include(commit).setNoCommit(true).call();
                    handleCherryPickConflict(patch.getPatchData().getPatchDirectory(), fork, result, commit, true, PatchKind.NON_ROLLUP, null, true, false);
                    // there are several files in ${karaf.home} that need to be changed together with patch
                    // commit, to make them reference updated bundles (paths, locations, ...)
                    updateFileReferences(fork, patch.getPatchData(), bundleUpdatesInThisPatch);
                    updateOverrides(fork.getRepository().getWorkTree(), patch.getPatchData());
                    fork.add().addFilepattern(".").call();
                    // always commit non-rollup patch
                    RevCommit c = gitPatchRepository.prepareCommit(fork, String.format(MARKER_P_PATCH_INSTALLATION_PATTERN, patch.getPatchData().getId())).call();
                    // we may have unadded changes - when file mode is changed
                    fork.reset().setMode(ResetCommand.ResetType.HARD).call();
                    // tag the installed patch (to easily rollback and to prevent another installation)
                    String tagName = String.format("patch-%s", patch.getPatchData().getId().replace(' ', '-'));
                    if (env == EnvType.STANDALONE_CHILD) {
                        tagName += "-" + gitPatchRepository.getStandaloneChildkarafName();
                    }
                    fork.tag().setName(tagName).setObjectId(c).call();
                    break;
                }
        }
    } catch (IOException | GitAPIException e) {
        throw new PatchException(e.getMessage(), e);
    }
}
Also used : Pattern(java.util.regex.Pattern) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) ListIterator(java.util.ListIterator) RevWalk(org.eclipse.jgit.revwalk.RevWalk) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) DiffEntry(org.eclipse.jgit.diff.DiffEntry) Git(org.eclipse.jgit.api.Git) CherryPickResult(org.eclipse.jgit.api.CherryPickResult) RefSpec(org.eclipse.jgit.transport.RefSpec) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) PatchException(io.fabric8.patch.management.PatchException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 18 with PatchException

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

the class GitPatchManagementServiceImpl method loadPatchData.

/**
 * Reads content of patch descriptor into non-(yet)-managed patch data structure
 * @param patchDescriptor
 * @return
 */
private PatchData loadPatchData(File patchDescriptor) throws IOException {
    Properties properties = new Properties();
    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(patchDescriptor);
        properties.load(inputStream);
        boolean ok = properties.containsKey("id") && properties.containsKey("bundle.count");
        if (!ok) {
            throw new PatchException("Patch descriptor is not valid");
        }
        return PatchData.load(properties);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : PatchException(io.fabric8.patch.management.PatchException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 19 with PatchException

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

the class GitPatchManagementServiceImpl method findLatestPatchRevision.

@Override
public String findLatestPatchRevision(File gitRepository, String versionId) {
    Git git = null;
    try {
        git = Git.open(gitRepository);
        Iterable<RevCommit> log = git.log().add(git.getRepository().resolve(versionId)).call();
        List<RevCommit> oldestToNewest = new LinkedList<>();
        RevCommit patchBaseRevision = null;
        for (RevCommit rc : log) {
            // in case we don't find previous R patch, we'll have to find it the hard way
            oldestToNewest.add(0, rc);
            if (rc.getShortMessage().startsWith("Installing rollup patch ")) {
                if (rc.getParents() != null && rc.getParents().length == 2) {
                    // it's 2nd parent with "Installing profiles from patch ..." message
                    if (rc.getParents()[0].getShortMessage().startsWith("Installing profiles from patch ")) {
                        patchBaseRevision = rc.getParents()[0];
                        break;
                    } else if (rc.getParents()[1].getShortMessage().startsWith("Installing profiles from patch ")) {
                        patchBaseRevision = rc.getParents()[1];
                        break;
                    }
                }
                // if "Installing rollup patch ..." commit doesn't have 2 parents, it means it was created by
                // some old patch mechanism from early 6.2.1 which just copied profiles over and didn't use
                // merging
                patchBaseRevision = rc;
                break;
            }
        }
        if (patchBaseRevision == null) {
            // from this change
            for (RevCommit rc : oldestToNewest) {
                if (rc.getShortMessage().startsWith("Update configurations for profile: ")) {
                    patchBaseRevision = rc;
                    break;
                }
            }
        }
        if (patchBaseRevision == null) {
            // we didn't find good place to start... we'll start from HEAD then
            return versionId;
        }
        String patchBranchName = String.format("__%s-%d", versionId, new Date().getTime());
        Ref branch = git.checkout().setCreateBranch(true).setName(patchBranchName).setStartPoint(patchBaseRevision).call();
        return patchBranchName;
    } catch (Exception e) {
        throw new PatchException(e.getMessage(), e);
    } finally {
        if (git != null) {
            gitPatchRepository.closeRepository(git, false);
        }
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) PatchException(io.fabric8.patch.management.PatchException) LinkedList(java.util.LinkedList) Date(java.util.Date) 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 20 with PatchException

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

the class GitPatchManagementServiceImpl method uploadPatchArtifacts.

@Override
public void uploadPatchArtifacts(PatchData patchData, URI uploadAddress, UploadCallback callback) throws PatchException {
    try {
        Activator.log2(LogService.LOG_INFO, "Uploading artifacts to " + uploadAddress);
        List<File> artifacts = new LinkedList<>();
        for (String bundle : patchData.getBundles()) {
            String newUrl = Utils.mvnurlToPath(bundle);
            if (newUrl != null) {
                File repoLocation = new File(Utils.getSystemRepository(karafHome, bundleContext), newUrl);
                if (repoLocation.isFile()) {
                    artifacts.add(repoLocation);
                }
            }
        }
        for (String featureRepository : patchData.getFeatureFiles()) {
            String newUrl = Utils.mvnurlToPath(featureRepository);
            if (newUrl != null) {
                File repoLocation = new File(Utils.getSystemRepository(karafHome, bundleContext), newUrl);
                if (repoLocation.isFile()) {
                    artifacts.add(repoLocation);
                }
            }
        }
        for (String artifact : patchData.getOtherArtifacts()) {
            String newUrl = Utils.mvnurlToPath(artifact);
            if (newUrl != null) {
                File repoLocation = new File(Utils.getSystemRepository(karafHome, bundleContext), newUrl);
                if (repoLocation.isFile()) {
                    artifacts.add(repoLocation);
                }
            }
        }
        int delta = artifacts.size() / 10;
        int count = 0;
        for (File f : artifacts) {
            if (++count % delta == 0) {
                Activator.log2(LogService.LOG_DEBUG, String.format("Uploaded %d/%d", count, artifacts.size()));
            }
            String relativeName = Utils.relative(Utils.getSystemRepository(karafHome, bundleContext), f.getCanonicalFile());
            relativeName = relativeName.replace('\\', '/');
            URL uploadUrl = uploadAddress.resolve(relativeName).toURL();
            URLConnection con = uploadUrl.openConnection();
            callback.doWithUrlConnection(con);
            con.setDoInput(true);
            con.setDoOutput(true);
            con.connect();
            OutputStream os = con.getOutputStream();
            InputStream is = new FileInputStream(f);
            try {
                IOUtils.copy(is, os);
                if (con instanceof HttpURLConnection) {
                    int code = ((HttpURLConnection) con).getResponseCode();
                    if (code < 200 || code >= 300) {
                        throw new IOException("Error uploading patched artifacts: " + ((HttpURLConnection) con).getResponseMessage());
                    }
                }
            } finally {
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(os);
            }
        }
        Activator.log2(LogService.LOG_DEBUG, String.format("Uploaded %d/%d", count, artifacts.size()));
    } catch (Exception e) {
        throw new PatchException(e.getMessage(), e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) LinkedList(java.util.LinkedList) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) 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) HttpURLConnection(java.net.HttpURLConnection) PatchException(io.fabric8.patch.management.PatchException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Aggregations

PatchException (io.fabric8.patch.management.PatchException)30 IOException (java.io.IOException)19 File (java.io.File)15 Patch (io.fabric8.patch.management.Patch)14 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)11 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)10 Git (org.eclipse.jgit.api.Git)10 LinkedList (java.util.LinkedList)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)6 URISyntaxException (java.net.URISyntaxException)6 HashMap (java.util.HashMap)6 RevCommit (org.eclipse.jgit.revwalk.RevCommit)6 PatchResult (io.fabric8.patch.management.PatchResult)5 FileInputStream (java.io.FileInputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 BundleException (org.osgi.framework.BundleException)5 ManagedPatch (io.fabric8.patch.management.ManagedPatch)4 URL (java.net.URL)4