def get_running_builds(server: Jenkins): # running_builds = [] # get all running builds so we know if a duplicate job is already running running_builds = server.get_running_builds() queued_builds = server.get_queue_info() builds_with_params = [] for build in queued_builds: if "task" in build and "name" in build["task"]: try: parameters = parameters_from_actions(build["actions"]) builds_with_params.append({ "name": build["task"]["name"], "parameters": parameters }) except Exception: traceback.print_exc() for build in running_builds: try: parameters = parameters_for_job(server, build['name'], build['number']) builds_with_params.append({ "name": build['name'], "number": build['number'], "parameters": parameters }) except Exception: traceback.print_exc() return builds_with_params
class myjenkins: def __init__(self): self.server = Jenkins(jenkins_url, username=username, password=token_dict) def getjobconfig(self, job_name): job_info = self.server.get_job_config(job_name) return job_info # 根据给来的app id查询job的完整name,目前只看'0-Site'和'1-API'两个view下的,如果有需要可以加,需要保证这两个view下的所有job在view下都是唯一的 def search_job_name(self, job_id): site = self.server.get_jobs(view_name='0-Site') web = self.server.get_jobs(view_name='1-API') b = site + web job_name = '' for i in b: w = re.findall(job_id, i['name']) if w != []: job_name = i['name'] return job_name # 根据job的配置文件,找到他的git仓库地址,用于判断用户传入的分支名是否存在 def get_git_url(self, job_name): info = self.getjobconfig(job_name) convertedDict = xmltodict.parse(info) git_repo_url = convertedDict['project']['scm']['userRemoteConfigs']['hudson.plugins.git.UserRemoteConfig'][ 'url'] return git_repo_url
def slave_create(node_name, working_dir, executors, labels): j = Jenkins(os.environ['JENKINS_URL'], jenkins_user, jenkins_password) j.node_create(node_name, working_dir, num_executors=int(executors), labels=labels, launcher=NodeLaunchMethod.JNLP)
def __init__(self, config: RootConfig): self.config = config self._create_dirs() if config.db_type == Postgres.DB_TYPE: self.db = Postgres(config.db) elif config.db_type == Mssql.DB_TYPE: self.db = Mssql(config.db) elif config.db_type == Pgdocker.DB_TYPE: self.db = Pgdocker(config.db) else: raise RuntimeError('Unsupported database type') self._write_hibernate_properties() self.jenkins = Jenkins(config.jenkins) os.environ['CATALINA_OPTS'] = config.catalina_opts self.tomcat = None self.last_error = None self.active_task = None self.last_task = None log.info('Test tools started')
def __init__(self): self.url = config.JENKINS_URL self.username = config.JENKINS_NAME self.token = config.JENKINS_TOKEN self.j = Jenkins(config.JENKINS_URL, username=config.JENKINS_NAME, password=config.JENKINS_TOKEN)
def getStatus(): rid = request.args.get("rid") JS_URL = app.config["JENKINS_URL"] app.logger.debug('Connecting to Jenkins %s as %s', JS_URL, app.config["UID"]) js = JenkinsServer(JS_URL, username=app.config["UID"], password=app.config["PWD"]) app.logger.debug('Connected') app.logger.info('Hello from Jenkins %s', js.get_version()) allBuilds = js.get_job_info(app.config["FOLDER_NAME"] + '/' + app.config["PIPELINE_NAME"], fetch_all_builds=True) ret = "{" for build in allBuilds['builds']: bi = js.get_build_info( app.config["FOLDER_NAME"] + '/' + app.config["PIPELINE_NAME"], build['number']) if (bi['displayName'] == rid): ret += " \"displayName\": \"" + str(bi['displayName']) + "\", " ret += " \"buildNumber\": \"" + str(build['number']) + "\", " ret += " \"building\": \"" + str(bi['building']) + "\", " ret += " \"result\": \"" + str(bi['result']) + "\"" continue ret += "}" return ret
class JenkinsInfo: config = Config.get_jenkins_config() def __init__(self): self.jks = Jenkins(url=self.config["url"], username=self.config["user"], password=self.config["pwd"]) self.job_name = self.config["job_name"] def get_info(self): result = dict() last_success_info = self.jks.get_job_info(self.job_name)["lastSuccessfulBuild"] url = last_success_info["url"] last_num = last_success_info["number"] # result["last_num"] = last_num build_info = self.jks.get_build_info(self.job_name, last_num) artifact = build_info["artifacts"][0] apk_name = artifact["fileName"] result["apk_name"] = apk_name relative_path = artifact["relativePath"] download_url = f"{url}artifact/{relative_path}" result["download_url"] = download_url return result
def main(argv): parser = OptionParser(version=VERSION) parser.add_option("-c", "--create-job", dest="create_job", help="Task to create a new job", action="store_true") parser.add_option("-j", "--job-name", dest="job_name", help="Job's name", metavar="CREATE") parser.add_option("-f", "--file", dest="filename", help="Path do jenkins config file", metavar="FILE") parser.add_option("-u", "--url", dest="url", help="Jenkins server URL", metavar="FILE") options, _ = parser.parse_args() validate(options) ci = Jenkins(url=options.url) config = "" with open(options.filename, "r") as config_file: config = config_file.read() config_file.close() job_name = options.job_name if options.create_job: ci.create_job(job_name, config)
def getjenkinsviews(self, msg, args): server = Jenkins(JENKINS_URL, username=JENKINS_USERNAME, password=JENKINS_PASSWORD) #server = Jenkins(JENKINS_URL, username=JENKINS_USERNAME, password=JENKINS_PASSWORD) views = server.get_views() return views.json()
def start_job(server_label, job_name): job_data = models.Servers.query.filter_by(label=server_label).first() job_settings = configure_build(job_data.address, job_data.branch, job_data.env) server = Jenkins(jenkins_server, jenkins_user, jenkins_pass) server.build_job(job_name, job_settings) return "task planned"
def __init__(self, jenkinsURL): self.j = Jenkins(jenkinsURL) self.jobName = "" #with open("config.xml") as file: with open("./builds/config/job/config.xml") as file: self.templateConfig = file.read() self.template = Template(unicode(self.templateConfig))
def __init__(self, token): """ Create the JenkinsTrigger instance. """ self.token = token self.repo_name = get_repo_name() self.jenkins_instance = Jenkins(JENKINS_URL)
def main(build_url, sleeptime=15, pushover=False): if pushover and not have_pushover: raise SystemExit("ERROR: to use pushover notifications, please `pip install python-pushover` and configure it.") job_name, build_no = get_job_name_and_build_number(build_url) jenkins_url = get_jenkins_base_url(build_url) logger.debug("Connecting to Jenkins...") """ NOTE: this is using unauthenticated / anonymous access. If that doesn't work for you, change this to something like: j = Jenkins(jenkins_url, 'username', 'password') """ j = Jenkins(jenkins_url) logger.debug("Connected.") if build_no is None: jobinfo = j.get_job_info(job_name) build_no = jobinfo['nextBuildNumber'] - 1 print("Using latest build, #{b}".format(b=build_no)) build_url = get_formal_build_url(jenkins_url, job_name, build_no) print("Watching job {j} #{b} until completion <{u}>...".format(j=job_name, b=build_no, u=build_url)) while True: buildinfo = j.get_build_info(job_name, build_no) if not buildinfo['building']: # job is not still building duration = datetime.timedelta(seconds=(buildinfo['duration'] / 1000)) if pushover: notify_pushover(buildinfo['result'], job_name, build_no, duration, build_url) if buildinfo['result'] == "SUCCESS": print("SUCCESS for {j} #{b} in {d} <{bu}>".format(j=job_name, b=build_no, bu=build_url, d=duration)) raise SystemExit(0) print("{r}: {j} #{b} failed in {d}".format(j=job_name, b=build_no, r=buildinfo['result'], d=duration)) raise SystemExit(1) else: duration = datetime.datetime.now() - datetime.datetime.fromtimestamp(buildinfo['timestamp'] / 1000) print("still running ({d})...".format(d=duration)) time.sleep(sleeptime)
def connect_to_jenkins_server(self): # Because if there is a problem in reading server.conf file # both IP address and Port number becomes none # so we just check server address to be none or not fh=open("C:\\Users\{0}\JC\controls".format(getuser())) for line in fh: if line.startswith('address'): self.JenkinsServerAddress=(line.split("=")[1]).strip() elif line.startswith('port'): self.JenkinsServerPort=(line.split("=")[1]).strip() fh.close() if self.JenkinsServerAddress !='none': print("Connecting {0}:{1}".format(self.JenkinsServerAddress, self.JenkinsServerPort)) UserName = input("Username : "******"Password : "******"Connected successfully as {2}".format(self.JenkinsServerAddress, self.JenkinsServerPort, UserName)) except: self.ServerHandler = None print("Connection to server Failed") else: print("-Server configuration parameters are not defined properly") print("-Check server.conf for parameters and then use jcr.exe to set them")
def getjobs(self, msg, args): #server = self.get_server_instance() server = Jenkins(JENKINS_URL, username=JENKINS_USERNAME, password=JENKINS_PASSWORD) for j in server.get_jobs(): jobs = server.get_job(j[0]) return jobs.name
def connect_to_jenkins(self): """Connect to a Jenkins instance using configuration.""" self.log.debug('Connecting to Jenkins ({0})'.format( self.config['URL'])) self.jenkins = Jenkins(url=self.config['URL'], username=self.config['USERNAME'], password=self.config['PASSWORD']) return
def build_jobs(credentials, test_plans, args): jenkins = Jenkins('http://juju-ci.vapour.ws:8080', *credentials) for test_plan in test_plans: test_id = generate_test_id() for controller in args.controllers: job_name = get_job_name(controller) parameter = make_parameters(test_plan, args, controller, test_id) jenkins.build_job(job_name, parameter, token=args.cwr_test_token)
def deleteJobs(self): j=Jenkins('http://jenkins-ccp.citrix.com','talluri','vmops.com') for i in range(757,918): print "deleting job %s"%("report_generator_"+str(i)+"_zone-xen_XenServer.xml") try: j.delete_job("report_generator_"+str(i)+"_zone-xen_XenServer.xml") except Exception,e: print e pass
def _ensure_jenkins_folder_exists(server: Jenkins, folder_path: str) -> bool: if server.job_exists(folder_path): return True elif writeback: server.create_folder(folder_path) return True print(f'Jenkins folder at {csvcubed_folder_path} does not exist.') return False
def slave_create(node_name, working_dir, executors, labels): print(node_name, working_dir, executors, labels) j = Jenkins(os.environ['JENKINS_URL'], os.environ['JENKINS_USER'], os.environ['JENKINS_PASS']) j.node_create(node_name, working_dir, num_executors=int(executors), labels=labels, launcher=NodeLaunchMethod.JNLP)
def config_jenkins(self, jenkins_url, jenkins_config_file, job_name): ci = Jenkins(url=jenkins_url) config = "" with open(jenkins_config_file, "r") as config_file: config = config_file.read() config_file.close() ci.create_job(job_name, config)
def reportUsingJenkinsEmailPlugin(self, marvinConfigJson, env, execOnOneZone = True): try: jobIdentifierList = [] bugLoggerData = [] j = Jenkins('http://jenkins-ccp.citrix.com', 'bharatk', 'BharatK') for zone in cscfg.zones: self.logger.info(zone.name) for pod in zone.pods: for cluster in pod.clusters: self.logger.info('creating a jeknins job to generate results and email notfication for hypervisor %s and zone %s' % (cluster.hypervisor, zone.name)) modifiedjob = jobModifier.modifyReportGenerator(env['build_number'] + '_' + zone.name + '_' + cluster.hypervisor, mailto) jobname = modifiedjob file = open('/root/cloud-autodeploy2/newcode/' + modifiedjob, 'r') config = file.read() file.close() j.create_job(modifiedjob, config) j.build_job(modifiedjob, {'buildNumber': env['build_number'], 'BuildNo': env['build_number'], 'MGMT_SVR': env['hostip'], 'BASEDIR': env['virtenvPath'], 'version': env['version'], 'BranchInfo': env['version'], 'GitRepoUrl': env['repo_url'], 'GitCommitId': env['commit_id'], 'CIRunStartDateTime': env['startTime'], 'CIRunEndDateTime': time.strftime('%c'), 'WikiLinks': 'https://cwiki.apache.org/confluence/display/CLOUDSTACK/Infrastructure%2CCI%2CSimulator%2CAutomation+Changes', 'hypervisor': cluster.hypervisor.lower(), 'HyperVisorInfo': cluster.hypervisor.lower(), 'zoneName': zone.name, 'BuildReport': 'https://www.dropbox.com/sh/yj3wnzbceo9uef2/AAB6u-Iap-xztdm6jHX9SjPja?dl=0', 'token': 'bharat'}) jobIdentifierList.append('report_' + zone.name) jobDetails = {'job_name': modifiedjob, 'related_data_path': env['virtenvPath']} self.resourceMgr.addJobDetails(jobDetails) bugLoggerData.append({'hypervisor': cluster.hypervisor.lower(), 'branch': env['version'], 'logname': cluster.hypervisor.lower() + '__Log_' + env['build_number'], 'type': 'BVT'}) self.logger.info('bug logger data in zone looop %s' % bugLoggerData) self.waitForJobComplete(env['virtenvPath'], jobIdentifierList) self.archiveTestRunLogs(env, cluster.hypervisor.lower(), jobname) break break if execOnOneZone: break self.logger.info('job identifier list %s' % jobIdentifierList) self.logger.info('cleaning up the workspace') bash('rm -f /root/cloud-autodeploy2/newcode/%s' % modifiedjob) self.logger.info('running bug logger') except Exception as e: self.logger.exception(e)
def __init__(self, username, password, can_use_colour): """ Create the JenkinsTrigger instance. """ self.jobs = {} self.can_use_colour = can_use_colour and not os.name.startswith('nt') self.repo_name = get_repo_name() self.server = Jenkins(JENKINS_URL, username=username, password=password)
def build_job(credentials, root, job_name, candidates, suite): parameters = {'suite': ','.join(suite), 'attempts': '10'} jenkins = Jenkins('http://juju-ci.vapour.ws:8080', credentials.user, credentials.password) for candidate, revision_build in candidates: call_parameters = { 'revision_build': '{:d}'.format(revision_build), } call_parameters.update(parameters) jenkins.build_job(job_name, call_parameters)
class JobBuilder: def __init__(self, jenkinsURL): self.j = Jenkins(jenkinsURL) self.jobName = "" def add_job(self, jobName, configString): self.jobName = jobName if self.j.job_exists(jobName): #Job exist in the job list return False else: self.j.create_job(self.jobName, configString) self.j.enable_job(self.jobName) return True def run_job(self, **params): if self.jobName == "": print "Have to add job firstly" return False else: self.j.enable_job(self.jobName) self.j.build_job(self.jobName, params)
def jenkin_server_instance(url, username, password): print("Connecting to jenkin instance \n") try: server = Jenkins(url, username, password) user = server.get_whoami() version = server.get_version() print("Hello %s from Jenkins %s \n" % (user["fullName"], version)) return server except JenkinsException: print("Authentication Error: Invalid Password/Token for user \n") return
def get_job_name(adaptation_release): tmp = 'adaptations_trunk_' + adaptation_release + '_ris' jenkins = Jenkins_Url(URL, USER, PASS) if jenkins.job_exists(tmp): return tmp tmp = 'adaptation_trunk_' + adaptation_release + '_ris' if jenkins.job_exists(tmp): return tmp print "Jenkins doesn't exist: " + tmp log("FAILED", " jenkins name dosen't exits " + tmp) return 0
class TaskBuilder: def __init__(self, jenkinsURL): self.j = Jenkins(jenkinsURL) self.jobName = "" #with open("config.xml") as file: with open("./builds/config/job/config.xml") as file: self.templateConfig = file.read() self.template = Template(unicode(self.templateConfig)) def set_new_config(self, **params): self.newConfig = self.template.render(repos=params['repos'], description=params['repos']) def add_build(self, **params): self.set_job_name(**params) self.set_new_config(**params) if self.j.job_exists(self.jobName): self.do_build(**params) else: self.j.create_job(self.jobName, self.newConfig) self.do_build(**params) def do_build(self, **params): self.set_job_name(**params) self.set_new_config(**params) self.j.enable_job(self.jobName) self.j.build_job(self.jobName, {'branch': params['branch'], 'version': params['version'], 'author': params['author'], \ 'styleguide_repo': params['styleguide_repo'], 'styleguide_branch': params['styleguide_branch'], 'sidecar_repo': params['sidecar_repo'], \ 'sidecar_branch': params['sidecar_branch'], 'package_list': params['package_list'], 'upgrade_package': params['upgrade_package']}) def set_job_name(self,**params): buildUtil = BuildUtil() self.jobName = buildUtil.get_job_name(repos=params['repos']) def get_build_status(self, jobName): #job_info = self.j.get_job_info(self.jobName) #return build_status color_status = {"aborted":"Aborted", "red": "Failed", "blue": "Succcess"} if jobName == "": print "Have to specify job name" return False else: if self.j.job_exists(jobName): #Job exist in the job list job_info = self.j.get_job_info(jobName) if color_status.has_key(job_info['color']): return color_status[job_info['color']] else: return 'Running' else: print "Have to specify a validate job name" return False def get_job_name(self): return self.jobName
def get_job_name(adaptation_release): tmp = 'adaptations_trunk_' + adaptation_release +'_ris' jenkins = Jenkins_Url(URL,USER,PASS) if jenkins.job_exists(tmp): return tmp tmp = 'adaptation_trunk_' + adaptation_release +'_ris' if jenkins.job_exists(tmp): return tmp print "Jenkins doesn't exist: " + tmp log("FAILED"," jenkins name dosen't exits " + tmp) return 0
def run(self): # Run the egg_info step to find our VCS url. self.run_command('egg_info') if not self.distribution.metadata.url: log.warn("This package does not appear to be in any repository, " "aborting.") sys.exit(1) # Pull down Jenkins package base.fetch_build_eggs(['python-jenkins'], dist=self.distribution) from jenkins import Jenkins server = CONFIG.jenkins_url log.info("Connecting to Jenkins at %s" % server) jenkins = Jenkins(server, self.username, self.password) name = self.distribution.metadata.name if (self.matrix): log.info("Matrix job") if CONFIG.jenkins_matrix_job_xml: path, fname = CONFIG.jenkins_matrix_job.split(':') else: path, fname = None, 'jenkins_job_matrix.xml' else: log.info( "Non-matrix job - use \'--matrix\' option for matrix builds") if CONFIG.jenkins_job_xml: path, fname = CONFIG.jenkins_job.split(':') else: path, fname = None, 'jenkins_job.xml' with open(base.get_resource_file(fname, path)) as f: jenkins_config_xml = Template(f.read()) cfg_xml = jenkins_config_xml.safe_substitute( name=cgi.escape(name), hyphen_escaped_name=cgi.escape(name).replace("-", "?").replace( "_", "?"), description=cgi.escape(self.distribution.metadata.description), repository=self.distribution.metadata.url, email=self.distribution.metadata.author_email, python_string_xml=self._construct_string_values( self._get_active_python_versions()), virtualenv=CONFIG.virtualenv_executable, username=self.username) if jenkins.job_exists(name): log.error( "Job found at %s/job/%s Please delete this before creating a new one." % (server, name)) else: if (not self.dry_run): log.info("Creating job at %s/job/%s" % (server, name)) jenkins.create_job(name, cfg_xml)
def getConfig(self,job,jenkins_username,jenkins_password): # server = Jenkins("https://jenkins.cn-x-cloud-pg.com.cn",username="******",password="******",timeout=3600) server = Jenkins("https://jenkins.cn-x-cloud-pg.com.cn",username=jenkins_username,password=jenkins_password,timeout=3600) jobConfig = server.get_job_config(job) root = ET.fromstring(jobConfig) for i in root.iter('url'): gitlabProject = i.text.split('/')[-1].split('.')[0] for j in root.iter('scriptPath'): scriptPath = j.text return (i.text,gitlabProject,scriptPath)
def __init__(self, jenkins_url, login_id, password): """ Initialize the jenkins connection object :param jenkins_url: :param login_id: :param password: """ self._jenkins_url = jenkins_url self._login_id = login_id self._password = password self.server_obj = Jenkins(jenkins_url, username=self._login_id, password=self._password)
def build_jobs(credentials, test_plans, args): jenkins = Jenkins('http://juju-ci.vapour.ws:8080', *credentials) for test_plan in test_plans: test_id = generate_test_id() test_plan_content = load_test_plan(test_plan) test_label = test_plan_content.get('test_label') if test_label and isinstance(test_label, str): test_label = [test_label] for controller in test_label or args.controllers: job_name = get_job_name(controller) parameter = make_parameters(test_plan, controller, test_id) jenkins.build_job(job_name, parameter, token=args.cwr_test_token)
def create_jenkins_job(init_job_config, job_name): """ Create new job by getting config file from existing job """ j = Jenkins("http://localhost:8080") str_config = j.get_job_config(init_job_config) if j.job_exists(job_name) == False: j.create_job(job_name, str_config)
class JenkinsBot(BotPlugin): def __init__(self): self.jenkins = Jenkins(JENKINS_URL, JENKINS_USERNAME, JENKINS_PASSWORD) @botcmd def jenkins_list(self, mess, args): """List all jobs, optionally filter them using a search term.""" self.send(mess.getFrom(), u'/me is getting the list of jobs from Jenkins... ', message_type=mess.getType()) search_term = args.strip().lower() jobs = [ job for job in self.jenkins.get_jobs() if search_term.lower() in job['name'].lower() ] return self.format_jobs(jobs) @botcmd def jenkins_running(self, mess, args): """List all running jobs.""" self.send(mess.getFrom(), u'/me is getting the list of jobs from Jenkins... ', message_type=mess.getType()) jobs = [ job for job in self.jenkins.get_jobs() if 'anime' in job['color'] ] return self.format_running_jobs(jobs) def format_jobs(self, jobs): if len(jobs) == 0: return u'No jobs found.' max_length = max([len(job['name']) for job in jobs]) return '\n'.join([ '%s (%s)' % (job['name'].ljust(max_length), job['url']) for job in jobs ]).strip() def format_running_jobs(self, jobs): if len(jobs) == 0: return u'No running jobs.' jobs_info = [self.jenkins.get_job_info(job['name']) for job in jobs] return '\n\n'.join([ '%s (%s)\n%s' % (job['name'], job['lastBuild']['url'], job['healthReport'][0]['description']) for job in jobs_info ]).strip()
def jenkins(): print('Looking for a running Jenkins instance') webapi = Jenkins('http://127.0.0.1:60888') print('Removing all jobs ...') for job in webapi.jobs: webapi.job_delete(job.name) print('Creating test jobs ...') configxml = 'master-job-svn-config.xml' configxml = open(configxml).read().encode('utf8') webapi.job_create('master-job-svn', configxml) return webapi
def __init__(self, url, username, password): """ 初始化Jenkins连接信息 :param url: Jenkins地址 :param username: 登录用户名 :param password: 登录密码 """ try: self.session = Jenkins(url=url, username=username, password=password) except Exception: logger.exception('初始化Jenkins连接过程中发生异常,请检查!')
def errors(job_name, jenkins_username, jenkins_token): """Console script for jenkins_triage.""" global server # job_name = 'enterprise_pe-acceptance-tests_integration-system_pe_full-upgrade_weekend_2016.4.x' # 'enterprise_pe-orchestrator_intn-van-sys-pez-multi_2016.4.x-2016.4.x' # 'enterprise_pe-modules-vanagon-suite_intn-van-sys-pez-multi_daily-pe-modules-2016.4.x' server = Jenkins( 'https://cinext-jenkinsmaster-enterprise-prod-1.delivery.puppetlabs.net', username=jenkins_username, password=jenkins_token) info = server.get_job_info(job_name) builds = [ server.get_build_info(job_name, build['number']) for build in info['builds'] ] failed_build_numbers = [b for b in builds if b['result'] == 'FAILURE'] last_job_errors = None counts = defaultdict(int) similar = set() for build in failed_build_numbers: output = server.get_build_console_output(job_name, build['number']) finder = get_strategy(output) errors = finder(output) print "Errors: {}".format(errors) if last_job_errors: seq = difflib.SequenceMatcher(a=last_job_errors, b=errors) if seq.ratio() == 1.0: counts['exact'] += 1 if seq.ratio() >= 0.7 and seq.ratio() < 1.0: counts['similar'] += 1 similar.append(errors) else: last_job_errors = errors if last_job_errors: click.echo('Last job errors were:') click.echo('\t{}'.format('\n\t'.join(last_job_errors))) if last_job_errors and 'exact' in counts: click.echo( 'There were {} jobs that failed with errors exactly the same as the last failed job:' .format(counts['exact'])) click.echo('\t{}'.format('\n\t'.join(last_job_errors))) if last_job_errors and 'similar' in counts: click.echo( 'There were {} jobs that failed with experienced similar errors as the last failed job:' .format(counts['exact'])) click.echo('\t{}'.format('\n\t'.join(last_job_errors))) for s in similar: click.echo('Additional Failed Job:') click.echo('\t{}'.format('\n\t'.join(s)))
def run(self): # Run the egg_info step to find our VCS url. self.run_command('egg_info') if not self.distribution.metadata.url: log.warn("This package does not appear to be in any repository, " "aborting.") sys.exit(1) # Pull down Jenkins package base.fetch_build_eggs(['python-jenkins'], dist=self.distribution) from jenkins import Jenkins server = CONFIG.jenkins_url log.info("Connecting to Jenkins at %s" % server) jenkins = Jenkins(server, self.username, self.password) name = self.distribution.metadata.name if (self.matrix): log.info("Matrix job") if CONFIG.jenkins_matrix_job_xml: path, fname = CONFIG.jenkins_matrix_job.split(':') else: path, fname = None, 'jenkins_job_matrix.xml' else: log.info("Non-matrix job - use \'--matrix\' option for matrix builds") if CONFIG.jenkins_job_xml: path, fname = CONFIG.jenkins_job.split(':') else: path, fname = None, 'jenkins_job.xml' with open(base.get_resource_file(fname, path)) as f: jenkins_config_xml = Template(f.read()) cfg_xml = jenkins_config_xml.safe_substitute( name=cgi.escape(name), hyphen_escaped_name=cgi.escape(name).replace("-", "?").replace("_", "?"), description=cgi.escape(self.distribution.metadata.description), repository=self.distribution.metadata.url, email=self.distribution.metadata.author_email, python_string_xml=self._construct_string_values(self._get_active_python_versions()), virtualenv=CONFIG.virtualenv_executable, username=self.username ) if jenkins.job_exists(name): log.error("Job found at %s/job/%s Please delete this before creating a new one." % (server, name)) else: if (not self.dry_run): log.info("Creating job at %s/job/%s" % (server, name)) jenkins.create_job(name, cfg_xml)
def get_plugins_dict(jenkins_url, puppet=False, user=None, password=None): if user is not None: logger.debug("Connecting to Jenkins (<%s>) as user %s ...", jenkins_url, user) j = Jenkins(jenkins_url, user, password) else: logger.debug("Connecting to Jenkins (<%s>) anonymously...", jenkins_url) j = Jenkins(jenkins_url) logger.debug("Connected.") p = j.get_plugins() plugins = {} for k, v in p.items(): plugins[k[0]] = v['version'] return plugins
def create_jenkins_job(config_path, job_name): """ Create new job with new config file """ f = open(config_path, "r") str_config = "" for line in f: str_config = str_config + line j = Jenkins("http://localhost:8080") if j.job_exists(job_name) == False: j.create_job(job_name, str_config)
class JenkinsConnection(object): def __init__(self, username, password): self.jenkins = Jenkins(URL, username=username, password=password) def store_job(self, job_name): job_config = self.jenkins.get_job_config(job_name) with open(self._config_filename(job_name), 'w') as config: config.write(job_config) def get_jobs(self, view): raw_config = self.jenkins.get_view_config(view) config = ET.fromstring(raw_config) jobs = [j.text for j in config.find('jobNames').getchildren()] return filter(None, jobs)
def restartJenkins(self): self.checkForSuccess(bash("service jenkins restart")) retry=20 while retry > 0: retry-=1 try: j=Jenkins(self.jenkinsUrl, "admin", self.jenkinsPasswd) j.get_plugins() break except Exception as e: if retry==0: self.logger.info("Failed to restart jenkins") else: time.sleep(20) self.logger.info("waiting for jenkins to restart, this may take a while")
def check_running_job(self): #Check Running job j = Jenkins(appconfig.jenkins_url) job_list = j.get_jobs() job_queue_list = j.get_queue_info() running_job = [] for job in job_list: if re.search('anime', job['color']): running_job.append(job['name']) for job_queue in job_queue_list: running_job.append(job_queue['task']['name']) return running_job
def jenkins_connection(): """ Return an initialized python-jenkins object. """ configp = config() try: user = configp.get('rhcephpkg', 'user') token = configp.get('rhcephpkg.jenkins', 'token') url = configp.get('rhcephpkg.jenkins', 'url') except configparser.Error as err: raise SystemExit('Problem parsing .rhcephpkg.conf: %s', err.message) jenkins = Jenkins(url, username=user, password=token) # These "password" and "url" attributes are not formally part of # python-jenkins' API, but they are nice to make available to consumers # (for logging/debugging, for example.) jenkins.password = token jenkins.url = url return jenkins
def poll(): jenkins = Jenkins(JENKINS_URL) while True: remote_jobs = jenkins.get_jobs() for remote in remote_jobs: for local in JOBS: if local['name'] == remote['name']: led = local['led'] if remote['color'] == 'blue': led.green(True) elif remote['color'] == 'red': led.red(True) else: if led.off(): led.yellow(True) else: led.off(True) time.sleep(1)
def deleteJobs(self,jobList): j=Jenkins('http://jenkins-ccp.citrix.com','bharatk','BharatK') for jobdetail in jobList: try: print "deleteing job %s"%jobdetail[0] j.delete_job(jobdetail[0]) except Exception,e: # try: # j.delete_job(jobdetail[0].replace("Sandbox-simulator_simula","Sandbox-simulator_simul").replace("Sandbox-simulator_simul","Sandbox-simulator_simulator.xml")) # print "deleting job %s"%jobdetail[0].replace("Sandbox-simulator_simula","Sandbox-simulator_simul").replace("Sandbox-simulator_simul","Sandbox-simulator_simulator") # except Exception,e: print e print "deleting job %s"%jobdetail[0] self.resourceMgr.removeJob(jobdetail[0]) print "deleted job %s"%jobdetail[0] print "cleaning up related data" os.system("rm -rf %s"%jobdetail[1]) os.system("rm -rf /automation/jenkins/workspace/%s"%jobdetail[0])
def main(jenkins_url, user=None, password=None): if user is not None: logger.debug("Connecting to Jenkins as user %s ...", user) j = Jenkins(jenkins_url, user, password) else: logger.debug("Connecting to Jenkins anonymously...") j = Jenkins(jenkins_url) logger.debug("Connected.") p = j.get_plugins() plugins = {} namelen = 0 for k, v in p.items(): plugins[k[0]] = v['version'] if len(k[0]) > namelen: namelen = len(k[0]) # format for name, ver in sorted(plugins.items()): print(" jenkins::plugin {'%s': version => '%s'}" % (name, ver))
def generate_jenkins(release): jenkins = Jenkins_Url(URL,USER,PASS) trunk = trunk_jenkins(release) if jenkins.job_exists(trunk): log("trunk","https://eslinv70.emea.nsn-net.net:8080/job/" + trunk) else: log("failed","trunk " + release) n15_5 = n15_5_jenkins(release) if jenkins.job_exists(n15_5): log("n15.5","https://eslinv70.emea.nsn-net.net:8080/job/" + n15_5) else: log("failed","n15.5 " + release) n16 = n16_jenkins(release) if jenkins.job_exists(n16): log("n16","https://eslinv70.emea.nsn-net.net:8080/job/" + n16) else: log("failed","n16 " + release)
def __init__(self, url, username, password, key, configuration): self.configuration = configuration self.configuration_prefix = configuration.jenkins_configuration_prefix() self.grouped_components = configuration.jenkins_grouped_components() self.pullrequest_job = configuration.pullrequest_job() self.jenkins_configurations = Jenkins(url, username, password) self.build_components_jobs_matrix() self.sort_components() self.jenkins_builds = jenkinsapi.jenkins.Jenkins(url, username, password)
def connect(credentials): """ Since a user can have simple authentication with a single user/password or define a set of IRC nick to Jenkin's users with API tokens, this helper untangles this and returns a connection. If no authentication is configured, just a connection is returned with no authentication (probably read-only, depending on Jenkins settings) """ connection = Jenkins( credentials['url'], username=credentials['username'], password=credentials['password'], ) connection.password = credentials['password'] # try an actual request so we can bail if something is off connection.get_info() return connection
class JenkinsControl(object): war = pjoin(here, "tmp/jenkins.war") cli = pjoin(here, "tmp/jenkins-cli.jar") home = pjoin(here, "tmp/jenkins") def __init__(self, addr="127.0.0.1:60888", cport="60887"): self.addr, self.port = addr.split(":") self.url = "http://%s" % addr self.py = Jenkins(self.url) def start_server(self): cmd = pjoin(here, "./bin/start-jenkins.sh 1>/dev/null 2>&1") env = { "JENKINS_HOME": self.home, "JENKINS_PORT": self.port, "JENKINS_CPORT": self.cport, "JENKINS_ADDR": self.addr, } check_call(cmd, shell=True, env=env) def shutdown_server(self): cmd = "echo 0 | nc %s %s" % (self.addr, self.cport) check_call(cmd, shell=True) def clean_home(self): rmtree(self.home) def createjob(self, name, configxml_fn): configxml = open(configxml_fn).read().encode("utf8") self.py.job_create(name, configxml) def getjobs(self): return {i.name: i for i in self.py.jobs} def enabled(self, name): return self.py.job(name).info["buildable"] def job_etree(self, job): res = self.py.job(job).config res = etree.fromstring(res) return res