Search in sources :

Example 36 with Session

use of io.fabric8.arquillian.kubernetes.Session in project fabric8 by jboss-fuse.

the class MQCreateAction method createDTO.

private MQBrokerConfigDTO createDTO() {
    if (Strings.isNullOrBlank(username)) {
        username = ShellUtils.retrieveFabricUser(session);
    }
    if (Strings.isNullOrBlank(password)) {
        password = ShellUtils.retrieveFabricUserPassword(session);
    }
    MQBrokerConfigDTO dto = new MQBrokerConfigDTO();
    if (config != null) {
        dto.setConfigUrl(config);
    } else {
        if (nossl) {
            dto.setConfigUrl("broker.xml");
        } else {
            dto.setConfigUrl("ssl-broker.xml");
        }
    }
    dto.setData(data);
    if (ports != null && ports.length > 0) {
        for (String port : ports) {
            addConfig(port, dto.getPorts());
        }
    }
    dto.setGroup(group);
    dto.setJvmOpts(jvmOpts);
    dto.setBrokerName(name);
    dto.setProfile(profile);
    dto.setClientProfile(clientProfile);
    dto.setClientParentProfile(clientParentProfile);
    dto.setNetworks(networks);
    dto.setNetworksPassword(networksPassword);
    dto.setNetworksUserName(networksUserName);
    dto.setParentProfile(parentProfile);
    dto.setProperties(properties);
    if (version == null) {
        version = fabricService.getDefaultVersionId();
    }
    dto.setVersion(version);
    dto.setMinimumInstances(minimumInstances);
    dto.setReplicas(replicas);
    dto.setSsl(!nossl);
    dto.setKind(kind);
    return dto;
}
Also used : MQBrokerConfigDTO(io.fabric8.api.jmx.MQBrokerConfigDTO)

Example 37 with Session

use of io.fabric8.arquillian.kubernetes.Session in project fabric8 by jboss-fuse.

the class GroupTest method testGroupClose.

// Tests that if close() is executed right after start(), there are no left over entries.
// (see  https://github.com/jboss-fuse/fuse/issues/133)
@Test
public void testGroupClose() throws Exception {
    int port = findFreePort();
    NIOServerCnxnFactory cnxnFactory = startZooKeeper(port);
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + port).connectionTimeoutMs(6000).sessionTimeoutMs(6000).retryPolicy(new RetryNTimes(10, 100));
    CuratorFramework curator = builder.build();
    curator.start();
    curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    String groupNode = "/singletons/test" + System.currentTimeMillis();
    curator.create().creatingParentsIfNeeded().forPath(groupNode);
    for (int i = 0; i < 100; i++) {
        ZooKeeperGroup<NodeState> group = new ZooKeeperGroup<NodeState>(curator, groupNode, NodeState.class);
        group.add(listener);
        group.update(new NodeState("foo"));
        group.start();
        group.close();
        List<String> entries = curator.getChildren().forPath(groupNode);
        assertTrue(entries.isEmpty() || group.isUnstable());
        if (group.isUnstable()) {
            // let's wait for session timeout
            curator.close();
            curator = builder.build();
            curator.start();
            curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
        }
    }
    curator.close();
    cnxnFactory.shutdown();
    cnxnFactory.join();
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) Test(org.junit.Test)

Example 38 with Session

use of io.fabric8.arquillian.kubernetes.Session 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 39 with Session

use of io.fabric8.arquillian.kubernetes.Session in project fabric8 by jboss-fuse.

the class ContainerConnectAction method doExecute.

protected Object doExecute() throws Exception {
    validateContainerName(container);
    String cmdStr = "";
    if (command != null) {
        StringBuilder sb = new StringBuilder();
        for (String cmd : command) {
            if (sb.length() > 0) {
                sb.append(' ');
            }
            sb.append(cmd);
        }
        sb.append("\n");
        cmdStr = sb.toString();
    }
    Container found = FabricCommand.getContainer(fabricService, container);
    String sshUrl = found.getSshUrl();
    if (sshUrl == null) {
        throw new IllegalArgumentException("Container " + container + " has no SSH URL.");
    }
    String[] ssh = sshUrl.split(":");
    if (ssh.length < 2) {
        throw new IllegalArgumentException("Container " + container + " has an invalid SSH URL '" + sshUrl + "'");
    }
    username = username != null && !username.isEmpty() ? username : ShellUtils.retrieveFabricUser(session);
    password = password != null ? password : ShellUtils.retrieveFabricUserPassword(session);
    try {
        executSshCommand(session, username, password, ssh[0], ssh[1], cmdStr);
    } catch (FabricAuthenticationException ex) {
        username = null;
        password = null;
        promptForSshCredentialsIfNeeded();
        executSshCommand(session, username, password, ssh[0], ssh[1], cmdStr);
    }
    return null;
}
Also used : Container(io.fabric8.api.Container) FabricAuthenticationException(io.fabric8.api.FabricAuthenticationException)

Example 40 with Session

use of io.fabric8.arquillian.kubernetes.Session in project fabric8 by jboss-fuse.

the class SshContainerProvider method runScriptOnHost.

protected void runScriptOnHost(Session session, String script) throws Exception {
    ChannelExec executor = null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ByteArrayOutputStream error = new ByteArrayOutputStream();
    try {
        executor = (ChannelExec) session.openChannel("exec");
        executor.setPty(true);
        executor.setCommand(script);
        executor.setOutputStream(output);
        executor.setErrStream(error);
        executor.connect();
        int errorStatus = -1;
        for (int i = 0; !executor.isClosed(); i++) {
            if (i > 0) {
                long delayMs = (long) (200L * Math.pow(i, 2));
                Thread.sleep(delayMs);
            }
            if ((errorStatus = executor.getExitStatus()) != -1) {
                break;
            }
        }
        LOGGER.debug("Output: {}", output.toString());
        LOGGER.debug("Error:  {}", error.toString());
        if (verbose) {
            System.out.println("Output : " + output.toString());
            System.out.println("Error : " + error.toString());
        }
        if (errorStatus != 0) {
            throw new Exception(String.format("%s@%s:%d: received exit status %d executing \n--- command ---\n%s\n--- output ---\n%s\n--- error ---\n%s\n------\n", session.getUserName(), session.getHost(), session.getPort(), executor.getExitStatus(), script, output.toString(), error.toString()));
        }
    } finally {
        if (executor != null) {
            executor.disconnect();
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChannelExec(com.jcraft.jsch.ChannelExec) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)14 Session (io.fabric8.arquillian.kubernetes.Session)8 FabricException (io.fabric8.api.FabricException)7 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)7 Test (org.junit.Test)6 Session (com.jcraft.jsch.Session)5 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)5 File (java.io.File)5 Logger (io.fabric8.arquillian.kubernetes.log.Logger)4 Pod (io.fabric8.kubernetes.api.model.Pod)4 Service (io.fabric8.kubernetes.api.model.Service)4 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)4 ArrayList (java.util.ArrayList)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 FabricAuthenticationException (io.fabric8.api.FabricAuthenticationException)3 Util.readAsString (io.fabric8.arquillian.utils.Util.readAsString)3 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)3 GeneratorContext (io.fabric8.maven.generator.api.GeneratorContext)3 MultiException (io.fabric8.utils.MultiException)3 HashMap (java.util.HashMap)3