Polyglot programming using Jenkins
November 1, 2016 Leave a comment
Facility for languages develops when one does not squander existing opportunities to code. That is what I think.
Jenkins, the CI enabler supports a few languages like Python and Groovy. The Python package I used to make the Rest API calls is ‘Python Jenkins’.It is interesting to note that run_script executes Groovy code.
I didn’t test it exactly when the Unix server runs out of disk space but assumed the text from the console output will match.Moreover the encryption routine works as expected but the decryption function doesn’t work. It seems that since I call the Rest API there could be a encryption/decryption key mismatch.
''' Created on Oct 12, 2016 @author: Mohan Radhakrishnan This python module gets the console output of the latest build and if the text 'No space left on device' is found in the output it sends a mail. I've taken liberties with the 'functional paradigm' ''' import smtplib import jenkins import os def main(): overrideenvironmentvariables() server = jenkins.Jenkins('http://localhost:8080', username='Mohan', password='Welcome1') notifydisaster(server) ''' Notify ''' def notifydisaster( server ): print( getconsoleoutput(server) ) name,buildnumber,consoleoutput = getconsoleoutput(server) if (consoleoutput.find("Caused by: java.io.IOException: No space left on device") != -1): print("Caused by: java.io.IOException: No space left on device") sendmail( name,buildnumber ) ''' Notify Password Encryption/decryption code has to be tested and used ''' def sendmail(name,buildnumber): smtp = smtplib.SMTP('smtp.gmail.com', 587) smtp.ehlo() smtp.starttls() smtp.login("x.y@z.com","Password") smtp.sendmail('x.y@z.com', 'x.y@z.com', 'Subject: No space left on device\n \ Job ' + name + ' Build ' + str(buildnumber) + ' fails due to lack of disk space') ''' Get the console output of the particular Job's build ''' def getconsoleoutput(server): information = getJobName(server) if information: return information[ 0 ]['name'] ,getlastjobDetails(server),server.get_build_console_output(information[ 0 ]['name'], getlastjobDetails(server)) ''' Get Job and other details and filter the Job we are interested in ''' def getJobName(server): jobs = server.get_all_jobs(0) filtercriterion = ['CITestPipeline'] return list(filter( lambda d: d['fullname'] in filtercriterion, jobs)) ''' Get Job and other details Return '0' as the build number assuming it signifies that there is no such build number ''' def getlastjobDetails(server): information = getJobName(server) if information: last_build_number = server.get_job_info(information[ 0 ]['name'])['lastCompletedBuild']['number'] return last_build_number else: return 0 ''' Attempt here to encrypt Passwords using Jenkins' key Not tested properly ''' def encrypt(server ): value = server.run_script(""" secret = hudson.util.Secret.fromString("Password") println secret.getEncryptedValue() println secret.getPlainText() """) print (value) def decrypt(server ): decryptedvalue = server.run_script(""" secret = hudson.util.Secret.fromString("aiJREkuBjWHX9UWIyhEzwnnAJReuZnQVEtUr0KgvXKg") println hudson.util.Secret.toString(secret) """) print (decryptedvalue) return decryptedvalue ''' Override this proxy setting as we don't need it and it causes an error. ''' def overrideenvironmentvariables(): os.environ["HTTP_PROXY"] = '' if __name__=="__main__": main()