Search in sources :

Example 1 with GitVersion

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

the class DummyBatchingProgressMonitor method gitVersions.

@Override
public GitVersions gitVersions() {
    LockHandle readLock = aquireReadLock();
    try {
        assertValid();
        GitOperation<GitVersions> gitop = new GitOperation<GitVersions>() {

            public GitVersions call(Git git, GitContext context) throws Exception {
                List<GitVersion> localVersions = GitHelpers.gitVersions(git);
                return new GitVersions(localVersions);
            }
        };
        return executeInternal(newGitReadContext(), null, gitop);
    } finally {
        readLock.unlock();
    }
}
Also used : LockHandle(io.fabric8.api.LockHandle) Git(org.eclipse.jgit.api.Git) GitVersion(io.fabric8.api.commands.GitVersion) GitVersions(io.fabric8.api.commands.GitVersions) GitContext(io.fabric8.api.GitContext)

Example 2 with GitVersion

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

the class FabricGitSummaryAction method printVersions.

private void printVersions(String title, List<GitVersion> versions) {
    System.out.println(title);
    TablePrinter table = new TablePrinter();
    table.columns("version", "SHA1", "timestamp", "message");
    for (GitVersion version : versions) {
        table.row(version.getVersion(), version.getSha1(), version.getTimestamp(), version.getMessage());
    }
    table.print();
}
Also used : GitVersion(io.fabric8.api.commands.GitVersion) TablePrinter(io.fabric8.utils.TablePrinter)

Example 3 with GitVersion

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

the class FabricGitSummaryAction method afterEachContainer.

@Override
protected void afterEachContainer(Collection<String> names) {
    System.out.printf("Scheduled git-summary command to %d containers. Awaiting response(s).\n", names.size());
    final CountDownLatch latch = new CountDownLatch(requests.size() + (masterRequest == null ? 0 : 1));
    Thread waiter = null;
    try {
        waiter = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    String currentContainer = null;
                    try {
                        // master request
                        if (masterRequest != null && masterResult == null) {
                            currentContainer = master;
                            List<JMXResult> results = fetchResponses(master);
                            for (JMXResult result : results) {
                                if (result.getCorrelationId().equals(masterRequest.getId())) {
                                    masterResult = result;
                                    latch.countDown();
                                    break;
                                }
                            }
                        }
                        // requests for containers
                        for (Map.Entry<String, JMXRequest> req : requests.entrySet()) {
                            currentContainer = req.getKey();
                            List<JMXResult> containerResults = fetchResponses(currentContainer);
                            for (JMXResult result : containerResults) {
                                if (result.getCorrelationId().equals(req.getValue().getId())) {
                                    results.put(currentContainer, result);
                                    latch.countDown();
                                    break;
                                }
                            }
                        }
                        if ((masterRequest == null || masterResult != null) && results.size() == requests.size()) {
                            break;
                        } else {
                            // active waiting - so no need for ZK watchers, etc...
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break;
                    } catch (Exception e) {
                        System.err.println("Problem occurred while fetching response from " + currentContainer + " container: " + e.getMessage());
                        // active waiting - so no need for ZK watchers, etc...
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                        }
                    }
                }
            }
        });
        waiter.start();
        boolean finished = latch.await(timeout, TimeUnit.MILLISECONDS);
        if (!finished) {
            waiter.interrupt();
            System.out.println("Timeout waiting for git-summary response");
            return;
        }
        // before printing summary, let's check if there are out of sync containers
        Map<String, String> centralGitRepo = new HashMap<>();
        Set<String> outOfSyncContainers = new TreeSet<>();
        if (masterResult != null) {
            for (GitVersion version : ((GitVersions) masterResult.getResponse()).getVersions()) {
                centralGitRepo.put(version.getVersion(), version.getSha1());
            }
            for (String containerName : results.keySet()) {
                List<GitVersion> localRepo = ((GitVersions) results.get(containerName).getResponse()).getVersions();
                for (GitVersion version : localRepo) {
                    String ref = centralGitRepo.get(version.getVersion());
                    if (ref == null) {
                        // local container knows about version, which is not tracked in central repo
                        outOfSyncContainers.add(containerName);
                    } else if (!ref.equals(version.getSha1())) {
                        // version not in sync
                        outOfSyncContainers.add(containerName);
                    }
                }
                if (localRepo.size() != centralGitRepo.size()) {
                    // extra or not-in-sync versions handled, so this must mean some central version is not
                    // available locally
                    outOfSyncContainers.add(containerName);
                }
            }
            if (outOfSyncContainers.size() > 0) {
                System.out.println();
                System.out.println("Containers that require synchronization: " + outOfSyncContainers);
                System.out.println("Please use \"fabric:git-synchronize\" command");
            }
        } else {
            System.out.println();
            System.out.println("Can't determine synchronization status of containers - no master Git repository detected");
        }
        // now summary time
        if (masterResult != null) {
            System.out.println();
            printVersions("=== Summary for master Git repository (container: " + master + ") ===", ((GitVersions) masterResult.getResponse()).getVersions());
        }
        System.out.println();
        for (String containerName : results.keySet()) {
            printVersions("=== Summary for local Git repository (container: " + containerName + ") ===", ((GitVersions) results.get(containerName).getResponse()).getVersions());
            System.out.println();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        waiter.interrupt();
        System.out.println("Interrupted waiting for git-summary response");
    }
}
Also used : GitVersion(io.fabric8.api.commands.GitVersion) JMXRequest(io.fabric8.api.commands.JMXRequest) HashMap(java.util.HashMap) JMXResult(io.fabric8.api.commands.JMXResult) CountDownLatch(java.util.concurrent.CountDownLatch) GitVersions(io.fabric8.api.commands.GitVersions) TreeSet(java.util.TreeSet) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 4 with GitVersion

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

the class GitHelpers method gitVersions.

/**
 * Fetches summary information (list of {@link GitVersion}) for given {@link Git} instance.
 * @param git
 * @return
 * @throws GitAPIException
 * @throws IOException
 */
public static List<GitVersion> gitVersions(Git git) throws GitAPIException, IOException {
    List<Ref> refs = git.branchList().call();
    List<GitVersion> localVersions = new LinkedList<>();
    for (Ref ref : refs) {
        String v = ref.getName();
        if (v.startsWith("refs/heads/")) {
            String name = v.substring(("refs/heads/").length());
            if (name.startsWith("patch-") || name.startsWith("patches-") || name.startsWith("container-history")) {
                continue;
            }
            GitVersion gv = new GitVersion(name);
            gv.setSha1(ref.getObjectId().getName());
            RevCommit headCommit = new RevWalk(git.getRepository()).parseCommit(ref.getObjectId());
            if (headCommit != null) {
                gv.setMessage(headCommit.getShortMessage());
                gv.setTimestamp(TIMESTAMP.format(new Date(headCommit.getCommitTime() * 1000L)));
            }
            localVersions.add(gv);
        }
    }
    return localVersions;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) GitVersion(io.fabric8.api.commands.GitVersion) RevWalk(org.eclipse.jgit.revwalk.RevWalk) LinkedList(java.util.LinkedList) Date(java.util.Date) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 5 with GitVersion

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

the class FabricManagerTest method toJson.

@Test
public void toJson() throws JsonProcessingException {
    JMXRequest r = new JMXRequest();
    r.setId("id");
    r.withObjectName("io.fabric8:type=manager");
    r.withParam(Boolean.class, true);
    System.out.println(om().writeValueAsString(r));
    JMXResult res = new JMXResult();
    res.setCode(0);
    res.setMessage("OK");
    res.setCorrelationId("id");
    GitVersion v = new GitVersion("1.0");
    v.setSha1("SHA1");
    GitVersions versions = new GitVersions();
    versions.getVersions().addAll(Arrays.asList(v, v, v));
    res.setResponse(versions);
    System.out.println(om().writeValueAsString(res));
    System.out.println(om().writeValueAsString(versions));
}
Also used : GitVersion(io.fabric8.api.commands.GitVersion) JMXRequest(io.fabric8.api.commands.JMXRequest) GitVersions(io.fabric8.api.commands.GitVersions) JMXResult(io.fabric8.api.commands.JMXResult) Test(org.junit.Test)

Aggregations

GitVersion (io.fabric8.api.commands.GitVersion)5 GitVersions (io.fabric8.api.commands.GitVersions)3 JMXRequest (io.fabric8.api.commands.JMXRequest)2 JMXResult (io.fabric8.api.commands.JMXResult)2 GitContext (io.fabric8.api.GitContext)1 LockHandle (io.fabric8.api.LockHandle)1 TablePrinter (io.fabric8.utils.TablePrinter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Git (org.eclipse.jgit.api.Git)1 Ref (org.eclipse.jgit.lib.Ref)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 Test (org.junit.Test)1