Search in sources :

Example 6 with Session

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

the class MQCreateAction method doExecute.

@Override
protected Object doExecute() throws Exception {
    MQBrokerConfigDTO dto = createDTO();
    Profile profile = MQManager.createOrUpdateProfile(dto, fabricService, runtimeProperties);
    if (profile == null) {
        return null;
    }
    String profileId = profile.getId();
    System.out.println("MQ profile " + profileId + " ready");
    // assign profile to existing containers
    if (assign != null) {
        String[] assignContainers = assign.split(",");
        MQManager.assignProfileToContainers(fabricService, profile, assignContainers);
    }
    // create containers
    if (create != null) {
        String[] createContainers = create.split(",");
        List<CreateContainerBasicOptions.Builder> builderList = MQManager.createContainerBuilders(dto, fabricService, "child", profileId, dto.version(), createContainers);
        for (CreateContainerBasicOptions.Builder builder : builderList) {
            CreateContainerMetadata[] metadatas;
            try {
                if (builder instanceof CreateChildContainerOptions.Builder) {
                    CreateChildContainerOptions.Builder childBuilder = (CreateChildContainerOptions.Builder) builder;
                    builder = childBuilder.jmxUser(username).jmxPassword(password);
                }
                metadatas = fabricService.createContainers(builder.build());
                // check if there was a FabricAuthenticationException as failure then we can try again
                if (metadatas != null) {
                    for (CreateContainerMetadata meta : metadatas) {
                        if (meta.getFailure() != null && meta.getFailure() instanceof FabricAuthenticationException) {
                            throw (FabricAuthenticationException) meta.getFailure();
                        }
                    }
                }
                ShellUtils.storeFabricCredentials(session, username, password);
            } catch (FabricAuthenticationException fae) {
                // If authentication fails, prompts for credentials and try again.
                if (builder instanceof CreateChildContainerOptions.Builder) {
                    CreateChildContainerOptions.Builder childBuilder = (CreateChildContainerOptions.Builder) builder;
                    promptForJmxCredentialsIfNeeded();
                    metadatas = fabricService.createContainers(childBuilder.jmxUser(username).jmxPassword(password).build());
                    ShellUtils.storeFabricCredentials(session, username, password);
                }
            }
        }
    }
    return null;
}
Also used : FabricAuthenticationException(io.fabric8.api.FabricAuthenticationException) CreateChildContainerOptions(io.fabric8.api.CreateChildContainerOptions) CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) Profile(io.fabric8.api.Profile) MQBrokerConfigDTO(io.fabric8.api.jmx.MQBrokerConfigDTO) CreateContainerBasicOptions(io.fabric8.api.CreateContainerBasicOptions)

Example 7 with Session

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

the class PatchApplyAction method doExecute.

@Override
protected Object doExecute() throws Exception {
    List<Version> versions;
    ProfileService profileService = fabricService.adapt(ProfileService.class);
    if (versionId != null && !versionId.isEmpty()) {
        Version version = profileService.getRequiredVersion(versionId);
        versions = Collections.singletonList(version);
    } else if (allVersions) {
        versions = new ArrayList<>();
        for (String versionId : profileService.getVersions()) {
            versions.add(profileService.getRequiredVersion(versionId));
        }
    } else {
        versions = Collections.singletonList(fabricService.getRequiredDefaultVersion());
    }
    username = username != null && !username.isEmpty() ? username : ShellUtils.retrieveFabricUser(session);
    password = password != null ? password : ShellUtils.retrieveFabricUserPassword(session);
    promptForJmxCredentialsIfNeeded();
    for (Version version : versions) {
        fabricService.getPatchService().applyPatch(version, patch, username, password);
    }
    return null;
}
Also used : ProfileService(io.fabric8.api.ProfileService) Version(io.fabric8.api.Version) ArrayList(java.util.ArrayList)

Example 8 with Session

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

the class SshContainerProvider method start.

@Override
public void start(Container container) {
    CreateContainerMetadata metadata = container.getMetadata();
    if (!(metadata instanceof CreateSshContainerMetadata)) {
        throw new IllegalStateException("Container doesn't have valid create container metadata type");
    } else {
        CreateSshContainerMetadata sshContainerMetadata = (CreateSshContainerMetadata) metadata;
        CreateSshContainerOptions options = sshContainerMetadata.getCreateOptions();
        Session session = null;
        try {
            String script = buildStartScript(container.getId(), options);
            session = createSession(options);
            runScriptOnHost(session, script);
        } catch (Throwable t) {
            LOGGER.error("Failed to start container: " + container.getId(), t);
            throw new FabricException(t);
        } finally {
            if (session != null) {
                session.disconnect();
            }
        }
    }
}
Also used : CreateContainerMetadata(io.fabric8.api.CreateContainerMetadata) FabricException(io.fabric8.api.FabricException) Session(com.jcraft.jsch.Session)

Example 9 with Session

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

the class SshContainerProvider method uploadTo.

protected void uploadTo(Session session, URL url, String path) {
    Channel channel = null;
    try (InputStream is = url.openStream()) {
        channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        final CountDownLatch uploadLatch = new CountDownLatch(1);
        sftpChannel.put(is, path, new SftpProgressMonitor() {

            @Override
            public void init(int op, String src, String dest, long max) {
            }

            @Override
            public boolean count(long count) {
                try {
                    return is.available() > 0;
                } catch (IOException e) {
                    return false;
                }
            }

            @Override
            public void end() {
                uploadLatch.countDown();
            }
        }, ChannelSftp.OVERWRITE);
        uploadLatch.await(10, TimeUnit.MINUTES);
    } catch (Exception e) {
        LOGGER.warn("Failed to upload. Will attempt downloading distribution via maven.");
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) SftpProgressMonitor(com.jcraft.jsch.SftpProgressMonitor) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException)

Example 10 with Session

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

the class SshContainerProvider method createSession.

protected Session createSession(CreateSshContainerOptions options) throws Exception {
    Session session = null;
    Exception connectException = null;
    for (int i = 0; i <= options.getSshRetries(); i++) {
        if (i > 0) {
            long delayMs = (long) (200L * Math.pow(i, 2));
            Thread.sleep(delayMs);
        }
        try {
            JSch jsch = new JSch();
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            byte[] privateKey = readFile(options.getPrivateKeyFile());
            byte[] passPhrase = options.getPassPhrase() != null ? options.getPassPhrase().getBytes() : null;
            if (privateKey != null && options.getPassword() == null) {
                jsch.addIdentity(options.getUsername(), privateKey, null, passPhrase);
                session = jsch.getSession(options.getUsername(), options.getHost(), options.getPort());
                config.put("PreferredAuthentications", "publickey");
            } else {
                session = jsch.getSession(options.getUsername(), options.getHost(), options.getPort());
                session.setPassword(options.getPassword());
                config.put("PreferredAuthentications", "password,keyboard-interactive");
            }
            session.setTimeout(60000);
            session.setConfig(config);
            session.connect();
            connectException = null;
            break;
        } catch (Exception from) {
            connectException = from;
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
            session = null;
        }
    }
    if (connectException != null) {
        throw connectException;
    }
    return session;
}
Also used : JSch(com.jcraft.jsch.JSch) Properties(org.apache.felix.scr.annotations.Properties) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException) Session(com.jcraft.jsch.Session)

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