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

Commit

Permalink
Merge pull request #15 from osbornjd/jay/MarkIIINotifications
Browse files Browse the repository at this point in the history
Notifications
  • Loading branch information
osbornjd committed Jun 11, 2020
2 parents 0ee95ee + 5d9f23e commit 03d3063
Show file tree
Hide file tree
Showing 10 changed files with 649 additions and 4 deletions.
Expand Up @@ -131,7 +131,8 @@ public void widgetSelected(SelectionEvent e) {
*
* @param password
*/
public void setPassword(char[] password) {
this.password = password;
@Override
public void setOption(String option) {
this.password = option.toCharArray();
}
}
10 changes: 10 additions & 0 deletions org.eclipse.ice.commands/README.md
Expand Up @@ -45,6 +45,16 @@ ConnectionManagerFactory.getConnectionManager().setRequireStrictHostKeyChecking(

Note that this is also a way through which ssh validation can be performed in the package for running actual remote commands/file transfers.

#### EmailHandler test
To test the `EmailUpdateHandler` class, a similar file to the `/tmp/ice-remote-creds.txt` file must be created. Instead, a file in the location `/tmp/ice-email-creds.txt` must exist which contains the following information:

```
email@address
password
SmtpHost
```

The EmailHandler will send an email from your own address to the same address with updates on when the job finishes. In order for this to happen, the email address must be authenticated. In the case of the tests, and for CI purposes, these authentications are placed in the above text file. For developer use, one could simply enter this information as it is entered in EmailHandlerTest, or you could implement another method (e.g. through use of the text file).

#### KeyGen Tests and Connections

Expand Down
10 changes: 10 additions & 0 deletions org.eclipse.ice.commands/pom.xml
Expand Up @@ -35,5 +35,15 @@
<artifactId>sshd-sftp</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
</dependencies>
</project>
Expand Up @@ -70,6 +70,12 @@ public abstract class Command {
*/
protected ConnectionManager manager = ConnectionManagerFactory.getConnectionManager();


/**
* An optional update handler that can provide status updates for the command
*/
ICommandUpdateHandler updater = null;

/**
* Default constructor
*/
Expand Down Expand Up @@ -104,6 +110,18 @@ public CommandStatus execute() {

// Confirm the job finished with some status
logger.info("The job finished with status: " + status);
if(updater != null) {
String message = "Job number " + commandConfig.getCommandId()
+ " finished with status " + status +"\n "
+ "Check your log/out/error files for more details";
updater.setMessage(message);
try {
updater.postUpdate();
} catch (IOException e) {
// Just warn since it is not indicative of a job failing
logger.warn("Couldn't post update. Check stack trace" , e);
}
}
return status;

}
Expand Down Expand Up @@ -343,8 +361,12 @@ public boolean checkStatus(CommandStatus current_status) {
logger.info("The current status is: " + current_status);
return true;
} else {
logger.error("The job failed with status: " + current_status);
logger.error("Check your error logfile for more details! Exiting now!");
String statusString = "The job failed with status: " + current_status;
String checkString = "Check your error logfile for more details! Exiting now!";

logger.error(statusString);
logger.error(checkString);

return false;
}

Expand Down Expand Up @@ -430,4 +452,12 @@ public void setConnectionManager(ConnectionManager manager) {
public ConnectionManager getConnectionManager() {
return manager;
}

/**
* Setter for update handler, see {@link org.eclipse.ice.commands.Command#updater}
* @param updater
*/
public void setUpdateHandler(ICommandUpdateHandler updater) {
this.updater = updater;
}
}
@@ -0,0 +1,149 @@
/*******************************************************************************
* 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/or initial documentation -
* Jay Jay Billings, Joe Osborn
*******************************************************************************/
package org.eclipse.ice.commands;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* This class provides email updates for Command job statuses. It takes a
* provided email address, password, etc. and just sends an email to the same
* address
*
* @author Joe Osborn
*
*/
public class EmailUpdateHandler implements ICommandUpdateHandler {

// The email address to send the message to
private String emailAddress = "";

// The text that the message should contain
private String emailText = "";

// The subject of the message
private String emailSubject = "Commands API Message";

// The host smtp server for the email address
private String emailHost = "";

// The password for the provided email to be able to send to itself
private String emailPassword = "";

/**
* Default constructor
*/
public EmailUpdateHandler() {
}

/**
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#postUpdate()}
*/
@Override
public void postUpdate() throws IOException {
// Create some properties and setup the default gmail
// server properties
Properties properties = System.getProperties();
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true"); // TLS
// Setup mail server
properties.setProperty("mail.smtp.host", emailHost);

// Get the default Session object.
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailAddress, emailPassword);
}
});
// Set session to debug just to explicit
session.setDebug(true);

try {
// Create a default MimeMessage object
MimeMessage message = new MimeMessage(session);

// Set the sender and recipient of the email
message.setFrom(new InternetAddress(emailAddress));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));

// Give the email a subject and message content
message.setSubject(emailSubject);
message.setText(emailText);

Transport.send(message);
logger.info("Email sent successfully");
} catch (MessagingException mex) {
logger.error("Couldn't send email.", mex);
throw new IOException();
}

}

/**
* Setter for the email subject, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailSubject}
*
* @param emailSubject
*/
public void setSubject(String emailSubject) {
this.emailSubject = emailSubject;
}

/**
* Setter for the email address, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailAddress}
*
* @param option
*/
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

/**
* Setter for the email host for email authentication, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailHost}
*
* @param emailPassword
*/
public void setSmtpHost(String emailHost) {
this.emailHost = emailHost;
}

/**
* Setter for the email password for email authentication, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailPassword}
*
* @param emailPassword
*/
public void setPassword(String emailPassword) {
this.emailPassword = emailPassword;
}

/**
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#setMessage(String)}
*/
@Override
public void setMessage(String message) {
this.emailText = message;
}

}
@@ -0,0 +1,87 @@
/*******************************************************************************
* 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/or initial documentation -
* Jay Jay Billings, Joe Osborn
*******************************************************************************/
package org.eclipse.ice.commands;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

/**
* This class posts Command job updates to an HTTP link via POST
*
* @author Joe Osborn
*
*/
public class HTTPCommandUpdateHandler implements ICommandUpdateHandler {

// HTTP address to post the update to
private String HTTPAddress = "";

// Message to be posted
String message = "";

/**
* Default constructor
*/
public HTTPCommandUpdateHandler() {
}

/**
* Setter for http address to post to
*
* @param HTTPAddress
*/
public void setHTTPAddress(String HTTPAddress) {
this.HTTPAddress = HTTPAddress;
}

/**
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#postUpdate()}
*/
@Override
public void postUpdate() throws IOException {

// Get a default httpClient to use to send the post
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(HTTPAddress);

// Setup the parameters to be passed in the post
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("body", message));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

// Execute and get the response
try {
HttpResponse response = httpClient.execute(httpPost);
} catch (Exception e) {
logger.info("HTTP Post was not successful.", e);
throw new IOException();
}
}

/**
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#setMessage(String)}
*/
@Override
public void setMessage(String message) {
this.message = message;
}

}
@@ -0,0 +1,45 @@
/*******************************************************************************
* 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/or initial documentation -
* Jay Jay Billings, Joe Osborn
*******************************************************************************/
package org.eclipse.ice.commands;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This interface lays out the format for any Command update class. Command
* update classes notify users via their implemented methods about the status of
* jobs, e.g. if they are completed or not.
*
* @author Joe Osborn
*
*/
public interface ICommandUpdateHandler {

/**
* Logger for handling event messages and other information.
*/
static final Logger logger = LoggerFactory.getLogger(ICommandUpdateHandler.class);

/*
* This function processes the logic required to post an update about the job
* for the class
*/
abstract void postUpdate() throws IOException;

/**
* Function to set the message that will be updated on
* @param message
*/
abstract void setMessage(String message);
}

0 comments on commit 03d3063

Please sign in to comment.