Search in sources :

Example 56 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class GitHttpServerRegistrationHandler method registerServlet.

private void registerServlet(Path dataPath, String realm, String role) throws Exception {
    synchronized (gitRemoteUrl) {
        basePath = dataPath.resolve(Paths.get("git", "servlet"));
        Path fabricRepoPath = basePath.resolve("fabric");
        String servletBase = basePath.toFile().getAbsolutePath();
        // Init and clone the local repo.
        File fabricRoot = fabricRepoPath.toFile();
        if (!fabricRoot.exists()) {
            LOGGER.info("Cloning master root repo into {}", fabricRoot);
            File localRepo = gitDataStore.get().getGit().getRepository().getDirectory();
            git = Git.cloneRepository().setTimeout(10).setBare(true).setNoCheckout(true).setCloneAllBranches(true).setDirectory(fabricRoot).setURI(localRepo.toURI().toString()).call();
        } else {
            LOGGER.info("{} already exists", fabricRoot);
            git = Git.open(fabricRoot);
        }
        HttpContext base = httpService.get().createDefaultHttpContext();
        HttpContext secure = new GitSecureHttpContext(base, curator.get(), realm, role);
        Dictionary<String, Object> initParams = new Hashtable<String, Object>();
        initParams.put("base-path", servletBase);
        initParams.put("repository-root", servletBase);
        initParams.put("export-all", "true");
        httpService.get().registerServlet("/git", new FabricGitServlet(git, curator.get()), initParams, secure);
        registerGitHttpEndpoint();
    }
}
Also used : ZkPath(io.fabric8.zookeeper.ZkPath) Path(java.nio.file.Path) Hashtable(java.util.Hashtable) HttpContext(org.osgi.service.http.HttpContext) File(java.io.File)

Example 57 with Repository

use of io.fabric8.agent.model.Repository 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 58 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method alignTo.

@Override
public boolean alignTo(Map<String, String> versions, List<String> urls, File localMavenRepository, Runnable callback) throws PatchException {
    if (aligning.getAndSet(true)) {
        return false;
    }
    try {
        if (!env.isFabric()) {
            try {
                // we probably survived fabric:create without refreshing patch-management bundle
                env = envService.determineEnvironmentType();
                File patchRepositoryLocation = new File(patchesDir, GitPatchRepositoryImpl.MAIN_GIT_REPO_LOCATION);
                getGitPatchRepository().close();
                GitPatchRepositoryImpl repository = new GitPatchRepositoryImpl(env, patchRepositoryLocation, karafHome, karafBase, karafData, patchesDir);
                setGitPatchRepository(repository);
                start();
                // let's tweak the configuration when entering fabric mode
                // this way we will track other kinds of baselines
                ensurePatchManagementInitialized();
                if (master) {
                    // let the caller know that we've configured patch management in "master" container
                    // this is mainly to push the changes from local to cluster git repository
                    // so child/ssh containers created in fabric can fetch correct baselines
                    callback.run();
                }
            } catch (Exception e) {
                throw new PatchException(e.getMessage(), e);
            }
        }
        if (env.isFabric()) {
            Git fork = null;
            try {
                ensuringLock.lock();
                String version = versions.get(env.getProductId());
                String tagName = String.format(env.getBaselineTagFormat(), version);
                // we have to be at that tag
                Git mainRepository = gitPatchRepository.findOrCreateMainGitRepository();
                fetchFabricPatchData(mainRepository);
                fork = gitPatchRepository.cloneRepository(mainRepository, true);
                gitPatchRepository.checkout(fork).setName(gitPatchRepository.getMainBranchName()).call();
                RevTag tag = gitPatchRepository.findCurrentBaseline(fork);
                if (tag != null && tagName.equals(tag.getTagName())) {
                    if (master) {
                        // and then to data/git/servlet
                        try {
                            gitPatchRepository.pushPatchBranches();
                            callback.run();
                        } catch (Exception e) {
                            Activator.log(LogService.LOG_WARNING, null, e.getMessage(), e, false);
                        } catch (Error e) {
                            // in case newer patch management calls agent which is still wired to old patch management
                            Activator.log(LogService.LOG_WARNING, null, e.getMessage(), e, false);
                        }
                    }
                    return false;
                }
                boolean baselineSwitched = handleNonCurrentBaseline(fork, version, tagName, false, true);
                if (localMavenRepository != null) {
                    try {
                        File systemRepo = getSystemRepository(karafHome, systemContext);
                        // let's copy artifacts referenced in etc/startup.properties from localMavenRepository to system
                        File etcStartupProperties = new File(karafBase, "etc/startup.properties");
                        try (FileInputStream fis = new FileInputStream(etcStartupProperties)) {
                            Properties props = new Properties();
                            props.load(fis);
                            for (String artifact : props.stringPropertyNames()) {
                                File target = new File(systemRepo, artifact);
                                File src = new File(localMavenRepository, artifact);
                                if (!target.exists() && src.isFile()) {
                                    FileUtils.copyFile(src, target);
                                }
                            }
                        }
                        // now the URLs from the passed lis
                        for (String url : urls) {
                            String path = Utils.mvnurlToPath(url);
                            if (path != null) {
                                File target = new File(systemRepo, path);
                                File src = new File(localMavenRepository, path);
                                if (!target.exists() && src.isFile()) {
                                    FileUtils.copyFile(src, target);
                                }
                            }
                        }
                    } catch (Exception e) {
                        Activator.log(LogService.LOG_ERROR, null, e.getMessage(), e, false);
                    }
                }
                return baselineSwitched;
            } catch (Exception e) {
                throw new PatchException(e.getMessage(), e);
            } finally {
                ensuringLock.unlock();
                if (fork != null) {
                    gitPatchRepository.closeRepository(fork, true);
                }
            }
        }
        return false;
    } finally {
        aligning.set(false);
    }
}
Also used : Git(org.eclipse.jgit.api.Git) RevTag(org.eclipse.jgit.revwalk.RevTag) PatchException(io.fabric8.patch.management.PatchException) Properties(java.util.Properties) 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) FileInputStream(java.io.FileInputStream)

Example 59 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method installProfiles.

@Override
public void installProfiles(File gitRepository, String versionId, Patch patch, ProfileUpdateStrategy strategy) {
    // remember - we operate on totally different git repository!
    // we should have version branch already checked out.
    // here we don't merge/cherry-pick anything - we're preparing new commit simply by copying
    // one set of profiles (from R patch) over another (current profile definitions from Fabric)
    // we won't have merge conflicts, but we can't simply copy profiles over, because existing profiles
    // may have custom changes
    Git git = null;
    try {
        git = Git.open(gitRepository);
        File src = new File(patch.getPatchData().getPatchDirectory(), "fabric/import/fabric/profiles");
        File dst = new File(git.getRepository().getWorkTree(), "fabric/profiles");
        // ENTESB-6003:
        // let's clean the target directory first, so we can detect file removals in patches (like moving
        // jmx.* and org.apache.karaf.command.* PIDs from jboss-fuse-full to acls profile)
        FileUtils.deleteDirectory(dst);
        dst.mkdir();
        ProfileFileUtils.copyDirectory(src, dst, strategy);
        git.add().addFilepattern(".").call();
        // remove the deletes
        for (String missing : git.status().call().getMissing()) {
            git.rm().addFilepattern(missing).call();
        }
        // commit profile changes in patch branch - ultimate commit will be the merge commit
        git.commit().setMessage("Installing profiles from patch \"" + patch.getPatchData().getId() + "\"").call();
        PatchResult result = patch.getResult();
        if (result == null) {
            result = new PatchResult(patch.getPatchData(), false, new Date().getTime(), null, null);
            patch.setResult(result);
        }
        result.getVersions().add(versionId);
        result.store();
    } catch (Exception e) {
        throw new PatchException(e.getMessage(), e);
    } finally {
        if (git != null) {
            gitPatchRepository.closeRepository(git, false);
        }
    }
}
Also used : Git(org.eclipse.jgit.api.Git) PatchResult(io.fabric8.patch.management.PatchResult) PatchException(io.fabric8.patch.management.PatchException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) 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)

Example 60 with Repository

use of io.fabric8.agent.model.Repository in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method fetchPatches.

@Override
public List<PatchData> fetchPatches(URL url) throws PatchException {
    try {
        List<PatchData> patches = new ArrayList<>(1);
        File patchFile = new File(patchesDir, Long.toString(System.currentTimeMillis()) + ".patch.tmp");
        InputStream input = url.openStream();
        FileOutputStream output = new FileOutputStream(patchFile);
        ZipFile zf = null;
        try {
            IOUtils.copy(input, output);
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
        }
        try {
            zf = new ZipFile(patchFile);
        } catch (IOException ignored) {
            if (!FilenameUtils.getExtension(url.getFile()).equals("patch")) {
                throw new PatchException("Patch should be ZIP file or *.patch descriptor");
            }
        }
        // patchFile may "be" a patch descriptor or be a ZIP file containing descriptor
        PatchData patchData = null;
        // in case patch ZIP file has no descriptor, we'll "generate" patch data on the fly
        // no descriptor -> assume we have rollup patch or even full, new distribution
        PatchData fallbackPatchData = new PatchData(FilenameUtils.getBaseName(url.getPath()));
        fallbackPatchData.setGenerated(true);
        fallbackPatchData.setRollupPatch(true);
        fallbackPatchData.setPatchDirectory(new File(patchesDir, fallbackPatchData.getId()));
        fallbackPatchData.setPatchLocation(patchesDir);
        if (zf != null) {
            File systemRepo = getSystemRepository(karafHome, systemContext);
            try {
                List<ZipArchiveEntry> otherResources = new LinkedList<>();
                boolean skipRootDir = false;
                for (Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements(); ) {
                    ZipArchiveEntry entry = e.nextElement();
                    if (!skipRootDir && entry.isDirectory() && (entry.getName().startsWith("jboss-fuse-") || entry.getName().startsWith("jboss-a-mq-"))) {
                        skipRootDir = true;
                    }
                    if (entry.isDirectory() || entry.isUnixSymlink()) {
                        continue;
                    }
                    String name = entry.getName();
                    if (skipRootDir) {
                        name = name.substring(name.indexOf('/') + 1);
                    }
                    if (!name.contains("/") && name.endsWith(".patch")) {
                        // patch descriptor in ZIP's root directory
                        if (patchData == null) {
                            // load data from patch descriptor inside ZIP. This may or may not be a rollup
                            // patch
                            File target = new File(patchesDir, name);
                            extractZipEntry(zf, entry, target);
                            patchData = loadPatchData(target);
                            // ENTESB-4600: try checking the target version of the patch
                            Version version = Utils.findVersionInName(patchData.getId());
                            if (version.getMajor() == 6 && version.getMinor() == 1) {
                                throw new PatchException("Can't install patch \"" + patchData.getId() + "\", it is released for version 6.1 of the product");
                            }
                            patchData.setGenerated(false);
                            File targetDirForPatchResources = new File(patchesDir, patchData.getId());
                            patchData.setPatchDirectory(targetDirForPatchResources);
                            patchData.setPatchLocation(patchesDir);
                            target.renameTo(new File(patchesDir, patchData.getId() + ".patch"));
                            patches.add(patchData);
                        } else {
                            throw new PatchException(String.format("Multiple patch descriptors: already have patch %s and now encountered entry %s", patchData.getId(), name));
                        }
                    } else {
                        File target = null;
                        String relativeName = null;
                        if (name.startsWith("system/")) {
                            // copy to ${karaf.default.repository}
                            relativeName = name.substring("system/".length());
                            target = new File(systemRepo, relativeName);
                        } else if (name.startsWith("repository/")) {
                            // copy to ${karaf.default.repository}
                            relativeName = name.substring("repository/".length());
                            target = new File(systemRepo, relativeName);
                        } else {
                            // other files that should be applied to ${karaf.home} when the patch is installed
                            otherResources.add(entry);
                        }
                        if (target != null) {
                            // we unzip to system repository
                            extractAndTrackZipEntry(fallbackPatchData, zf, entry, target, skipRootDir);
                        }
                    }
                }
                File targetDirForPatchResources = new File(patchesDir, patchData == null ? fallbackPatchData.getId() : patchData.getId());
                // now copy non-maven resources (we should now know where to copy them)
                for (ZipArchiveEntry entry : otherResources) {
                    String name = entry.getName();
                    if (skipRootDir) {
                        name = name.substring(name.indexOf('/'));
                    }
                    File target = new File(targetDirForPatchResources, name);
                    extractAndTrackZipEntry(fallbackPatchData, zf, entry, target, skipRootDir);
                }
            } finally {
                if (zf != null) {
                    zf.close();
                }
                if (patchFile != null) {
                    patchFile.delete();
                }
            }
        } else {
            // If the file is not a zip/jar, assume it's a single patch file
            patchData = loadPatchData(patchFile);
            // no patch directory - no attached content, assuming only references to bundles
            patchData.setPatchDirectory(null);
            patchFile.renameTo(new File(patchesDir, patchData.getId() + ".patch"));
            patches.add(patchData);
        }
        if (patches.size() == 0) {
            // let's use generated patch descriptor
            File generatedPatchDescriptor = new File(patchesDir, fallbackPatchData.getId() + ".patch");
            FileOutputStream out = new FileOutputStream(generatedPatchDescriptor);
            try {
                fallbackPatchData.storeTo(out);
            } finally {
                IOUtils.closeQuietly(out);
            }
            patches.add(fallbackPatchData);
        }
        return patches;
    } catch (IOException e) {
        throw new PatchException("Unable to download patch from url " + url, e);
    }
}
Also used : PatchData(io.fabric8.patch.management.PatchData) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) Version(org.osgi.framework.Version) Artifact.isSameButVersion(io.fabric8.patch.management.Artifact.isSameButVersion) EOLFixingFileOutputStream(io.fabric8.patch.management.io.EOLFixingFileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) PatchException(io.fabric8.patch.management.PatchException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Aggregations

File (java.io.File)49 Test (org.junit.Test)32 Git (org.eclipse.jgit.api.Git)30 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)27 IOException (java.io.IOException)24 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)20 HashMap (java.util.HashMap)15 ObjectId (org.eclipse.jgit.lib.ObjectId)13 MalformedURLException (java.net.MalformedURLException)11 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 PatchException (io.fabric8.patch.management.PatchException)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 Bundle (org.osgi.framework.Bundle)8 Version (org.osgi.framework.Version)8 MavenResolver (io.fabric8.maven.MavenResolver)7 HashSet (java.util.HashSet)7 Repository (io.fabric8.agent.model.Repository)6 BundleContext (org.osgi.framework.BundleContext)6 Feature (io.fabric8.agent.model.Feature)5