Merge pull request #27 from jakubjosef/master
Implemented SMTP settings from client side via script api.
diff --git a/README.rst b/README.rst
index c808f28..5c489d0 100644
--- a/README.rst
+++ b/README.rst
@@ -475,7 +475,7 @@
lbl_text: label-offline
append: false # set true for label append instead of replace
-SMTP server settings
+SMTP server settings from master
.. code-block:: yaml
@@ -488,6 +488,20 @@
password: "smtp-password"
port: 25
+SMTP server settings from client
+
+.. code-block:: yaml
+
+ jenkins:
+ client:
+ smtp:
+ host: "smtp.domain.com"
+ username: "user@domain.cz"
+ password: "smtp-password"
+ port: 25
+ ssl: false
+ reply_to: reply_to@address.com
+
Jenkins script approvals
.. code-block:: yaml
diff --git a/_states/jenkins_smtp.py b/_states/jenkins_smtp.py
new file mode 100644
index 0000000..48e19c4
--- /dev/null
+++ b/_states/jenkins_smtp.py
@@ -0,0 +1,75 @@
+import logging
+logger = logging.getLogger(__name__)
+
+set_smtp_groovy = """\
+def desc = Jenkins.getInstance().getDescriptor("hudson.tasks.Mailer")
+if(desc.getSmtpServer().equals("{host}") &&
+ desc.getSmtpAuthUserName().equals("{username}") &&
+ desc.getSmtpAuthPassword().toString().equals("{password}") &&
+ desc.getSmtpPort().equals("{port}") &&
+ desc.getUseSsl() == {ssl} &&
+ desc.getCharset().equals("{charset}") &&
+ (!{reply_to_exists} || desc.getReplyAddress().equals("{reply_to}"))){{
+ print("EXISTS")
+}}else{{
+ desc.setSmtpAuth("{username}", "{password}")
+ desc.setSmtpHost("{host}")
+ desc.setUseSsl({ssl})
+ desc.setSmtpPort("{port}")
+ desc.setCharset("{charset}")
+ if({reply_to_exists}){{
+ desc.setReplyToAddress("{reply_to}")
+ }}
+ desc.save()
+ print("SUCCESS")
+}}
+""" # noqa
+
+def config(name, host, username, password, reply_to=None, port=25, ssl=False, charset="UTF-8"):
+ """
+ Jenkins SMTP server config state method
+
+ :param name: configuration name
+ :param host: SMTP host
+ :param username: SMTP username
+ :param password: SMTP password
+ :param reply_to: sent emails ReplyTo header (optional)
+ :param port: SMTP port (optional, default 25)
+ :param ssl: use SSL for SMTP (optional, default False)
+ :param charset: SMTP charset (optional, default UTF-8)
+ :returns: salt-specified state dict
+ """
+ test = __opts__['test'] # noqa
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'result': False,
+ 'comment': '',
+ }
+ result = False
+ if test:
+ status = "SUCCESS"
+ ret['changes'][name] = status
+ ret['comment'] = 'Jenkins SMTP config %s %s' % (name, status.lower())
+ else:
+ call_result = __salt__['jenkins_common.call_groovy_script'](
+ set_smtp_groovy, {"username": username, "password": password, "host": host,
+ "reply_to_exists": "true" if reply_to else "false",
+ "reply_to": reply_to,
+ "port": port if port else 25,
+ "ssl": "true" if ssl else "false",
+ "charset": charset if charset else "UTF-8"})
+ if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
+ status = call_result["msg"]
+ if status == "SUCCESS":
+ ret['changes'][name] = status
+ ret['comment'] = 'Jenkins smtp config %s %s' % (name, status.lower())
+ result = True
+ else:
+ status = 'FAILED'
+ logger.error(
+ "Jenkins smtp API call failure: %s", call_result["msg"])
+ ret['comment'] = 'Jenkins smtp API call failure: %s' % (call_result[
+ "msg"])
+ ret['result'] = None if test else result
+ return ret
diff --git a/jenkins/client/init.sls b/jenkins/client/init.sls
index 855ac9d..8ec9b7f 100644
--- a/jenkins/client/init.sls
+++ b/jenkins/client/init.sls
@@ -26,6 +26,9 @@
{%- if client.view is defined %}
- jenkins.client.view
{%- endif %}
+{%- if client.smtp is defined %}
+ - jenkins.client.smtp
+{%- endif %}
jenkins_client_install:
pkg.installed:
diff --git a/jenkins/client/smtp.sls b/jenkins/client/smtp.sls
new file mode 100644
index 0000000..ee3a577
--- /dev/null
+++ b/jenkins/client/smtp.sls
@@ -0,0 +1,12 @@
+{%- from "jenkins/map.jinja" import client with context %}
+{%- if client.smtp is defined %}
+set_jenkins_smtp:
+ jenkins_smtp.config:
+ - host: {{ client.smtp.host }}
+ - username: {{ client.smtp.get('username','') }}
+ - password: {{ client.smtp.get('password', '') }}
+ - reply_to: {{ client.smtp.get('reply_to','') }}
+ - port: {{ client.smtp.get('port', '') }}
+ - ssl: {{ client.smtp.get('ssl', '') }}
+ - charset: {{ client.smtp.get('charset', '') }}
+{%- endif %}