Search in sources :

Example 76 with Group

use of io.fabric8.kubernetes.model.annotation.Group in project fabric8 by jboss-fuse.

the class GroupTest method testOrder.

@Test
public void testOrder() throws Exception {
    int port = findFreePort();
    CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryNTimes(10, 100)).build();
    curator.start();
    final String path = "/singletons/test/Order" + System.currentTimeMillis();
    ArrayList<ZooKeeperGroup> members = new ArrayList<ZooKeeperGroup>();
    for (int i = 0; i < 4; i++) {
        ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, path, NodeState.class);
        group.add(listener);
        members.add(group);
    }
    for (ZooKeeperGroup group : members) {
        assertFalse(group.isConnected());
        assertFalse(group.isMaster());
    }
    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    // first to start should be master if members are ordered...
    int i = 0;
    for (ZooKeeperGroup group : members) {
        group.start();
        group.update(new NodeState("foo" + i));
        i++;
        // wait for registration
        while (group.getId() == null) {
            TimeUnit.MILLISECONDS.sleep(100);
        }
    }
    boolean firsStartedIsMaster = members.get(0).isMaster();
    for (ZooKeeperGroup group : members) {
        group.close();
    }
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
    assertTrue("first started is master", firsStartedIsMaster);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) ArrayList(java.util.ArrayList) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) Test(org.junit.Test)

Example 77 with Group

use of io.fabric8.kubernetes.model.annotation.Group in project fabric8 by jboss-fuse.

the class MQManager method addMasterSlaveStatus.

protected void addMasterSlaveStatus(List<MQBrokerStatusDTO> answer) throws Exception {
    Map<String, Map<String, MQBrokerStatusDTO>> groupMap = new HashMap<String, Map<String, MQBrokerStatusDTO>>();
    for (MQBrokerStatusDTO status : answer) {
        String key = status.getGroup();
        Map<String, MQBrokerStatusDTO> list = groupMap.get(key);
        if (list == null) {
            list = new HashMap<String, MQBrokerStatusDTO>();
            groupMap.put(key, list);
        }
        String statusPath = String.format("%s/%s", status.getContainer(), status.getBrokerName());
        list.put(statusPath, status);
    }
    CuratorFramework curator = getCurator();
    // now lets check the cluster status for each group
    Set<Map.Entry<String, Map<String, MQBrokerStatusDTO>>> entries = groupMap.entrySet();
    for (Map.Entry<String, Map<String, MQBrokerStatusDTO>> entry : entries) {
        String group = entry.getKey();
        Map<String, MQBrokerStatusDTO> containerMap = entry.getValue();
        String groupPath = ZkPath.MQ_CLUSTER.getPath(group);
        List<String> children = getChildrenSafe(curator, groupPath);
        for (String child : children) {
            String childPath = groupPath + "/" + child;
            byte[] data = curator.getData().forPath(childPath);
            if (data != null && data.length > 0) {
                String text = new String(data).trim();
                if (!text.isEmpty()) {
                    ObjectMapper mapper = new ObjectMapper();
                    Map<String, Object> map = mapper.readValue(data, HashMap.class);
                    String id = stringValue(map, "id", "container");
                    if (id != null) {
                        String container = stringValue(map, "container", "agent");
                        String statusPath = String.format("%s/%s", container, id);
                        MQBrokerStatusDTO containerStatus = containerMap.get(statusPath);
                        if (containerStatus != null) {
                            Boolean master = null;
                            List services = listValue(map, "services");
                            if (services != null) {
                                if (!services.isEmpty()) {
                                    List<String> serviceTexts = new ArrayList<String>();
                                    for (Object service : services) {
                                        String serviceText = getSubstitutedData(curator, service.toString());
                                        if (Strings.isNotBlank(serviceText)) {
                                            serviceTexts.add(serviceText);
                                        }
                                        containerStatus.setServices(serviceTexts);
                                    }
                                    master = Boolean.TRUE;
                                } else {
                                    master = Boolean.FALSE;
                                }
                            } else {
                                master = Boolean.FALSE;
                            }
                            containerStatus.setMaster(master);
                        }
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MQBrokerStatusDTO(io.fabric8.api.jmx.MQBrokerStatusDTO) CuratorFramework(org.apache.curator.framework.CuratorFramework) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 78 with Group

use of io.fabric8.kubernetes.model.annotation.Group in project fabric8 by jboss-fuse.

the class MQManager method createOrUpdateProfile.

/**
 * Creates or updates the broker profile for the given DTO and updates the requirements so that the
 * minimum number of instances of the profile is updated
 */
public static Profile createOrUpdateProfile(MQBrokerConfigDTO dto, FabricService fabricService, RuntimeProperties runtimeProperties) throws IOException {
    FabricRequirements requirements = fabricService.getRequirements();
    MQService mqService = createMQService(fabricService, runtimeProperties);
    Map<String, String> configuration = new HashMap<String, String>();
    List<String> properties = dto.getProperties();
    String version = dto.version();
    boolean changeInCurrentVersion = requirements.getVersion().equals(dto.getVersion());
    if (properties != null) {
        for (String entry : properties) {
            String[] parts = entry.split("=", 2);
            if (parts.length == 2) {
                configuration.put(parts[0], parts[1]);
            } else {
                configuration.put(parts[0], "");
            }
        }
    }
    String data = dto.getData();
    String profileName = dto.profile();
    try {
        FabricValidations.validateProfileName(profileName);
    } catch (IllegalArgumentException e) {
        // we do not want exception in the server log, so print the error message to the console
        System.out.println(e.getMessage());
        return null;
    }
    String brokerName = dto.getBrokerName();
    if (data == null) {
        // lets use a relative path so we work on any karaf container
        data = "${runtime.data}" + brokerName;
    }
    configuration.put(DATA, data);
    for (Map.Entry<String, String> port : dto.getPorts().entrySet()) {
        configuration.put(port.getKey() + "-port", port.getValue());
    }
    BrokerKind kind = dto.kind();
    configuration.put(KIND, kind.toString());
    String config = dto.getConfigUrl();
    if (config != null) {
        configuration.put(CONFIG_URL, mqService.getConfig(version, config));
    }
    String group = dto.getGroup();
    if (group != null) {
        configuration.put(GROUP, group);
    }
    Maps.setStringValues(configuration, NETWORKS, dto.getNetworks());
    String networksUserName = dto.getNetworksUserName();
    if (networksUserName != null) {
        configuration.put(NETWORK_USER_NAME, networksUserName);
    }
    String networksPassword = dto.getNetworksPassword();
    if (networksPassword != null) {
        configuration.put(NETWORK_PASSWORD, networksPassword);
    }
    String parentProfile = dto.getParentProfile();
    if (parentProfile != null) {
        configuration.put(PARENT, parentProfile);
    }
    Boolean ssl = dto.getSsl();
    if (ssl != null) {
        configuration.put(SSL, ssl.toString());
    }
    Integer replicas = dto.getReplicas();
    if (replicas != null) {
        configuration.put(REPLICAS, replicas.toString());
    }
    Integer minInstances = dto.getMinimumInstances();
    if (minInstances != null) {
        configuration.put(MINIMUM_INSTANCES, minInstances.toString());
    }
    Profile profile = mqService.createOrUpdateMQProfile(version, profileName, brokerName, configuration, dto.kind().equals(BrokerKind.Replicated));
    String profileId = profile.getId();
    ProfileRequirements profileRequirement = requirements.getOrCreateProfileRequirement(profileId);
    Integer minimumInstances = profileRequirement.getMinimumInstances();
    // lets reload the DTO as we may have inherited some values from the parent profile
    List<MQBrokerConfigDTO> list = createConfigDTOs(mqService, profile);
    // lets assume 2 required instances for master/slave unless folks use
    // N+1 or replicated
    int requiredInstances = 2;
    if (list.size() == 1) {
        MQBrokerConfigDTO loadedDTO = list.get(0);
        requiredInstances = loadedDTO.requiredInstances();
    } else {
        // assume N+1 broker as there's more than one broker in the profile; so lets set the required size to N+1
        requiredInstances = list.size() + 1;
    }
    if (changeInCurrentVersion && (minimumInstances == null || minimumInstances.intValue() < requiredInstances)) {
        profileRequirement.setMinimumInstances(requiredInstances);
        fabricService.setRequirements(requirements);
    }
    String clientProfile = dto.clientProfile();
    if (Strings.isNotBlank(clientProfile)) {
        String clientParentProfile = dto.getClientParentProfile();
        if (Strings.isNullOrBlank(clientParentProfile)) {
            clientParentProfile = "mq-client-base";
        }
        mqService.createOrUpdateMQClientProfile(version, clientProfile, group, clientParentProfile);
    }
    return profile;
}
Also used : MQService(io.fabric8.api.MQService) ProfileRequirements(io.fabric8.api.ProfileRequirements) BrokerKind(io.fabric8.api.jmx.BrokerKind) HashMap(java.util.HashMap) Profile(io.fabric8.api.Profile) MQBrokerConfigDTO(io.fabric8.api.jmx.MQBrokerConfigDTO) FabricRequirements(io.fabric8.api.FabricRequirements) Map(java.util.Map) HashMap(java.util.HashMap)

Example 79 with Group

use of io.fabric8.kubernetes.model.annotation.Group in project fabric8 by jboss-fuse.

the class MQServiceImpl method createOrUpdateMQClientProfile.

@Override
public Profile createOrUpdateMQClientProfile(String versionId, String profileId, String group, String parentProfileName) {
    Version version = profileService.getRequiredVersion(versionId);
    Profile parentProfile = null;
    if (Strings.isNotBlank(parentProfileName)) {
        parentProfile = version.getRequiredProfile(parentProfileName);
    }
    if (group == null || profileId == null)
        return parentProfile;
    ProfileBuilder builder;
    // create a profile if it doesn't exist
    boolean create = !version.hasProfile(profileId);
    if (create) {
        builder = ProfileBuilder.Factory.create(versionId, profileId);
    } else {
        Profile profile = version.getRequiredProfile(profileId);
        builder = ProfileBuilder.Factory.createFrom(profile);
    }
    // set the parent if its specified
    if (parentProfile != null) {
        builder.addParent(parentProfile.getId());
    }
    Map<String, String> config = builder.getConfiguration(MQ_CONNECTION_FACTORY_PID);
    config.put(GROUP, group);
    builder.addConfiguration(MQ_CONNECTION_FACTORY_PID, config);
    Profile profile = builder.getProfile();
    return create ? profileService.createProfile(profile) : profileService.updateProfile(profile);
}
Also used : Version(io.fabric8.api.Version) ProfileBuilder(io.fabric8.api.ProfileBuilder) Profile(io.fabric8.api.Profile)

Example 80 with Group

use of io.fabric8.kubernetes.model.annotation.Group 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)

Aggregations

Test (org.junit.jupiter.api.Test)32 Test (org.junit.Test)29 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)17 Map (java.util.Map)16 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 List (java.util.List)14 File (java.io.File)13 HashMap (java.util.HashMap)13 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)12 Collectors (java.util.stream.Collectors)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)10 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)9 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)9 CuratorFramework (org.apache.curator.framework.CuratorFramework)9 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)8 Pod (io.fabric8.kubernetes.api.model.Pod)8 KubernetesClientBuilder (io.fabric8.kubernetes.client.KubernetesClientBuilder)8 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)8