Search in sources :

Example 6 with CreateEnsembleOptions

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

the class ZooKeeperClusterBootstrapImpl method create.

@Override
public void create(CreateEnsembleOptions options) {
    assertValid();
    try {
        // Wait for bootstrap to be complete
        ServiceLocator.awaitService(BootstrapComplete.class);
        LOGGER.info("Create fabric with: {}", options);
        stopBundles();
        BundleContext syscontext = bundleContext.getBundle(0).getBundleContext();
        long bootstrapTimeout = options.getBootstrapTimeout();
        RuntimeProperties runtimeProps = runtimeProperties.get();
        BootstrapConfiguration bootConfig = bootstrapConfiguration.get();
        if (options.isClean()) {
            bootConfig = cleanInternal(syscontext, bootConfig, runtimeProps);
        }
        // before we start fabric, register CM listener that'll mark end of work of FabricConfigAdminBridge
        final CountDownLatch fcabLatch = new CountDownLatch(1);
        final ServiceRegistration<ConfigurationListener> registration = bundleContext.registerService(ConfigurationListener.class, new ConfigurationListener() {

            @Override
            public void configurationEvent(ConfigurationEvent event) {
                if (event.getType() == ConfigurationEvent.CM_UPDATED && event.getPid() != null && event.getPid().equals(Constants.CONFIGADMIN_BRIDGE_PID)) {
                    fcabLatch.countDown();
                }
            }
        }, null);
        BootstrapCreateHandler createHandler = new BootstrapCreateHandler(syscontext, bootConfig, runtimeProps);
        createHandler.bootstrapFabric(name, homeDir, options);
        startBundles(options);
        long startTime = System.currentTimeMillis();
        ServiceLocator.awaitService(FabricComplete.class, bootstrapTimeout, TimeUnit.MILLISECONDS);
        // FabricComplete is registered somewhere in the middle of registering CuratorFramework (SCR activates
        // it when CuratorFramework is registered), but CuratorFramework leads to activation of >100 SCR
        // components, so let's wait for new CuratorComplete service - it is registered after registration
        // of CuratorFramework finishes
        ServiceLocator.awaitService(CuratorComplete.class, bootstrapTimeout, TimeUnit.MILLISECONDS);
        CuratorFramework curatorFramework = ServiceLocator.getService(CuratorFramework.class);
        Map<String, String> dataStoreProperties = options.getDataStoreProperties();
        if (ZooKeeperUtils.exists(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL.getPath()) == null)
            ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL.getPath());
        if (dataStoreProperties != null) {
            String remoteUrl = dataStoreProperties.get(Constants.GIT_REMOTE_URL);
            if (remoteUrl != null) {
                ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_URL.getPath());
                ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_URL.getPath(), remoteUrl);
            }
            String remoteUser = dataStoreProperties.get("gitRemoteUser");
            if (remoteUser != null) {
                ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_USER.getPath());
                ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_USER.getPath(), remoteUser);
            }
            String remotePassword = dataStoreProperties.get("gitRemotePassword");
            if (remotePassword != null) {
                ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_PASSWORD.getPath());
                ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_PASSWORD.getPath(), remotePassword);
            }
        }
        // HttpService is registered differently. not as SCR component activation, but after
        // FabricConfigAdminBridge updates (or doesn't touch) org.ops4j.pax.web CM configuration
        // however in fabric, this PID contains URL to jetty configuration in the form "profile:jetty.xml"
        // so we have to have FabricService and ProfileUrlHandler active
        // some ARQ (single container instance) tests failed because tests ended without http service running
        // of course we have to think if all fabric instances need http service
        ServiceLocator.awaitService("org.osgi.service.http.HttpService", bootstrapTimeout, TimeUnit.MILLISECONDS);
        // and last wait - too much synchronization never hurts
        fcabLatch.await(bootstrapTimeout, TimeUnit.MILLISECONDS);
        registration.unregister();
        long timeDiff = System.currentTimeMillis() - startTime;
        createHandler.waitForContainerAlive(name, syscontext, bootstrapTimeout);
        if (options.isWaitForProvision() && options.isAgentEnabled()) {
            long currentTime = System.currentTimeMillis();
            createHandler.waitForSuccessfulDeploymentOf(name, syscontext, bootstrapTimeout - (currentTime - startTime));
        }
    } catch (RuntimeException rte) {
        throw rte;
    } catch (Exception ex) {
        throw new FabricException("Unable to create zookeeper server configuration", ex);
    }
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) FabricException(io.fabric8.api.FabricException) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException) ConfigurationListener(org.osgi.service.cm.ConfigurationListener) CuratorFramework(org.apache.curator.framework.CuratorFramework) RuntimeProperties(io.fabric8.api.RuntimeProperties) BundleContext(org.osgi.framework.BundleContext)

Example 7 with CreateEnsembleOptions

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

the class ContainerStartupTest method testLocalFabricCluster.

@Test
public void testLocalFabricCluster() throws Exception {
    Builder<?> builder = CreateEnsembleOptions.builder().agentEnabled(false).clean(true).zookeeperPassword(ADMIN_PASSWORD).waitForProvision(false);
    CreateEnsembleOptions options = builder.build();
    ZooKeeperClusterBootstrap bootstrap = ServiceLocator.getRequiredService(ZooKeeperClusterBootstrap.class);
    bootstrap.create(options);
    FabricService fabricService = ServiceLocator.getRequiredService(FabricService.class);
    Container[] containers = fabricService.getContainers();
    Assert.assertNotNull("Containers not null", containers);
    // Test that a provided default password exists
    ConfigurationAdmin configurationAdmin = ServiceLocator.getRequiredService(ConfigurationAdmin.class);
    org.osgi.service.cm.Configuration configuration = configurationAdmin.getConfiguration(io.fabric8.api.Constants.ZOOKEEPER_CLIENT_PID);
    Dictionary<String, Object> dictionary = configuration.getProperties();
    Assert.assertEquals("Expected provided zookeeper password", PasswordEncoder.encode(ADMIN_PASSWORD), dictionary.get("zookeeper.password"));
}
Also used : ZooKeeperClusterBootstrap(io.fabric8.api.ZooKeeperClusterBootstrap) CreateEnsembleOptions(io.fabric8.api.CreateEnsembleOptions) Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Example 8 with CreateEnsembleOptions

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

the class CreateAction method doExecute.

protected Object doExecute() throws Exception {
    String adminRole = "admin";
    String administratorRole = "administrator";
    String superUserRole = "superuser";
    boolean adminExists = hasRole(adminRole, superUserRole, administratorRole);
    if (!adminExists) {
        System.out.println("The fabric:create command can be executed only by admin user");
        return null;
    }
    Path propsPath = runtimeProperties.getConfPath().resolve("users.properties");
    Properties userProps = new Properties(propsPath.toFile());
    if (!adminExists && !usersPropertiesFileContainsRole(userProps, adminRole, superUserRole, administratorRole)) {
        System.out.println("The etc/user.properties file must contain at least one admin user, if no other admin exists");
        return null;
    }
    // prevent creating fabric if already created
    ServiceReference<FabricService> sref = bundleContext.getServiceReference(FabricService.class);
    FabricService fabricService = sref != null ? bundleContext.getService(sref) : null;
    if (!force && (fabricService != null && fabricService.getCurrentContainer().isEnsembleServer())) {
        System.out.println("Current container " + fabricService.getCurrentContainerName() + " is already in the current fabric ensemble. Cannot create fabric.");
        System.out.println("You can use the --force option, if you want to force re-create the fabric.");
        return null;
    }
    Configuration bootConfiguration = configAdmin.getConfiguration(BootstrapConfiguration.COMPONENT_PID, null);
    Dictionary<String, Object> bootProperties = bootConfiguration.getProperties();
    if (bootProperties == null) {
        bootProperties = new Hashtable<>();
    }
    String runtimeIdentity = runtimeProperties.getRuntimeIdentity();
    CreateEnsembleOptions.Builder<?> builder = CreateEnsembleOptions.builder().zooKeeperServerTickTime(zooKeeperTickTime).zooKeeperServerInitLimit(zooKeeperInitLimit).zooKeeperServerSyncLimit(zooKeeperSyncLimit).zooKeeperServerDataDir(zooKeeperDataDir).zookeeperSnapRetainCount(zookeeperSnapRetainCount).zookeeperPurgeInterval(zookeeperPurgeInterval).fromRuntimeProperties(runtimeProperties).bootstrapTimeout(bootstrapTimeout).waitForProvision(waitForProvisioning).autoImportEnabled(!noImport).clean(clean);
    builder.version(version);
    if (containers == null || containers.isEmpty()) {
        containers = Arrays.asList(runtimeIdentity);
    }
    if (!noImport && importDir != null) {
        builder.autoImportEnabled(true);
        builder.importPath(importDir);
    }
    if (globalResolver != null) {
        if (!isInEnum(globalResolver, ResolverPolicyEnum.class)) {
            System.out.println("The globalResolver value must be one of the following: localip, localhostname, publicip, publichostname, manualip");
            return null;
        }
        builder.globalResolver(globalResolver);
        bootProperties.put(ZkDefs.GLOBAL_RESOLVER_PROPERTY, globalResolver);
    }
    if (resolver != null) {
        if (!isInEnum(resolver, ResolverPolicyEnum.class)) {
            System.out.println("The resolver value must be one of the following: localip, localhostname, publicip, publichostname, manualip");
            return null;
        }
        builder.resolver(resolver);
        bootProperties.put(ZkDefs.LOCAL_RESOLVER_PROPERTY, resolver);
    }
    if (manualIp != null) {
        builder.manualIp(manualIp);
        bootProperties.put(ZkDefs.MANUAL_IP, manualIp);
    }
    if (bindAddress != null) {
        if (!bindAddress.contains(":")) {
            builder.bindAddress(bindAddress);
            bootProperties.put(ZkDefs.BIND_ADDRESS, bindAddress);
        } else {
            String[] parts = bindAddress.split(":");
            builder.bindAddress(parts[0]);
            builder.zooKeeperServerPort(Integer.parseInt(parts[1]));
            bootProperties.put(ZkDefs.BIND_ADDRESS, parts[0]);
        }
    }
    if (zooKeeperServerPort > 0) {
        // --zookeeper-server-port option has higher priority than
        // CreateEnsembleOptions.ZOOKEEPER_SERVER_PORT and CreateEnsembleOptions.ZOOKEEPER_SERVER_CONNECTION_PORT
        // system/runtime properties
        builder.setZooKeeperServerPort(zooKeeperServerPort);
        builder.setZooKeeperServerConnectionPort(zooKeeperServerPort);
    } else {
        int shiftedPort = Ports.mapPortToRange(2181, minimumPort, maximumPort);
        builder.setZooKeeperServerPort(shiftedPort);
        builder.setZooKeeperServerConnectionPort(shiftedPort);
    }
    // Configure External Git Repository.
    if (externalGitUrl != null) {
        builder.dataStoreProperty(Constants.GIT_REMOTE_URL, externalGitUrl);
    }
    if (externalGitUser != null) {
        builder.dataStoreProperty(GIT_REMOTE_USER, externalGitUser);
    }
    if (externalGitPassword != null) {
        builder.dataStoreProperty(GIT_REMOTE_PASSWORD, externalGitPassword);
    }
    if ((externalGitUrl != null) || (externalGitUser != null) || (externalGitPassword != null)) {
        Configuration configuration = configAdmin.getConfiguration(Constants.DATASTORE_PID);
        Dictionary<String, Object> existingProperties = configuration.getProperties();
        Map<String, String> changedProperties = builder.getDataStoreProperties();
        for (String key : changedProperties.keySet()) {
            existingProperties.put(key, changedProperties.get(key));
        }
        configuration.update(existingProperties);
    }
    if (profiles != null && profiles.size() > 0) {
        builder.profiles(profiles);
    }
    if (nonManaged) {
        builder.agentEnabled(false);
    } else {
        builder.agentEnabled(true);
    }
    builder.minimumPort(minimumPort);
    builder.maximumPort(maximumPort);
    bootProperties.put(ZkDefs.MINIMUM_PORT, String.valueOf(minimumPort));
    bootProperties.put(ZkDefs.MAXIMUM_PORT, String.valueOf(maximumPort));
    newUser = newUser != null ? newUser : ShellUtils.retrieveFabricUser(session);
    newUserPassword = newUserPassword != null ? newUserPassword : ShellUtils.retrieveFabricUserPassword(session);
    if (userProps.isEmpty()) {
        String[] credentials = promptForNewUser(newUser, newUserPassword);
        newUser = credentials[0];
        newUserPassword = credentials[1];
    } else {
        if (newUser == null || newUserPassword == null) {
            newUser = "" + userProps.keySet().iterator().next();
            newUserPassword = "" + userProps.get(newUser);
            if (newUserPassword.contains(ROLE_DELIMITER)) {
                newUserPassword = newUserPassword.substring(0, newUserPassword.indexOf(ROLE_DELIMITER));
            }
        }
        String passwordWithroles = userProps.get(newUser);
        if (passwordWithroles != null && passwordWithroles.contains(ROLE_DELIMITER)) {
            String[] infos = passwordWithroles.split(",");
            String oldUserRole = "";
            if (newUserIsAdmin(infos)) {
                newUserRole = "_g_:admin";
                oldUserRole = newUserRole;
            }
            for (int i = 1; i < infos.length; i++) {
                if (infos[i].trim().startsWith(BackingEngine.GROUP_PREFIX)) {
                    // it's a group reference
                    String groupInfo = (String) userProps.get(infos[i].trim());
                    if (groupInfo != null) {
                        String[] roles = groupInfo.split(",");
                        for (int j = 1; j < roles.length; j++) {
                            if (!roles[j].trim().equals(oldUserRole)) {
                                if (!newUserRole.isEmpty()) {
                                    newUserRole = newUserRole + ROLE_DELIMITER + roles[j].trim();
                                } else {
                                    newUserRole = roles[j].trim();
                                }
                            }
                        }
                    }
                } else {
                    // it's an user reference
                    if (!infos[i].trim().equals(oldUserRole)) {
                        if (!newUserRole.isEmpty()) {
                            newUserRole = newUserRole + ROLE_DELIMITER + infos[i].trim();
                        } else {
                            newUserRole = infos[i].trim();
                        }
                    }
                }
            }
        }
    }
    if (Strings.isNullOrEmpty(newUser)) {
        System.out.println("No user specified. Cannot create a new fabric ensemble.");
        return null;
    }
    StringBuilder sb = new StringBuilder();
    // session is unset when this is called from FMC
    if (session != null) {
        ShellUtils.storeFabricCredentials(session, newUser, newUserPassword);
    }
    if (generateZookeeperPassword) {
    // do nothing use the generated password.
    } else if (zookeeperPassword == null) {
        zookeeperPassword = PasswordEncoder.decode(runtimeProperties.getProperty(CreateEnsembleOptions.ZOOKEEPER_PASSWORD, PasswordEncoder.encode(newUserPassword)));
        builder.zookeeperPassword(zookeeperPassword);
    } else {
        builder.zookeeperPassword(zookeeperPassword);
    }
    OsgiUtils.updateCmConfigurationAndWait(bundleContext, bootConfiguration, bootProperties, bootstrapTimeout, TimeUnit.MILLISECONDS);
    String roleToAssign = newUserRole.isEmpty() ? "_g_:admin" : newUserRole;
    CreateEnsembleOptions options = builder.users(userProps).withUser(newUser, newUserPassword, roleToAssign).build();
    if (containers.size() == 1 && containers.contains(runtimeIdentity)) {
        bootstrap.create(options);
    } else {
        ServiceProxy<ZooKeeperClusterService> serviceProxy = ServiceProxy.createServiceProxy(bundleContext, ZooKeeperClusterService.class);
        try {
            serviceProxy.getService().createCluster(containers, options);
        } finally {
            serviceProxy.close();
        }
    }
    ShellUtils.storeZookeeperPassword(session, options.getZookeeperPassword());
    if (zookeeperPassword == null && !generateZookeeperPassword) {
        sb.append("Zookeeper password: (reusing users ").append(newUser).append(" password:").append(options.getZookeeperPassword()).append(")\n");
        sb.append("(You can use the --zookeeper-password / --generate-zookeeper-password option to specify one.)\n");
    } else if (generateZookeeperPassword) {
        sb.append("Generated zookeeper password:").append(options.getZookeeperPassword());
    }
    System.out.println(sb.toString());
    if (!nonManaged && !waitForProvisioning) {
        System.out.println("It may take a couple of seconds for the container to provision...");
        System.out.println("You can use the --wait-for-provisioning option, if you want this command to block until the container is provisioned.");
    }
    return null;
}
Also used : Path(java.nio.file.Path) Configuration(org.osgi.service.cm.Configuration) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) ResolverPolicyEnum(io.fabric8.boot.commands.support.ResolverPolicyEnum) Properties(org.apache.felix.utils.properties.Properties)

Example 9 with CreateEnsembleOptions

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

the class ClusterBootstrapManager method getCreateEnsembleOptions.

static CreateEnsembleOptions getCreateEnsembleOptions(RuntimeProperties sysprops, Map<String, Object> options) {
    String username = (String) options.remove("username");
    String password = (String) options.remove("password");
    String role = (String) options.remove("role");
    if (username == null || password == null || role == null) {
        throw new FabricException("Must specify an administrator username, password and administrative role when creating a fabric");
    }
    Object profileObject = options.remove("profiles");
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    CreateEnsembleOptions.Builder builder = mapper.convertValue(options, CreateEnsembleOptions.Builder.class);
    if (profileObject != null) {
        List profiles = mapper.convertValue(profileObject, List.class);
        builder.profiles(profiles);
    }
    org.apache.felix.utils.properties.Properties userProps = null;
    try {
        userProps = new org.apache.felix.utils.properties.Properties(sysprops.getConfPath().resolve("users.properties").toFile());
    } catch (IOException e) {
        userProps = new org.apache.felix.utils.properties.Properties();
    }
    if (userProps.get(username) == null) {
        userProps.put(username, password + "," + role);
    }
    CreateEnsembleOptions answer = builder.users(userProps).withUser(username, password, role).build();
    LOG.debug("Creating ensemble with options: {}", answer);
    System.setProperty(ZkDefs.GLOBAL_RESOLVER_PROPERTY, answer.getGlobalResolver());
    System.setProperty(ZkDefs.LOCAL_RESOLVER_PROPERTY, answer.getResolver());
    System.setProperty(ZkDefs.MANUAL_IP, answer.getManualIp());
    System.setProperty(ZkDefs.BIND_ADDRESS, answer.getBindAddress());
    System.setProperty(ZkDefs.MINIMUM_PORT, "" + answer.getMinimumPort());
    System.setProperty(ZkDefs.MAXIMUM_PORT, "" + answer.getMaximumPort());
    return answer;
}
Also used : FabricException(io.fabric8.api.FabricException) CreateEnsembleOptions(io.fabric8.api.CreateEnsembleOptions) IOException(java.io.IOException) RuntimeProperties(io.fabric8.api.RuntimeProperties) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 10 with CreateEnsembleOptions

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

the class ClusterServiceManager method addToCluster.

@Override
public void addToCluster(List<String> containers, Map<String, Object> options) {
    assertValid();
    RuntimeProperties sysprops = runtimeProperties.get();
    CreateEnsembleOptions createEnsembleOptions = ClusterBootstrapManager.getCreateEnsembleOptions(sysprops, options);
    addToCluster(containers, createEnsembleOptions);
}
Also used : CreateEnsembleOptions(io.fabric8.api.CreateEnsembleOptions) RuntimeProperties(io.fabric8.api.RuntimeProperties)

Aggregations

CreateEnsembleOptions (io.fabric8.api.CreateEnsembleOptions)6 RuntimeProperties (io.fabric8.api.RuntimeProperties)5 BootstrapConfiguration (io.fabric8.zookeeper.bootstrap.BootstrapConfiguration)4 FabricException (io.fabric8.api.FabricException)2 IOException (java.io.IOException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Container (io.fabric8.api.Container)1 FabricService (io.fabric8.api.FabricService)1 ZooKeeperClusterBootstrap (io.fabric8.api.ZooKeeperClusterBootstrap)1 AbstractRuntimeProperties (io.fabric8.api.scr.AbstractRuntimeProperties)1 ResolverPolicyEnum (io.fabric8.boot.commands.support.ResolverPolicyEnum)1 ComponentConfigurer (io.fabric8.service.ComponentConfigurer)1 ZkDataStoreImpl (io.fabric8.service.ZkDataStoreImpl)1 BundleUtils (io.fabric8.utils.BundleUtils)1 DataStoreBootstrapTemplate (io.fabric8.zookeeper.bootstrap.DataStoreBootstrapTemplate)1 Path (java.nio.file.Path)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeoutException (java.util.concurrent.TimeoutException)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1