private void waitForPort(VirtualMachine vm, int port, long timeout) { GuestOperationsManager gom = serviceInstance.get().getInstance().getGuestOperationsManager(); GuestAuthManager gam = gom.getAuthManager(vm); NamePasswordAuthentication npa = new NamePasswordAuthentication(); npa.setUsername("root"); npa.setPassword(vmInitPassword); GuestProgramSpec gps = new GuestProgramSpec(); gps.programPath = "/bin/sh"; StringBuilder openPortScript = new StringBuilder("netstat -nat | grep LIST | grep -q ':" + port + " ' && touch /tmp/portopen.txt"); gps.arguments = "-c \"" + openPortScript.toString() + "\""; List<String> env = Lists.newArrayList("PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin", "SHELL=/bin/bash"); gps.setEnvVariables(env.toArray(new String[env.size()])); GuestProcessManager gpm = gom.getProcessManager(vm); try { long pid = gpm.startProgramInGuest(npa, gps); GuestFileManager guestFileManager = vm.getServerConnection().getServiceInstance().getGuestOperationsManager().getFileManager(vm); FileTransferInformation fti = guestFileManager.initiateFileTransferFromGuest(npa, "/tmp/portopen.txt"); if (fti.getSize() == 0) logger.debug(" "); } catch (RemoteException e) { logger.error(e.getMessage(), e); Throwables.propagate(e); } }
@Override public boolean configure() throws TargetException { try { // Is the VM up? if( this.vmwareServiceInstance == null ) this.vmwareServiceInstance = VmwareIaasHandler.getServiceInstance( this.targetProperties ); VirtualMachine vm = VmwareIaasHandler.getVirtualMachine( this.vmwareServiceInstance, this.rootInstanceName ); if( vm == null ) { this.logger.warning( "No VM named '" + this.rootInstanceName + "' could be found. The VM might not yet be ready." ); return false; } // If yes, write user data. GuestOperationsManager gom = this.vmwareServiceInstance.getGuestOperationsManager(); NamePasswordAuthentication npa = new NamePasswordAuthentication(); npa.username = this.targetProperties.get( VmwareIaasHandler.VM_USER ); npa.password = this.targetProperties.get( VmwareIaasHandler.VM_PASSWORD ); GuestProgramSpec spec = new GuestProgramSpec(); spec.programPath = "/bin/echo"; spec.arguments = "$\'" + this.userData + "\' > /tmp/roboconf.properties"; this.logger.fine(spec.programPath + " " + spec.arguments); GuestProcessManager gpm = gom.getProcessManager( vm ); gpm.startProgramInGuest(npa, spec); return true; } catch( Exception e ) { if(++ this.vmwareToolsCheckCount < 20) { this.logger.fine("VMWare tools not yet started... check #" + this.vmwareToolsCheckCount + " failed"); return false; } throw new TargetException( e ); } }
private void postConfiguration(VirtualMachine vm, String name, String group, Set<NetworkConfig> networkConfigs) { if (!vm.getConfig().isTemplate()) VSpherePredicate.WAIT_FOR_VMTOOLS(10 * 1000 * 60, TimeUnit.MILLISECONDS).apply(vm); GuestOperationsManager gom = serviceInstance.get().getInstance().getGuestOperationsManager(); NamePasswordAuthentication npa = new NamePasswordAuthentication(); npa.setUsername("root"); npa.setPassword(vmInitPassword); GuestProgramSpec gps = new GuestProgramSpec(); InputStream in = VSphereComputeServiceAdapter.class.getResourceAsStream("/postConfigurationScript.sh"); GuestFilesUtils.loadFileToGuest(vm, in, npa, serviceInstance.get(), "/tmp/ping_dns.sh"); gps.programPath = "/bin/sh"; StringBuilder ethScript = new StringBuilder(); ethScript.append("/tmp/ping_dns.sh > jclouds.log 2>&1"); gps.arguments = "-c \"" + ethScript.toString() + "\""; List<String> env = Lists.newArrayList("PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin", "SHELL=/bin/bash"); waitForStateToChange(); if (!vm.getConfig().isTemplate()) VSpherePredicate.WAIT_FOR_VMTOOLS(10 * 1000 * 60, TimeUnit.MILLISECONDS).apply(vm); waitForStateToChange(); gps.setEnvVariables(env.toArray(new String[env.size()])); GuestProcessManager gpm = gom.getProcessManager(vm); try { long pid = gpm.startProgramInGuest(npa, gps); GuestProcessInfo[] processInfos = gpm.listProcessesInGuest(npa, new long[]{pid}); if (null != processInfos) { for (GuestProcessInfo processInfo : processInfos) { while (processInfo.getExitCode() == null) { processInfos = gpm.listProcessesInGuest(npa, new long[]{pid}); processInfo = processInfos[0]; } if (processInfo.getExitCode() != 0) { logger.warn("failed to run init script on node ( " + name + " ) exit code : " + processInfo.getExitCode()); //Throwables.propagate(new Exception("Failed to customize vm ( " + name + " )")); } } } logger.trace("<< process pid : " + pid); } catch (Exception e) { } }
public long startProgramInGuest(GuestAuthentication auth, GuestProgramSpec spec) throws GuestOperationsFault, InvalidState, TaskInProgress, FileFault, RuntimeFault, RemoteException { return getVimService().startProgramInGuest(getMOR(), vm.getMOR(), auth, spec); }