def echo_contents(self, text1, text2): """ Echo contents sample jenkins job. """ jenkinsapi_obj = api.Jenkins(self.jenkinsurl, username=self.jenkins_username, password=self.jenkins_password) jenkinsapi_obj.build_job('echo_contents', { 'text1': text1, 'text2': text2 })
def workerGetLastBuildNumber(jobName, userName, password): j = api.Jenkins('http://acrobuild', userName, password) job = j.get_job(jobName) buildNumber = job.get_last_buildnumber() lastGoodBuildNumber = job.get_last_good_buildnumber() lastGoodBuildTime = datetime.datetime.fromtimestamp( job.get_last_good_build().get_timestamp() / 1000).strftime('%Y-%m-%d %H:%M:%S') state = "Ready" if (job.is_running()): state = "Runnig..." elif (job.is_queued()): state = "Queued..." print "'%s'=%s is %s, LastGood=%s @ %s" % ( jobName, buildNumber, state, lastGoodBuildNumber, lastGoodBuildTime) return
def __init__(self): if len(sys.argv) != 2: sys.exit("Usage: ./%s jobname" %sys.argv[0]) with open('datamap.yml','r') as ymlfile: config=yaml.load(ymlfile) yamlMeta = (config['datamap']) self.jenkinsUrl = yamlMeta['jenkinshost'] self.user = yamlMeta['jenkinsuser'] self.token = yamlMeta['token'] self.sparkRoom = yamlMeta['sparkRoom'] self.jobName = sys.argv[1] self.jenkins = api.Jenkins(self.jenkinsUrl, username=self.user, password=self.token) self.job = self.jenkins.get_job(self.jobName) self.build = self.job.get_last_build()
def server_create_test(self, subnet, profile, node_name): """ Create a server on aws_obj. """ jenkinsapi_obj = api.Jenkins(self.jenkinsurl, username=self.jenkins_username, password=self.jenkins_password) if profile == "ops-general": jenkinsapi_obj.build_job('server_create_test', { 'subnet': subnet, 'profile': profile, 'name': node_name }) else: jenkinsapi_obj.build_job('server_create_test', { 'subnet': subnet, 'profile': profile })
def check_build_status(self, job_name): status_dict = {} try: jenkinsapi_obj = api.Jenkins(self.jenkinsurl, username=self.jenkins_username, password=self.jenkins_password) job = jenkinsapi_obj.get_job(job_name) build = job.get_last_build() other_info = self.get_jenkins_job_info(job_name) if other_info: status_dict['other_info'] = self.get_jenkins_job_info(job_name) status_dict['console_output'] = self.get_console_output(build) if build.is_running(): status_dict['exit_status'] = "Build not complete" status_dict['action_state'] = "action_in_progress" else: if build.is_good(): status_dict['exit_status'] = "Build Successful" status_dict['action_state'] = "action_completed" return status_dict except Exception as exp_object: status_dict['action_state'] = 'action_failed' return status_dict
# version: 2.002 import xmlrpclib from jenkinsapi import api jenkins = api.Jenkins('http://localhost:8080') twister_job = jenkins.get_job( 'twister') # The Jenkins job is called `twister` ! twister_build = twister_job.get_last_build() twister_status = twister_build.get_status() print 'Status:', twister_status if twister_build.is_good(): print 'The build passed successfully' else: print 'The build is not successful!' exit(1) # Central Engine is considered to run on localhost:8000 server = xmlrpclib.ServerProxy('http://127.0.0.1:8000/') to_send = 'Jenkins: Job `{0}`, Build `{1}`, Status `{2}`!'.format( twister_job, twister_build, twister_status) server.echo(to_send) # The Twister user is called `jenkins` server.run_plugin('jenkins', 'Jenkins', { "command": True, "build": twister_build })
def jenkins_build_userinfo(self, jobname): job_info_dict, job_info = {}, {} jenkinsapi_obj = api.Jenkins(self.jenkinsurl, username=self.jenkins_username, password=self.jenkins_password) jenkins_url = self.python_jenkinsurl + self.get_jenkins_job_folder( jobname) jenkins_obj = jenkins.Jenkins(jenkins_url, username=self.jenkins_username, password=self.jenkins_password) build_user_info_dict = collections.defaultdict(dict) try: if jenkins_obj.job_exists(jobname): job_info = jenkins_obj.get_job_info(jobname) build_information_list = job_info['builds'] latest_build_number = build_information_list[0]['number'] build_info = jenkins_obj.get_build_info( jobname, latest_build_number) for build_number in range( latest_build_number - self.build_record_count, latest_build_number + 1): try: build_info_dict = jenkins_obj.get_build_info( jobname, build_number) build_user_info_dict[build_number] = { 'deployed_by': "", 'branch': "", 'last_deployed': "", 'subnet': "", 'commit_hash': "" } branch = "" if 'actions' in build_info_dict: if 'parameters' in build_info_dict['actions'][0]: for parameter_dict in build_info_dict[ 'actions'][0]['parameters']: if parameter_dict['name'] == 'subnet': build_user_info_dict[build_number][ 'subnet'] = parameter_dict['value'] if parameter_dict['name'] == 'branch': build_user_info_dict[build_number][ 'branch'] = parameter_dict['value'] branch = parameter_dict['value'] if 'causes' in build_info_dict['actions'][1]: actions = build_info_dict['actions'][1] if 'userName' in actions['causes'][0]: build_user_info_dict[build_number][ 'deployed_by'] = build_info_dict[ 'actions'][1]['causes'][0][ 'userName'] if 'buildsByBranchName' in build_info_dict[ 'actions'][2]: commit_hash = build_info_dict['actions'][2][ 'buildsByBranchName']['origin/develop'][ 'revision']['SHA1'][:7] build_user_info_dict[build_number][ 'commit_hash'] = commit_hash if 'timestamp' in build_info_dict: timestamp = str(datetime.datetime.now() - datetime.datetime.fromtimestamp( build_info_dict['timestamp'] / 1000)) deployed_before = "" if isinstance(timestamp, list): hours_minutes = timestamp[1].split(":")[:2] deployed_before = timestamp[ 0] + " " + hours_minutes[ 0] + "hrs " + hours_minutes[1] + "mins" else: hours_minutes = timestamp.split(":")[:2] deployed_before = hours_minutes[ 0] + " hrs " + hours_minutes[1] + " mins" build_user_info_dict[build_number][ 'last_deployed'] = deployed_before except: continue return self.ah_obj.defaultdict_to_dict(build_user_info_dict) except Exception as exp_object: return {}
if __name__ == "__main__": # Store the command line arguments jenkins_url = sys.argv[1] build_job_name = sys.argv[2] build_job_build_number = int(sys.argv[3]) valid_test_jobs = set(sys.argv[4].split()) required_tests = set(sys.argv[5].split()) # Fetch the downstream build info from jenkins import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) req = Requester(None, None, baseurl=jenkins_url, ssl_verify=False) jenkins = api.Jenkins(jenkins_url, requester=req) assert jenkins.get_jobs_list() # A test we are logged in job = jenkins.get_job(build_job_name) build = job.get_build(build_job_build_number) # Gather the fingerprints that link together the jobs downstream_jobs_names = build.job.get_downstream_job_names() fingerprint_data = build.get_data( "%s?depth=2&tree=fingerprint[fileName,usage[name,ranges[ranges[start,end]]]]" % build.python_api_url(build.baseurl)) usages = [ ] # Usages are a record of a fingerprint being used in specific test runs for fingerprint in fingerprint_data["fingerprint"]: if fingerprint["fileName"] == "build_info.txt": usages.append(fingerprint["usage"])
def buildCrsPackage(self,event): try: self.logger.debug('jobParams: %s, %s' % (self.jobParams, self.packageName)) except AttributeError: self.logger.error('Get error %s when fetching parameters ' % traceback.format_exc()) return req_data = urllib.urlencode(self.jobParams) self.logger.debug("request data : %s" % req_data) crsPackage = self.packageName crsUrl = CRS_URL + '/job/' + crsPackage + '/buildWithParameters' self.logger.debug('CRS URL used is ' ,crsUrl) req = urllib2.Request(crsUrl,req_data) req.add_header('Content-Type', 'application/x-www-form-urlencoded') count = 0 queueIDCount = 0 if Debug: return # Firstly try to push it into queue while count < 3: self.logger.debug(count,' time build with link ',crsUrl, " ",req_data) try: response = urllib2.urlopen(req) if response.code == 201: self.logger.debug('Build response successfully') else: self.logger.error('Not expected response code %s ' % response.code) return self.logger.debug('Build parameter response %s ' % response.info()) queueLocation = response.info().get('Location') queueUrl = queueLocation + 'api/json' jobId = None while True: try: queueResp = urllib2.urlopen(queueUrl) queueInfo = queueResp.read() self.logger.debug(queueInfo) time.sleep(5) # wait till it past quiet period if re.search('"why":null', queueInfo, re.I): jobGrp = re.search('"executable":{"number":(\d+),', queueInfo, re.I) if jobGrp: jobId = jobGrp.groups()[0] self.logger.info('Get jobId %s for package %s' % (jobId, crsPackage)) break elif re.search('"cancelled":true', queueInfo, re.I): self.logger.info('******** One job cancelled during quieting...*******') return else: #1. "why":"In the quiet period. Expires in ..." #2. "why":"Waiting for next available executor on..." self.logger.debug('wait till past quieting or waiting for executor...') except SocketError as se: if se.errno != errno.ECONNRESET and se.errno != errno.ETIMEDOUT: raise self.logger.warning('Server too busy. Torture it a little bit later') time.sleep(5) except URLError as ue: if re.search('Errno 104', ue.reason, re.I) \ or re.search('Errno 110', ue.reason, re.I): self.logger.warning('Server too busy. Access it a little bit later') time.sleep(5) else: raise # Now I got a jobId jenkins = api.Jenkins(CRS_URL) job = jenkins.get_job(crsPackage) currentBuild = job.get_build(int(jobId)) self.logger.debug(currentBuild) jobLink = CRS_URL + '/job/' + crsPackage + '/' + jobId while True: try: while currentBuild.is_running() : self.logger.info('Build %s for package %s is running' % (jobId, crsPackage)) time.sleep(30) break except Exception as ex: if hasattr(ex, 'message'): msg = repr(ex.message) self.log.debug('Got exception of %s' % msg) if re.search('JenkinsAPIException', msg, re.I): self.logger.debug('Temp error from message %s , ignore it' % msg) else: raise elif re.search('JenkinsAPIException', repr(ex), re.I): self.logger.debug('Temp error from ex %s, ignore it' % ex) else: info = traceback.format_exc() if re.search('JenkinsAPIException', info, re.I): self.logger.debug('Temp error from stacktrace %s, ignore it' % info) else: raise self.logger.info('current job status %s' % currentBuild.get_status() ) jobStatus = currentBuild.get_status().strip().upper() self.logger.info('Build %s completed with status %s.' % (jobLink, jobStatus)) if jobStatus == 'SUCCESS': self.logger.info('job success, run next job') else: self.logger.info('job failure, run next job') return except HTTPError as err: errMsg = err.read() self.logger.debug('Get HTTP error %s ' % errMsg) #Ignore other 500 error if err.code == 500 and re.search('IllegalArgumentException', errMsg, re.I): self.logger.info('!!!HTTP 500 error!!! Caused by illegal argument and \ check configuration of %s !!!' % crsUrl) return elif err.code == 404: self.logger.info('!!!HTTP 404 error!!! Which is usually caused by unmatched packagename!!!') return except xmlrpclib.Fault as xmlErr: self.logger.error("A fault occurred: Fault code: " "%s Fault string: %s. Retry until success." % (err.faultCode, err.faultString)) except Exception as ex: if re.match('HTTPSConnectionPool', repr(ex), re.I): self.logger.info('Temp https connection issue %s. Retry until success.' % ex) else: self.logger.error('!!!Hitting abnormal and exit %s:%s !!!' % (ex, traceback.format_exc())) return
if __name__ == '__main__': # Store the command line arguments jenkins_url = sys.argv[1] username = sys.argv[2] password = sys.argv[3] build_job_name = sys.argv[4] build_job_build_number = int(sys.argv[5]) valid_test_jobs = set(sys.argv[6].split()) required_tests = set(sys.argv[7].split()) # Fetch the downstream build info from jenkins requests.packages.urllib3.disable_warnings() req = Requester(username, password, baseurl=jenkins_url, ssl_verify=False) jenkins = api.Jenkins(jenkins_url, username=username, password=password, requester=req) assert jenkins.get_jobs_list() # A test we are logged in job = jenkins.get_job(build_job_name) build = job.get_build(build_job_build_number) # Gather the fingerprints that link together the jobs downstream_jobs_names = build.job.get_downstream_job_names() fingerprint_data = build.get_data("%s?depth=2&tree=fingerprint[fileName,usage[name,ranges[ranges[start,end]]]]" % build.python_api_url(build.baseurl)) usages = [] # Usages are a record of a fingerprint being used in specific test runs for fingerprint in fingerprint_data['fingerprint']: if fingerprint['fileName'] == 'build_info.txt': usages.append(fingerprint['usage']) # Use the fingerprint data to get a unique list of test runs for the build unique_runs = compile_unique_runs(usages)