Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 1 | import logging |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 2 | |
Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 3 | logger = logging.getLogger(__name__) |
| 4 | |
| 5 | set_smtp_groovy = """\ |
Alexander Evseev | 729d3ed | 2018-07-23 08:05:39 +0000 | [diff] [blame] | 6 | def result = "" |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 7 | for(desc in [Jenkins.getInstance().getDescriptor("hudson.plugins.emailext.ExtendedEmailPublisher"),Jenkins.getInstance().getDescriptor("hudson.tasks.Mailer")]){ |
| 8 | if(desc.getSmtpServer().equals("${host}") && |
Alexander Evseev | 729d3ed | 2018-07-23 08:05:39 +0000 | [diff] [blame] | 9 | ((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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 12 | desc.getSmtpPort().equals("${port}") && |
| 13 | desc.getUseSsl() == ${ssl} && |
| 14 | desc.getCharset().equals("${charset}") && |
Jakub Josef | 917ab11 | 2017-10-06 15:08:42 +0200 | [diff] [blame] | 15 | (!${reply_to_exists} || desc.getReplyAddress().equals("${reply_to}"))){ |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 16 | result = "EXISTS" |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 17 | }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 Josef | d6e39b2 | 2017-11-15 18:50:40 +0100 | [diff] [blame] | 27 | if(${reply_to_exists}){ |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 28 | desc.setReplyToAddress("${reply_to}") |
| 29 | } |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 30 | desc.save() |
| 31 | result = "SUCCESS" |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 34 | print(result) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 35 | """ # noqa |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 36 | |
| 37 | set_admin_email_groovy = """ |
| 38 | def jenkinsLocationConfiguration = JenkinsLocationConfiguration.get() |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 39 | if(jenkinsLocationConfiguration.getAdminAddress().equals("${email}")){ |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 40 | print("EXISTS") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 41 | }else{ |
| 42 | jenkinsLocationConfiguration.setAdminAddress("${email}") |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 43 | jenkinsLocationConfiguration.save() |
Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 44 | print("SUCCESS") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 45 | } |
| 46 | """ # noqa |
Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 47 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 48 | |
| 49 | def __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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 61 | def config(name, host, username, password, reply_to=None, |
| 62 | port=25, ssl=False, charset="UTF-8"): |
Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 63 | """ |
| 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 90 | set_smtp_groovy, {"username": username, "password": password, "host": host, |
Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 91 | "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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 96 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 97 | "SUCCESS", "EXISTS"]: |
Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 98 | status = call_result["msg"] |
| 99 | if status == "SUCCESS": |
| 100 | ret['changes'][name] = status |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 101 | ret['comment'] = 'Jenkins smtp config %s %s' % ( |
| 102 | name, status.lower()) |
Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 103 | 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 109 | "msg"]) |
Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 110 | ret['result'] = None if test else result |
| 111 | return ret |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 112 | |
| 113 | |
| 114 | def 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 132 | ret['comment'] = 'Jenkins admin email config %s %s' % ( |
| 133 | name, status.lower()) |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 134 | else: |
| 135 | call_result = __salt__['jenkins_common.call_groovy_script']( |
Jakub Josef | a081153 | 2017-05-16 18:00:56 +0200 | [diff] [blame] | 136 | set_admin_email_groovy, {"email": email}) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 137 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 138 | "SUCCESS", "EXISTS"]: |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 139 | status = call_result["msg"] |
| 140 | if status == "SUCCESS": |
| 141 | ret['changes'][name] = status |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 142 | ret['comment'] = 'Jenkins admin email config %s %s' % ( |
| 143 | name, status.lower()) |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 144 | 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 |