Search in sources :

Example 6 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.

the class Delete method killVm.

private boolean killVm(final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    String expandedVm = vm;
    EnvVars env;
    try {
        env = run.getEnvironment(listener);
    } catch (Exception e) {
        throw new VSphereException(e);
    }
    if (run instanceof AbstractBuild) {
        // Add in matrix axes..
        env.overrideAll(((AbstractBuild) run).getBuildVariables());
        expandedVm = env.expand(vm);
    }
    VSphereLogger.vsLogger(jLogger, "Destroying VM \"" + expandedVm + ".\" Please wait ...");
    vsphere.destroyVm(expandedVm, failOnNoExist);
    VSphereLogger.vsLogger(jLogger, "Destroyed!");
    return true;
}
Also used : PrintStream(java.io.PrintStream) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException)

Example 7 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.

the class Deploy method deployFromTemplate.

private boolean deployFromTemplate(final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    String expandedClone = clone;
    String expandedTemplate = template;
    String expandedCluster = cluster;
    String expandedDatastore = datastore;
    String expandedFolder = folder;
    String expandedCustomizationSpec = customizationSpec;
    EnvVars env;
    try {
        env = run.getEnvironment(listener);
    } catch (Exception e) {
        throw new VSphereException(e);
    }
    if (run instanceof AbstractBuild) {
        // Add in matrix axes..
        env.overrideAll(((AbstractBuild) run).getBuildVariables());
        expandedClone = env.expand(clone);
        expandedTemplate = env.expand(template);
        expandedCluster = env.expand(cluster);
        expandedDatastore = env.expand(datastore);
        expandedFolder = env.expand(folder);
        expandedCustomizationSpec = env.expand(customizationSpec);
    }
    String resourcePoolName;
    if ("".equals(resourcePool) || (resourcePool.length() == 0)) {
        // Not all installations are using resource pools. But there is always a hidden "Resources" resource
        // pool, even if not visible in the vSphere Client.
        resourcePoolName = "Resources";
    } else {
        resourcePoolName = env.expand(resourcePool);
    }
    vsphere.deployVm(expandedClone, expandedTemplate, linkedClone, resourcePoolName, expandedCluster, expandedDatastore, expandedFolder, powerOn, expandedCustomizationSpec, jLogger);
    VSphereLogger.vsLogger(jLogger, "\"" + expandedClone + "\" successfully deployed!");
    if (!powerOn) {
        // don't try to obtain IP if VM isn't being turned on.
        return true;
    }
    final int timeoutInSecondsForGetIp = getTimeoutInSeconds();
    if (timeoutInSecondsForGetIp <= 0) {
        // don't try to obtain IP if disabled
        return true;
    }
    VSphereLogger.vsLogger(jLogger, "Trying to get the IP address of \"" + expandedClone + "\" for the next " + timeoutInSecondsForGetIp + " seconds.");
    IP = vsphere.getIp(vsphere.getVmByName(expandedClone), timeoutInSecondsForGetIp);
    if (IP != null) {
        VSphereLogger.vsLogger(jLogger, "Successfully retrieved IP for \"" + expandedClone + "\" : " + IP);
        VSphereLogger.vsLogger(jLogger, "Exposing " + IP + " as environment variable VSPHERE_IP");
        if (run instanceof AbstractBuild) {
            VSphereEnvAction envAction = new VSphereEnvAction();
            envAction.add("VSPHERE_IP", IP);
            run.addAction(envAction);
        }
        return true;
    } else {
        VSphereLogger.vsLogger(jLogger, "Error: Timed out after waiting " + timeoutInSecondsForGetIp + " seconds to get IP for \"" + expandedClone + "\" ");
        return false;
    }
}
Also used : PrintStream(java.io.PrintStream) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException)

Example 8 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.

the class Clone method cloneFromSource.

private boolean cloneFromSource(final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    String expandedClone = clone;
    String expandedSource = sourceName;
    String expandedCluster = cluster;
    String expandedDatastore = datastore;
    String expandedFolder = folder;
    String expandedResourcePool = resourcePool;
    String expandedCustomizationSpec = customizationSpec;
    EnvVars env;
    try {
        env = run.getEnvironment(listener);
    } catch (Exception e) {
        throw new VSphereException(e);
    }
    if (run instanceof AbstractBuild) {
        // Add in matrix axes..
        env.overrideAll(((AbstractBuild) run).getBuildVariables());
        expandedClone = env.expand(clone);
        expandedSource = env.expand(sourceName);
        expandedCluster = env.expand(cluster);
        expandedDatastore = env.expand(datastore);
        expandedFolder = env.expand(folder);
        expandedResourcePool = env.expand(resourcePool);
        expandedCustomizationSpec = env.expand(customizationSpec);
    }
    vsphere.cloneVm(expandedClone, expandedSource, linkedClone, expandedResourcePool, expandedCluster, expandedDatastore, expandedFolder, powerOn, expandedCustomizationSpec, jLogger);
    final int timeoutInSecondsForGetIp = getTimeoutInSeconds();
    if (powerOn && timeoutInSecondsForGetIp > 0) {
        VSphereLogger.vsLogger(jLogger, "Powering on VM \"" + expandedClone + "\".  Waiting for its IP for the next " + timeoutInSecondsForGetIp + " seconds.");
        IP = vsphere.getIp(vsphere.getVmByName(expandedClone), timeoutInSecondsForGetIp);
    }
    VSphereLogger.vsLogger(jLogger, "\"" + expandedClone + "\" successfully cloned " + (powerOn ? "and powered on" : "") + "!");
    return true;
}
Also used : PrintStream(java.io.PrintStream) AbstractBuild(hudson.model.AbstractBuild) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException)

Example 9 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.

the class ConvertToVm method convert.

private boolean convert(final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    VSphereLogger.vsLogger(jLogger, "Converting template to VM. Please wait ...");
    String expandedTemplate = template;
    String expandedCluster = cluster;
    String expandedResourcePool = resourcePool;
    EnvVars env;
    try {
        env = run.getEnvironment(listener);
    } catch (Exception e) {
        throw new VSphereException(e);
    }
    // TODO:  take in a comma delimited list and convert all
    if (run instanceof AbstractBuild) {
        // Add in matrix axes..
        env.overrideAll(((AbstractBuild) run).getBuildVariables());
        expandedTemplate = env.expand(template);
        expandedCluster = env.expand(cluster);
        expandedResourcePool = env.expand(resourcePool);
    }
    vsphere.markAsVm(expandedTemplate, expandedResourcePool, expandedCluster);
    VSphereLogger.vsLogger(jLogger, "\"" + expandedTemplate + "\" is a VM!");
    return true;
}
Also used : PrintStream(java.io.PrintStream) AbstractBuild(hudson.model.AbstractBuild) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException)

Example 10 with VSphereException

use of org.jenkinsci.plugins.vsphere.tools.VSphereException in project vsphere-cloud-plugin by jenkinsci.

the class DeleteSnapshot method deleteSnapshot.

private boolean deleteSnapshot(final Run<?, ?> run, Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    String expandedSnap = snapshotName;
    String expandedVm = vm;
    EnvVars env;
    try {
        env = run.getEnvironment(listener);
    } catch (Exception e) {
        throw new VSphereException(e);
    }
    if (run instanceof AbstractBuild) {
        // Add in matrix axes..
        env.overrideAll(((AbstractBuild) run).getBuildVariables());
        expandedSnap = env.expand(snapshotName);
        expandedVm = env.expand(vm);
    }
    VSphereLogger.vsLogger(jLogger, "Deleting snapshot \"" + expandedSnap + "\" of VM " + expandedVm + "...");
    vsphere.deleteSnapshot(expandedVm, expandedSnap, consolidate, failOnNoExist);
    VSphereLogger.vsLogger(jLogger, "Complete.");
    return true;
}
Also used : PrintStream(java.io.PrintStream) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException)

Aggregations

VSphereException (org.jenkinsci.plugins.vsphere.tools.VSphereException)22 IOException (java.io.IOException)19 PrintStream (java.io.PrintStream)18 AbstractBuild (hudson.model.AbstractBuild)7 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)4 ServletException (javax.servlet.ServletException)4 OptionValue (com.vmware.vim25.OptionValue)1 VirtualMachineConfigInfo (com.vmware.vim25.VirtualMachineConfigInfo)1 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)1 Datastore (com.vmware.vim25.mo.Datastore)1 DistributedVirtualPortgroup (com.vmware.vim25.mo.DistributedVirtualPortgroup)1 DistributedVirtualSwitch (com.vmware.vim25.mo.DistributedVirtualSwitch)1 ManagedEntity (com.vmware.vim25.mo.ManagedEntity)1 Network (com.vmware.vim25.mo.Network)1 Task (com.vmware.vim25.mo.Task)1 FormException (hudson.model.Descriptor.FormException)1 ComputerLauncher (hudson.slaves.ComputerLauncher)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1