def login(self): ''' :return: jenkins login instance ''' server = jenkins.Jenkins(self.jenkins_url, username=self.jenkins_username, password=self.jenkins_password) return server
def __init__(self, jenkins_server, Username, Password): self.jenkins_server = jenkins_server self.Username = Username self.Password = Password # global server try: self.server = jenkins.Jenkins(self.jenkins_server, username=self.Username, password=self.Password) self.logger.info("jenkins server is connected!") except requests.exceptions.ConnectionError: infos = sys.exc_info() self.logger.error(infos[0], ":", infos[1]) except requests.exceptions.HTTPError: info = sys.exc_info() if "401 Client Error" in info[1]: try: self.server = jenkins.Jenkins(self.jenkins_server, username=self.Username, password=self.Password) except requests.exceptions.HTTPError: infos = sys.exc_info() if "401 Client Error" in info[1]: self.logger.error(infos[0], ":", infos[1], "failed again!!!!!!!!!!") self.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0", "Content-Type": "application/x-www-form-urlencoded" } autherization_info = { "j_username": self.Username, "j_password": self.Password, "Submit": "登录", "from": "" } autherization_url = self.jenkins_server + "j_acegi_security_check" self.opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cookielib.CookieJar())) req = urllib2.Request(autherization_url, data=urllib.urlencode(autherization_info), headers=self.headers) self.opener.open(req).read() urllib2.install_opener(self.opener)
def get_jobs(): jenk = jenkins.Jenkins(JENKINS_URL) jobs = [] for item in jenk.items(): if str(item[1]) in [str(job) for job in jobs]: continue elif str(item[1]) in NON_GPU_JOBS: continue jobs.append(item[1]) return jobs
def connect_to_jenkins(): server = None try: server = jenkins.Jenkins(jenkins_url, username=jenkins_username, password=jenkins_password) except Exception as e: # Ideally, this would be logged, and would not be a wildcard catchall. print e.message return server
def trgrTlchnJbs(self): url = 'http://%s-' % (self.senv.lower()) try: j = jenkins.Jenkins(url, self.sloginusr, self.sloginpswd) except Exception, e: serr = ( '%s::trgrTlchnJbs():\ Jenkins(%s, %s, %s), %s' % (self.sclsnme, url, self.sloginusr, self.sloginpswd, str(e))) self.opygenericroutines.prntLogErrWarnInfo(serr)
def load(self): """Loads the Jenkins object""" # Resets the logging level. logger = logging.getLogger('jenkinsapi.job') logger.setLevel(logging.WARNING) logger = logging.getLogger('jenkinsapi.build') logger.setLevel(logging.WARNING) # Loads the jenkins object. self._jenkins = jenkins.Jenkins(self.url, requester=CrumbRequester(baseurl=self.url))
def __init__(self, config): if not config: raise RuntimeError("No configuration file provided") jenkins_instance_url = config.get('defaults', 'url') jenkins_user = config.get('defaults', 'username') jenkins_passwd = config.get('defaults', 'password') self.config = config self.connection = jenkins.Jenkins(baseurl=jenkins_instance_url, username=jenkins_user, password=jenkins_passwd)
def sync_jenkins_jobs(): J = jenkins.Jenkins(app.config['JENKINS_URL'], app.config['JENKINS_USER'], app.config['JENKINS_PASSWD']) print(J.jobs.__repr__()) from inspect import isgenerator assert isgenerator(J.jobs.iteritems()) for job in J.jobs.iteritems(): job_name = job[0] job_description = job[1].get_description() job_params = str(job[1].get_params_list()) job_state = 1 if job[1].is_enabled() else 0 #last_build = job[1].get_build_metadata(job[1].get_last_buildnumber()) # print last_build.get_status() #print last_build.get_params() # print last_build.get_causes() # print last_build.get_actions() # print last_build.get_console() # print last_build.baseurl #print Jenkins_job.query.filter_by(job_name=job[0]).scalar() != None # print Jenkins_job.query.filter_by(job_name=job[0]).count() != 1 # #print db.session.query(Jenkins_job.job_name).filter_by(job_name=job[0]).scalar() is not None if db.session.query( exists().where(Jenkins_job.job_name == job[0])).scalar(): print "the job %s exists , update ..." % job[0] row_changed = Jenkins_job.query.filter_by(job_name=job[0]).update( dict(job_description=job_description, job_params=job_params, job_state=job_state)) db.session.commit() else: jenkins_job = Jenkins_job(job_name=job_name, job_description=job_description, job_params=job_params, job_state=job_state) db.session.add(jenkins_job) db.session.commit() # delete exists in db but not jenkins db_jobs = [ x[0] for x in db.session.query(Jenkins_job.job_name).filter().all() ] print db_jobs jk_jobs = [x[0] for x in J.jobs.iteritems()] print jk_jobs for job in list(set(db_jobs).difference(set(jk_jobs))): print "the job %s exists in db , but not jenkins , clean it " % job
def __init__(self, jenkins_server, Username, Password, job_name): self.jenkins_server = jenkins_server self.Username = Username self.Password = Password self.job_name = job_name #global server try: self.server = jenkins.Jenkins(self.jenkins_server, username=self.Username, password=self.Password) print "jenkins server is connected!" except requests.exceptions.ConnectionError: info = sys.exc_info() print info[0], ":", info[1] sys.exit(1)
def __init__(self, server, username, token, email, integrate_slack=False, slack_channel=None, slack_token=None): self.jk = jenkins.Jenkins(server, username=username, password=token, timeout=100, lazy=True) self.email = email self.integrate_slack = integrate_slack self.slack_channel = slack_channel if integrate_slack: self.slack = Slack(slack_token)
def startbuild(site,target,country,ip): record = { 'url':site, 'target':target, 'country':country, 'ip':ip } uploadurl = "http://127.0.0.1:8081/artifactory/phish/"+ip+"/" propurl = "http://127.0.0.1:8081/artifactory/api/storage/phish/"+ip+"/?properties=target="+target+";country="+country+";ip="+ip+";url="+site r = requests.put(uploadurl, auth=(username,password)) #Creates directory r = requests.put(propurl, auth=(username,password)) #Adds properties to directories J = jenkins.Jenkins("http://127.0.0.1:8080", username="******", password="******") J.poll() url = 'http://127.0.0.1:8080/job/Recon/buildWithParameters?ip='+ip"&url="+site headers = {'Content-Type': 'application/x-www-form-urlencoded'} response = requests.post(url, headers=headers,auth=(jusername,jpassword))
def verb_compare(args, pr_job_name, pr_build_num, ci_job_name): """Compare test failures in a PR job to the history in a CI job.""" print('Comparing', pr_job_name, '#', pr_build_num, 'to', ci_job_name) username, password = get_credentials() server = jenkins.Jenkins(args.jenkins_uri, username=username, password=password) pr_job = server.get_job(pr_job_name) ci_job = server.get_job(ci_job_name) # Get failures from the PR job pr_build = pr_job.get_build(pr_build_num) pr_failures = {} for result in test_results(pr_build): if result.status not in ['PASSED', 'FIXED']: print('PR', result.identifier(), result.status) pr_failures[result.identifier()] = [] if not pr_failures: print("No failures found in {name}".format(name=pr_build.name)) # Get failures from recent CI jobs for build in recent_builds(ci_job): print("Checking", build.name) for result in test_results(build): if result.status not in ['PASSED', 'FIXED']: if result.identifier() in pr_failures: pr_failures[result.identifier()].append((build, result)) print('---------------Markdown---------------') print('**Build [{name}]({url})**\n'.format(name=pr_build.name, url=pr_build.baseurl)) for test_name, others in pr_failures.items(): print('* Test `{test_name}`'.format(test_name=test_name)) if not others: print(' * **Did not fail in other recent builds**') else: for ci_build, result in others: print(' * Also failed in [{name}]({url})'.format( name=ci_build.name, url=ci_build.baseurl)) print('---------------Markdown---------------')
def __init__(self, jenkins_server, username, password): """ 初始化Jenkins客户端的连接 :param jenkins_server: Jenkins server的地址, 如:http://10.115.101.230:8080/jenkins/ :param username: 认证的用户名 :param password: 认证的密码 :return: None """ self.jenkins_server = jenkins_server self.username = username self.password = password try: self.server = jenkins.Jenkins(self.jenkins_server, username=self.username, password=self.password) logging.info("jenkins server is connected!") except requests.exceptions.ConnectionError: info = sys.exc_info() logging.error("jenkins is connected failed!") sys.exit(1) self.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0", "Content-Type": "application/x-www-form-urlencoded" } autherization_info = { "j_username": self.username, "j_password": self.password, "Submit": "登录", "from": "" } autherization_url = self.jenkins_server + "j_acegi_security_check" self.opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cookielib.CookieJar())) req = urllib2.Request(autherization_url, data=urllib.urlencode(autherization_info), headers=self.headers) self.opener.open(req).read() urllib2.install_opener(self.opener)
def verb_tally_failures(args, job_names): print('Tallying recent flaky tests for ', job_names) username, password = get_credentials() server = jenkins.Jenkins(args.jenkins_uri, username=username, password=password) # Get failures from recent jobs build_urls = {} test_failures = {} for job_name in job_names: job = server.get_job(job_name) for build in recent_builds(job, num=7): build_urls[build.name] = build.baseurl print("Checking", build.name) for result in test_results(build): if result.status not in ['PASSED', 'FIXED']: if result.identifier() not in test_failures: test_failures[result.identifier()] = 0 test_failures[result.identifier()] += 1 # Order failures from most to least test_failures = list(test_failures.items()) test_failures.sort(key=lambda t: t[1], reverse=True) print('---------------Markdown---------------') print('**In these %d builds**\n' % len(build_urls)) for build_name, build_url in build_urls.items(): print('* [{name}]({url})'.format(name=build_name, url=build_url)) print('\n**These were the test failures**\n') for test_name, count in test_failures: print('* *{per}%* **{count}** failures of `{test_name}`'.format( test_name=test_name, count=count, per=int(float(count) / len(build_urls) * 100.0))) print('---------------Markdown---------------')
def __init__(self, username=None, passwd=None, url="http://hudson.lab.vmops.com", job='CloudStack-PRIVATE'): #TODO: Change the username to "vogon" for automation self.hudsonurl = url self.tarball = None self.build_number = 0 #self.jenkinsurl = "http://jenkins.jobclient.org" if username and passwd: self.username = username self.password = passwd else: logging.warning("no username given, logging in with default creds") self.username = "******" self.password = "******" try: j = jenkins.Jenkins(self.hudsonurl, self.username, self.password) self.jobclient = j.get_job(job) except Exception as e: logging.error("Failed to login to Hudson") raise e else: logging.debug("successfully logged into hudson instance %s \ using username, passwd : %s, %s" \ %(self.hudsonurl, self.username, self.password))
print 'Deleting %s...' % host, print api.delete_node(host) def creds(path, section): """An ini file with a section per master. Should look something like this: [master-a] user=foo key=7deadbeef1234098 [master-b] user=bar key=7deadbeef9999999 """ config = ConfigParser.SafeConfigParser() config.read(ini) return config.get(section, 'user'), config.get(section, 'key') if __name__ == '__main__': cmd, host, kind, ini, agent = sys.argv[1:] user, key = creds(ini, agent) J = jenkins.Jenkins('http://localhost:8080', user, key) if sys.argv[1] == 'delete': delete(J, host) else: create(J, host, info(host, kind))
""" A lower level example of how we login with authentication """ from __future__ import print_function from jenkinsapi import jenkins J = jenkins.Jenkins("http://localhost:8080", username="******", password="******") J.poll() print(J.items())
# _*_ coding: utf-8 _*_ # # ref doc: http://stackoverflow.com/questions/14531919/node-labels-from-jenkins-api # JENKINS_URL = "http://$Jenkins_server:8080" from jenkinsapi import jenkins from BeautifulSoup import BeautifulSoup from urllib2 import urlopen import re node_labels = dict() jenkins_obj = jenkins.Jenkins(JENKINS_URL) node_names = jenkins_obj.get_nodes().keys() #TODO # hard-code the to-be-find node name patterns pattern = 'PVGD' for node_name in node_names: if node_name != "master" and re.match(pattern, node_name): req = urlopen('{}/computer/{}'.format(JENKINS_URL, node_name)) soup = BeautifulSoup(req.read()) node_labels[node_name] = [ tag.text for tag in soup.findAll("a", {"class": "tag0 model-link inside"}) ] # data structure: # for li in node_labels.items(): # print li # #output: ('$Node1', [u'builder', u'windows'])
} projects = {} for i in range(948, 1064): try: build_id = HandleJenkins.server.jobs["Monkey"].get_build(i) if build_id.is_good(): upstream_job_name = build_id.get_upstream_job_name() project_name = upstream_job_name[:-( len(upstream_job_name.split("_")[-1]) + 1)] if project_name not in projects.keys(): projects[project_name] = 1 else: projects[project_name] = projects[project_name] + 1 except: HandleJenkins.server = jenkins.Jenkins( "http://10.115.101.230:8080/jenkins/", "jenkins.cdval", "I8IBz0U2TC") build_id = HandleJenkins.server.jobs["Monkey"].get_build(i) if build_id.is_good(): upstream_job_name = build_id.get_upstream_job_name() project_name = upstream_job_name[:-len( upstream_job_name.split("_")[-1]) + 1] if project_name not in projects.keys(): projects[project_name] = 1 else: projects[project_name] = projects[project_name] + 1 for key in projects.keys(): print key, projects[key] # HandleJenkins.build_job_new(job_name, params) # HandleJenkins.online_node("CD_VAL97_10104_x1_plus_6.0.0_6DCE5HVW8PNFPNQS")
# Time: 2021/1/4 9:54 # Author: jiangzhw # FileName: jenkinsapi_study.py from jenkinsapi import jenkins jenkins_url = 'http://172.22.56.97:8080' jenkins_user = '******' api_token = '11e526dc0b7e7a419de30614eb81fda868' jenkins_password = '******' job_name = r'jiangzhw/python_jenkins_api_demo01' # 实例化jenkins对象,连接远程的jenkins master server J = jenkins.Jenkins(jenkins_url, jenkins_user, api_token) # J.build_job(job_name) # print(J.get_job(job_name)) # 打印一下server查是否连接成功 # 返回一个jenkins对象<jenkins.Jenkins object at 0x10807d190> print(J) # 打印jenkins 版本信息 print(J.version) # 构建job名为testJob的job(不带构建参数) J.build_job(job_name) # 查看某个job的构建信息 job_info = J.get_job(job_name) print(job_info)
def __missing__(self, key): self[key] = jenkins.Jenkins(key) return self[key]
# if result.identifier() in new_failures: # # Confirmed for being in multiple jobs of the most recent build # del new_failures[result.identifier()] # confirmed_failures.append(result) # else: # # Possibly new # new_failures[result.identifier()] = result # # # Now look at second job to confirm failures that way # if len(job.builds) == 1: # continue # second_build = job.builds[1] if __name__ == '__main__': server = jenkins.Jenkins('https://ci.ros2.org', username=os.getenv('JENKINS_GITHUB_USER'), password=os.getenv('JENKINS_GITHUB_TOKEN')) # The very latest failures print_latest_failures(server, [ 'nightly_linux-aarch64_debug', 'nightly_linux-aarch64_release', 'nightly_linux-aarch64_extra_rmw_release', 'nightly_linux_debug', 'nightly_linux_release', 'nightly_linux_extra_rmw_release', 'nightly_osx_debug', 'nightly_osx_release', 'nightly_osx_extra_rmw_release', 'nightly_win_deb', 'nightly_win_rel', 'nightly_win_extra_rmw_rel', 'nightly_xenial_linux-aarch64_release', 'nightly_xenial_linux_release' ]) # Confirmed vs new failures # TODO
def _get_api(self, baseurl, username, password): return jenkins.Jenkins(baseurl, username, password)
from jenkinsapi import api, jenkins, job import string import xlwt wbk = xlwt.Workbook() username = "******" password = '' j = jenkins.Jenkins('http://10.223.240.215', username, password) #jobs = ['Adv_XS_BVT_Report', 'Adv_XS_Regression_Report', 'Basic_XS_BVT_Report', 'Basic_XS_Regression_Report'] #, 'Eip_elb_BVT_Report', 'Eip_elb_XS_Regression_Report'] #jobs = ['Adv_XS_BVT_Report','Adv_VMware_BVT_Report', 'Adv_KVM_BVT_Report','Basic_KVM_BVT_Report', 'Basic_XS_BVT_Report'] #jobs = ['Adv_Simulator_BVT_Report'] #jobs = ['Adv_BVT_Report', 'Adv_Regression_Report'] jobs = ['Adv_Regression_Report'] for job in jobs: jobclient = j.get_job(job) sheet = wbk.add_sheet(job) # indexing is zero based, row then column sheet.write(0, 0, 'test case') sheet.write(0, 1, 'status') sheet.write(0, 2, 'Duration') sheet.write(0, 3, 'Error Description') sheet.write(0, 4, 'Comments') sheet.write(0, 5, 'Bug') sheet.write(0, 6, 'Bug_type') row = 1 #for matrix job #p = jobclient.get_last_build().get_resultset()._data ##cases = p["suites"][0]["cases"]
subdomain) + " - " + subdomain print(Style.RESET_ALL) ip = socket.gethostbyname(subdomain) if ip not in ips: ips.append(ip) else: print "duplicate IP" domains.append(subdomain) except Exception: pass print ips print domains for ipp in ips: J = jenkins.Jenkins("http://127.0.0.1:8080", username="******", password="") #insert jenkins password J.poll() url = 'http://127.0.0.1:8080/job/nmapscan/buildWithParameters?ip=' + ipp headers = {'Content-Type': 'application/x-www-form-urlencoded'} response = requests.post(url, headers=headers, auth=(jusername, jpassword)) #Calls your Jenkins builds for port scanning #nmap -T4 -A -v $ip for domain in domains: J = jenkins.Jenkins("http://127.0.0.1:8080", username="******", password="") #insert jenkins password J.poll() url = 'http://127.0.0.1:8080/job/nmapscan/buildWithParameters?=' + domain headers = {'Content-Type': 'application/x-www-form-urlencoded'} response = requests.post(url, headers=headers, auth=(jusername, jpassword)) #Calls your Jenkins builds for nikto scan
def jenkins(self): if self.__jenkins__ is None: self.__jenkins__ = jenkins.Jenkins(self.url, username=self.username, password=self.password) return self.__jenkins__
return warnings def num_failed_tests(build): """Return the number of tests that failed.""" num = 0 for result in test_results(build): if result.status not in ['PASSED', 'FIXED', 'SKIPPED']: num += 1 return num if __name__ == '__main__': username, password = get_credentials() server = jenkins.Jenkins('https://build.osrfoundation.org', username=username, password=password) # TODO get jobs using regex job_names = [ u'gazebo-ci-default-artful-amd64-gpu-none', u'gazebo-ci-default-bionic-amd64-gpu-none', u'gazebo-ci-default-homebrew-amd64', u'gazebo-ci-default-windows7-amd64', u'gazebo-ci-default-xenial-amd64-gpu-none', u'gazebo-ci-default-xenial-amd64-gpu-nvidia', u'gazebo-ci-default-xenial-i386-gpu-none', u'gazebo-ci-gazebo7-homebrew-amd64', u'gazebo-ci-gazebo7-xenial-amd64-gpu-nvidia', u'gazebo-ci-gazebo8-homebrew-amd64', u'gazebo-ci-gazebo8-windows7-amd64',
def get_jenkins(): return jenkins.Jenkins('http://192.168.10.13:8080', 'krahul', 'ebbd7121de324db16ccdc304111be3d4',timeout=10)
from jenkinsapi import api, jenkins, job username = "" password = "" j = jenkins.Jenkins('http://ccp-tests.xenrt.xs.citrite.net/jenkins', username, password) jobclient = j.get_job('xenrt-regression-adv-hyperv') #for matrix job #p = jobclient.get_last_build().get_resultset()._data ##cases = p["suites"][0]["cases"] ##print len(cases) #count = 0 #f = open('/Users/talluri/example.csv','w') #f.write("%s, %s, %s\n"%('Test Case', 'Status', 'error')) #for childreport in p['childReports']: # cases= childreport['result']['suites'][0]['cases'] # for case in cases: # #if str(case['errorDetails']).find('Unable to stop') != -1: # if case['status'] == 'FAILED' or case['status'] == 'ERROR' or case['status'] == 'REGRESSION': # if count == 1: # print case # #print "test case:{0}, status: {1}, error: {2} ".format(case['name'], case['status'], case['errorDetails']) # print "test case:{0}, status: {1}".format(case['name'], case['status']) # f.write("%s, %s, %s\n" % (case['name'],case['status'], case['status'])) # count += 1 #f.close() #print count p = jobclient.get_last_build().get_resultset()._data ##cases = p["suites"][0]["cases"] cases = p["suites"][0]["cases"]
print 'Deleting %s...' % host, print api.delete_node(host) def creds(path, section): """An ini file with a section per master. Should look something like this: [master-a] user=foo key=7deadbeef1234098 [master-b] user=bar key=7deadbeef9999999 """ config = ConfigParser.SafeConfigParser() config.read(path) return config.get(section, 'user'), config.get(section, 'key') if __name__ == '__main__': CMD, HOST, KIND, INI, AGENT = sys.argv[1:] # pylint: disable=unbalanced-tuple-unpacking USER, KEY = creds(INI, AGENT) J = jenkins.Jenkins('http://localhost:8080', USER, KEY) if sys.argv[1] == 'delete': delete(J, HOST) else: create(J, HOST, info(HOST, KIND))