Search in sources :

Example 21 with VSphereException

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

the class PowerOff method powerOff.

private boolean powerOff(final Run<?, ?> run, Launcher launcher, final TaskListener listener) throws VSphereException {
    PrintStream jLogger = listener.getLogger();
    EnvVars env;
    String expandedVm = vm;
    if (run instanceof AbstractBuild) {
        try {
            env = run.getEnvironment(listener);
        } catch (Exception e) {
            throw new VSphereException(e);
        }
        // Add in matrix axes..
        env.overrideAll(((AbstractBuild) run).getBuildVariables());
        expandedVm = env.expand(vm);
    }
    VSphereLogger.vsLogger(jLogger, "Shutting Down VM " + expandedVm + "...");
    VirtualMachine vsphereVm = vsphere.getVmByName(expandedVm);
    if (vsphereVm == null && !ignoreIfNotExists) {
        throw new RuntimeException(Messages.validation_notFound("vm " + expandedVm));
    }
    if (vsphereVm != null) {
        vsphere.powerOffVm(vsphereVm, evenIfSuspended, shutdownGracefully);
        VSphereLogger.vsLogger(jLogger, "Successfully shutdown \"" + expandedVm + "\"");
    } else {
        VSphereLogger.vsLogger(jLogger, "Does not exists, BUT ignore it! \"" + expandedVm + "\"");
    }
    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) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 22 with VSphereException

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

the class ExposeGuestInfo method exposeInfo.

public boolean exposeInfo(Run<?, ?> run, Launcher launcher, TaskListener listener) throws Exception {
    PrintStream jLogger = listener.getLogger();
    String vmName = 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());
        vmName = env.expand(vm);
        resolvedEnvVariablePrefix = env.expand(envVariablePrefix).replace("-", "_");
    }
    VSphereLogger.vsLogger(jLogger, "Exposing guest info for VM \"" + vmName + "\" as environment variables");
    VirtualMachine vsphereVm = vsphere.getVmByName(vmName);
    if (vsphereVm == null) {
        throw new RuntimeException(Messages.validation_notFound("vm " + vmName));
    }
    VSphereEnvAction envAction = createGuestInfoEnvAction(vsphereVm, jLogger);
    if (waitForIp4) {
        String prefix = resolvedEnvVariablePrefix == null ? envVariablePrefix : resolvedEnvVariablePrefix;
        String machineIP = envAction.data.get(prefix + "_IpAddress");
        while (!ipv4Pattern.matcher(machineIP).find()) {
            try {
                TimeUnit.SECONDS.sleep(30);
            } catch (InterruptedException e) {
            }
            envAction = createGuestInfoEnvAction(vsphere.getVmByName(vmName), jLogger);
            machineIP = envAction.data.get(prefix + "_IpAddress");
        }
    }
    run.addAction(envAction);
    VSphereLogger.vsLogger(jLogger, "Successfully exposed guest info for VM \"" + vmName + "\"");
    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) InvocationTargetException(java.lang.reflect.InvocationTargetException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

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