Search in sources :

Example 1 with SMTPConfiguration

use of com.hubspot.singularity.config.SMTPConfiguration in project Singularity by HubSpot.

the class MailTemplateHelpers method getLogErrorRegex.

private Optional<Pattern> getLogErrorRegex(final Optional<SingularityTask> task) {
    Optional<String> maybeRegex;
    SMTPConfiguration configuration = smtpConfiguration.get();
    if (task.isPresent() && task.get().getTaskRequest().getRequest().getTaskLogErrorRegex().isPresent() && !task.get().getTaskRequest().getRequest().getTaskLogErrorRegex().get().equals("")) {
        maybeRegex = task.get().getTaskRequest().getRequest().getTaskLogErrorRegex();
    } else {
        maybeRegex = configuration.getTaskLogErrorRegex();
    }
    if (!maybeRegex.isPresent()) {
        LOG.trace("No task log error regex provided.");
        return Optional.absent();
    }
    String regex = maybeRegex.get();
    Boolean caseSensitive;
    if (task.isPresent() && task.get().getTaskRequest().getRequest().getTaskLogErrorRegexCaseSensitive().isPresent()) {
        caseSensitive = task.get().getTaskRequest().getRequest().getTaskLogErrorRegexCaseSensitive().get();
    } else if (configuration.getTaskLogErrorRegexCaseSensitive().isPresent()) {
        caseSensitive = configuration.getTaskLogErrorRegexCaseSensitive().get();
    } else {
        caseSensitive = true;
    }
    try {
        if (caseSensitive) {
            return Optional.of(Pattern.compile(regex));
        } else {
            return Optional.of(Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE));
        }
    } catch (PatternSyntaxException e) {
        LOG.error("Invalid task log error regex supplied: \"{}\". Received exception: {}", regex, e);
        return Optional.absent();
    }
}
Also used : SMTPConfiguration(com.hubspot.singularity.config.SMTPConfiguration) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with SMTPConfiguration

use of com.hubspot.singularity.config.SMTPConfiguration in project Singularity by HubSpot.

the class SingularitySmtpSender method sendMail.

private void sendMail(List<String> toList, List<String> ccList, String subject, String body) {
    final SMTPConfiguration smtpConfiguration = maybeSmtpConfiguration.get();
    boolean useAuth = false;
    if (smtpConfiguration.getUsername().isPresent() && smtpConfiguration.getPassword().isPresent()) {
        useAuth = true;
    } else if (smtpConfiguration.getUsername().isPresent() || smtpConfiguration.getPassword().isPresent()) {
        LOG.warn("Not using smtp authentication because username ({}) or password ({}) was not present", smtpConfiguration.getUsername().isPresent(), smtpConfiguration.getPassword().isPresent());
    }
    try {
        final Session session = createSession(smtpConfiguration, useAuth);
        MimeMessage message = new MimeMessage(session);
        Address[] toArray = getAddresses(toList);
        message.addRecipients(RecipientType.TO, toArray);
        if (!ccList.isEmpty()) {
            Address[] ccArray = getAddresses(ccList);
            message.addRecipients(RecipientType.CC, ccArray);
        }
        message.setFrom(new InternetAddress(smtpConfiguration.getFrom()));
        message.setSubject(MimeUtility.encodeText(subject, "utf-8", null));
        message.setContent(body, "text/html; charset=utf-8");
        LOG.trace("Sending a message to {} - {}", Arrays.toString(toArray), getEmailLogFormat(toList, subject));
        Transport.send(message);
    } catch (Throwable t) {
        LOG.warn("Unable to send message {}", getEmailLogFormat(toList, subject), t);
        exceptionNotifier.notify(String.format("Unable to send message (%s)", t.getMessage()), t, ImmutableMap.of("subject", subject));
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) SMTPConfiguration(com.hubspot.singularity.config.SMTPConfiguration) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) Session(javax.mail.Session)

Example 3 with SMTPConfiguration

use of com.hubspot.singularity.config.SMTPConfiguration in project Singularity by HubSpot.

the class SingularityTestModule method getSingularityConfigurationForTestingServer.

private static SingularityConfiguration getSingularityConfigurationForTestingServer(final TestingServer ts) {
    SingularityConfiguration config = new SingularityConfiguration();
    config.setLoadBalancerUri("test");
    MesosConfiguration mc = new MesosConfiguration();
    mc.setDefaultCpus(1);
    mc.setDefaultMemory(128);
    mc.setDefaultDisk(1024);
    config.setMesosConfiguration(mc);
    config.setSmtpConfiguration(new SMTPConfiguration());
    ZooKeeperConfiguration zookeeperConfiguration = new ZooKeeperConfiguration();
    zookeeperConfiguration.setQuorum(ts.getConnectString());
    config.setZooKeeperConfiguration(zookeeperConfiguration);
    config.setConsiderTaskHealthyAfterRunningForSeconds(0);
    return config;
}
Also used : SMTPConfiguration(com.hubspot.singularity.config.SMTPConfiguration) MesosConfiguration(com.hubspot.singularity.config.MesosConfiguration) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) ZooKeeperConfiguration(com.hubspot.singularity.config.ZooKeeperConfiguration)

Aggregations

SMTPConfiguration (com.hubspot.singularity.config.SMTPConfiguration)3 MesosConfiguration (com.hubspot.singularity.config.MesosConfiguration)1 SingularityConfiguration (com.hubspot.singularity.config.SingularityConfiguration)1 ZooKeeperConfiguration (com.hubspot.singularity.config.ZooKeeperConfiguration)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 Address (javax.mail.Address)1 Session (javax.mail.Session)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1