blob: 45f024485b4f163a008dc072b10277027db8f8c2 [file] [log] [blame]
Jakub Josef60cc9d22017-01-18 12:02:14 +01001import logging
Adam Tengler70763e02017-08-21 16:50:32 +00002
Jakub Josef60cc9d22017-01-18 12:02:14 +01003logger = logging.getLogger(__name__)
4
5set_smtp_groovy = """\
Alexander Evseev729d3ed2018-07-23 08:05:39 +00006def result = ""
Adam Tengler70763e02017-08-21 16:50:32 +00007for(desc in [Jenkins.getInstance().getDescriptor("hudson.plugins.emailext.ExtendedEmailPublisher"),Jenkins.getInstance().getDescriptor("hudson.tasks.Mailer")]){
8 if(desc.getSmtpServer().equals("${host}") &&
Alexander Evseev729d3ed2018-07-23 08:05:39 +00009 ((desc instanceof hudson.plugins.emailext.ExtendedEmailPublisherDescriptor && desc.getSmtpAuthUsername().equals("${username}")) ||
10 (desc instanceof hudson.tasks.Mailer$DescriptorImpl && desc.getSmtpAuthUserName().equals("${username}"))) &&
11 desc.getSmtpAuthPassword().toString().equals("${password}") &&
Adam Tengler70763e02017-08-21 16:50:32 +000012 desc.getSmtpPort().equals("${port}") &&
13 desc.getUseSsl() == ${ssl} &&
14 desc.getCharset().equals("${charset}") &&
Jakub Josef917ab112017-10-06 15:08:42 +020015 (!${reply_to_exists} || desc.getReplyAddress().equals("${reply_to}"))){
Jakub Josefdfb288c2017-04-27 17:29:00 +020016 result = "EXISTS"
Adam Tengler70763e02017-08-21 16:50:32 +000017 }else{
18 desc.setSmtpAuth("${username}", "${password}")
19 desc.setUseSsl(${ssl})
20 if(desc instanceof hudson.plugins.emailext.ExtendedEmailPublisherDescriptor){
21 desc.setSmtpServer("${host}")
22 }else{
23 desc.setSmtpHost("${host}")
24 }
25 desc.setSmtpPort("${port}")
26 desc.setCharset("${charset}")
Jakub Josefd6e39b22017-11-15 18:50:40 +010027 if(${reply_to_exists}){
Adam Tengler70763e02017-08-21 16:50:32 +000028 desc.setReplyToAddress("${reply_to}")
29 }
Jakub Josefdfb288c2017-04-27 17:29:00 +020030 desc.save()
31 result = "SUCCESS"
Adam Tengler70763e02017-08-21 16:50:32 +000032 }
33}
Jakub Josefdfb288c2017-04-27 17:29:00 +020034print(result)
Adam Tengler70763e02017-08-21 16:50:32 +000035""" # noqa
Jakub Josefdfb288c2017-04-27 17:29:00 +020036
37set_admin_email_groovy = """
38def jenkinsLocationConfiguration = JenkinsLocationConfiguration.get()
Adam Tengler70763e02017-08-21 16:50:32 +000039if(jenkinsLocationConfiguration.getAdminAddress().equals("${email}")){
Jakub Josefdfb288c2017-04-27 17:29:00 +020040 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000041}else{
42 jenkinsLocationConfiguration.setAdminAddress("${email}")
Jakub Josefdfb288c2017-04-27 17:29:00 +020043 jenkinsLocationConfiguration.save()
Jakub Josef60cc9d22017-01-18 12:02:14 +010044 print("SUCCESS")
Adam Tengler70763e02017-08-21 16:50:32 +000045}
46""" # noqa
Jakub Josef60cc9d22017-01-18 12:02:14 +010047
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040048
49def __virtual__():
50 '''
51 Only load if jenkins_common module exist.
52 '''
53 if 'jenkins_common.call_groovy_script' not in __salt__:
54 return (
55 False,
56 'The jenkins_smtp state module cannot be loaded: '
57 'jenkins_common not found')
58 return True
59
60
Adam Tengler70763e02017-08-21 16:50:32 +000061def config(name, host, username, password, reply_to=None,
62 port=25, ssl=False, charset="UTF-8"):
Jakub Josef60cc9d22017-01-18 12:02:14 +010063 """
64 Jenkins SMTP server config state method
65
66 :param name: configuration name
67 :param host: SMTP host
68 :param username: SMTP username
69 :param password: SMTP password
70 :param reply_to: sent emails ReplyTo header (optional)
71 :param port: SMTP port (optional, default 25)
72 :param ssl: use SSL for SMTP (optional, default False)
73 :param charset: SMTP charset (optional, default UTF-8)
74 :returns: salt-specified state dict
75 """
76 test = __opts__['test'] # noqa
77 ret = {
78 'name': name,
79 'changes': {},
80 'result': False,
81 'comment': '',
82 }
83 result = False
84 if test:
85 status = "SUCCESS"
86 ret['changes'][name] = status
87 ret['comment'] = 'Jenkins SMTP config %s %s' % (name, status.lower())
88 else:
89 call_result = __salt__['jenkins_common.call_groovy_script'](
Adam Tengler70763e02017-08-21 16:50:32 +000090 set_smtp_groovy, {"username": username, "password": password, "host": host,
Jakub Josef60cc9d22017-01-18 12:02:14 +010091 "reply_to_exists": "true" if reply_to else "false",
92 "reply_to": reply_to,
93 "port": port if port else 25,
94 "ssl": "true" if ssl else "false",
95 "charset": charset if charset else "UTF-8"})
Adam Tengler70763e02017-08-21 16:50:32 +000096 if call_result["code"] == 200 and call_result["msg"] in [
97 "SUCCESS", "EXISTS"]:
Jakub Josef60cc9d22017-01-18 12:02:14 +010098 status = call_result["msg"]
99 if status == "SUCCESS":
100 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +0000101 ret['comment'] = 'Jenkins smtp config %s %s' % (
102 name, status.lower())
Jakub Josef60cc9d22017-01-18 12:02:14 +0100103 result = True
104 else:
105 status = 'FAILED'
106 logger.error(
107 "Jenkins smtp API call failure: %s", call_result["msg"])
108 ret['comment'] = 'Jenkins smtp API call failure: %s' % (call_result[
Adam Tengler70763e02017-08-21 16:50:32 +0000109 "msg"])
Jakub Josef60cc9d22017-01-18 12:02:14 +0100110 ret['result'] = None if test else result
111 return ret
Jakub Josefdfb288c2017-04-27 17:29:00 +0200112
113
114def admin_email(name, email):
115 """
116 Jenkins Admin user email config state method
117
118 :param name: jenkins admin email
119 :returns: salt-specified state dict
120 """
121 test = __opts__['test'] # noqa
122 ret = {
123 'name': name,
124 'changes': {},
125 'result': False,
126 'comment': '',
127 }
128 result = False
129 if test:
130 status = "SUCCESS"
131 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +0000132 ret['comment'] = 'Jenkins admin email config %s %s' % (
133 name, status.lower())
Jakub Josefdfb288c2017-04-27 17:29:00 +0200134 else:
135 call_result = __salt__['jenkins_common.call_groovy_script'](
Jakub Josefa0811532017-05-16 18:00:56 +0200136 set_admin_email_groovy, {"email": email})
Adam Tengler70763e02017-08-21 16:50:32 +0000137 if call_result["code"] == 200 and call_result["msg"] in [
138 "SUCCESS", "EXISTS"]:
Jakub Josefdfb288c2017-04-27 17:29:00 +0200139 status = call_result["msg"]
140 if status == "SUCCESS":
141 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +0000142 ret['comment'] = 'Jenkins admin email config %s %s' % (
143 name, status.lower())
Jakub Josefdfb288c2017-04-27 17:29:00 +0200144 result = True
145 else:
146 status = 'FAILED'
147 logger.error(
148 "Jenkins admin email API call failure: %s", call_result["msg"])
149 ret['comment'] = 'Jenkins admin email API call failure: %s' % (call_result[
150 "msg"])
151 ret['result'] = None if test else result
152 return ret