<commit_before>/*
 * The MIT License
 *
 * Copyright (c) 2004-, all the contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jenkinsci.plugins.ovirt;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.framework.io.IOException2;
import org.ovirt.engine.sdk.decorators.VM;
import org.ovirt.engine.sdk.entities.IP;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.trilead.ssh2.ChannelCondition;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.SCPClient;
import com.trilead.ssh2.SFTPv3FileAttributes;
import com.trilead.ssh2.Session;
import hudson.AbortException;
import hudson.Extension;
import hudson.model.Descriptor;
import hudson.model.Slave;
import hudson.model.TaskListener;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.SlaveComputer;
import hudson.util.NamingThreadFactory;
import hudson.util.NullStream;

/**
 * Part of code taken from ssh slaves plugin
 */
public class OVirtSshLauncher extends ComputerLauncher {

    // TODO: use credentials
    private String username;

    private String password;

    // TODO: Don't hardcode these values
    private int launchTimeout = 300000;
    private int maxRetries = 5;
    private int retryWaitTime = 30;

    /**
     * SSH connection to the slave.
     */
    private transient Connection connection;

    @DataBoundConstructor
    public OVirtSshLauncher(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    /**
     * This method is called when the node is about to be used.
     * So what it will do (in theory) is to start the vm (if
     * needed), then find a way to link to the vm, and run slave.jar on the vm
     *
     * @param slaveComputer the node to be launched
     * @param taskListener  listener
     */
    @Override
    public synchronized void launch(final SlaveComputer slaveComputer, final TaskListener taskListener)
            throws IOException, InterruptedException {

        OVirtVMSlave slave = (OVirtVMSlave) slaveComputer.getNode();
        String hypervisor = slave.getHypervisorDescription();
        String vmName = slave.getVirtualMachineName();

        String ip = null;

        VM vm = OVirtHypervisor.find(hypervisor).getVM(vmName);
        for (int i = 0; i < maxRetries; i++) {
            try {
                List<IP> ips = vm.getGuestInfo().getIps().getIPs();

                if (ips.size() >= 1) {
                    // use first IP to connect via ssh
                    ip = ips.get(0).getAddress();
                    taskListener.getLogger().println("IP of VM Obtained! " + ip);
                    break;
                }
            } catch (NullPointerException e) {
                taskListener.error("Couldn't get IP address of VM.. retrying in " + retryWaitTime + " s");
                Thread.sleep(TimeUnit.SECONDS.toMillis(retryWaitTime));
            }
        }
        if (ip == null) {
            throw new InterruptedException("Couldn't find IP address of VM. Abandoning...");
        }
        connection = new Connection(ip, 22);<commit_msg>Request new vm object if IP not obtained<commit_after>/*
 * The MIT License
 *
 * Copyright (c) 2004-, all the contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jenkinsci.plugins.ovirt;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.framework.io.IOException2;
import org.ovirt.engine.sdk.decorators.VM;
import org.ovirt.engine.sdk.entities.IP;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.trilead.ssh2.ChannelCondition;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.SCPClient;
import com.trilead.ssh2.SFTPv3FileAttributes;
import com.trilead.ssh2.Session;
import hudson.AbortException;
import hudson.Extension;
import hudson.model.Descriptor;
import hudson.model.Slave;
import hudson.model.TaskListener;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.SlaveComputer;
import hudson.util.NamingThreadFactory;
import hudson.util.NullStream;

/**
 * Part of code taken from ssh slaves plugin
 */
public class OVirtSshLauncher extends ComputerLauncher {

    // TODO: use credentials
    private String username;

    private String password;

    // TODO: Don't hardcode these values
    private int launchTimeout = 300000;
    private int maxRetries = 5;
    private int retryWaitTime = 30;

    /**
     * SSH connection to the slave.
     */
    private transient Connection connection;

    @DataBoundConstructor
    public OVirtSshLauncher(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    /**
     * This method is called when the node is about to be used.
     * So what it will do (in theory) is to start the vm (if
     * needed), then find a way to link to the vm, and run slave.jar on the vm
     *
     * @param slaveComputer the node to be launched
     * @param taskListener  listener
     */
    @Override
    public synchronized void launch(final SlaveComputer slaveComputer, final TaskListener taskListener)
            throws IOException, InterruptedException {

        OVirtVMSlave slave = (OVirtVMSlave) slaveComputer.getNode();
        String hypervisor = slave.getHypervisorDescription();
        String vmName = slave.getVirtualMachineName();

        String ip = null;

        for (int i = 0; i < maxRetries; i++) {
            try {
                VM vm = OVirtHypervisor.find(hypervisor).getVM(vmName);
                List<IP> ips = vm.getGuestInfo().getIps().getIPs();

                if (ips.size() >= 1) {
                    // use first IP to connect via ssh
                    ip = ips.get(0).getAddress();
                    taskListener.getLogger().println("IP of VM Obtained! " + ip);
                    break;
                }
            } catch (NullPointerException e) {
                taskListener.error("Couldn't get IP address of VM.. retrying in " + retryWaitTime + " s");
                Thread.sleep(TimeUnit.SECONDS.toMillis(retryWaitTime));
            }
        }
        if (ip == null) {
            throw new InterruptedException("Couldn't find IP address of VM. Abandoning...");
        }
        connection = new Connection(ip, 22);
{'fraction_non_alphanumeric': 0.08370091447014524, 'fraction_numerical': 0.0053792361484669175, 'mean_word_length': 3.138913624220837, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6035688', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2467, 'n_words': 882}