blob: f27972e75c1e6dda9f4ea9c4cdb26727ac909315 [file] [log] [blame]
Jakub Josef60cc9d22017-01-18 12:02:14 +01001import logging
2logger = logging.getLogger(__name__)
3
4set_smtp_groovy = """\
Jakub Josefdfb288c2017-04-27 17:29:00 +02005def result = ""
6for(desc in [Jenkins.getInstance().getDescriptor("hudson.plugins.emailext.ExtendedEmailPublisher"),Jenkins.getInstance().getDescriptor("hudson.tasks.Mailer")]){{
7 if(desc.getSmtpServer().equals("{host}") &&
Jakub Josef8fd82942017-05-18 16:39:01 +02008 ((desc instanceof hudson.plugins.emailext.ExtendedEmailPublisherDescriptor && desc.getSmtpAuthUsername().equals("{username}")) ||
9 (desc instanceof hudson.tasks.Mailer$DescriptorImpl && desc.getSmtpAuthUserName().equals("{username}"))) &&
Jakub Josefdfb288c2017-04-27 17:29:00 +020010 desc.getSmtpAuthPassword().toString().equals("{password}") &&
11 desc.getSmtpPort().equals("{port}") &&
12 desc.getUseSsl() == {ssl} &&
13 desc.getCharset().equals("{charset}") &&
14 (!{reply_to_exists} || desc.getReplyAddress().equals("{reply_to}"))){{
15 result = "EXISTS"
16 }}else{{
17 desc.setSmtpAuth("{username}", "{password}")
Jakub Josefdfb288c2017-04-27 17:29:00 +020018 desc.setUseSsl({ssl})
Jakub Josefa0811532017-05-16 18:00:56 +020019 if(desc instanceof hudson.plugins.emailext.ExtendedEmailPublisherDescriptor){{
20 desc.setSmtpServer("{host}")
21 }}else{{
22 desc.setSmtpHost("{host}")
23 }}
Jakub Josefdfb288c2017-04-27 17:29:00 +020024 desc.setSmtpPort("{port}")
25 desc.setCharset("{charset}")
26 if({reply_to_exists}){{
27 desc.setReplyToAddress("{reply_to}")
28 }}
29 desc.save()
30 result = "SUCCESS"
Jakub Josef60cc9d22017-01-18 12:02:14 +010031 }}
Jakub Josefdfb288c2017-04-27 17:29:00 +020032}}
33print(result)
34""" # noqa
35
36set_admin_email_groovy = """
37def jenkinsLocationConfiguration = JenkinsLocationConfiguration.get()
38if(jenkinsLocationConfiguration.getAdminAddress().equals("{email}")){{
39 print("EXISTS")
40}}else{{
41 jenkinsLocationConfiguration.setAdminAddress("{email}")
42 jenkinsLocationConfiguration.save()
Jakub Josef60cc9d22017-01-18 12:02:14 +010043 print("SUCCESS")
44}}
45""" # noqa
46
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040047
48def __virtual__():
49 '''
50 Only load if jenkins_common module exist.
51 '''
52 if 'jenkins_common.call_groovy_script' not in __salt__:
53 return (
54 False,
55 'The jenkins_smtp state module cannot be loaded: '
56 'jenkins_common not found')
57 return True
58
59
Jakub Josef60cc9d22017-01-18 12:02:14 +010060def config(name, host, username, password, reply_to=None, port=25, ssl=False, charset="UTF-8"):
61 """
62 Jenkins SMTP server config state method
63
64 :param name: configuration name
65 :param host: SMTP host
66 :param username: SMTP username
67 :param password: SMTP password
68 :param reply_to: sent emails ReplyTo header (optional)
69 :param port: SMTP port (optional, default 25)
70 :param ssl: use SSL for SMTP (optional, default False)
71 :param charset: SMTP charset (optional, default UTF-8)
72 :returns: salt-specified state dict
73 """
74 test = __opts__['test'] # noqa
75 ret = {
76 'name': name,
77 'changes': {},
78 'result': False,
79 'comment': '',
80 }
81 result = False
82 if test:
83 status = "SUCCESS"
84 ret['changes'][name] = status
85 ret['comment'] = 'Jenkins SMTP config %s %s' % (name, status.lower())
86 else:
87 call_result = __salt__['jenkins_common.call_groovy_script'](
88 set_smtp_groovy, {"username": username, "password": password, "host": host,
89 "reply_to_exists": "true" if reply_to else "false",
90 "reply_to": reply_to,
91 "port": port if port else 25,
92 "ssl": "true" if ssl else "false",
93 "charset": charset if charset else "UTF-8"})
94 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
95 status = call_result["msg"]
96 if status == "SUCCESS":
97 ret['changes'][name] = status
98 ret['comment'] = 'Jenkins smtp config %s %s' % (name, status.lower())
99 result = True
100 else:
101 status = 'FAILED'
102 logger.error(
103 "Jenkins smtp API call failure: %s", call_result["msg"])
104 ret['comment'] = 'Jenkins smtp API call failure: %s' % (call_result[
105 "msg"])
106 ret['result'] = None if test else result
107 return ret
Jakub Josefdfb288c2017-04-27 17:29:00 +0200108
109
110def admin_email(name, email):
111 """
112 Jenkins Admin user email config state method
113
114 :param name: jenkins admin email
115 :returns: salt-specified state dict
116 """
117 test = __opts__['test'] # noqa
118 ret = {
119 'name': name,
120 'changes': {},
121 'result': False,
122 'comment': '',
123 }
124 result = False
125 if test:
126 status = "SUCCESS"
127 ret['changes'][name] = status
128 ret['comment'] = 'Jenkins admin email config %s %s' % (name, status.lower())
129 else:
130 call_result = __salt__['jenkins_common.call_groovy_script'](
Jakub Josefa0811532017-05-16 18:00:56 +0200131 set_admin_email_groovy, {"email": email})
Jakub Josefdfb288c2017-04-27 17:29:00 +0200132 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
133 status = call_result["msg"]
134 if status == "SUCCESS":
135 ret['changes'][name] = status
136 ret['comment'] = 'Jenkins admin email config %s %s' % (name, status.lower())
137 result = True
138 else:
139 status = 'FAILED'
140 logger.error(
141 "Jenkins admin email API call failure: %s", call_result["msg"])
142 ret['comment'] = 'Jenkins admin email API call failure: %s' % (call_result[
143 "msg"])
144 ret['result'] = None if test else result
145 return ret