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 ); } }
public static boolean loadFileToGuest(VirtualMachine vm, InputStream in, NamePasswordAuthentication npa, VSphereServiceInstance serviceInstance, String fullPathToFileInGuest) { try { GuestPosixFileAttributes posixFileAttributes = new GuestPosixFileAttributes(); posixFileAttributes.setPermissions(Long.valueOf(500)); posixFileAttributes.setAccessTime(GregorianCalendar.getInstance()); Calendar modCal = Calendar.getInstance(); modCal.setTimeInMillis(System.currentTimeMillis()); posixFileAttributes.setModificationTime(modCal); int fileSize = in.available(); String upUrlStr = serviceInstance.getInstance().getGuestOperationsManager().getFileManager(vm).initiateFileTransferToGuest(npa, fullPathToFileInGuest, posixFileAttributes, fileSize, true); //upUrlStr.replace("\\*", serviceInstance.getInstance().getServerConnection().getUrl().getHost()); HttpURLConnection putCon = (HttpURLConnection) new URL(upUrlStr).openConnection(); putCon.setDoInput(true); putCon.setDoOutput(true); putCon.setRequestProperty("Content-Type", "application/octet-stream"); putCon.setRequestMethod("PUT"); putCon.setRequestProperty("Content-Length", Long.toString(fileSize)); byte[] buffer = new byte[VSphereRestClient.CHUNKLEN]; if (fileSize > VSphereRestClient.CHUNKLEN) putCon.setChunkedStreamingMode(VSphereRestClient.CHUNKLEN); else { putCon.setChunkedStreamingMode(fileSize); buffer = new byte[fileSize]; } DataOutputStream out = new DataOutputStream(putCon.getOutputStream()); int len = 0; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } if (out != null) { out.flush(); out.close(); } if (in != null) in.close(); return true; } catch (Exception e) { return false; } }
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) { } }