Search in sources :

Example 51 with Repository

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

the class DummyBatchingProgressMonitor method executeInternal.

private <T> T executeInternal(GitContext context, PersonIdent personIdent, GitOperation<T> operation) {
    if (context.isRequirePull() || context.isRequireCommit()) {
        assertWriteLock();
    } else {
        assertReadLock();
    }
    // [FABRIC-887] Must set the TCCL to the classloader that loaded GitDataStore as we need the classloader
    // that could load this class, as jgit will load resources from classpath using the TCCL
    // and that requires the TCCL to the classloader that could load GitDataStore as the resources
    // jgit requires are in the same bundle as GitDataSource (eg embedded inside fabric-git)
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        ClassLoader gitcl = GitDataStoreImpl.class.getClassLoader();
        Thread.currentThread().setContextClassLoader(gitcl);
        LOGGER.trace("Setting ThreadContextClassLoader to {} instead of {}", gitcl, tccl);
        Git git = getGit();
        Repository repository = git.getRepository();
        if (personIdent == null) {
            personIdent = new PersonIdent(repository);
        }
        if (context.isRequirePull()) {
            doPullInternal(context, getCredentialsProvider(), false, gitTimeout);
        }
        T result = operation.call(git, context);
        if (context.isRequireCommit()) {
            doCommit(git, context);
            Object cacheKey = context.getCacheKey();
            if (cacheKey == null || cacheKey.equals(GitHelpers.MASTER_BRANCH)) {
                versionCache.invalidateAll();
            } else {
                versionCache.invalidate(cacheKey);
            }
            notificationRequired = true;
        }
        if (context.isRequirePush()) {
            PushPolicyResult pushResult = doPushInternal(context, getCredentialsProvider());
            if (!pushResult.getRejectedUpdates().isEmpty()) {
                Exception gitex = pushResult.getLastException();
                throw new IllegalStateException("Push rejected: " + pushResult.getRejectedUpdates().values(), gitex);
            }
        }
        return result;
    } catch (Exception e) {
        throw FabricException.launderThrowable(e);
    } finally {
        LOGGER.trace("Restoring ThreadContextClassLoader to {}", tccl);
        Thread.currentThread().setContextClassLoader(tccl);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) PersonIdent(org.eclipse.jgit.lib.PersonIdent) PushPolicyResult(io.fabric8.git.PullPushPolicy.PushPolicyResult) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) FabricException(io.fabric8.api.FabricException) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException)

Example 52 with Repository

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

the class DummyBatchingProgressMonitor method activateInternal.

private void activateInternal() throws Exception {
    LOGGER.info("Starting up GitDataStore " + this);
    // Call the bootstrap {@link DataStoreTemplate}
    DataStoreTemplate template = runtimeProperties.get().removeRuntimeAttribute(DataStoreTemplate.class);
    if (template != null) {
        // Do the initial commit and set the root tag
        Ref rootTag = getGit().getRepository().getRef(GitHelpers.ROOT_TAG);
        if (rootTag == null) {
            getGit().commit().setMessage("First Commit").setCommitter("fabric", "user@fabric").call();
            getGit().tag().setName(GitHelpers.ROOT_TAG).setMessage("Tag the root commit").call();
        }
        LOGGER.debug("Running datastore bootstrap template: " + template);
        template.doWith(this, dataStore.get());
    }
    // Setup proxy service
    GitProxyService proxyService = gitProxyService.get();
    defaultProxySelector = ProxySelector.getDefault();
    // authenticator disabled, until properly tested it does not affect others, as Authenticator is static in the JVM
    // Authenticator.setDefault(new FabricGitLocalHostAuthenticator(proxyService));
    ProxySelector fabricProxySelector = new FabricGitLocalHostProxySelector(defaultProxySelector, proxyService);
    ProxySelector.setDefault(fabricProxySelector);
    LOGGER.debug("Setting up FabricProxySelector: {}", fabricProxySelector);
    if (gitRemoteUrl != null) {
        gitListener.runRemoteUrlChanged(gitRemoteUrl);
        remoteUrl = gitRemoteUrl;
    } else {
        gitService.get().addGitListener(gitListener);
        remoteUrl = gitService.get().getRemoteUrl();
        if (remoteUrl != null) {
            gitListener.runRemoteUrlChanged(remoteUrl);
        }
    }
    // Get initial versions
    getInitialVersions();
    // poll logic in case of remote git repo
    if (gitRemoteUrl != null) {
        // i need this old logic in case of remote repos
        LOGGER.info("Starting to pull from remote git repository every {} millis", gitRemotePollInterval);
        threadPool.scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                LockHandle writeLock = aquireWriteLock();
                try {
                    LOGGER.trace("Performing timed pull");
                    doPullInternal();
                    LOGGER.debug("Performed timed pull from external git repo");
                } catch (Throwable e) {
                    LOGGER.debug("Error during performed timed pull/push due " + e.getMessage(), e);
                    LOGGER.warn("Error during performed timed pull/push due " + e.getMessage() + ". This exception is ignored.");
                } finally {
                    writeLock.unlock();
                }
            }

            @Override
            public String toString() {
                return "TimedPushTask";
            }
        }, 1000, gitRemotePollInterval, TimeUnit.MILLISECONDS);
    }
    LOGGER.info("Using ZooKeeper SharedCount to react when master git repo is changed, so we can do a git pull to the local git repo.");
    counter = new SharedCount(curator.get(), ZkPath.GIT_TRIGGER.getPath(), 0);
    counter.addListener(new SharedCountListener() {

        @Override
        public void countHasChanged(final SharedCountReader sharedCountReader, final int value) throws Exception {
            Runnable task = new Runnable() {

                @Override
                public void run() {
                    doPullInternal();
                }
            };
            if (gitRandomFetchDelay == 0) {
                LOGGER.debug("Watch counter updated to " + value + ", scheduling immediate pull");
                threadPool.submit(task);
            } else {
                int delay = RND.nextInt(gitRandomFetchDelay) + 1;
                LOGGER.debug("Watch counter updated to " + value + ", scheduling pull with random delay=" + delay + "s");
                threadPool.schedule(task, delay, TimeUnit.SECONDS);
            }
        }

        @Override
        public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
            switch(connectionState) {
                case SUSPENDED:
                case READ_ONLY:
                case LOST:
                    // do nothing
                    break;
                case CONNECTED:
                case RECONNECTED:
                    LOGGER.info("Shared Counter (Re)connected, doing a pull");
                    doPullInternal();
                    break;
            }
        }
    });
    try {
        counter.start();
    } catch (KeeperException.NotReadOnlyException ex) {
    // In read only mode the counter is not going to start.
    // If the connection is reestablished the component will reactivate.
    // We need to catch this error so that the component gets activated.
    }
    if (gitGcOnLoad) {
        LockHandle writeLock = aquireWriteLock();
        try {
            GitOperation<Void> gitop = new GitOperation<Void>() {

                public Void call(Git git, GitContext context) throws Exception {
                    long before = System.currentTimeMillis();
                    try {
                        git.gc().call();
                        LOGGER.debug("git gc took " + ((System.currentTimeMillis() - before)) + " ms.");
                    } catch (GitAPIException e) {
                        LOGGER.debug("git gc threw an exception!", e);
                    }
                    return null;
                }
            };
            executeInternal(new GitContext(), null, gitop);
        } finally {
            writeLock.unlock();
        }
    }
    // failing to activate the component
    if (readWriteLock.isWriteLockedByCurrentThread() || readWriteLock.getWriteHoldCount() == 0) {
        try {
            // let's delegate to other thread in order to free MCF thread
            // ENTESB-7843: there's a problem when jetty is configured with TLS and keystore location using
            // profile: URI. while Jetty continues to be configured if profile:jetty.xml isn't available
            // (and it isn't initially), it fails trying to access keystore via profile: URI.
            // we should optimistically assume we can pull, but there's nothing wrong if we can't
            // - we'll be able to pull later
            threadPoolInitial.execute(new Runnable() {

                @Override
                public void run() {
                    doPullInternal(5);
                }
            });
        } catch (IllegalStateException e) {
            LOGGER.info("Another thread acquired write lock and GitDataStore can't pull from remote repository.");
        }
    } else {
        LOGGER.info("Another thread is keeping git write lock. GitDataStore will continue activation.");
    }
}
Also used : SharedCount(org.apache.curator.framework.recipes.shared.SharedCount) ProxySelector(java.net.ProxySelector) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) CuratorFramework(org.apache.curator.framework.CuratorFramework) DataStoreTemplate(io.fabric8.api.DataStoreTemplate) SharedCountListener(org.apache.curator.framework.recipes.shared.SharedCountListener) GitProxyService(io.fabric8.git.GitProxyService) LockHandle(io.fabric8.api.LockHandle) SharedCountReader(org.apache.curator.framework.recipes.shared.SharedCountReader) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) FabricException(io.fabric8.api.FabricException) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) GitContext(io.fabric8.api.GitContext) ConnectionState(org.apache.curator.framework.state.ConnectionState) KeeperException(org.apache.zookeeper.KeeperException)

Example 53 with Repository

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

the class FabricFeaturesServiceImpl method listFeatures.

@Override
public Feature[] listFeatures() throws Exception {
    assertValid();
    Set<Feature> allfeatures = new HashSet<Feature>();
    Repository[] repositories = listRepositories();
    for (Repository repository : repositories) {
        try {
            for (Feature feature : repository.getFeatures()) {
                if (!allfeatures.contains(feature)) {
                    allfeatures.add(feature);
                }
            }
        } catch (Exception ex) {
            LOGGER.debug("Could not load features from %s.", repository.getURI());
        }
    }
    return allfeatures.toArray(new Feature[allfeatures.size()]);
}
Also used : Repository(org.apache.karaf.features.Repository) Feature(org.apache.karaf.features.Feature) InvalidComponentException(io.fabric8.api.InvalidComponentException)

Example 54 with Repository

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

the class FabricFeaturesServiceImpl method getFeatures.

private Map<String, Map<String, Feature>> getFeatures(Iterable<Repository> repositories) throws Exception {
    Map<String, Map<String, Feature>> features = new HashMap<String, Map<String, Feature>>();
    for (Repository repo : repositories) {
        try {
            for (Feature f : repo.getFeatures()) {
                if (features.get(f.getName()) == null) {
                    Map<String, Feature> versionMap = new TreeMap<String, Feature>();
                    versionMap.put(f.getVersion(), f);
                    features.put(f.getName(), versionMap);
                } else {
                    features.get(f.getName()).put(f.getVersion(), f);
                }
            }
        } catch (Exception ex) {
            LOGGER.debug("Could not load features from %s.", repo.getURI());
        }
    }
    return features;
}
Also used : Repository(org.apache.karaf.features.Repository) Feature(org.apache.karaf.features.Feature) InvalidComponentException(io.fabric8.api.InvalidComponentException)

Example 55 with Repository

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

the class FabricFeaturesServiceImpl method listInstalledRepositories.

/**
 * Lists all {@link Repository} enties found in the {@link Profile}s assigned to the current {@link Container}.
 */
private Repository[] listInstalledRepositories() {
    Set<String> repositoryUris = new LinkedHashSet<String>();
    Set<Repository> repos = new LinkedHashSet<Repository>();
    Profile overlayProfile = fabricService.get().getCurrentContainer().getOverlayProfile();
    Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService.get(), overlayProfile);
    if (effectiveProfile.getRepositories() != null) {
        for (String uri : effectiveProfile.getRepositories()) {
            repositoryUris.add(uri);
        }
    }
    for (String uri : repositoryUris) {
        try {
            populateRepositories(uri, repos);
        } catch (Exception ex) {
            LOGGER.warn("Error while populating repositories from uri: {} ", uri, ex);
        }
    }
    return repos.toArray(new Repository[repos.size()]);
}
Also used : Repository(org.apache.karaf.features.Repository) Profile(io.fabric8.api.Profile) InvalidComponentException(io.fabric8.api.InvalidComponentException)

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