Search in sources :

Example 21 with DataStore

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

the class DataStoreBootstrapTemplate method doWith.

@Override
public void doWith(ProfileRegistry profileRegistry, DataStore dataStore) throws Exception {
    String versionId = options.getVersion();
    int minimumPort = options.getMinimumPort();
    int maximumPort = options.getMaximumPort();
    String zooKeeperServerHost = options.getBindAddress();
    int zooKeeperServerPort = options.getZooKeeperServerPort();
    int zooKeeperServerConnectionPort = options.getZooKeeperServerConnectionPort();
    int mappedPort = Ports.mapPortToRange(zooKeeperServerPort, minimumPort, maximumPort);
    CuratorFramework curator = null;
    try {
        curator = createCuratorFramework(connectionUrl, options);
        curator.start();
        curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
        LockHandle writeLock = profileRegistry.aquireWriteLock();
        try {
            // Make the import path absolute
            File importPath = new File(options.getImportPath());
            if (!importPath.isAbsolute()) {
                importPath = new File(homeDir, options.getImportPath());
            }
            // Import data into the DataStore
            if (options.isAutoImportEnabled()) {
                if (importPath.isDirectory()) {
                    profileRegistry.importFromFileSystem(importPath.getAbsolutePath());
                } else {
                    LOGGER.warn("Profile import dir does not exist: {}", importPath);
                }
            }
            // set the fabric configuration
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_DEFAULT_VERSION.getPath(), versionId);
            // Default JAAS config
            Map<String, String> jaasConfig = Collections.singletonMap("encryption.enabled", "${zk:/fabric/authentication/encryption.enabled}");
            // Default Zookeeper config
            Properties zkProps = new Properties();
            zkProps.setProperty("zookeeper.url", "${zk:" + ZkPath.CONFIG_ENSEMBLE_URL.getPath() + "}");
            zkProps.setProperty("zookeeper.password", "${zk:" + ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath() + "}");
            // Create or update default profile
            Profile defaultProfile = profileRegistry.getProfile(versionId, "default");
            if (defaultProfile == null) {
                ProfileBuilder prfBuilder = ProfileBuilder.Factory.create(versionId, "default");
                prfBuilder.addConfiguration("io.fabric8.jaas", jaasConfig);
                prfBuilder.addFileConfiguration("io.fabric8.zookeeper.properties", DataStoreUtils.toBytes(zkProps));
                Profile profile = prfBuilder.getProfile();
                if (profileRegistry.hasVersion(versionId)) {
                    profileRegistry.createProfile(profile);
                } else {
                    VersionBuilder verBuilder = VersionBuilder.Factory.create(versionId);
                    Version version = verBuilder.addProfile(profile).getVersion();
                    profileRegistry.createVersion(version);
                }
            } else {
                ProfileBuilder builder = ProfileBuilder.Factory.createFrom(defaultProfile);
                // be careful not to mess with existing *.properties files - for better "diffability"
                // during git-based patching
                Map<String, String> existingJaasConfig = builder.getConfiguration("io.fabric8.jaas");
                Map<String, String> existingZkConfig = builder.getConfiguration("io.fabric8.zookeeper");
                existingJaasConfig.putAll(jaasConfig);
                existingZkConfig.put("zookeeper.url", zkProps.getProperty("zookeeper.url"));
                existingZkConfig.put("zookeeper.password", zkProps.getProperty("zookeeper.password"));
                builder.addConfiguration("io.fabric8.jaas", existingJaasConfig);
                builder.addConfiguration("io.fabric8.zookeeper", existingZkConfig);
                profileRegistry.updateProfile(builder.getProfile());
            }
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_ENSEMBLE_URL.getPath(), "${zk:" + name + "/ip}:" + zooKeeperServerConnectionPort);
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath(), PasswordEncoder.encode(options.getZookeeperPassword()));
            // Ensemble properties
            Properties ensembleProps = new Properties();
            ensembleProps.put("tickTime", String.valueOf(options.getZooKeeperServerTickTime()));
            ensembleProps.put("initLimit", String.valueOf(options.getZooKeeperServerInitLimit()));
            ensembleProps.put("syncLimit", String.valueOf(options.getZooKeeperServerSyncLimit()));
            ensembleProps.put("snapRetainCount", String.valueOf(options.getZookeeperSnapRetainCount()));
            ensembleProps.put("purgeInterval", String.valueOf(options.getZookeeperPurgeInterval()));
            ensembleProps.put("dataDir", options.getZooKeeperServerDataDir() + File.separator + "0000");
            loadPropertiesFrom(ensembleProps, importPath + "/fabric/profiles/default.profile/io.fabric8.zookeeper.server.properties");
            // Create ensemble profile
            String profileId = "fabric-ensemble-0000";
            IllegalStateAssertion.assertFalse(profileRegistry.hasProfile(versionId, profileId), "Profile already exists: " + versionId + "/" + profileId);
            ProfileBuilder ensembleProfileBuilder = ProfileBuilder.Factory.create(versionId, profileId);
            ensembleProfileBuilder.addAttribute(Profile.ABSTRACT, "true").addAttribute(Profile.HIDDEN, "true");
            ensembleProfileBuilder.addFileConfiguration("io.fabric8.zookeeper.server-0000.properties", DataStoreUtils.toBytes(ensembleProps));
            String ensembleProfileId = profileRegistry.createProfile(ensembleProfileBuilder.getProfile());
            // Ensemble server properties
            Properties serverProps = new Properties();
            serverProps.put("clientPort", String.valueOf(mappedPort));
            serverProps.put("clientPortAddress", zooKeeperServerHost);
            // Create ensemble server profile
            profileId = "fabric-ensemble-0000-1";
            IllegalStateAssertion.assertFalse(profileRegistry.hasProfile(versionId, profileId), "Profile already exists: " + versionId + "/" + profileId);
            ProfileBuilder serverProfileBuilder = ProfileBuilder.Factory.create(versionId, profileId);
            serverProfileBuilder.addAttribute(Profile.HIDDEN, "true").addAttribute(Profile.PARENTS, ensembleProfileId);
            serverProfileBuilder.addFileConfiguration("io.fabric8.zookeeper.server-0000.properties", DataStoreUtils.toBytes(serverProps));
            profileRegistry.createProfile(serverProfileBuilder.getProfile());
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_ENSEMBLES.getPath(), "0000");
            ZooKeeperUtils.setData(curator, ZkPath.CONFIG_ENSEMBLE.getPath("0000"), name);
            // configure fabric profile
            Profile fabricProfile = profileRegistry.getProfile(versionId, "fabric");
            if (fabricProfile == null) {
                ProfileBuilder builder = ProfileBuilder.Factory.create(versionId, "fabric");
                Properties agentProps = new Properties();
                agentProps.put("feature.fabric-commands", "fabric-commands");
                builder.addFileConfiguration("io.fabric8.agent.properties", DataStoreUtils.toBytes(agentProps));
                String createdId = profileRegistry.createProfile(builder.getProfile());
                fabricProfile = profileRegistry.getRequiredProfile(versionId, createdId);
            } else {
                ProfileBuilder builder = ProfileBuilder.Factory.createFrom(fabricProfile);
                Map<String, String> agentProps = builder.getConfiguration(Constants.AGENT_PID);
                agentProps.put("feature.fabric-commands", "fabric-commands");
                builder.addConfiguration(Constants.AGENT_PID, agentProps);
                String updatedId = profileRegistry.updateProfile(builder.getProfile());
                fabricProfile = profileRegistry.getRequiredProfile(versionId, updatedId);
            }
        } finally {
            writeLock.unlock();
        }
        ZooKeeperUtils.createDefault(curator, ZkPath.CONFIG_CONTAINER.getPath(name), versionId);
        StringBuilder profilesBuilder = new StringBuilder();
        Set<String> profiles = options.getProfiles();
        profilesBuilder.append("fabric").append(" ").append("fabric-ensemble-0000-1");
        for (String p : profiles) {
            if (!(p.equals("fabric") || p.equals("fabric-ensemble-0000-1"))) {
                profilesBuilder.append(" ").append(p);
            }
        }
        if (!options.isAgentEnabled()) {
            profilesBuilder.append(" ").append("unmanaged");
        }
        ZooKeeperUtils.createDefault(curator, ZkPath.CONFIG_VERSIONS_CONTAINER.getPath(versionId, name), profilesBuilder.toString());
        // outside of the profile storage area, so we'll keep these in zk
        EncryptionSupport encryption = addUsersToZookeeper(curator, options.getUsers());
        ZooKeeperUtils.createDefault(curator, "/fabric/authentication/encryption.enabled", Boolean.valueOf(encryption != null).toString());
        ZooKeeperUtils.createDefault(curator, "/fabric/authentication/domain", "karaf");
        ZooKeeperUtils.createDefault(curator, ZkPath.AUTHENTICATION_CRYPT_ALGORITHM.getPath(), "PBEWithMD5AndDES");
        ZooKeeperUtils.createDefault(curator, ZkPath.AUTHENTICATION_CRYPT_PASSWORD.getPath(), PasswordEncoder.encode(options.getZookeeperPassword()));
        // Ensure ACLs are from the beggining of the fabric tree.
        aclManager.fixAcl(curator, "/fabric", true);
    } finally {
        if (curator != null) {
            curator.close();
        }
    }
}
Also used : LockHandle(io.fabric8.api.LockHandle) Properties(java.util.Properties) ProfileBuilder(io.fabric8.api.ProfileBuilder) Profile(io.fabric8.api.Profile) CuratorFramework(org.apache.curator.framework.CuratorFramework) Version(io.fabric8.api.Version) VersionBuilder(io.fabric8.api.VersionBuilder) File(java.io.File) EncryptionSupport(org.apache.karaf.jaas.modules.encryption.EncryptionSupport)

Example 22 with DataStore

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

the class OpenShiftDeployAgent method groupEvent.

@Override
public void groupEvent(Group<ControllerNode> group, GroupEvent event) {
    if (isValid()) {
        if (group.isMaster()) {
            LOGGER.info("OpenShiftDeployAgent is the master");
        } else {
            LOGGER.info("OpenShiftDeployAgent is not the master");
        }
        try {
            DataStore dataStore = null;
            if (fabricService != null) {
                dataStore = fabricService.get().adapt(DataStore.class);
            } else {
                LOGGER.warn("No fabricService yet!");
            }
            if (group.isMaster()) {
                ControllerNode state = createState();
                group.update(state);
            }
            if (dataStore != null) {
                if (group.isMaster()) {
                    dataStore.trackConfiguration(runnable);
                    onConfigurationChanged();
                } else {
                    dataStore.untrackConfiguration(runnable);
                }
            }
        } catch (IllegalStateException e) {
        // Ignore
        }
    }
}
Also used : DataStore(io.fabric8.api.DataStore)

Aggregations

Test (org.junit.Test)11 Version (io.fabric8.api.Version)8 Ignore (org.junit.Ignore)7 DataStore (io.fabric8.api.DataStore)6 Profile (io.fabric8.api.Profile)5 Before (org.junit.Before)5 FabricService (io.fabric8.api.FabricService)4 PlaceholderResolver (io.fabric8.api.PlaceholderResolver)4 ContainerImpl (io.fabric8.internal.ContainerImpl)4 File (java.io.File)4 IOException (java.io.IOException)4 DataStoreTemplate (io.fabric8.api.DataStoreTemplate)3 ProfileBuilder (io.fabric8.api.ProfileBuilder)3 RuntimeProperties (io.fabric8.api.RuntimeProperties)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 Container (io.fabric8.api.Container)2 FabricException (io.fabric8.api.FabricException)2 GitContext (io.fabric8.api.GitContext)2