While using jenkins API, getting a failure on reconfig_job

我与影子孤独终老i 提交于 2019-12-25 04:09:57

问题


I am using jenkins rest API to recurse through jobs and then reconfigure this one. All methods work except one. He's is my code :

def get_server_instance():
    jenkins_url = 'xxxx'
    #server = Jenkins(jenkins_url, username = '', password = '')
    # Connect to instance - username and password are optional
    server = jenkins.Jenkins(jenkins_url, username = '', password = '')
    return server


def get_job_details():
    # Refer Example #1 for definition of function 'get_server_instance'
    server = get_server_instance()
    for job in server.get_jobs_list():
        if job == "GithubMigration":
            configuration = server.get_job(job).get_config().encode('utf-8')
            #server.reconfig_job(job, configuration)
            if server.has_job("GithubMigration"):
                server.reconfig_job('GithubMigration', config_xml)

It gets my configuration.xml, find the job as well but fails on server.reconfig_job('GithubMigration', config_xml) with the error , AttributeError: 'Jenkins' object has no attribute 'reconfig_job'

when obviously this functions exists in the jenkins rest API and yes I'm importing jenkins, from jenkinsapi.jenkins import Jenkins .

Edit 1 - I uninstalled Jenkinsapi and have only python-jenkins module and now it fails even before saying

AttributeError: 'module' object has no attribute 'Jenkins' for line : AttributeError: 'module' object has no attribute 'Jenkins'

Any ideas?

Edit 2 :

I tries solely python-jenkins API and tried their own example as you see here http://python-jenkins.readthedocs.org/en/latest/example.html

import jenkins
j = jenkins.Jenkins('http://your_url_here', 'username', 'password')
j.get_jobs()
j.create_job('empty', jenkins.EMPTY_CONFIG_XML)
j.disable_job('empty')
j.copy_job('empty', 'empty_copy')
j.enable_job('empty_copy')
j.reconfig_job('empty_copy', jenkins.RECONFIG_XML)

Even this fails at jenkins.Jenkins with attribute error at Jenkins - No module.

I am pretty sure the API is broken.


回答1:


Your script is probably importing wrong module. You can check it as follows:

import jenkins
print jenkins.__file__

If printed path is other than installation path of jenkins module (eg. C:\Python27_32\lib\site-packages\jenkins\__init__.pyc), then you should check pythonpath:

import sys
print sys.path

Common problem is existence of python script with same name as imported module in current directory, which is at the first place in search path ''.

For more info on import order see module search path




回答2:


Following @Chemik answer, I realized that the script I wrote was named jenkins.py and it was conflicting with python-jenkins import.

The library isn't broken. Check your script name.



来源:https://stackoverflow.com/questions/28267884/while-using-jenkins-api-getting-a-failure-on-reconfig-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!