Search in sources :

Example 1 with GitService

use of io.fabric8.git.GitService in project fabric8 by jboss-fuse.

the class ProjectDeployerTest method setUp.

@Before
public void setUp() throws Exception {
    URL.setURLStreamHandlerFactory(new CustomBundleURLStreamHandlerFactory());
    basedir = System.getProperty("basedir", ".");
    String karafRoot = basedir + "/target/karaf";
    System.setProperty("karaf.root", karafRoot);
    System.setProperty("karaf.data", karafRoot + "/data");
    sfb = new ZKServerFactoryBean();
    delete(sfb.getDataDir());
    delete(sfb.getDataLogDir());
    sfb.setPort(9123);
    sfb.afterPropertiesSet();
    int zkPort = sfb.getClientPortAddress().getPort();
    LOG.info("Connecting to ZK on port: " + zkPort);
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zkPort).retryPolicy(new RetryOneTime(1000)).connectionTimeoutMs(360000);
    curator = builder.build();
    curator.start();
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    // setup a local and remote git repo
    File root = new File(basedir + "/target/git").getCanonicalFile();
    delete(root);
    new File(root, "remote").mkdirs();
    remote = Git.init().setDirectory(new File(root, "remote")).setGitDir(new File(root, "remote/.git")).call();
    remote.commit().setMessage("First Commit").setCommitter("fabric", "user@fabric").call();
    String remoteUrl = "file://" + new File(root, "remote").getCanonicalPath();
    new File(root, "local").mkdirs();
    git = Git.init().setDirectory(new File(root, "local")).setGitDir(new File(root, "local/.git")).call();
    git.commit().setMessage("First Commit").setCommitter("fabric", "user@fabric").call();
    StoredConfig config = git.getRepository().getConfig();
    config.setString("remote", "origin", "url", remoteUrl);
    config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
    config.save();
    runtimeProperties = EasyMock.createMock(RuntimeProperties.class);
    EasyMock.expect(runtimeProperties.getRuntimeIdentity()).andReturn("root").anyTimes();
    EasyMock.expect(runtimeProperties.getHomePath()).andReturn(Paths.get("target")).anyTimes();
    EasyMock.expect(runtimeProperties.getDataPath()).andReturn(Paths.get("target/data")).anyTimes();
    EasyMock.expect(runtimeProperties.getProperty(EasyMock.eq(SystemProperties.FABRIC_ENVIRONMENT))).andReturn("").anyTimes();
    EasyMock.expect(runtimeProperties.removeRuntimeAttribute(DataStoreTemplate.class)).andReturn(null).anyTimes();
    EasyMock.replay(runtimeProperties);
    FabricGitServiceImpl gitService = new FabricGitServiceImpl();
    gitService.bindRuntimeProperties(runtimeProperties);
    gitService.activate();
    gitService.setGitForTesting(git);
    /*
        dataStore = new GitDataStoreImpl();
        dataStore.bindCurator(curator);
        dataStore.bindGitService(gitService);
        dataStore.bindRuntimeProperties(runtimeProperties);
        dataStore.bindConfigurer(new Configurer() {
        

            @Override
            public <T> Map<String, ?> configure(Map<String, ?> configuration, T target, String... ignorePrefix) throws Exception {
                return null;
            }

            @Override
            public <T> Map<String, ?> configure(Dictionary<String, ?> configuration, T target, String... ignorePrefix) throws Exception {
                return null;
            }
        });
        Map<String, Object> datastoreProperties = new HashMap<String, Object>();
        datastoreProperties.put(GitDataStore.GIT_REMOTE_URL, remoteUrl);
        dataStore.activate(datastoreProperties);
        */
    fabricService = new FabricServiceImpl();
    // fabricService.bindDataStore(dataStore);
    fabricService.bindRuntimeProperties(runtimeProperties);
    fabricService.bindPlaceholderResolver(new DummyPlaceholerResolver("port"));
    fabricService.bindPlaceholderResolver(new DummyPlaceholerResolver("zk"));
    fabricService.bindPlaceholderResolver(new ProfilePropertyPointerResolver());
    fabricService.bindPlaceholderResolver(new ChecksumPlaceholderResolver());
    fabricService.bindPlaceholderResolver(new VersionPropertyPointerResolver());
    fabricService.bindPlaceholderResolver(new EnvPlaceholderResolver());
    fabricService.activateComponent();
    projectDeployer = new ProjectDeployerImpl();
    projectDeployer.bindFabricService(fabricService);
    projectDeployer.bindMBeanServer(ManagementFactory.getPlatformMBeanServer());
    // dataStore.getDefaultVersion();
    String defaultVersion = null;
    assertEquals("defaultVersion", "1.0", defaultVersion);
    // now lets import some data - using the old non-git file layout...
    String importPath = basedir + "/../fabric8-karaf/src/main/resources/distro/fabric/import";
    assertFolderExists(importPath);
    dataStore.importFromFileSystem(importPath);
    assertHasVersion(defaultVersion);
}
Also used : ProfilePropertyPointerResolver(io.fabric8.service.ProfilePropertyPointerResolver) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) FabricGitServiceImpl(io.fabric8.git.internal.FabricGitServiceImpl) ZKServerFactoryBean(io.fabric8.zookeeper.spring.ZKServerFactoryBean) VersionPropertyPointerResolver(io.fabric8.service.VersionPropertyPointerResolver) EnvPlaceholderResolver(io.fabric8.service.EnvPlaceholderResolver) FabricServiceImpl(io.fabric8.service.FabricServiceImpl) StoredConfig(org.eclipse.jgit.lib.StoredConfig) DataStoreTemplate(io.fabric8.api.DataStoreTemplate) ChecksumPlaceholderResolver(io.fabric8.service.ChecksumPlaceholderResolver) File(java.io.File) RuntimeProperties(io.fabric8.api.RuntimeProperties) Before(org.junit.Before)

Example 2 with GitService

use of io.fabric8.git.GitService in project fabric8 by jboss-fuse.

the class ContainerBuilder method synchronizeGitRepositories.

/**
 * ENTESB-6368: Ensures that local git repository is synchronized with fabric-wide git repository.
 */
protected void synchronizeGitRepositories() {
    GitService gitService = ServiceLocator.awaitService(GitService.class);
    GitDataStore gitDataStore = ServiceLocator.awaitService(GitDataStore.class);
    for (int i = 0; i < 10; i++) {
        try {
            LOG.info("Synchronizing Git repositories");
            Iterable<PushResult> results = gitDataStore.doPush(gitService.getGit(), new GitContext().requirePush());
            if (results.iterator().hasNext()) {
                return;
            }
            throw new Exception("No reference was pushed");
        } catch (Exception e) {
            LOG.warn("Synchronization of Git repositories failed: " + e.getMessage());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(ie);
            }
        }
    }
}
Also used : GitContext(io.fabric8.api.GitContext) GitService(io.fabric8.git.GitService) PushResult(org.eclipse.jgit.transport.PushResult) GitDataStore(io.fabric8.git.GitDataStore) FabricException(io.fabric8.api.FabricException)

Example 3 with GitService

use of io.fabric8.git.GitService in project fabric8 by jboss-fuse.

the class ContainerBuilder method synchronizeGitRepositories.

/**
 * ENTESB-6368: Ensures that local git repository is synchronized with fabric-wide git repository.
 */
protected void synchronizeGitRepositories() {
    GitService gitService = io.fabric8.api.gravia.ServiceLocator.awaitService(GitService.class);
    GitDataStore gitDataStore = io.fabric8.api.gravia.ServiceLocator.awaitService(GitDataStore.class);
    for (int i = 0; i < 10; i++) {
        try {
            LOG.info("Synchronizing Git repositories");
            Iterable<PushResult> results = gitDataStore.doPush(gitService.getGit(), new GitContext().requirePush());
            if (results.iterator().hasNext()) {
                return;
            }
            throw new Exception("No reference was pushed");
        } catch (Exception e) {
            LOG.warn("Synchronization of Git repositories failed: " + e.getMessage());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(ie);
            }
        }
    }
}
Also used : GitContext(io.fabric8.api.GitContext) GitService(io.fabric8.git.GitService) PushResult(org.eclipse.jgit.transport.PushResult) GitDataStore(io.fabric8.git.GitDataStore) FabricException(io.fabric8.api.FabricException)

Example 4 with GitService

use of io.fabric8.git.GitService in project fabric8 by jboss-fuse.

the class GitDataStoreImplProfilesIT method init.

@Before
public void init() throws NoSuchFieldException, IllegalAccessException, IOException, GitAPIException {
    File repo = new File("target/fabric-git");
    FileUtils.deleteDirectory(repo);
    repo.mkdirs();
    git = Git.init().setDirectory(repo).setGitDir(new File(repo, ".git")).call();
    git.commit().setMessage("init").call();
    git.tag().setName(GitHelpers.ROOT_TAG).call();
    dataStore = mock(DataStore.class);
    when(dataStore.getDefaultVersion()).thenReturn("1.0");
    gitService = mock(GitService.class);
    when(gitService.getGit()).thenReturn(git);
    runtimeProperties = mock(RuntimeProperties.class);
    when(runtimeProperties.getRuntimeIdentity()).thenReturn("root");
    curator = mock(CuratorFramework.class);
    pullPushPolicy = mock(PullPushPolicy.class);
    PullPushPolicy.PushPolicyResult ppResult = mock(PullPushPolicy.PushPolicyResult.class);
    when(ppResult.getPushResults()).thenReturn(new ArrayList<PushResult>());
    when(pullPushPolicy.doPush(any(GitContext.class), any(CredentialsProvider.class))).thenReturn(ppResult);
    mockStatic(ZooKeeperUtils.class);
    PowerMockito.when(ZooKeeperUtils.generateContainerToken(runtimeProperties, curator)).thenReturn("token");
    gdsi = new GitDataStoreImpl();
    this.<ValidationSupport>getField(gdsi, "active", ValidationSupport.class).setValid();
    this.<ValidatingReference<DataStore>>getField(gdsi, "dataStore", ValidatingReference.class).bind(dataStore);
    this.<ValidatingReference<GitService>>getField(gdsi, "gitService", ValidatingReference.class).bind(gitService);
    this.<ValidatingReference<RuntimeProperties>>getField(gdsi, "runtimeProperties", ValidatingReference.class).bind(runtimeProperties);
    this.<ValidatingReference<CuratorFramework>>getField(gdsi, "curator", ValidatingReference.class).bind(curator);
    setField(gdsi, "dataStoreProperties", Map.class, new HashMap<String, Object>());
    setField(gdsi, "pullPushPolicy", PullPushPolicy.class, pullPushPolicy);
    profileRegistry = gdsi;
}
Also used : ValidatingReference(io.fabric8.api.scr.ValidatingReference) ValidationSupport(io.fabric8.api.scr.ValidationSupport) PullPushPolicy(io.fabric8.git.PullPushPolicy) PushResult(org.eclipse.jgit.transport.PushResult) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) GitContext(io.fabric8.api.GitContext) DataStore(io.fabric8.api.DataStore) GitService(io.fabric8.git.GitService) File(java.io.File) RuntimeProperties(io.fabric8.api.RuntimeProperties) Before(org.junit.Before)

Example 5 with GitService

use of io.fabric8.git.GitService in project fabric8 by jboss-fuse.

the class GitMasterListener method updateMasterUrl.

/**
 * Updates the git master url, if needed.
 */
private void updateMasterUrl(Group<GitNode> group) {
    GitNode master = group.master();
    String masterUrl = master != null ? master.getUrl() : null;
    try {
        if (masterUrl != null) {
            GitService gitservice = gitService.get();
            String substitutedUrl = getSubstitutedData(curator.get(), masterUrl);
            if (!Strings.isNotBlank(substitutedUrl)) {
                LOGGER.warn("Could not render git master URL {}.", masterUrl);
            }
            // Catch any possible issue indicating that the URL is invalid.
            if (!FabricValidations.isURIValid(substitutedUrl)) {
                LOGGER.warn("Not changing master Git URL to \"" + substitutedUrl + "\". There may be pending ZK connection shutdown.");
            } else {
                gitservice.notifyRemoteChanged(substitutedUrl);
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to point origin to the new master.", e);
    }
}
Also used : GitService(io.fabric8.git.GitService) GitNode(io.fabric8.git.GitNode) IOException(java.io.IOException)

Aggregations

GitService (io.fabric8.git.GitService)4 GitContext (io.fabric8.api.GitContext)3 RuntimeProperties (io.fabric8.api.RuntimeProperties)3 File (java.io.File)3 PushResult (org.eclipse.jgit.transport.PushResult)3 Before (org.junit.Before)3 DataStoreTemplate (io.fabric8.api.DataStoreTemplate)2 FabricException (io.fabric8.api.FabricException)2 GitDataStore (io.fabric8.git.GitDataStore)2 ZKServerFactoryBean (io.fabric8.zookeeper.spring.ZKServerFactoryBean)2 IOException (java.io.IOException)2 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)2 RetryOneTime (org.apache.curator.retry.RetryOneTime)2 StoredConfig (org.eclipse.jgit.lib.StoredConfig)2 DataStore (io.fabric8.api.DataStore)1 Configurer (io.fabric8.api.scr.Configurer)1 ValidatingReference (io.fabric8.api.scr.ValidatingReference)1 ValidationSupport (io.fabric8.api.scr.ValidationSupport)1 GitNode (io.fabric8.git.GitNode)1 PullPushPolicy (io.fabric8.git.PullPushPolicy)1