blob: 98bbd6dada2ab6cb321453bbf83f1923f0120e36 [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 Josef9f6c5702017-05-11 19:13:56 +02008 desc.getSmtpAuthUsername().equals("{username}") &&
Jakub Josefdfb288c2017-04-27 17:29:00 +02009 desc.getSmtpAuthPassword().toString().equals("{password}") &&
10 desc.getSmtpPort().equals("{port}") &&
11 desc.getUseSsl() == {ssl} &&
12 desc.getCharset().equals("{charset}") &&
13 (!{reply_to_exists} || desc.getReplyAddress().equals("{reply_to}"))){{
14 result = "EXISTS"
15 }}else{{
16 desc.setSmtpAuth("{username}", "{password}")
Jakub Josefdfb288c2017-04-27 17:29:00 +020017 desc.setUseSsl({ssl})
Jakub Josefa0811532017-05-16 18:00:56 +020018 if(desc instanceof hudson.plugins.emailext.ExtendedEmailPublisherDescriptor){{
19 desc.setSmtpServer("{host}")
20 }}else{{
21 desc.setSmtpHost("{host}")
22 }}
Jakub Josefdfb288c2017-04-27 17:29:00 +020023 desc.setSmtpPort("{port}")
24 desc.setCharset("{charset}")
25 if({reply_to_exists}){{
26 desc.setReplyToAddress("{reply_to}")
27 }}
28 desc.save()
29 result = "SUCCESS"
Jakub Josef60cc9d22017-01-18 12:02:14 +010030 }}
Jakub Josefdfb288c2017-04-27 17:29:00 +020031}}
32print(result)
33""" # noqa
34
35set_admin_email_groovy = """
36def jenkinsLocationConfiguration = JenkinsLocationConfiguration.get()
37if(jenkinsLocationConfiguration.getAdminAddress().equals("{email}")){{
38 print("EXISTS")
39}}else{{
40 jenkinsLocationConfiguration.setAdminAddress("{email}")
41 jenkinsLocationConfiguration.save()
Jakub Josef60cc9d22017-01-18 12:02:14 +010042 print("SUCCESS")
43}}
44""" # noqa
45
46def config(name, host, username, password, reply_to=None, port=25, ssl=False, charset="UTF-8"):
47 """
48 Jenkins SMTP server config state method
49
50 :param name: configuration name
51 :param host: SMTP host
52 :param username: SMTP username
53 :param password: SMTP password
54 :param reply_to: sent emails ReplyTo header (optional)
55 :param port: SMTP port (optional, default 25)
56 :param ssl: use SSL for SMTP (optional, default False)
57 :param charset: SMTP charset (optional, default UTF-8)
58 :returns: salt-specified state dict
59 """
60 test = __opts__['test'] # noqa
61 ret = {
62 'name': name,
63 'changes': {},
64 'result': False,
65 'comment': '',
66 }
67 result = False
68 if test:
69 status = "SUCCESS"
70 ret['changes'][name] = status
71 ret['comment'] = 'Jenkins SMTP config %s %s' % (name, status.lower())
72 else:
73 call_result = __salt__['jenkins_common.call_groovy_script'](
74 set_smtp_groovy, {"username": username, "password": password, "host": host,
75 "reply_to_exists": "true" if reply_to else "false",
76 "reply_to": reply_to,
77 "port": port if port else 25,
78 "ssl": "true" if ssl else "false",
79 "charset": charset if charset else "UTF-8"})
80 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
81 status = call_result["msg"]
82 if status == "SUCCESS":
83 ret['changes'][name] = status
84 ret['comment'] = 'Jenkins smtp config %s %s' % (name, status.lower())
85 result = True
86 else:
87 status = 'FAILED'
88 logger.error(
89 "Jenkins smtp API call failure: %s", call_result["msg"])
90 ret['comment'] = 'Jenkins smtp API call failure: %s' % (call_result[
91 "msg"])
92 ret['result'] = None if test else result
93 return ret
Jakub Josefdfb288c2017-04-27 17:29:00 +020094
95
96def admin_email(name, email):
97 """
98 Jenkins Admin user email config state method
99
100 :param name: jenkins admin email
101 :returns: salt-specified state dict
102 """
103 test = __opts__['test'] # noqa
104 ret = {
105 'name': name,
106 'changes': {},
107 'result': False,
108 'comment': '',
109 }
110 result = False
111 if test:
112 status = "SUCCESS"
113 ret['changes'][name] = status
114 ret['comment'] = 'Jenkins admin email config %s %s' % (name, status.lower())
115 else:
116 call_result = __salt__['jenkins_common.call_groovy_script'](
Jakub Josefa0811532017-05-16 18:00:56 +0200117 set_admin_email_groovy, {"email": email})
Jakub Josefdfb288c2017-04-27 17:29:00 +0200118 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
119 status = call_result["msg"]
120 if status == "SUCCESS":
121 ret['changes'][name] = status
122 ret['comment'] = 'Jenkins admin email config %s %s' % (name, status.lower())
123 result = True
124 else:
125 status = 'FAILED'
126 logger.error(
127 "Jenkins admin email API call failure: %s", call_result["msg"])
128 ret['comment'] = 'Jenkins admin email API call failure: %s' % (call_result[
129 "msg"])
130 ret['result'] = None if test else result
131 return ret