private void stopAllInner() throws Exception { for (ServerInfo server : servers) { Socket s = new Socket(server.getHost(), server.getTerminationPort()); s.getOutputStream(); s.getInputStream(); // If the MiniServer is remote, terminate the local SSH window for it too if (server.getHost().equals("localhost") == false) { s = new Socket("localhost", server.getTerminationPort()); s.getOutputStream(); s.getInputStream(); } } Runtime.getRuntime().halt(0); }
private void spawnInner( int processes, int serverIndex, int maxHeap, String frameworkCode, ArrayList<ServerInfo> logins, String[] args, int terminationPort) throws Exception { String ownLocation = new File(".").getAbsolutePath(); System.out.println("ownLocation=" + ownLocation); DistributedLockProvider provider = Globals.instantiateLockProvider(frameworkCode); String localClasspath; if (System.getProperty("java.class.path").contains("Chainbench.jar")) localClasspath = ownLocation + "/Chainbench.jar"; else { final String CP_STUB = ".;./bin;./lib/*;./lib/ojdbc6.jar;./lib/aspectjrt.jar"; localClasspath = provider.getMiniServerClasspath(CP_STUB); } int pathEnvVarIndex = -1; // Need to copy env vars across or get a Windows networking error Map<String, String> variables = System.getenv(); String[] envp = new String[variables.size()]; int i = 0; for (Map.Entry<String, String> entry : variables.entrySet()) { String name = entry.getKey(); String value = entry.getValue(); if (name.toLowerCase().equals("path")) { pathEnvVarIndex = i; System.out.println("PATH AT INDEX" + i); System.out.println("PATH=" + value); } envp[i++] = name + "=" + value; } Params.setServerIndex(args, serverIndex); String flattened = Params.spaceSeparatedList(args); System.out.println("Flattened=" + flattened); String debugFlags = (serverIndex == 0) ? " -debug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1234 " : ""; String cmdStub; String miniserverClasspath; String extraProps = ""; if (serverIndex != 0 && logins.size() != 0) { int remoteBoxIndex = serverIndex % (logins.size()); ServerInfo login = logins.get(remoteBoxIndex); cmdStub = "cmd /K start java -cp " + localClasspath + " com.hp.software.chainbench.SSH " + login.getHost() + " " + login.getUsername() + " " + login.getPassword() + " " + terminationPort + " " + " java "; miniserverClasspath = "./Chainbench.jar"; // Speeds up JDBC cxns on Linux - // see http://stackoverflow.com/questions/5503063/oracle-getconnection-slow extraProps = "-Djava.security.egd=file:/dev/./urandom"; ServerInfo remote = (ServerInfo) login.clone(); remote.setTerminationPort(terminationPort); servers.add(remote); } else { miniserverClasspath = localClasspath; cmdStub = "cmd /K start java "; ServerInfo local = new ServerInfo("localhost", null, null, terminationPort); servers.add(local); } String loadTimeWeaver = ""; if (Params.getSlowdownMode(args).equals(Constants.SLOWDOWN_ASPECT)) { loadTimeWeaver = " -javaagent:./lib/aspectjweaver.jar "; } else if (Params.getSlowdownMode(args).equals(Constants.SLOWDOWN_REVISER)) { loadTimeWeaver = " -javaagent:./lib/gluonj.jar=debug:com.hp.software.chainbench.SlowdownReviser "; } String propsStub = // " -XX:+HeapDumpOnOutOfMemoryError " + " -Xms" + maxHeap + "m" + " -Xmx" + maxHeap + "m" + loadTimeWeaver + debugFlags + extraProps; String props = provider.getJavaProps(propsStub, args); String cmd = cmdStub + " " + props + " " + " -cp " + miniserverClasspath + " com.hp.software.chainbench.MiniServer " + flattened; System.out.println(cmd); /* " -Dgemfire.remove-unresponsive-client=true " + " -Dgemfire.enable-network-partition-detection=true " + " -Dgemfire.departure-correlation-window=30 " + " -Dgemfire.conserve-sockets=false " + " -Dgemfire.ack-severe-alert-threshold=5 " + */ // /* " -Dhazelcast.initial.min.cluster.size=" + processes + // " -Dhazelcast.restart.on.max.idle=true " + // " -Dhazelcast.max.no.heartbeat.seconds=15 " + // " -Dhazelcast.in.thread.priority=10 " + // " -Dhazelcast.out.thread.priority=10 " + // " -Dhazelcast.service.thread.priority=10 " + */ // /* " -Djgroups.bind_addr=" + bindAddress + " " + " -Djgroups.diagnostics_addr=" + bindAddress + " " + " -Dsm.ini.groupname=testGroupName " + */ File dir = new File(ownLocation); Process p = Runtime.getRuntime().exec(cmd, envp, dir); localProcesses.add(p); }
/** * This will not reuse any session, it will create the session and close it at the end * * @param commandInfo Encapsulated information about command. E.g :- executable name parameters * etc ... * @param serverInfo The SSHing server information. * @param authenticationInfo Security data needs to be communicated with remote server. * @param commandOutput The output of the command. * @param configReader configuration required for ssh/gshissh connection * @throws SSHApiException throw exception when error occurs */ public static void executeCommand( CommandInfo commandInfo, ServerInfo serverInfo, AuthenticationInfo authenticationInfo, CommandOutput commandOutput, ConfigReader configReader) throws SSHApiException { if (authenticationInfo instanceof GSIAuthenticationInfo) { System.setProperty( X509_CERT_DIR, (String) ((GSIAuthenticationInfo) authenticationInfo).getProperties().get("X509_CERT_DIR")); } JSch jsch = new ExtendedJSch(); log.debug( "Connecting to server - " + serverInfo.getHost() + ":" + serverInfo.getPort() + " with user name - " + serverInfo.getUserName()); Session session; try { session = jsch.getSession(serverInfo.getUserName(), serverInfo.getHost(), serverInfo.getPort()); } catch (JSchException e) { throw new SSHApiException( "An exception occurred while creating SSH session." + "Connecting server - " + serverInfo.getHost() + ":" + serverInfo.getPort() + " connecting user name - " + serverInfo.getUserName(), e); } java.util.Properties config = configReader.getProperties(); session.setConfig(config); // ============================================================= // Handling vanilla SSH pieces // ============================================================= if (authenticationInfo instanceof SSHPasswordAuthentication) { String password = ((SSHPasswordAuthentication) authenticationInfo) .getPassword(serverInfo.getUserName(), serverInfo.getHost()); session.setUserInfo(new SSHAPIUIKeyboardInteractive(password)); // TODO figure out why we need to set password to session session.setPassword(password); } else if (authenticationInfo instanceof SSHPublicKeyFileAuthentication) { SSHPublicKeyFileAuthentication sshPublicKeyFileAuthentication = (SSHPublicKeyFileAuthentication) authenticationInfo; String privateKeyFile = sshPublicKeyFileAuthentication.getPrivateKeyFile( serverInfo.getUserName(), serverInfo.getHost()); logDebug("The private key file for vanilla SSH " + privateKeyFile); String publicKeyFile = sshPublicKeyFileAuthentication.getPrivateKeyFile( serverInfo.getUserName(), serverInfo.getHost()); logDebug("The public key file for vanilla SSH " + publicKeyFile); Identity identityFile; try { identityFile = GSISSHIdentityFile.newInstance(privateKeyFile, null, jsch); } catch (JSchException e) { throw new SSHApiException( "An exception occurred while initializing keys using files. " + "(private key and public key)." + "Connecting server - " + serverInfo.getHost() + ":" + serverInfo.getPort() + " connecting user name - " + serverInfo.getUserName() + " private key file - " + privateKeyFile + ", public key file - " + publicKeyFile, e); } // Add identity to identity repository GSISSHIdentityRepository identityRepository = new GSISSHIdentityRepository(jsch); identityRepository.add(identityFile); // Set repository to session session.setIdentityRepository(identityRepository); // Set the user info SSHKeyPasswordHandler sshKeyPasswordHandler = new SSHKeyPasswordHandler((SSHKeyAuthentication) authenticationInfo); session.setUserInfo(sshKeyPasswordHandler); } else if (authenticationInfo instanceof SSHPublicKeyAuthentication) { SSHPublicKeyAuthentication sshPublicKeyAuthentication = (SSHPublicKeyAuthentication) authenticationInfo; Identity identityFile; try { String name = serverInfo.getUserName() + "_" + serverInfo.getHost(); identityFile = GSISSHIdentityFile.newInstance( name, sshPublicKeyAuthentication.getPrivateKey( serverInfo.getUserName(), serverInfo.getHost()), sshPublicKeyAuthentication.getPublicKey( serverInfo.getUserName(), serverInfo.getHost()), jsch); } catch (JSchException e) { throw new SSHApiException( "An exception occurred while initializing keys using byte arrays. " + "(private key and public key)." + "Connecting server - " + serverInfo.getHost() + ":" + serverInfo.getPort() + " connecting user name - " + serverInfo.getUserName(), e); } // Add identity to identity repository GSISSHIdentityRepository identityRepository = new GSISSHIdentityRepository(jsch); identityRepository.add(identityFile); // Set repository to session session.setIdentityRepository(identityRepository); // Set the user info SSHKeyPasswordHandler sshKeyPasswordHandler = new SSHKeyPasswordHandler((SSHKeyAuthentication) authenticationInfo); session.setUserInfo(sshKeyPasswordHandler); } // Not a good way, but we dont have any choice if (session instanceof ExtendedSession) { if (authenticationInfo instanceof GSIAuthenticationInfo) { ((ExtendedSession) session) .setAuthenticationInfo((GSIAuthenticationInfo) authenticationInfo); } } try { session.connect(); } catch (JSchException e) { throw new SSHApiException( "An exception occurred while connecting to server." + "Connecting server - " + serverInfo.getHost() + ":" + serverInfo.getPort() + " connecting user name - " + serverInfo.getUserName(), e); } String command = commandInfo.getCommand(); Channel channel; try { channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); } catch (JSchException e) { session.disconnect(); throw new SSHApiException( "Unable to execute command - " + command + " on server - " + serverInfo.getHost() + ":" + serverInfo.getPort() + " connecting user name - " + serverInfo.getUserName(), e); } channel.setInputStream(null); ((ChannelExec) channel).setErrStream(commandOutput.getStandardError()); try { channel.connect(); } catch (JSchException e) { channel.disconnect(); session.disconnect(); throw new SSHApiException( "Unable to retrieve command output. Command - " + command + " on server - " + serverInfo.getHost() + ":" + serverInfo.getPort() + " connecting user name - " + serverInfo.getUserName(), e); } commandOutput.onOutput(channel); channel.disconnect(); session.disconnect(); }