| import logging |
| |
| logger = logging.getLogger(__name__) |
| |
| set_smtp_groovy = """\ |
| def result = "" |
| for(desc in [Jenkins.getInstance().getDescriptor("hudson.plugins.emailext.ExtendedEmailPublisher"),Jenkins.getInstance().getDescriptor("hudson.tasks.Mailer")]){ |
| if(desc.getSmtpServer().equals("${host}") && |
| ((desc instanceof hudson.plugins.emailext.ExtendedEmailPublisherDescriptor && desc.getSmtpAuthUsername().equals("${username}")) || |
| (desc instanceof hudson.tasks.Mailer$DescriptorImpl && desc.getSmtpAuthUserName().equals("${username}"))) && |
| desc.getSmtpAuthPassword().toString().equals("${password}") && |
| desc.getSmtpPort().equals("${port}") && |
| desc.getUseSsl() == ${ssl} && |
| desc.getCharset().equals("${charset}") && |
| (!${reply_to_exists} || desc.getReplyAddress().equals("${reply_to}"))){ |
| result = "EXISTS" |
| }else{ |
| desc.setSmtpAuth("${username}", "${password}") |
| desc.setUseSsl(${ssl}) |
| if(desc instanceof hudson.plugins.emailext.ExtendedEmailPublisherDescriptor){ |
| desc.setSmtpServer("${host}") |
| }else{ |
| desc.setSmtpHost("${host}") |
| } |
| desc.setSmtpPort("${port}") |
| desc.setCharset("${charset}") |
| if({reply_to_exists}){ |
| desc.setReplyToAddress("${reply_to}") |
| } |
| desc.save() |
| result = "SUCCESS" |
| } |
| } |
| print(result) |
| """ # noqa |
| |
| set_admin_email_groovy = """ |
| def jenkinsLocationConfiguration = JenkinsLocationConfiguration.get() |
| if(jenkinsLocationConfiguration.getAdminAddress().equals("${email}")){ |
| print("EXISTS") |
| }else{ |
| jenkinsLocationConfiguration.setAdminAddress("${email}") |
| jenkinsLocationConfiguration.save() |
| print("SUCCESS") |
| } |
| """ # noqa |
| |
| |
| def __virtual__(): |
| ''' |
| Only load if jenkins_common module exist. |
| ''' |
| if 'jenkins_common.call_groovy_script' not in __salt__: |
| return ( |
| False, |
| 'The jenkins_smtp state module cannot be loaded: ' |
| 'jenkins_common not found') |
| return True |
| |
| |
| def config(name, host, username, password, reply_to=None, |
| port=25, ssl=False, charset="UTF-8"): |
| """ |
| Jenkins SMTP server config state method |
| |
| :param name: configuration name |
| :param host: SMTP host |
| :param username: SMTP username |
| :param password: SMTP password |
| :param reply_to: sent emails ReplyTo header (optional) |
| :param port: SMTP port (optional, default 25) |
| :param ssl: use SSL for SMTP (optional, default False) |
| :param charset: SMTP charset (optional, default UTF-8) |
| :returns: salt-specified state dict |
| """ |
| test = __opts__['test'] # noqa |
| ret = { |
| 'name': name, |
| 'changes': {}, |
| 'result': False, |
| 'comment': '', |
| } |
| result = False |
| if test: |
| status = "SUCCESS" |
| ret['changes'][name] = status |
| ret['comment'] = 'Jenkins SMTP config %s %s' % (name, status.lower()) |
| else: |
| call_result = __salt__['jenkins_common.call_groovy_script']( |
| set_smtp_groovy, {"username": username, "password": password, "host": host, |
| "reply_to_exists": "true" if reply_to else "false", |
| "reply_to": reply_to, |
| "port": port if port else 25, |
| "ssl": "true" if ssl else "false", |
| "charset": charset if charset else "UTF-8"}) |
| if call_result["code"] == 200 and call_result["msg"] in [ |
| "SUCCESS", "EXISTS"]: |
| status = call_result["msg"] |
| if status == "SUCCESS": |
| ret['changes'][name] = status |
| ret['comment'] = 'Jenkins smtp config %s %s' % ( |
| name, status.lower()) |
| result = True |
| else: |
| status = 'FAILED' |
| logger.error( |
| "Jenkins smtp API call failure: %s", call_result["msg"]) |
| ret['comment'] = 'Jenkins smtp API call failure: %s' % (call_result[ |
| "msg"]) |
| ret['result'] = None if test else result |
| return ret |
| |
| |
| def admin_email(name, email): |
| """ |
| Jenkins Admin user email config state method |
| |
| :param name: jenkins admin email |
| :returns: salt-specified state dict |
| """ |
| test = __opts__['test'] # noqa |
| ret = { |
| 'name': name, |
| 'changes': {}, |
| 'result': False, |
| 'comment': '', |
| } |
| result = False |
| if test: |
| status = "SUCCESS" |
| ret['changes'][name] = status |
| ret['comment'] = 'Jenkins admin email config %s %s' % ( |
| name, status.lower()) |
| else: |
| call_result = __salt__['jenkins_common.call_groovy_script']( |
| set_admin_email_groovy, {"email": email}) |
| if call_result["code"] == 200 and call_result["msg"] in [ |
| "SUCCESS", "EXISTS"]: |
| status = call_result["msg"] |
| if status == "SUCCESS": |
| ret['changes'][name] = status |
| ret['comment'] = 'Jenkins admin email config %s %s' % ( |
| name, status.lower()) |
| result = True |
| else: |
| status = 'FAILED' |
| logger.error( |
| "Jenkins admin email API call failure: %s", call_result["msg"]) |
| ret['comment'] = 'Jenkins admin email API call failure: %s' % (call_result[ |
| "msg"]) |
| ret['result'] = None if test else result |
| return ret |