Repository: airavata Updated Branches: refs/heads/moduleRefactor 82773c73b -> 7b8097476
http://git-wip-us.apache.org/repos/asf/airavata/blob/7b809747/tools/gsissh/src/main/java/org/apache/airavata/gsi/ssh/util/SSHUtils.java ---------------------------------------------------------------------- diff --git a/tools/gsissh/src/main/java/org/apache/airavata/gsi/ssh/util/SSHUtils.java b/tools/gsissh/src/main/java/org/apache/airavata/gsi/ssh/util/SSHUtils.java deleted file mode 100644 index 1741833..0000000 --- a/tools/gsissh/src/main/java/org/apache/airavata/gsi/ssh/util/SSHUtils.java +++ /dev/null @@ -1,758 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ -package org.apache.airavata.gsi.ssh.util; - -import com.jcraft.jsch.*; - -import org.apache.airavata.gsi.ssh.api.authentication.GSIAuthenticationInfo; -import org.apache.airavata.gsi.ssh.api.SSHApiException; -import org.apache.airavata.gsi.ssh.api.ServerInfo; -import org.apache.airavata.gsi.ssh.config.ConfigReader; -import org.apache.airavata.gsi.ssh.impl.StandardOutReader; -import org.apache.airavata.gsi.ssh.jsch.ExtendedJSch; -import org.slf4j.*; - -import java.io.*; -import java.util.Arrays; -import java.util.List; - -/** - * This class is going to be useful to SCP a file to a remote grid machine using my proxy credentials - */ -public class SSHUtils { - private static final org.slf4j.Logger log = LoggerFactory.getLogger(SSHUtils.class); - - static { - JSch.setConfig("gssapi-with-mic.x509", "org.apache.airavata.gsi.ssh.GSSContextX509"); - JSch.setConfig("userauth.gssapi-with-mic", "com.jcraft.jsch.UserAuthGSSAPIWithMICGSSCredentials"); - - } - - private ServerInfo serverInfo; - - private GSIAuthenticationInfo authenticationInfo; - - private ConfigReader configReader; - - /** - * We need to pass certificateLocation when we use SCPTo method standalone - * - * @param serverInfo - * @param authenticationInfo - * @param certificateLocation - * @param configReader - */ - public SSHUtils(ServerInfo serverInfo, GSIAuthenticationInfo authenticationInfo, String certificateLocation, ConfigReader configReader) { - System.setProperty("X509_CERT_DIR", certificateLocation); - this.serverInfo = serverInfo; - this.authenticationInfo = authenticationInfo; - this.configReader = configReader; - } - - /** - * This can be used when use SCPTo method within SSHAPi because SSHApiFactory already set the system property certificateLocation - * - * @param serverInfo - * @param authenticationInfo - * @param configReader - */ - public SSHUtils(ServerInfo serverInfo, GSIAuthenticationInfo authenticationInfo - , ConfigReader configReader) { - this.serverInfo = serverInfo; - this.authenticationInfo = authenticationInfo; - this.configReader = configReader; - } - - /** - * This method will scp the lFile to the rFile location - * - * @param rFile remote file Path to use in scp - * @param lFile local file path to use in scp - * @throws IOException - * @throws JSchException - * @throws org.apache.airavata.gsi.ssh.api.SSHApiException - * - */ - public void scpTo(String rFile, String lFile) throws IOException, JSchException, SSHApiException { - FileInputStream fis = null; - String prefix = null; - if (new File(lFile).isDirectory()) { - prefix = lFile + File.separator; - } - JSch jsch = new JSch(); - - log.debug("Connecting to server - " + serverInfo.getHost() + ":" + serverInfo.getPort() + " with user name - " - + serverInfo.getUserName()); - - Session session = null; - - 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 = this.configReader.getProperties(); - session.setConfig(config); - - // Not a good way, but we dont have any choice - if (session instanceof ExtendedSession) { - ((ExtendedSession) session).setAuthenticationInfo(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); - } - - boolean ptimestamp = true; - - // exec 'scp -t rfile' remotely - String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + rFile; - Channel channel = session.openChannel("exec"); - - StandardOutReader stdOutReader = new StandardOutReader(); - ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError()); - ((ChannelExec) channel).setCommand(command); - - // get I/O streams for remote scp - OutputStream out = channel.getOutputStream(); - InputStream in = channel.getInputStream(); - - channel.connect(); - - if (checkAck(in) != 0) { - String error = "Error Reading input Stream"; - log.error(error); - throw new SSHApiException(error); - } - - File _lfile = new File(lFile); - - if (ptimestamp) { - command = "T" + (_lfile.lastModified() / 1000) + " 0"; - // The access time should be sent here, - // but it is not accessible with JavaAPI ;-< - command += (" " + (_lfile.lastModified() / 1000) + " 0\n"); - out.write(command.getBytes()); - out.flush(); - if (checkAck(in) != 0) { - String error = "Error Reading input Stream"; - log.error(error); - throw new SSHApiException(error); - } - } - - // send "C0644 filesize filename", where filename should not include '/' - long filesize = _lfile.length(); - command = "C0644 " + filesize + " "; - if (lFile.lastIndexOf('/') > 0) { - command += lFile.substring(lFile.lastIndexOf('/') + 1); - } else { - command += lFile; - } - command += "\n"; - out.write(command.getBytes()); - out.flush(); - if (checkAck(in) != 0) { - String error = "Error Reading input Stream"; - log.error(error); - throw new SSHApiException(error); - } - - // send a content of lFile - fis = new FileInputStream(lFile); - byte[] buf = new byte[1024]; - while (true) { - int len = fis.read(buf, 0, buf.length); - if (len <= 0) break; - out.write(buf, 0, len); //out.flush(); - } - fis.close(); - fis = null; - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - if (checkAck(in) != 0) { - String error = "Error Reading input Stream"; - log.error(error); - throw new SSHApiException(error); - } - out.close(); - - stdOutReader.onOutput(channel); - - - if (stdOutReader.getStdErrorString().contains("scp:")) { - throw new SSHApiException(stdOutReader.getStdErrorString()); - } - channel.disconnect(); - } - - /** - * This will copy a local file to a remote location - * - * @param remoteFile remote location you want to transfer the file, this cannot be a directory, if user pass - * a dirctory we do copy it to that directory but we simply return the directory name - * todo handle the directory name as input and return the proper final output file name - * @param localFile Local file to transfer, this can be a directory - * @param session - * @return returns the final remote file path, so that users can use the new file location - * @throws IOException - * @throws JSchException - * @throws SSHApiException - */ - public static String scpTo(String remoteFile, String localFile, Session session) throws IOException, JSchException, SSHApiException { - FileInputStream fis = null; - String prefix = null; - if (new File(localFile).isDirectory()) { - prefix = localFile + File.separator; - } - boolean ptimestamp = true; - - // exec 'scp -t rfile' remotely - String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + remoteFile; - Channel channel = session.openChannel("exec"); - - StandardOutReader stdOutReader = new StandardOutReader(); - ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError()); - ((ChannelExec) channel).setCommand(command); - - // get I/O streams for remote scp - OutputStream out = channel.getOutputStream(); - InputStream in = channel.getInputStream(); - - channel.connect(); - - if (checkAck(in) != 0) { - String error = "Error Reading input Stream"; - log.error(error); - throw new SSHApiException(error); - } - - File _lfile = new File(localFile); - - if (ptimestamp) { - command = "T" + (_lfile.lastModified() / 1000) + " 0"; - // The access time should be sent here, - // but it is not accessible with JavaAPI ;-< - command += (" " + (_lfile.lastModified() / 1000) + " 0\n"); - out.write(command.getBytes()); - out.flush(); - if (checkAck(in) != 0) { - String error = "Error Reading input Stream"; - log.error(error); - throw new SSHApiException(error); - } - } - - // send "C0644 filesize filename", where filename should not include '/' - long filesize = _lfile.length(); - command = "C0644 " + filesize + " "; - if (localFile.lastIndexOf('/') > 0) { - command += localFile.substring(localFile.lastIndexOf('/') + 1); - } else { - command += localFile; - } - command += "\n"; - out.write(command.getBytes()); - out.flush(); - if (checkAck(in) != 0) { - String error = "Error Reading input Stream"; - log.error(error); - throw new SSHApiException(error); - } - - // send a content of lFile - fis = new FileInputStream(localFile); - byte[] buf = new byte[1024]; - while (true) { - int len = fis.read(buf, 0, buf.length); - if (len <= 0) break; - out.write(buf, 0, len); //out.flush(); - } - fis.close(); - fis = null; - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - if (checkAck(in) != 0) { - String error = "Error Reading input Stream"; - log.error(error); - throw new SSHApiException(error); - } - out.close(); - stdOutReader.onOutput(channel); - - - channel.disconnect(); - if (stdOutReader.getStdErrorString().contains("scp:")) { - throw new SSHApiException(stdOutReader.getStdErrorString()); - } - //since remote file is always a file we just return the file - return remoteFile; - } - - /** - * This method will copy a remote file to a local directory - * - * @param remoteFile remote file path, this has to be a full qualified path - * @param localFile This is the local file to copy, this can be a directory too - * @param session - * @return returns the final local file path of the new file came from the remote resource - */ - public static void scpFrom(String remoteFile, String localFile, Session session) throws IOException, JSchException, SSHApiException { - FileOutputStream fos = null; - try { - String prefix = null; - if (new File(localFile).isDirectory()) { - prefix = localFile + File.separator; - } - - // exec 'scp -f remotefile' remotely - String command = "scp -f " + remoteFile; - Channel channel = session.openChannel("exec"); - ((ChannelExec) channel).setCommand(command); - - StandardOutReader stdOutReader = new StandardOutReader(); - ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError()); - // get I/O streams for remote scp - OutputStream out = channel.getOutputStream(); - InputStream in = channel.getInputStream(); - - channel.connect(); - - byte[] buf = new byte[1024]; - - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - - while (true) { - int c = checkAck(in); - if (c != 'C') { - break; - } - - // read '0644 ' - in.read(buf, 0, 5); - - long filesize = 0L; - while (true) { - if (in.read(buf, 0, 1) < 0) { - // error - break; - } - if (buf[0] == ' ') break; - filesize = filesize * 10L + (long) (buf[0] - '0'); - } - - String file = null; - for (int i = 0; ; i++) { - in.read(buf, i, 1); - if (buf[i] == (byte) 0x0a) { - file = new String(buf, 0, i); - break; - } - } - - //System.out.println("filesize="+filesize+", file="+file); - - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - - // read a content of lfile - fos = new FileOutputStream(prefix == null ? localFile : prefix + file); - int foo; - while (true) { - if (buf.length < filesize) foo = buf.length; - else foo = (int) filesize; - foo = in.read(buf, 0, foo); - if (foo < 0) { - // error - break; - } - fos.write(buf, 0, foo); - filesize -= foo; - if (filesize == 0L) break; - } - fos.close(); - fos = null; - - if (checkAck(in) != 0) { - String error = "Error transfering the file content"; - log.error(error); - throw new SSHApiException(error); - } - - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - } - stdOutReader.onOutput(channel); - if (stdOutReader.getStdErrorString().contains("scp:")) { - throw new SSHApiException(stdOutReader.getStdErrorString()); - } - - } catch (Exception e) { - log.error(e.getMessage(), e); - } finally { - try { - if (fos != null) fos.close(); - } catch (Exception ee) { - } - } - } - - /** - * This method will copy a remote file to a local directory - * - * @param remoteFile remote file path, this has to be a full qualified path - * @param localFile This is the local file to copy, this can be a directory too - */ - public void scpFrom(String remoteFile, String localFile) throws SSHApiException { - JSch jsch = new JSch(); - - log.debug("Connecting to server - " + serverInfo.getHost() + ":" + serverInfo.getPort() + " with user name - " - + serverInfo.getUserName()); - - Session session = null; - - 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 = this.configReader.getProperties(); - session.setConfig(config); - - // Not a good way, but we dont have any choice - if (session instanceof ExtendedSession) { - ((ExtendedSession) session).setAuthenticationInfo(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); - } - - FileOutputStream fos = null; - try { - String prefix = null; - if (new File(localFile).isDirectory()) { - prefix = localFile + File.separator; - } - - // exec 'scp -f remotefile' remotely - StandardOutReader stdOutReader = new StandardOutReader(); - String command = "scp -f " + remoteFile; - Channel channel = session.openChannel("exec"); - ((ChannelExec) channel).setCommand(command); - ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError()); - // get I/O streams for remote scp - OutputStream out = channel.getOutputStream(); - InputStream in = channel.getInputStream(); - - channel.connect(); - - byte[] buf = new byte[1024]; - - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - - while (true) { - int c = checkAck(in); - if (c != 'C') { - break; - } - - // read '0644 ' - in.read(buf, 0, 5); - - long filesize = 0L; - while (true) { - if (in.read(buf, 0, 1) < 0) { - // error - break; - } - if (buf[0] == ' ') break; - filesize = filesize * 10L + (long) (buf[0] - '0'); - } - - String file = null; - for (int i = 0; ; i++) { - in.read(buf, i, 1); - if (buf[i] == (byte) 0x0a) { - file = new String(buf, 0, i); - break; - } - } - - //System.out.println("filesize="+filesize+", file="+file); - - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - - // read a content of lfile - fos = new FileOutputStream(prefix == null ? localFile : prefix + file); - int foo; - while (true) { - if (buf.length < filesize) foo = buf.length; - else foo = (int) filesize; - foo = in.read(buf, 0, foo); - if (foo < 0) { - // error - break; - } - fos.write(buf, 0, foo); - filesize -= foo; - if (filesize == 0L) break; - } - fos.close(); - fos = null; - - if (checkAck(in) != 0) { - String error = "Error transfering the file content"; - log.error(error); - throw new SSHApiException(error); - } - - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - } - -// session.disconnect(); - - stdOutReader.onOutput(channel); - if (stdOutReader.getStdErrorString().contains("scp:")) { - throw new SSHApiException(stdOutReader.getStdErrorString()); - } - } catch (Exception e) { - log.error(e.getMessage(), e); - } finally { - try { - if (fos != null) fos.close(); - } catch (Exception ee) { - } - } - } - - /** - * This method will copy a remote file to a local directory - * - * @param remoteFile remote file path, this has to be a full qualified path - * @param localFile This is the local file to copy, this can be a directory too - * @param session - * @return returns the final local file path of the new file came from the remote resource - */ - public static void scpThirdParty(String remoteFileSource, String remoteFileTarget, Session session) throws IOException, JSchException, SSHApiException { - FileOutputStream fos = null; - try { - String prefix = null; - - // exec 'scp -f remotefile' remotely - String command = "scp -3 " + remoteFileSource + " " + remoteFileTarget; - Channel channel = session.openChannel("exec"); - ((ChannelExec) channel).setCommand(command); - - StandardOutReader stdOutReader = new StandardOutReader(); - ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError()); - // get I/O streams for remote scp - OutputStream out = channel.getOutputStream(); - InputStream in = channel.getInputStream(); - - channel.connect(); - - byte[] buf = new byte[1024]; - - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - - while (true) { - int c = checkAck(in); - if (c != 'C') { - break; - } - - // read '0644 ' - in.read(buf, 0, 5); - - long filesize = 0L; - while (true) { - if (in.read(buf, 0, 1) < 0) { - // error - break; - } - if (buf[0] == ' ') break; - filesize = filesize * 10L + (long) (buf[0] - '0'); - } - int foo; - while (true) { - if (buf.length < filesize) foo = buf.length; - else foo = (int) filesize; - - int len = in.read(buf, 0, foo); - if (len <= 0) break; - out.write(buf, 0, len); - } - // send '\0' - buf[0] = 0; - out.write(buf, 0, 1); - out.flush(); - if (checkAck(in) != 0) { - String error = "Error transfering the file content"; - log.error(error); - throw new SSHApiException(error); - } - - } - out.close(); - - stdOutReader.onOutput(channel); - if (stdOutReader.getStdErrorString().contains("scp:")) { - throw new SSHApiException(stdOutReader.getStdErrorString()); - } - - } catch (Exception e) { - log.error(e.getMessage(), e); - } finally { - try { - if (fos != null) fos.close(); - } catch (Exception ee) { - } - } - } - - public static void makeDirectory(String path, Session session) throws IOException, JSchException, SSHApiException { - - // exec 'scp -t rfile' remotely - String command = "mkdir -p " + path; - Channel channel = session.openChannel("exec"); - StandardOutReader stdOutReader = new StandardOutReader(); - - ((ChannelExec) channel).setCommand(command); - - - ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError()); - try { - channel.connect(); - } catch (JSchException e) { - - channel.disconnect(); -// session.disconnect(); - - throw new SSHApiException("Unable to retrieve command output. Command - " + command + - " on server - " + session.getHost() + ":" + session.getPort() + - " connecting user name - " - + session.getUserName(), e); - } - stdOutReader.onOutput(channel); - if (stdOutReader.getStdErrorString().contains("mkdir:")) { - throw new SSHApiException(stdOutReader.getStdErrorString()); - } - - channel.disconnect(); - } - - public static List<String> listDirectory(String path, Session session) throws IOException, JSchException, SSHApiException { - - // exec 'scp -t rfile' remotely - String command = "ls " + path; - Channel channel = session.openChannel("exec"); - StandardOutReader stdOutReader = new StandardOutReader(); - - ((ChannelExec) channel).setCommand(command); - - - ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError()); - try { - channel.connect(); - } catch (JSchException e) { - - channel.disconnect(); -// session.disconnect(); - - throw new SSHApiException("Unable to retrieve command output. Command - " + command + - " on server - " + session.getHost() + ":" + session.getPort() + - " connecting user name - " - + session.getUserName(), e); - } - stdOutReader.onOutput(channel); - stdOutReader.getStdOutputString(); - if (stdOutReader.getStdErrorString().contains("ls:")) { - throw new SSHApiException(stdOutReader.getStdErrorString()); - } - channel.disconnect(); - return Arrays.asList(stdOutReader.getStdOutputString().split("\n")); - } - - static int checkAck(InputStream in) throws IOException { - int b = in.read(); - if (b == 0) return b; - if (b == -1) return b; - - if (b == 1 || b == 2) { - StringBuffer sb = new StringBuffer(); - int c; - do { - c = in.read(); - sb.append((char) c); - } - while (c != '\n'); - if (b == 1) { // error - System.out.print(sb.toString()); - } - if (b == 2) { // fatal error - System.out.print(sb.toString()); - } - } - return b; - } -} http://git-wip-us.apache.org/repos/asf/airavata/blob/7b809747/tools/gsissh/src/main/resources/schemas/gsissh-schemas.xsdconfig ---------------------------------------------------------------------- diff --git a/tools/gsissh/src/main/resources/schemas/gsissh-schemas.xsdconfig b/tools/gsissh/src/main/resources/schemas/gsissh-schemas.xsdconfig index 6be14f8..a46cadc 100644 --- a/tools/gsissh/src/main/resources/schemas/gsissh-schemas.xsdconfig +++ b/tools/gsissh/src/main/resources/schemas/gsissh-schemas.xsdconfig @@ -9,6 +9,6 @@ <xb:config xmlns:xb="http://www.bea.com/2002/09/xbean/config"> <xb:namespace uri="http://airavata.apache.org/schemas/gsi/ssh/2012/12"> - <xb:package>org.apache.airavata.gsi.ssh</xb:package> + <xb:package>org.apache.airavata.gfac.ssh</xb:package> </xb:namespace> </xb:config> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata/blob/7b809747/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/config/ConfigReaderTest.java ---------------------------------------------------------------------- diff --git a/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/config/ConfigReaderTest.java b/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/config/ConfigReaderTest.java new file mode 100644 index 0000000..a90dcba --- /dev/null +++ b/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/config/ConfigReaderTest.java @@ -0,0 +1,37 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.airavata.gfac.ssh.config; + +import org.testng.Assert; +import org.testng.annotations.Test; + +public class ConfigReaderTest { + + @Test + public void testGetConfiguration() throws Exception { + + System.out.println("Test case name " + this.getClass().getName()); + ConfigReader configReader = new ConfigReader(); + Assert.assertEquals(configReader.getConfiguration("StrictHostKeyChecking"), "no"); + + } +} http://git-wip-us.apache.org/repos/asf/airavata/blob/7b809747/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/impl/DefaultSSHApiTestWithMyProxyAuth.java ---------------------------------------------------------------------- diff --git a/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/impl/DefaultSSHApiTestWithMyProxyAuth.java b/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/impl/DefaultSSHApiTestWithMyProxyAuth.java new file mode 100644 index 0000000..61a7437 --- /dev/null +++ b/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/impl/DefaultSSHApiTestWithMyProxyAuth.java @@ -0,0 +1,77 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.airavata.gfac.ssh.impl; + +import org.apache.airavata.gfac.ssh.api.*; +import org.apache.airavata.gfac.ssh.config.ConfigReader; +import org.apache.airavata.gfac.ssh.impl.authentication.DefaultPublicKeyAuthentication; +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; + +public class DefaultSSHApiTestWithMyProxyAuth { + private static final Logger log = LoggerFactory.getLogger(PBSCluster.class); + + + + public void tearDown() throws Exception { + } + + + public static void main(String[]ars) throws IOException { + String myProxyUserName = "lg11w"; + +// DefaultPasswordAuthenticationInfo authenticationInfo +// = new DefaultPasswordAuthenticationInfo(""); + byte[] privateKey = IOUtils.toByteArray(new BufferedInputStream(new FileInputStream("/Users/lginnali/.ssh/id_dsa"))); + byte[] publicKey = IOUtils.toByteArray(new BufferedInputStream(new FileInputStream("/Users/lginnali/.ssh/id_dsa.pub"))); + DefaultPublicKeyAuthentication authenticationInfo = new DefaultPublicKeyAuthentication(privateKey,publicKey,""); + + // Create command + CommandInfo commandInfo = new RawCommandInfo("source /etc/bashrc; bsub </home/lg11w/mywork/sshEchoExperiment_9d267072-ca65-4ca8-847a-cd3d130f6050/366787899.lsf"); + + // Server info + //Stampede +// ServerInfo serverInfo = new ServerInfo(myProxyUserName, "stampede.tacc.utexas.edu", 2222); + //Trestles +// ServerInfo serverInfo = new ServerInfo(myProxyUserName, "trestles.sdsc.xsede.org", 22); + + //Lonestar + ServerInfo serverInfo = new ServerInfo(myProxyUserName, "ghpcc06.umassrc.org", 22); + // Output + CommandOutput commandOutput = new SystemCommandOutput(); + + // Execute command + try { + CommandExecutor.executeCommand(commandInfo, serverInfo, authenticationInfo, commandOutput, new ConfigReader()); + } catch (SSHApiException e) { + log.error(e.getMessage(), e); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } + + + +} http://git-wip-us.apache.org/repos/asf/airavata/blob/7b809747/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/impl/VanilaTestWithSSHAuth.java ---------------------------------------------------------------------- diff --git a/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/impl/VanilaTestWithSSHAuth.java b/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/impl/VanilaTestWithSSHAuth.java new file mode 100644 index 0000000..6f2840f --- /dev/null +++ b/tools/gsissh/src/test/java/org/apache/airavata/gfac/ssh/impl/VanilaTestWithSSHAuth.java @@ -0,0 +1,262 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.airavata.gfac.ssh.impl; + +import org.apache.airavata.gfac.ssh.api.*; +import org.apache.airavata.gfac.ssh.api.authentication.AuthenticationInfo; +import org.apache.airavata.gfac.ssh.api.job.JobDescriptor; +import org.apache.airavata.gfac.ssh.impl.authentication.DefaultPasswordAuthenticationInfo; +import org.apache.airavata.gfac.ssh.impl.authentication.DefaultPublicKeyFileAuthentication; +import org.apache.airavata.gfac.ssh.util.CommonUtils; +import org.testng.AssertJUnit; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; + +public class VanilaTestWithSSHAuth { + + private String userName; + private String password; + private String passPhrase; + private String hostName; + private String workingDirectory; + private String privateKeyPath; + private String publicKeyPath; + private String path; + + @BeforeTest + public void setUp() throws Exception { + System.out.println("Test case name " + this.getClass().getName()); + //Trestles + this.hostName = "trestles.sdsc.xsede.org"; + this.userName = "ogce"; + this.path="/opt/torque/bin/"; + //Stampede: +// this.hostName = "stampede.tacc.xsede.org"; +// this.userName = "ogce"; +// this.path="/usr/bin"; + //Lonestar: +// this.hostName = "lonestar.tacc.utexas.edu"; +// this.userName = "us3"; +// this.path="/opt/sge6.2/bin/lx24-amd64"; + //Alamo: +// this.hostName = "alamo.uthscsa.edu"; +// this.userName = "raminder"; +// this.path="/opt/torque/bin/"; + //Bigred: +// this.hostName = "bigred2.uits.iu.edu"; +// this.userName = "cgateway"; +// this.path="/opt/torque/torque-5.0.1/bin/"; + + System.setProperty("ssh.host",hostName); + System.setProperty("ssh.username", userName); + System.setProperty("private.ssh.key", "/home/lginnali/.ssh/id_dsa"); + System.setProperty("public.ssh.key", "/home/lginnali/.ssh/id_dsa.pub"); + System.setProperty("ssh.working.directory", "/tmp"); + + this.hostName = System.getProperty("ssh.host"); + this.userName = System.getProperty("ssh.username"); + this.password = System.getProperty("ssh.password"); + this.privateKeyPath = System.getProperty("private.ssh.key"); + this.publicKeyPath = System.getProperty("public.ssh.key"); + + System.setProperty("ssh.keypass", ""); + this.passPhrase = System.getProperty("ssh.keypass"); + this.workingDirectory = System.getProperty("ssh.working.directory"); + + + if (this.userName == null + || (this.password==null && (this.publicKeyPath == null || this.privateKeyPath == null)) || this.workingDirectory == null) { + System.out.println("########### In order to test you have to either username password or private,public keys"); + System.out.println("Use -Dssh.user=xxx -Dssh.password=yyy -Dssh.private.key.passphrase=zzz " + + "-Dssh.private.key.path -Dssh.public.key.path -Dssh.working.directory "); + } + } + + + @Test + public void testSimplePBSJob() throws Exception { + + AuthenticationInfo authenticationInfo = null; + if (password != null) { + authenticationInfo = new DefaultPasswordAuthenticationInfo(this.password); + } else { + authenticationInfo = new DefaultPublicKeyFileAuthentication(this.publicKeyPath, this.privateKeyPath, + this.passPhrase); + } + // Server info + ServerInfo serverInfo = new ServerInfo(this.userName, this.hostName); + Cluster pbsCluster = new PBSCluster(serverInfo, authenticationInfo, CommonUtils.getPBSJobManager(path)); + + String date = new Date().toString(); + date = date.replaceAll(" ", "_"); + date = date.replaceAll(":", "_"); + + String pomFile = new File("").getAbsolutePath() + File.separator + "pom.xml"; + + workingDirectory = workingDirectory + File.separator + + date + "_" + UUID.randomUUID(); + pbsCluster.makeDirectory(workingDirectory); + Thread.sleep(1000); + pbsCluster.makeDirectory(workingDirectory + File.separator + "inputs"); + Thread.sleep(1000); + pbsCluster.makeDirectory(workingDirectory + File.separator + "outputs"); + + + // doing file transfer to the remote resource + String remoteLocation = workingDirectory + File.separator + "inputs"; + pbsCluster.scpTo(remoteLocation, pomFile); + + int i = pomFile.lastIndexOf(File.separator); + String fileName = pomFile.substring(i + 1); + // constructing the job object + JobDescriptor jobDescriptor = new JobDescriptor(); + jobDescriptor.setWorkingDirectory(workingDirectory); + jobDescriptor.setShellName("/bin/bash"); + jobDescriptor.setJobName("GSI_SSH_SLEEP_JOB"); + jobDescriptor.setExecutablePath("/bin/echo"); + jobDescriptor.setAllEnvExport(true); + jobDescriptor.setMailOptions("n"); + jobDescriptor.setStandardOutFile(workingDirectory + File.separator + "application.out"); + jobDescriptor.setStandardErrorFile(workingDirectory + File.separator + "application.err"); + jobDescriptor.setNodes(1); + jobDescriptor.setProcessesPerNode(1); + jobDescriptor.setQueueName("normal"); + jobDescriptor.setMaxWallTime("5"); + //jobDescriptor.setJobSubmitter("aprun -n 1"); + List<String> inputs = new ArrayList<String>(); + inputs.add(remoteLocation + File.separator + fileName); + jobDescriptor.setInputValues(inputs); + //finished construction of job object + System.out.println(jobDescriptor.toXML()); + if(hostName.contains("trestles")){ + String jobID = pbsCluster.submitBatchJob(jobDescriptor); + System.out.println("JobID returned : " + jobID); + +// Cluster cluster = sshApi.getCluster(serverInfo, authenticationInfo); + Thread.sleep(1000); + JobDescriptor jobById = pbsCluster.getJobDescriptorById(jobID); + + //printing job data got from previous call + AssertJUnit.assertEquals(jobById.getJobId(), jobID); + System.out.println(jobById.getAcountString()); + System.out.println(jobById.getAllEnvExport()); + System.out.println(jobById.getCompTime()); + System.out.println(jobById.getExecutablePath()); + System.out.println(jobById.getEllapsedTime()); + System.out.println(jobById.getQueueName()); + System.out.println(jobById.getExecuteNode()); + System.out.println(jobById.getJobName()); + System.out.println(jobById.getCTime()); + System.out.println(jobById.getSTime()); + System.out.println(jobById.getMTime()); + System.out.println(jobById.getCompTime()); + System.out.println(jobById.getOwner()); + System.out.println(jobById.getQTime()); + System.out.println(jobById.getUsedCPUTime()); + System.out.println(jobById.getUsedMemory()); + System.out.println(jobById.getVariableList()); + } + } + + @Test + public void testSimpleLSFJob() throws Exception { + + AuthenticationInfo authenticationInfo = null; + if (password != null) { + authenticationInfo = new DefaultPasswordAuthenticationInfo(this.password); + } else { + authenticationInfo = new DefaultPublicKeyFileAuthentication(this.publicKeyPath, this.privateKeyPath, + this.passPhrase); + } + // Server info + ServerInfo serverInfo = new ServerInfo(this.userName, this.hostName); + + + // constructing the job object + JobDescriptor jobDescriptor = new JobDescriptor(); + jobDescriptor.setWorkingDirectory(workingDirectory); + jobDescriptor.setShellName("/bin/bash"); + jobDescriptor.setJobName("GSI_SSH_SLEEP_JOB"); + jobDescriptor.setExecutablePath("/bin/echo"); + jobDescriptor.setAllEnvExport(true); + jobDescriptor.setMailOptions("n"); + jobDescriptor.setMailAddress("[email protected]"); + jobDescriptor.setStandardOutFile(workingDirectory + File.separator + "application.out"); + jobDescriptor.setStandardErrorFile(workingDirectory + File.separator + "application.err"); + jobDescriptor.setNodes(1); + jobDescriptor.setProcessesPerNode(1); + jobDescriptor.setQueueName("long"); + jobDescriptor.setMaxWallTimeForLSF("5"); + jobDescriptor.setJobSubmitter("mpiexec"); + jobDescriptor.setModuleLoadCommands(new String[]{"module load openmpi/1.6.5"}); + jobDescriptor.setUsedMemory("1000"); + jobDescriptor.setChassisName("01"); + + //jobDescriptor.setJobSubmitter("aprun -n 1"); + List<String> inputs = new ArrayList<String>(); + jobDescriptor.setInputValues(inputs); + //finished construction of job object + System.out.println(jobDescriptor.toXML()); + Cluster pbsCluster = new PBSCluster(CommonUtils.getLSFJobManager("")); + ((PBSCluster) pbsCluster).generateJobScript(jobDescriptor); + } + + @Test + public void testSCPFromAndSCPTo() throws Exception { + + AuthenticationInfo authenticationInfo = null; + if (password != null) { + authenticationInfo = new DefaultPasswordAuthenticationInfo(this.password); + } else { + authenticationInfo = new DefaultPublicKeyFileAuthentication(this.publicKeyPath, this.privateKeyPath, + this.passPhrase); + } + // Server info + ServerInfo serverInfo = new ServerInfo(this.userName, this.hostName); + Cluster pbsCluster = new PBSCluster(serverInfo, authenticationInfo, CommonUtils.getPBSJobManager(path)); + new PBSCluster(serverInfo, authenticationInfo, CommonUtils.getPBSJobManager(path));; + + String date = new Date().toString(); + date = date.replaceAll(" ", "_"); + date = date.replaceAll(":", "_"); + + String pomFile = (new File(".")).getAbsolutePath() + File.separator + "pom.xml"; + File file = new File(pomFile); + if(!file.exists()){ + file.createNewFile(); + } + // Constructing theworking directory for demonstration and creating directories in the remote + // resource + workingDirectory = workingDirectory + File.separator + + date + "_" + UUID.randomUUID(); + pbsCluster.makeDirectory(workingDirectory); + pbsCluster.scpTo(workingDirectory, pomFile); + Thread.sleep(1000); + pbsCluster.scpFrom(workingDirectory + File.separator + "pom.xml", (new File(".")).getAbsolutePath()); + } +} http://git-wip-us.apache.org/repos/asf/airavata/blob/7b809747/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/ConfigReaderTest.java ---------------------------------------------------------------------- diff --git a/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/ConfigReaderTest.java b/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/ConfigReaderTest.java deleted file mode 100644 index ba5bf6a..0000000 --- a/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/ConfigReaderTest.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.airavata.gsi.ssh.config; - -import org.testng.Assert; -import org.testng.annotations.Test; - -public class ConfigReaderTest { - - @Test - public void testGetConfiguration() throws Exception { - - System.out.println("Test case name " + this.getClass().getName()); - ConfigReader configReader = new ConfigReader(); - Assert.assertEquals(configReader.getConfiguration("StrictHostKeyChecking"), "no"); - - } -} http://git-wip-us.apache.org/repos/asf/airavata/blob/7b809747/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApiTestWithMyProxyAuth.java ---------------------------------------------------------------------- diff --git a/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApiTestWithMyProxyAuth.java b/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApiTestWithMyProxyAuth.java deleted file mode 100644 index 7a2fce3..0000000 --- a/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/DefaultSSHApiTestWithMyProxyAuth.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.airavata.gsi.ssh.impl; - -import junit.framework.Assert; -import org.apache.airavata.gsi.ssh.api.*; -import org.apache.airavata.gsi.ssh.api.authentication.GSIAuthenticationInfo; -import org.apache.airavata.gsi.ssh.api.job.JobDescriptor; -import org.apache.airavata.gsi.ssh.config.ConfigReader; -import org.apache.airavata.gsi.ssh.impl.authentication.DefaultPasswordAuthenticationInfo; -import org.apache.airavata.gsi.ssh.impl.authentication.DefaultPublicKeyAuthentication; -import org.apache.airavata.gsi.ssh.impl.authentication.MyProxyAuthenticationInfo; -import org.apache.airavata.gsi.ssh.util.CommonUtils; -import org.apache.commons.io.IOUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.AssertJUnit; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; - -import java.io.*; -import java.util.ArrayList; -import java.util.List; - -public class DefaultSSHApiTestWithMyProxyAuth { - private static final Logger log = LoggerFactory.getLogger(PBSCluster.class); - - - - public void tearDown() throws Exception { - } - - - public static void main(String[]ars) throws IOException { - String myProxyUserName = "lg11w"; - -// DefaultPasswordAuthenticationInfo authenticationInfo -// = new DefaultPasswordAuthenticationInfo(""); - byte[] privateKey = IOUtils.toByteArray(new BufferedInputStream(new FileInputStream("/Users/lginnali/.ssh/id_dsa"))); - byte[] publicKey = IOUtils.toByteArray(new BufferedInputStream(new FileInputStream("/Users/lginnali/.ssh/id_dsa.pub"))); - DefaultPublicKeyAuthentication authenticationInfo = new DefaultPublicKeyAuthentication(privateKey,publicKey,""); - - // Create command - CommandInfo commandInfo = new RawCommandInfo("source /etc/bashrc; bsub </home/lg11w/mywork/sshEchoExperiment_9d267072-ca65-4ca8-847a-cd3d130f6050/366787899.lsf"); - - // Server info - //Stampede -// ServerInfo serverInfo = new ServerInfo(myProxyUserName, "stampede.tacc.utexas.edu", 2222); - //Trestles -// ServerInfo serverInfo = new ServerInfo(myProxyUserName, "trestles.sdsc.xsede.org", 22); - - //Lonestar - ServerInfo serverInfo = new ServerInfo(myProxyUserName, "ghpcc06.umassrc.org", 22); - // Output - CommandOutput commandOutput = new SystemCommandOutput(); - - // Execute command - try { - CommandExecutor.executeCommand(commandInfo, serverInfo, authenticationInfo, commandOutput, new ConfigReader()); - } catch (SSHApiException e) { - log.error(e.getMessage(), e); - } catch (IOException e) { - log.error(e.getMessage(), e); - } - } - - - -} http://git-wip-us.apache.org/repos/asf/airavata/blob/7b809747/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/VanilaTestWithSSHAuth.java ---------------------------------------------------------------------- diff --git a/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/VanilaTestWithSSHAuth.java b/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/VanilaTestWithSSHAuth.java deleted file mode 100644 index 186aca6..0000000 --- a/tools/gsissh/src/test/java/org/apache/airavata/gsi/ssh/impl/VanilaTestWithSSHAuth.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ - -package org.apache.airavata.gsi.ssh.impl; - -import org.apache.airavata.gsi.ssh.api.*; -import org.apache.airavata.gsi.ssh.api.authentication.AuthenticationInfo; -import org.apache.airavata.gsi.ssh.api.job.JobDescriptor; -import org.apache.airavata.gsi.ssh.config.ConfigReader; -import org.apache.airavata.gsi.ssh.impl.authentication.DefaultPasswordAuthenticationInfo; -import org.apache.airavata.gsi.ssh.impl.authentication.DefaultPublicKeyFileAuthentication; -import org.apache.airavata.gsi.ssh.util.CommonUtils; -import org.testng.AssertJUnit; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; - -import java.io.File; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.UUID; - -public class VanilaTestWithSSHAuth { - - private String userName; - private String password; - private String passPhrase; - private String hostName; - private String workingDirectory; - private String privateKeyPath; - private String publicKeyPath; - private String path; - - @BeforeTest - public void setUp() throws Exception { - System.out.println("Test case name " + this.getClass().getName()); - //Trestles - this.hostName = "trestles.sdsc.xsede.org"; - this.userName = "ogce"; - this.path="/opt/torque/bin/"; - //Stampede: -// this.hostName = "stampede.tacc.xsede.org"; -// this.userName = "ogce"; -// this.path="/usr/bin"; - //Lonestar: -// this.hostName = "lonestar.tacc.utexas.edu"; -// this.userName = "us3"; -// this.path="/opt/sge6.2/bin/lx24-amd64"; - //Alamo: -// this.hostName = "alamo.uthscsa.edu"; -// this.userName = "raminder"; -// this.path="/opt/torque/bin/"; - //Bigred: -// this.hostName = "bigred2.uits.iu.edu"; -// this.userName = "cgateway"; -// this.path="/opt/torque/torque-5.0.1/bin/"; - - System.setProperty("ssh.host",hostName); - System.setProperty("ssh.username", userName); - System.setProperty("private.ssh.key", "/home/lginnali/.ssh/id_dsa"); - System.setProperty("public.ssh.key", "/home/lginnali/.ssh/id_dsa.pub"); - System.setProperty("ssh.working.directory", "/tmp"); - - this.hostName = System.getProperty("ssh.host"); - this.userName = System.getProperty("ssh.username"); - this.password = System.getProperty("ssh.password"); - this.privateKeyPath = System.getProperty("private.ssh.key"); - this.publicKeyPath = System.getProperty("public.ssh.key"); - - System.setProperty("ssh.keypass", ""); - this.passPhrase = System.getProperty("ssh.keypass"); - this.workingDirectory = System.getProperty("ssh.working.directory"); - - - if (this.userName == null - || (this.password==null && (this.publicKeyPath == null || this.privateKeyPath == null)) || this.workingDirectory == null) { - System.out.println("########### In order to test you have to either username password or private,public keys"); - System.out.println("Use -Dssh.user=xxx -Dssh.password=yyy -Dssh.private.key.passphrase=zzz " + - "-Dssh.private.key.path -Dssh.public.key.path -Dssh.working.directory "); - } - } - - - @Test - public void testSimplePBSJob() throws Exception { - - AuthenticationInfo authenticationInfo = null; - if (password != null) { - authenticationInfo = new DefaultPasswordAuthenticationInfo(this.password); - } else { - authenticationInfo = new DefaultPublicKeyFileAuthentication(this.publicKeyPath, this.privateKeyPath, - this.passPhrase); - } - // Server info - ServerInfo serverInfo = new ServerInfo(this.userName, this.hostName); - Cluster pbsCluster = new PBSCluster(serverInfo, authenticationInfo, CommonUtils.getPBSJobManager(path)); - - String date = new Date().toString(); - date = date.replaceAll(" ", "_"); - date = date.replaceAll(":", "_"); - - String pomFile = new File("").getAbsolutePath() + File.separator + "pom.xml"; - - workingDirectory = workingDirectory + File.separator - + date + "_" + UUID.randomUUID(); - pbsCluster.makeDirectory(workingDirectory); - Thread.sleep(1000); - pbsCluster.makeDirectory(workingDirectory + File.separator + "inputs"); - Thread.sleep(1000); - pbsCluster.makeDirectory(workingDirectory + File.separator + "outputs"); - - - // doing file transfer to the remote resource - String remoteLocation = workingDirectory + File.separator + "inputs"; - pbsCluster.scpTo(remoteLocation, pomFile); - - int i = pomFile.lastIndexOf(File.separator); - String fileName = pomFile.substring(i + 1); - // constructing the job object - JobDescriptor jobDescriptor = new JobDescriptor(); - jobDescriptor.setWorkingDirectory(workingDirectory); - jobDescriptor.setShellName("/bin/bash"); - jobDescriptor.setJobName("GSI_SSH_SLEEP_JOB"); - jobDescriptor.setExecutablePath("/bin/echo"); - jobDescriptor.setAllEnvExport(true); - jobDescriptor.setMailOptions("n"); - jobDescriptor.setStandardOutFile(workingDirectory + File.separator + "application.out"); - jobDescriptor.setStandardErrorFile(workingDirectory + File.separator + "application.err"); - jobDescriptor.setNodes(1); - jobDescriptor.setProcessesPerNode(1); - jobDescriptor.setQueueName("normal"); - jobDescriptor.setMaxWallTime("5"); - //jobDescriptor.setJobSubmitter("aprun -n 1"); - List<String> inputs = new ArrayList<String>(); - inputs.add(remoteLocation + File.separator + fileName); - jobDescriptor.setInputValues(inputs); - //finished construction of job object - System.out.println(jobDescriptor.toXML()); - if(hostName.contains("trestles")){ - String jobID = pbsCluster.submitBatchJob(jobDescriptor); - System.out.println("JobID returned : " + jobID); - -// Cluster cluster = sshApi.getCluster(serverInfo, authenticationInfo); - Thread.sleep(1000); - JobDescriptor jobById = pbsCluster.getJobDescriptorById(jobID); - - //printing job data got from previous call - AssertJUnit.assertEquals(jobById.getJobId(), jobID); - System.out.println(jobById.getAcountString()); - System.out.println(jobById.getAllEnvExport()); - System.out.println(jobById.getCompTime()); - System.out.println(jobById.getExecutablePath()); - System.out.println(jobById.getEllapsedTime()); - System.out.println(jobById.getQueueName()); - System.out.println(jobById.getExecuteNode()); - System.out.println(jobById.getJobName()); - System.out.println(jobById.getCTime()); - System.out.println(jobById.getSTime()); - System.out.println(jobById.getMTime()); - System.out.println(jobById.getCompTime()); - System.out.println(jobById.getOwner()); - System.out.println(jobById.getQTime()); - System.out.println(jobById.getUsedCPUTime()); - System.out.println(jobById.getUsedMemory()); - System.out.println(jobById.getVariableList()); - } - } - - @Test - public void testSimpleLSFJob() throws Exception { - - AuthenticationInfo authenticationInfo = null; - if (password != null) { - authenticationInfo = new DefaultPasswordAuthenticationInfo(this.password); - } else { - authenticationInfo = new DefaultPublicKeyFileAuthentication(this.publicKeyPath, this.privateKeyPath, - this.passPhrase); - } - // Server info - ServerInfo serverInfo = new ServerInfo(this.userName, this.hostName); - - - // constructing the job object - JobDescriptor jobDescriptor = new JobDescriptor(); - jobDescriptor.setWorkingDirectory(workingDirectory); - jobDescriptor.setShellName("/bin/bash"); - jobDescriptor.setJobName("GSI_SSH_SLEEP_JOB"); - jobDescriptor.setExecutablePath("/bin/echo"); - jobDescriptor.setAllEnvExport(true); - jobDescriptor.setMailOptions("n"); - jobDescriptor.setMailAddress("[email protected]"); - jobDescriptor.setStandardOutFile(workingDirectory + File.separator + "application.out"); - jobDescriptor.setStandardErrorFile(workingDirectory + File.separator + "application.err"); - jobDescriptor.setNodes(1); - jobDescriptor.setProcessesPerNode(1); - jobDescriptor.setQueueName("long"); - jobDescriptor.setMaxWallTimeForLSF("5"); - jobDescriptor.setJobSubmitter("mpiexec"); - jobDescriptor.setModuleLoadCommands(new String[]{"module load openmpi/1.6.5"}); - jobDescriptor.setUsedMemory("1000"); - jobDescriptor.setChassisName("01"); - - //jobDescriptor.setJobSubmitter("aprun -n 1"); - List<String> inputs = new ArrayList<String>(); - jobDescriptor.setInputValues(inputs); - //finished construction of job object - System.out.println(jobDescriptor.toXML()); - Cluster pbsCluster = new PBSCluster(CommonUtils.getLSFJobManager("")); - ((PBSCluster) pbsCluster).generateJobScript(jobDescriptor); - } - - @Test - public void testSCPFromAndSCPTo() throws Exception { - - AuthenticationInfo authenticationInfo = null; - if (password != null) { - authenticationInfo = new DefaultPasswordAuthenticationInfo(this.password); - } else { - authenticationInfo = new DefaultPublicKeyFileAuthentication(this.publicKeyPath, this.privateKeyPath, - this.passPhrase); - } - // Server info - ServerInfo serverInfo = new ServerInfo(this.userName, this.hostName); - Cluster pbsCluster = new PBSCluster(serverInfo, authenticationInfo, CommonUtils.getPBSJobManager(path)); - new PBSCluster(serverInfo, authenticationInfo, CommonUtils.getPBSJobManager(path));; - - String date = new Date().toString(); - date = date.replaceAll(" ", "_"); - date = date.replaceAll(":", "_"); - - String pomFile = (new File(".")).getAbsolutePath() + File.separator + "pom.xml"; - File file = new File(pomFile); - if(!file.exists()){ - file.createNewFile(); - } - // Constructing theworking directory for demonstration and creating directories in the remote - // resource - workingDirectory = workingDirectory + File.separator - + date + "_" + UUID.randomUUID(); - pbsCluster.makeDirectory(workingDirectory); - pbsCluster.scpTo(workingDirectory, pomFile); - Thread.sleep(1000); - pbsCluster.scpFrom(workingDirectory + File.separator + "pom.xml", (new File(".")).getAbsolutePath()); - } -}
