Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 1 | import logging |
| 2 | logger = logging.getLogger(__name__) |
| 3 | |
| 4 | set_smtp_groovy = """\ |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 5 | def result = "" |
| 6 | for(desc in [Jenkins.getInstance().getDescriptor("hudson.plugins.emailext.ExtendedEmailPublisher"),Jenkins.getInstance().getDescriptor("hudson.tasks.Mailer")]){{ |
| 7 | if(desc.getSmtpServer().equals("{host}") && |
Jakub Josef | 9f6c570 | 2017-05-11 19:13:56 +0200 | [diff] [blame] | 8 | desc.getSmtpAuthUsername().equals("{username}") && |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 9 | 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 Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 17 | desc.setUseSsl({ssl}) |
Jakub Josef | a081153 | 2017-05-16 18:00:56 +0200 | [diff] [blame^] | 18 | if(desc instanceof hudson.plugins.emailext.ExtendedEmailPublisherDescriptor){{ |
| 19 | desc.setSmtpServer("{host}") |
| 20 | }}else{{ |
| 21 | desc.setSmtpHost("{host}") |
| 22 | }} |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 23 | 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 Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 30 | }} |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 31 | }} |
| 32 | print(result) |
| 33 | """ # noqa |
| 34 | |
| 35 | set_admin_email_groovy = """ |
| 36 | def jenkinsLocationConfiguration = JenkinsLocationConfiguration.get() |
| 37 | if(jenkinsLocationConfiguration.getAdminAddress().equals("{email}")){{ |
| 38 | print("EXISTS") |
| 39 | }}else{{ |
| 40 | jenkinsLocationConfiguration.setAdminAddress("{email}") |
| 41 | jenkinsLocationConfiguration.save() |
Jakub Josef | 60cc9d2 | 2017-01-18 12:02:14 +0100 | [diff] [blame] | 42 | print("SUCCESS") |
| 43 | }} |
| 44 | """ # noqa |
| 45 | |
| 46 | def 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 Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def 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 Josef | a081153 | 2017-05-16 18:00:56 +0200 | [diff] [blame^] | 117 | set_admin_email_groovy, {"email": email}) |
Jakub Josef | dfb288c | 2017-04-27 17:29:00 +0200 | [diff] [blame] | 118 | 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 |