Search in sources :

Example 11 with RuntimeProperties

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

the class KarafContainerRegistration method checkAlive.

private void checkAlive() throws Exception {
    RuntimeProperties sysprops = runtimeProperties.get();
    String runtimeIdentity = sysprops.getRuntimeIdentity();
    String nodeAlive = CONTAINER_ALIVE.getPath(runtimeIdentity);
    Stat stat = exists(curator.get(), nodeAlive);
    if (stat != null) {
        if (stat.getEphemeralOwner() != curator.get().getZookeeperClient().getZooKeeper().getSessionId()) {
            delete(curator.get(), nodeAlive);
            create(curator.get(), nodeAlive, CreateMode.EPHEMERAL);
        }
    } else {
        create(curator.get(), nodeAlive, CreateMode.EPHEMERAL);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) RuntimeProperties(io.fabric8.api.RuntimeProperties)

Example 12 with RuntimeProperties

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

the class ZooKeeperUtils method generateContainerToken.

public static String generateContainerToken(RuntimeProperties sysprops, CuratorFramework curator) {
    String container = sysprops.getRuntimeIdentity();
    long time = System.currentTimeMillis();
    String password = null;
    try {
        if (time - lastTokenGenerationTime < 60 * 1000) {
            try {
                password = getStringData(curator, CONTAINERS_NODE + "/" + container);
            } catch (KeeperException.NoNodeException ex) {
            // Node hasn't been created yet. It's safe to ignore.
            }
        }
        if (password == null) {
            password = generatePassword();
            setData(curator, CONTAINERS_NODE + "/" + container, password);
            lastTokenGenerationTime = time;
        }
    } catch (KeeperException.NotReadOnlyException e) {
        throw new FabricException("ZooKeeper server is partitioned. Currently working in read-only mode!");
    } catch (RuntimeException rte) {
        throw rte;
    } catch (Exception ex) {
        throw new IllegalStateException("Cannot generate container token", ex);
    }
    return password;
}
Also used : FabricException(io.fabric8.api.FabricException) KeeperException(org.apache.zookeeper.KeeperException) URISyntaxException(java.net.URISyntaxException) FabricException(io.fabric8.api.FabricException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 13 with RuntimeProperties

use of io.fabric8.api.RuntimeProperties 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 14 with RuntimeProperties

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

the class DummyBatchingProgressMonitor method getCredentialsProvider.

private CredentialsProvider getCredentialsProvider() {
    Map<String, String> properties = getDataStoreProperties();
    String username;
    String password;
    RuntimeProperties sysprops = runtimeProperties.get();
    username = ZooKeeperUtils.getContainerLogin(sysprops);
    password = ZooKeeperUtils.generateContainerToken(sysprops, curator.get());
    if (isExternalGitConfigured(properties)) {
        username = getExternalUser(properties);
        password = getExternalCredential(properties);
    }
    return new UsernamePasswordCredentialsProvider(username, password);
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) RuntimeProperties(io.fabric8.api.RuntimeProperties)

Example 15 with RuntimeProperties

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

the class CachingGitDataStoreTest method setUp.

@Before
public void setUp() throws Exception {
    sfb = new ZKServerFactoryBean();
    delete(sfb.getDataDir());
    delete(sfb.getDataLogDir());
    sfb.afterPropertiesSet();
    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.removeRuntimeAttribute(DataStoreTemplate.class)).andReturn(null).anyTimes();
    EasyMock.replay(runtimeProperties);
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + sfb.getClientPortAddress().getPort()).retryPolicy(new RetryOneTime(1000)).connectionTimeoutMs(360000);
    curator = builder.build();
    curator.start();
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    // setup a local and remote git repo
    basedir = System.getProperty("basedir", ".");
    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();
    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(GitDataStoreImpl.GIT_REMOTE_URL, remoteUrl);
    dataStore.activate(datastoreProperties);
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) HashMap(java.util.HashMap) ZKServerFactoryBean(io.fabric8.zookeeper.spring.ZKServerFactoryBean) IOException(java.io.IOException) StoredConfig(org.eclipse.jgit.lib.StoredConfig) DataStoreTemplate(io.fabric8.api.DataStoreTemplate) Configurer(io.fabric8.api.scr.Configurer) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) RuntimeProperties(io.fabric8.api.RuntimeProperties) Before(org.junit.Before)

Aggregations

RuntimeProperties (io.fabric8.api.RuntimeProperties)24 IOException (java.io.IOException)12 File (java.io.File)9 AbstractRuntimeProperties (io.fabric8.api.scr.AbstractRuntimeProperties)6 BootstrapConfiguration (io.fabric8.zookeeper.bootstrap.BootstrapConfiguration)6 HashMap (java.util.HashMap)6 FabricException (io.fabric8.api.FabricException)5 MavenResolver (io.fabric8.maven.MavenResolver)5 CreateEnsembleOptions (io.fabric8.api.CreateEnsembleOptions)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 Configuration (org.osgi.service.cm.Configuration)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Map (java.util.Map)3 KeeperException (org.apache.zookeeper.KeeperException)3 Server (org.eclipse.jetty.server.Server)3 ServerConnector (org.eclipse.jetty.server.ServerConnector)3 Container (io.fabric8.api.Container)2 GitContext (io.fabric8.api.GitContext)2