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