use of org.apache.oozie.action.ActionExecutorException in project oozie by apache.
the class JavaActionExecutor method parseJobXmlAndConfiguration.
public static void parseJobXmlAndConfiguration(Context context, Element element, Path appPath, Configuration conf, boolean isLauncher) throws IOException, ActionExecutorException, HadoopAccessorException, URISyntaxException {
Namespace ns = element.getNamespace();
@SuppressWarnings("unchecked") Iterator<Element> it = element.getChildren("job-xml", ns).iterator();
HashMap<String, FileSystem> filesystemsMap = new HashMap<String, FileSystem>();
HadoopAccessorService has = Services.get().get(HadoopAccessorService.class);
while (it.hasNext()) {
Element e = it.next();
String jobXml = e.getTextTrim();
Path pathSpecified = new Path(jobXml);
Path path = pathSpecified.isAbsolute() ? pathSpecified : new Path(appPath, jobXml);
FileSystem fs;
if (filesystemsMap.containsKey(path.toUri().getAuthority())) {
fs = filesystemsMap.get(path.toUri().getAuthority());
} else {
if (path.toUri().getAuthority() != null) {
fs = has.createFileSystem(context.getWorkflow().getUser(), path.toUri(), has.createConfiguration(path.toUri().getAuthority()));
} else {
fs = context.getAppFileSystem();
}
filesystemsMap.put(path.toUri().getAuthority(), fs);
}
Configuration jobXmlConf = new XConfiguration(fs.open(path));
try {
String jobXmlConfString = XmlUtils.prettyPrint(jobXmlConf).toString();
jobXmlConfString = XmlUtils.removeComments(jobXmlConfString);
jobXmlConfString = context.getELEvaluator().evaluate(jobXmlConfString, String.class);
jobXmlConf = new XConfiguration(new StringReader(jobXmlConfString));
} catch (ELEvaluationException ex) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.TRANSIENT, "EL_EVAL_ERROR", ex.getMessage(), ex);
} catch (Exception ex) {
context.setErrorInfo("EL_ERROR", ex.getMessage());
}
checkForDisallowedProps(jobXmlConf, "job-xml");
if (isLauncher) {
new LauncherConfigurationInjector(jobXmlConf).inject(conf);
} else {
XConfiguration.copy(jobXmlConf, conf);
}
}
Element e = element.getChild("configuration", ns);
if (e != null) {
String strConf = XmlUtils.prettyPrint(e).toString();
XConfiguration inlineConf = new XConfiguration(new StringReader(strConf));
checkForDisallowedProps(inlineConf, "inline configuration");
if (isLauncher) {
new LauncherConfigurationInjector(inlineConf).inject(conf);
} else {
XConfiguration.copy(inlineConf, conf);
}
}
}
use of org.apache.oozie.action.ActionExecutorException in project oozie by apache.
the class JavaActionExecutor method addActionLibs.
protected void addActionLibs(Path appPath, Configuration conf) throws ActionExecutorException {
String[] actionLibsStrArr = conf.getStrings("oozie.launcher.oozie.libpath");
if (actionLibsStrArr != null) {
try {
for (String actionLibsStr : actionLibsStrArr) {
actionLibsStr = actionLibsStr.trim();
if (actionLibsStr.length() > 0) {
Path actionLibsPath = new Path(actionLibsStr);
String user = conf.get("user.name");
FileSystem fs = Services.get().get(HadoopAccessorService.class).createFileSystem(user, appPath.toUri(), conf);
if (fs.exists(actionLibsPath)) {
FileStatus[] files = fs.listStatus(actionLibsPath);
for (FileStatus file : files) {
addToCache(conf, appPath, file.getPath().toUri().getPath(), false);
}
}
}
}
} catch (HadoopAccessorException ex) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.FAILED, ex.getErrorCode().toString(), ex.getMessage());
} catch (IOException ex) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.FAILED, "It should never happen", ex.getMessage());
}
}
}
use of org.apache.oozie.action.ActionExecutorException in project oozie by apache.
the class JavaActionExecutor method getCredProperties.
@SuppressWarnings("unchecked")
protected CredentialsProperties getCredProperties(Context context, String credName) throws Exception {
CredentialsProperties credProp = null;
String workflowXml = ((WorkflowJobBean) context.getWorkflow()).getWorkflowInstance().getApp().getDefinition();
XConfiguration wfjobConf = getWorkflowConf(context);
Element elementJob = XmlUtils.parseXml(workflowXml);
Element credentials = elementJob.getChild("credentials", elementJob.getNamespace());
if (credentials != null) {
for (Element credential : (List<Element>) credentials.getChildren("credential", credentials.getNamespace())) {
String name = credential.getAttributeValue("name");
String type = credential.getAttributeValue("type");
LOG.debug("getCredProperties: Name: " + name + ", Type: " + type);
if (name.equalsIgnoreCase(credName)) {
credProp = new CredentialsProperties(name, type);
for (Element property : (List<Element>) credential.getChildren("property", credential.getNamespace())) {
String propertyName = property.getChildText("name", property.getNamespace());
String propertyValue = property.getChildText("value", property.getNamespace());
ELEvaluator eval = new ELEvaluator();
for (Map.Entry<String, String> entry : wfjobConf) {
eval.setVariable(entry.getKey(), entry.getValue().trim());
}
propertyName = eval.evaluate(propertyName, String.class);
propertyValue = eval.evaluate(propertyValue, String.class);
credProp.getProperties().put(propertyName, propertyValue);
LOG.debug("getCredProperties: Properties name :'" + propertyName + "', Value : '" + propertyValue + "'");
}
}
}
if (credProp == null && credName != null) {
throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "JA021", "Could not load credentials with name [{0}]].", credName);
}
} else {
LOG.debug("credentials is null for the action");
}
return credProp;
}
use of org.apache.oozie.action.ActionExecutorException in project oozie by apache.
the class JavaActionExecutor method kill.
@Override
public void kill(Context context, WorkflowAction action) throws ActionExecutorException {
YarnClient yarnClient = null;
try {
Element actionXml = XmlUtils.parseXml(action.getConf());
final Configuration jobConf = createBaseHadoopConf(context, actionXml);
String launcherTag = LauncherHelper.getActionYarnTag(jobConf, context.getWorkflow().getParentId(), action);
jobConf.set(LauncherMain.CHILD_MAPREDUCE_JOB_TAGS, LauncherHelper.getTag(launcherTag));
yarnClient = createYarnClient(context, jobConf);
if (action.getExternalId() != null) {
try {
LOG.info("Killing action {0}'s external application {1}", action.getId(), action.getExternalId());
yarnClient.killApplication(ConverterUtils.toApplicationId(action.getExternalId()));
} catch (Exception e) {
LOG.warn("Could not kill {0}", action.getExternalId(), e);
}
}
String externalChildIDs = action.getExternalChildIDs();
if (externalChildIDs != null) {
for (String childId : externalChildIDs.split(",")) {
try {
LOG.info("Killing action {0}'s external child application {1}", action.getId(), childId);
yarnClient.killApplication(ConverterUtils.toApplicationId(childId.trim()));
} catch (Exception e) {
LOG.warn("Could not kill external child of {0}, {1}", action.getExternalId(), childId, e);
}
}
}
for (ApplicationId id : LauncherMain.getChildYarnJobs(jobConf, ApplicationsRequestScope.ALL, action.getStartTime().getTime())) {
try {
LOG.info("Killing action {0}'s external child application {1} based on tags", action.getId(), id.toString());
yarnClient.killApplication(id);
} catch (Exception e) {
LOG.warn("Could not kill child of {0}, {1}", action.getExternalId(), id, e);
}
}
context.setExternalStatus(KILLED);
context.setExecutionData(KILLED, null);
} catch (Exception ex) {
LOG.error("Error when killing YARN application", ex);
throw convertException(ex);
} finally {
try {
FileSystem actionFs = context.getAppFileSystem();
cleanUpActionDir(actionFs, context);
Closeables.closeQuietly(yarnClient);
} catch (Exception ex) {
LOG.error("Error when cleaning up action dir", ex);
throw convertException(ex);
}
}
}
use of org.apache.oozie.action.ActionExecutorException in project oozie by apache.
the class EmailActionExecutor method email.
public void email(String[] to, String[] cc, String[] bcc, String subject, String body, String[] attachments, String contentType, String user) throws ActionExecutorException {
// Get mailing server details.
String smtpHost = ConfigurationService.get(EMAIL_SMTP_HOST);
Integer smtpPortInt = ConfigurationService.getInt(EMAIL_SMTP_PORT);
Boolean smtpAuthBool = ConfigurationService.getBoolean(EMAIL_SMTP_AUTH);
String smtpUser = ConfigurationService.get(EMAIL_SMTP_USER);
String smtpPassword = ConfigurationService.getPassword(EMAIL_SMTP_PASS, "");
String fromAddr = ConfigurationService.get(EMAIL_SMTP_FROM);
Integer timeoutMillisInt = ConfigurationService.getInt(EMAIL_SMTP_SOCKET_TIMEOUT_MS);
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", smtpHost);
properties.setProperty("mail.smtp.port", smtpPortInt.toString());
properties.setProperty("mail.smtp.auth", smtpAuthBool.toString());
// Apply sensible timeouts, as defaults are infinite. See https://s.apache.org/javax-mail-timeouts
properties.setProperty("mail.smtp.connectiontimeout", timeoutMillisInt.toString());
properties.setProperty("mail.smtp.timeout", timeoutMillisInt.toString());
properties.setProperty("mail.smtp.writetimeout", timeoutMillisInt.toString());
Session session;
// (cause it may lead to issues when used second time).
if (!smtpAuthBool) {
session = Session.getInstance(properties);
} else {
session = Session.getInstance(properties, new JavaMailAuthenticator(smtpUser, smtpPassword));
}
Message message = new MimeMessage(session);
InternetAddress from;
List<InternetAddress> toAddrs = new ArrayList<InternetAddress>(to.length);
List<InternetAddress> ccAddrs = new ArrayList<InternetAddress>(cc.length);
List<InternetAddress> bccAddrs = new ArrayList<InternetAddress>(bcc.length);
try {
from = new InternetAddress(fromAddr);
message.setFrom(from);
} catch (AddressException e) {
throw new ActionExecutorException(ErrorType.ERROR, "EM002", "Bad from address specified in ${oozie.email.from.address}.", e);
} catch (MessagingException e) {
throw new ActionExecutorException(ErrorType.ERROR, "EM003", "Error setting a from address in the message.", e);
}
try {
// Add all <to>
for (String toStr : to) {
toAddrs.add(new InternetAddress(toStr.trim()));
}
message.addRecipients(RecipientType.TO, toAddrs.toArray(new InternetAddress[0]));
// Add all <cc>
for (String ccStr : cc) {
ccAddrs.add(new InternetAddress(ccStr.trim()));
}
message.addRecipients(RecipientType.CC, ccAddrs.toArray(new InternetAddress[0]));
// Add all <bcc>
for (String bccStr : bcc) {
bccAddrs.add(new InternetAddress(bccStr.trim()));
}
message.addRecipients(RecipientType.BCC, bccAddrs.toArray(new InternetAddress[0]));
// Set subject
message.setSubject(subject);
// when there is attachment
if (attachments != null && attachments.length > 0 && ConfigurationService.getBoolean(EMAIL_ATTACHMENT_ENABLED)) {
Multipart multipart = new MimeMultipart();
// Set body text
MimeBodyPart bodyTextPart = new MimeBodyPart();
bodyTextPart.setText(body);
multipart.addBodyPart(bodyTextPart);
for (String attachment : attachments) {
URI attachUri = new URI(attachment);
if (attachUri.getScheme() != null && attachUri.getScheme().equals("file")) {
throw new ActionExecutorException(ErrorType.ERROR, "EM008", "Encountered an error when attaching a file. A local file cannot be attached:" + attachment);
}
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new URIDataSource(attachUri, user);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(new File(attachment).getName());
multipart.addBodyPart(messageBodyPart);
}
message.setContent(multipart);
} else {
if (attachments != null && attachments.length > 0 && !ConfigurationService.getBoolean(EMAIL_ATTACHMENT_ENABLED)) {
body = body + EMAIL_ATTACHMENT_ERROR_MSG;
}
message.setContent(body, contentType);
}
} catch (AddressException e) {
throw new ActionExecutorException(ErrorType.ERROR, "EM004", "Bad address format in <to> or <cc> or <bcc>.", e);
} catch (MessagingException e) {
throw new ActionExecutorException(ErrorType.ERROR, "EM005", "An error occurred while adding recipients.", e);
} catch (URISyntaxException e) {
throw new ActionExecutorException(ErrorType.ERROR, "EM008", "Encountered an error when attaching a file", e);
} catch (HadoopAccessorException e) {
throw new ActionExecutorException(ErrorType.ERROR, "EM008", "Encountered an error when attaching a file", e);
}
try {
// Send over SMTP Transport
// (Session+Message has adequate details.)
Transport.send(message);
} catch (NoSuchProviderException e) {
throw new ActionExecutorException(ErrorType.ERROR, "EM006", "Could not find an SMTP transport provider to email.", e);
} catch (MessagingException e) {
throw new ActionExecutorException(ErrorType.ERROR, "EM007", "Encountered an error while sending the email message over SMTP.", e);
}
LOG.info("Email sent to [{0}]", to);
}
Aggregations