Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Example works, but needs checking as to why monitorJob does not wait
Browse files Browse the repository at this point in the history
for job to finish executing. Some weird Mina thing

Signed-off-by: Joe Osborn <osbornjd@ornl.gov>
  • Loading branch information
Joe Osborn committed Feb 20, 2020
1 parent d067265 commit f67a5ee
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 47 deletions.
Expand Up @@ -17,7 +17,6 @@
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -26,6 +25,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.sshd.client.channel.ClientChannelEvent;
import org.apache.sshd.client.subsystem.sftp.SftpClient;
import org.apache.sshd.client.subsystem.sftp.SftpClient.DirEntry;
Expand Down Expand Up @@ -288,7 +288,11 @@ protected CommandStatus monitorJob() {
// Just log this exception, see if thread can wait next iteration
logger.error("Thread couldn't wait for another second while monitoring job...");
}


// If the exit status is null, wait for a minute to see if it can process
if(connection.get().getExecChannel().getExitStatus() == null) {
continue;
}
// Query the exit status. 0 is normal completion, everything else is abnormal
exitValue = connection.get().getExecChannel().getExitStatus();

Expand Down
Expand Up @@ -13,14 +13,13 @@

import java.io.IOException;

import org.apache.sshd.client.subsystem.sftp.SftpClient;
import org.eclipse.ice.commands.Command;
import org.eclipse.ice.commands.CommandConfiguration;
import org.eclipse.ice.commands.CommandFactory;
import org.eclipse.ice.commands.CommandStatus;
import org.eclipse.ice.commands.Connection;
import org.eclipse.ice.commands.ConnectionAuthorizationHandler;
import org.eclipse.ice.commands.ConnectionAuthorizationHandlerFactory;
import org.eclipse.ice.commands.ConnectionConfiguration;
import org.eclipse.ice.commands.ConnectionManager;
import org.eclipse.ice.commands.ConnectionManagerFactory;
import org.eclipse.ice.commands.FileHandlerFactory;
import org.eclipse.ice.commands.IFileHandler;

Expand All @@ -41,9 +40,10 @@ public class MultiRemoteHostCommandExample {

/**
* @param args
* @throws IOException
* @throws IOException
*/
public static void main(String[] args) throws IOException {

/**
* We will run this command from host A (your local computer) such that it
* executes a Command on host B to be remotely executed on host C. In this case,
Expand All @@ -55,49 +55,76 @@ public static void main(String[] args) throws IOException {
* remote host assumption we put them there for you
*/

setupHostB();

// Now we will actually execute the remote command on remote host B, where in
// turn the script will execute a command on remote host C.
runRemoteCommand();


}

/**
* This function sets up host B in the assumed configuration that it would
* actually be in. Since this example assumes that you are using an arbitrary
* remote host, the files to run this dummy job would not nominally be on that
* remote host. So we move them there in this function.
* @throws IOException
*
* @throws IOException
*/
private static void setupHostB() throws IOException {
private static void runRemoteCommand() throws IOException {
// Setup the connection to the host
ConnectionConfiguration connectionConfig = createConnection();

// Setup some strings of files to move
String pwd = System.getProperty("user.dir");
String workingDir = pwd + "/src/org/eclipse/ice/demo/commands/";
String scriptName = "remoteCommand.sh";
// This is the script to be run on host B. Need to move it from host A to B
// so that the environment is set to run the script which will run scriptName
// through Commands
String scriptName = "test_code_execution.sh";
String inputFileName = "someInputFile.txt";

// Get the file handler for moving
FileHandlerFactory factory = new FileHandlerFactory();
IFileHandler handler = factory.getFileHandler(connectionConfig);

// handler will create a new remote directory if the destination
// directory doesn't already exist on the remote host B
String destination = "/tmp/MultiRemoteHostCommandExample/";
System.out.println("\n\n\n\n moving");
CommandStatus status = handler.move(workingDir+scriptName, destination);

// Check that the file was successful
assert(status == CommandStatus.SUCCESS);
System.out.println("\n\n\n\n\n\n\nMoving done with status " + status);
status = handler.move(workingDir+inputFileName, destination);

assert(status == CommandStatus.SUCCESS);

String remoteWorkingDir = "/tmp/remoteRemoteCommand/";

// We need to explicitly move the script and input file to host B since we don't
// want it appended as an argument to the script running on remote host B. The
// script remoteCommand.sh will take care of that
FileHandlerFactory FHFactory = new FileHandlerFactory();
IFileHandler handler = FHFactory.getFileHandler(connectionConfig);

CommandStatus fileStatus = handler.move(workingDir + inputFileName, remoteWorkingDir);

assert (fileStatus == CommandStatus.SUCCESS);

fileStatus = handler.move(workingDir + scriptName, remoteWorkingDir);

assert (fileStatus == CommandStatus.SUCCESS);

CommandConfiguration commandConfig = new CommandConfiguration();
commandConfig.setNumProcs("1");
commandConfig.setInstallDirectory("");
commandConfig.setWorkingDirectory(workingDir);
commandConfig.setOS(System.getProperty("os.name"));
commandConfig.setExecutable("./remoteCommand.sh");
commandConfig.setAppendInput(true);
commandConfig.setCommandId(1);
commandConfig.setErrFileName("RemoteRemoteErr.txt");
commandConfig.setOutFileName("RemoteRemoteOut.txt");
commandConfig.setRemoteWorkingDirectory(remoteWorkingDir);

// add arguments that remoteCommand.sh needs to run
commandConfig.addArgument("dummy@osbornjd-ice-host.ornl.gov /tmp/remoteDir ~/.ssh/dummykey");
CommandFactory factory = new CommandFactory();
Command remoteCommand = null;
System.out.println("Running remote command");
try {
remoteCommand = factory.getCommand(commandConfig, connectionConfig);
} catch (IOException e) {
e.printStackTrace();
}

CommandStatus status = remoteCommand.execute();

assert (status == CommandStatus.SUCCESS);

System.out.println("Finished command \n\n\n\n\n\n\n\n\n\n\n");
}

/**
Expand All @@ -111,29 +138,19 @@ private static ConnectionConfiguration createConnection() {
ConnectionAuthorizationHandlerFactory authFactory = new ConnectionAuthorizationHandlerFactory();
// Request a ConnectionAuthorization of type text file which contains the
// dummy remote host credentials
ConnectionAuthorizationHandler auth = authFactory.getConnectionAuthorizationHandler("text",
"/tmp/ice-remote-creds.txt");
/**
* Alternatively, one can authorize with their password at the console line by
* performing the following set of code
*
* ConnectionAuthorizationHandler auth =
* authFactory.getConnectionAuthorizationHandler("console");
* auth.setHostname("hostname"); auth.setUsername("username");
*/
ConnectionAuthorizationHandler auth = authFactory.getConnectionAuthorizationHandler("keypath",
"/path/to/key");
auth.setHostname("host");
auth.setUsername("user");
// Set it so that the connection can authorize itself
connectionConfig.setAuthorization(auth);
// Give the connection a name
connectionConfig.setName("dummyConnection");
connectionConfig.setName("denisovanConnection");

// Delete the remote working directory once we are finished running the job
connectionConfig.deleteWorkingDirectory(true);

return connectionConfig;
}

private static void runRemoteCommand() {

}

}
@@ -1,3 +1,18 @@
#!/bin/bash

echo "Hello World"
script="test_code_execution.sh"
inputFile="someInputFile.txt"
userhostname=$1
hostCdirectory=$2
keypath=$3

# Make a directory to work in on the server
ssh -i $keypath $userhostname "mkdir $2"

# scp the files to that directory
scp -i $keypath $inputFile $userhostname:$2
scp -i $keypath $script $userhostname:$2


# run the script which executes the job
ssh -i $keypath $userhostname "cd $2; chmod 700 $script; ./$script $inputFile;"
@@ -0,0 +1,49 @@
#!/bin/bash
#*******************************************************************************
# * Copyright (c) 2019- UT-Battelle, LLC.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http://www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# * Initial API and implementation and initial documentation -
# * Joe Osborn
# *****************************************************************************



# This is a test executable shell script for running a job locally.
# It is for testing the LocalCommand chain of execution.
# The script takes some input from a dummy text file and prints it
# to the screen. It is in no way intended to be robust, just to show
# basic usage of an executable command functioning.


# Print a boring string out to the console screen
STRING="This is a hello world executable"
echo $STRING

# list the contents of this directory
ls -lrt

# Read in a dummy file
echo "I will read from files "$1 "and "$2

# Set a variable to the argument value
input=$1
otherinput=$2

# Read in the data
#while IFS= read -r lineA
#do
# echo "$lineA"
#done < "$input"

paste $input $otherinput | while IFS="$(printf '\t')" read -r f1 f2
do
printf 'first file: %s\n' "$f1"
printf 'second file: %s\n' "$f2"
done


0 comments on commit f67a5ee

Please sign in to comment.