Giter VIP home page Giter VIP logo

pycontribs / jenkinsapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ramonvanalteren/jenkinsapi-old

855.0 855.0 482.0 1.61 MB

A Python API for accessing resources and configuring Hudson & Jenkins continuous-integration servers

Home Page: http://pypi.python.org/pypi/jenkinsapi

License: MIT License

Python 99.57% Shell 0.37% Makefile 0.06%
automation ci continuous-integration devops devops-packages jenkins scripting

jenkinsapi's People

Contributors

andrewgy8 avatar anrs avatar aoen avatar boaz0 avatar chrismaes87 avatar cleberjsantos avatar djonsson avatar fun4jimmy avatar imsardine avatar jameswhitworthvicon avatar jespermonsted avatar jollychang avatar jvale avatar lechat avatar lhupfeldt avatar lphoward avatar lruslan avatar mattvonrocketstein avatar mvdbeek avatar pkocandr avatar pre-commit-ci[bot] avatar ramonvanalteren avatar salimfadhley avatar sateeshkumarb avatar simonxming avatar ssbarnea avatar sudharsh avatar toumorokoshi avatar vjuranek avatar zzzuzik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jenkinsapi's Issues

get_revision doesn't work properly with multiple sources in the repository

I had to change the 'get_revision' method to work properly iterate over the revisions in a changeset and return the max (latest) one:

    def get_revision(self):
        maxRevision = 0
        try:
            for set in self._data["changeSet"]["revisions"]:
                maxRevision = max(set["revision"], maxRevision)
            return maxRevision
        except KeyError:
            return maxRevision

A key error was also produced sometimes, not sure why this was.

pip install (url to jenkinapi v1.5 egg) fails

pip install http://pypi.python.org/packages/2.6/j/jenkinsapi/jenkinsapi-0.1.5-py2.6.egg#md5=3a2302f1ac9da533e8350b1752abd6e7
Downloading/unpacking http://pypi.python.org/packages/2.6/j/jenkinsapi/jenkinsapi-0.1.5-py2.6.egg#md5=3a2302f1ac9da533e8350b1752abd6e7
Downloading jenkinsapi-0.1.5-py2.6.egg (52Kb): 52Kb downloaded
Running setup.py egg_info for package from http://pypi.python.org/packages/2.6/j/jenkinsapi/jenkinsapi-0.1.5-py2.6.egg#md5=3a2302f1ac9da533e8350b1752abd6e7
Traceback (most recent call last):
File "", line 14, in
IOError: [Errno 2] No such file or directory: '/tmp/pip-8YpJ8c-build/setup.py'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "", line 14, in

IOError: [Errno 2] No such file or directory: '/tmp/pip-8YpJ8c-build/setup.py'

Jenkins.create_job shall not throw exception

Hi,

I think that create_job shall not throw exception if job already exists and shall just return existing job object. This way code, that uses this function will be a bit simplier.

For example: when I am generating jobs in bulk and some of the jobs are already there my current code is this:

for new_job in my_new_jobs:
    try:
        created_job = jenkins.create_job(new_job)
    except JenkinsAPIException:
        created_job = jenkins.get_job(new_job)

If we change function it will be this:

for new_job in my_new_jobs:
    created_job = jenkins.create_job(new_job)

What do you think?

Unit tests fail

Hi,

Unit tests fail because there is no get_nodes() method in Jenkins class.

Build Class get_timestamp method not proper python time in seconds

Suggesting that line 264 in jenkinsapi/build.py be changed from

return self._data['timestamp']

to

return self._data['timestamp'] / 1000

The timestamp field in java is represented in milliseconds, which isn't compatible with other native time formats in python. IE time.time.ctime() or datetime.datetime.fromtimestamp()
passing the output from the build.get_timestamp() into these functions results in a failure because they are expecting seconds.

Parameterized builds not working

What i'm still stuck on is a parameterized build. The api seems to suggest that you need to post the build parameters as json string data. There seems to be a params input on the job.invoke method, but i can't seem to find any information on the format of the params and using a sample like params={'myBuildParam':'helloworld'} doesn't seem to work. Any insight would be appreciated.

Artifact download will produce lots of errors if fingerprint recording is not enabled for a build

If a build is storing artifacts, but not recording fingerprints, using Artifact.save() will result in an error like this:

WARNING:jenkinsapi.jenkinsbase:Error reading http://xxx/jenkins//fingerprint/486233ba133a092c0568de51faa10571/api/python/
ERROR:jenkinsapi.jenkinsbase:HTTP Error 404: Not Found
Traceback (most recent call last):
  File "/scratch/nps-umm/virtualenv-dir/local/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 68, in get_data
    stream = fn_urlopen(url)
  File "/usr/lib/python2.7/urllib2.py", line 406, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found
ERROR:jenkinsapi.utils.retry:HTTP Error 404: Not Found

The reason seems to be that when Artifact.save calls _verify_download, it will create a Fingerprint object, which in turn will try to fetch its data from Jenkins. Since there is no fingerprint tracking for these artifacts, that will fail.

The files are downloaded, since the error is captured and occurs after the file has been saved.

In this case, I think Fingerprint should recognize the case of 404 and set some status code, or propagate the exception upwards so that _verify_download can see that no fingerprint is available, and bypass (e.g. always return True).

pip install jenkinsapi fails for v1.5

pip install jenkinsapi
Downloading/unpacking jenkinsapi
Downloading jenkinsapi-0.1.5.tar.gz
Running setup.py egg_info for package jenkinsapi
Traceback (most recent call last):
File "", line 14, in
File "/root/build/jenkinsapi/setup.py", line 5, in
DESCRIPTION = open(os.path.join(PROJECT_ROOT, "README.rst")).read()
IOError: [Errno 2] No such file or directory: '/root/build/jenkinsapi/README.rst'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "", line 14, in

File "/root/build/jenkinsapi/setup.py", line 5, in

DESCRIPTION = open(os.path.join(PROJECT_ROOT, "README.rst")).read()

IOError: [Errno 2] No such file or directory: '/root/build/jenkinsapi/README.rst'


Command python setup.py egg_info failed with error code 1

Problem with create_node

Hi,
I'm having problem to make create_node work. Other methods like delete_node, has_node works just fine.
I'm trying to run following towards a Jenkins [version v. 1.485] instance which runs on a Tomcat6: (I'm running version 0.1.11 of jenkinsapi)

#!/usr/bin/python

from jenkinsapi.jenkins import *

if __name__ == '__main__':

    try:
        jenkins = Jenkins('http://localhost:8080/jenkins/')
        jenkins.create_node("FOO")
    except urllib2.HTTPError, error:
        print error.read()

This creates following error:

[me@my_laptop Jenkins]$ ./create_node.py 
http://localhost:8080/jenkins//computer//doCreateItem?json=%7B%22launcher%22%3A+%7B%22stapler-class%22%3A+%22jenkins.slaves.JNLPLauncher%22%7D%2C+%22numExecutors%22%3A+2%2C+%22nodeProperties%22%3A+%7B%22stapler-class-bag%22%3A+%22true%22%7D%2C+%22name%22%3A+%22FOO%22%2C+%22retentionStrategy%22%3A+%7B%22stapler-class%22%3A+%22jenkins.slaves.RetentionStrategy%24Always%22%7D%2C+%22remoteFS%22%3A+%22%2Fvar%2Flib%2Fjenkins%22%2C+%22type%22%3A+%22jenkins.slaves.DumbSlave%24DescriptorImpl%22%2C+%22nodeDescription%22%3A+null%2C+%22labelString%22%3A+null%2C+%22mode%22%3A+%22NORMAL%22%7D&type=jenkins.slaves.DumbSlave%24DescriptorImpl&name=FOO
No handlers could be found for logger "jenkinsapi.jenkins"
<html><head><title>Error 500</title></head>
<body bgcolor=#fff><h1>Status Code: 500</h1>Exception: java.lang.NullPointerException<br>Stacktrace: <pre>javax.servlet.ServletException: java.lang.NullPointerException
        at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:616)
        at org.kohsuke.stapler.Stapler.invoke(Stapler.java:659)
        at org.kohsuke.stapler.MetaClass$4.doDispatch(MetaClass.java:203)
        at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:53)
        at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:574)
        at org.kohsuke.stapler.Stapler.invoke(Stapler.java:659)
        at org.kohsuke.stapler.Stapler.invoke(Stapler.java:488)
        at org.kohsuke.stapler.Stapler.service(Stapler.java:162)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:95)
        at hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:87)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:47)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
        at hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:76)
        at hudson.security.HudsonFilter.doFilter(HudsonFilter.java:164)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:50)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:81)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
        at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:1728)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
        at hudson.model.ComputerSet.doDoCreateItem(ComputerSet.java:262)
        at sun.reflect.GeneratedMethodAccessor293.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:288)
        at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:151)
        at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:90)
        at org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:111)
        at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:53)
        at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:574)
        ... 42 more
</pre><br><hr>
<i>Generated by Stapler at Sun Oct 14 10:08:04 CEST 2012</i></body></html>

def get_master_job_name(self) returns first parametr of job

def get_master_job_name(self) really returns me first job parametr which isn't a name of job.

def get_master_job_name(self):
"""
Get the master job name if it exist, None otherwise
:return: String or None
"""
try:
return self.get_actions()['parameters'][0]['value']
except KeyError:
return None

Jenkins ver. 1.487

domain is lost when querying a remote jenkins server

Our company has a jenkins server, that is accessible from within our internal network as:

http://jenkins:8080

And remotely, as

http://jenkins.domain.name:8080

However, when I am at a remote location, and do:

Jenkins('http://jenkins.domain.name:8080').get_job('my_job')

the url that it ends up trying to access is:

http://jenkins:8080/job/my_job/

ie, it has lost the domain name.

This seems to be a fairly serious issue - it has certainly made jenkinsapi unusable for us...

Refactor of job.invoke() behavior

The API for launching jobs is confusing and unhelpful.

Currently you might have to do something like:

myJob.invoke(block=myBool)

If you want to get the job that was just invoked you need to call another function. I think we should move to something more like this:

Launch and wait for a job to complete:

build = myJob.invoke()
build.block_until_started()
build.block()

And what of the behavior when you try to build a job which is already running or queued?

build1 = longJob.invoke() # Starts a job
build2 = longJob.invoke() # Queues the 2nd job because the 1st is still running
build3 = longJob.invoke() # Guess what happens?
assert build2 == build3 # That's what happens

Please update to python 3.2

It will be really nice to have this working on python3 ;)
(I tried 2to3, but lib does not work after that)

Refactor of jenkinsbase

Most of our objects set the self.jenkins attribute in their own init methods. This should be done in the jenkinsbase object given that it's a required attribute on just about every object in the API.

Getting repo & url from a build

I want to find out the repo url and commit id in terms of a build id (Not build number).

Is it possible to do this using jenkinsapi? I tried a lot, but I failed to find a solution.

But I do know the RESTful API is working, however that requires me to login to do so.

Any advice?

Test crashes: Console says No valid crumb was included in request for /job/foo/doDelete. Returning 403

Broken in Jenkins 1.519

Seems OK in Jenkins 1.480.3 (the version that comes with Ubuntu).

Getting lots of errors like this:

ERROR: test_invoke_job_parameterized (main.TestDownstreamUpstream)

Traceback (most recent call last):
File "/home/sal/workspace/jenkinsapi/src/jenkinsapi_tests/systests/base.py", line 30, in setUp
self._delete_all_jobs()
File "/home/sal/workspace/jenkinsapi/src/jenkinsapi_tests/systests/base.py", line 39, in _delete_all_jobs
self.jenkins.delete_job(name)
File "/home/sal/workspace/jenkinsapi/src/jenkinsapi/jenkins.py", line 169, in delete_job
data='some random bytes...'
File "/home/sal/workspace/jenkinsapi/src/jenkinsapi/utils/requester.py", line 67, in post_and_confirm_status
response.url, data, headers, response.status_code, response.text.encode('UTF-8')))
JenkinsAPIException: Operation failed. url=http://localhost:8080/job/foo/doDelete, data=some random bytes..., headers={'Content-Type': 'application/x-www-form-urlencoded'}, status=403, text=<title>Error 403</title>

Status Code: 403

Exception: No valid crumb was included in the request
Stacktrace:
(none)


Generated by Winstone Servlet Engine v0.9.10 at Tue Jun 18 23:39:50 BST 2013


Ran 1 test in 0.519s

job.Job.is_running() does not actually handle NoBuildData situation.

Although is_running() handles the NoBuildData exception, it does not appear to execute the code which would raise this exception when appropriate.

NoBuildData is only raised by get_build_dict() and get_revision_dict(). These are not in the call chain for is_running, so the NoBuildData situation is not detected.

This appears to eventually fail in _buildid_for_type with (see traceback below)
TypeError: 'NoneType' object is not subscriptable

I am not sure where would be the best place in the chain to introduce the check, but I think something should perform a check similar to get_build_dict().

    if not self._data.has_key( "builds" ):
        raise NoBuildData( repr(self) )

Perhaps _buildid_for_type is most appropriate to make the test effective for the broadest range of calling functions.

Traceback (most recent call last):
...
if running != job.is_running():
File ".../Python-2.7.2/lib/python2.7/site-packages/jenkinsapi/job.py", line 195, in is_running
return self.get_last_build().is_running()
File ".../Python-2.7.2/lib/python2.7/site-packages/jenkinsapi/job.py", line 151, in get_last_build
bn = self.get_last_buildnumber()
File ".../Python-2.7.2/lib/python2.7/site-packages/jenkinsapi/job.py", line 110, in get_last_buildnumber
return self._buildid_for_type(buildtype="lastBuild")
File ".../Python-2.7.2/lib/python2.7/site-packages/jenkinsapi/job.py", line 96, in _buildid_for_type
buildid = self._data[buildtype]["number"]
TypeError: 'NoneType' object is not subscriptable

job.update_config doesn't pass in XML config to the POST

diff --git a/jenkinsapi/job.py b/jenkinsapi/job.py
index 9fe7925..c451c2f 100644
--- a/jenkinsapi/job.py
+++ b/jenkinsapi/job.py
@@ -363,7 +363,7 @@ class Job(JenkinsBase, MutableJenkinsThing):
         Also refresh the ElementTree object since the config has changed
         """
         url = self.get_config_xml_url()
-        response = self.jenkins.requester.post_url(url, params={}, data='')
+        response = self.jenkins.requester.post_url(url, params={}, data=config)
         self._element_tree = ET.fromstring(config)
         return response.text

Views behavior is broken

The views behaviour is currently untested and does not actually seem to be able to create views. I've tried to develop a simplified model of how it's done, however this is not quite right yet:

from jenkinsapi_tests.test_utils.random_strings import random_string
import requests
import json

url = 'http://localhost:8080/newView'
str_view_name = random_string()
params = {}# {'name': str_view_name}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {
"mode": "hudson.model.ListView",
"Submit": "OK",
"name": str_view_name
}

Try 1

result = requests.post(url, params=params, data={'json':json.dumps(data)}, headers=headers)
print result.text.encode('UTF-8')
print str_view_name

Build.block_until_complete() fails (import issue)

error is:

usr/local/lib/python2.7/dist-packages/jenkinsapi-0.1.6-py2.7.egg/jenkinsapi/build.pyc in block_until_complete(self, delay)
73 total_wait = delay * count
74 log.info("Waited %is for %s #%s to complete" % ( total_wait, self.job.id(), self.id() ) )
---> 75 time.sleep( delay )
76 count += 1
77

AttributeError: type object 'datetime.time' has no attribute 'sleep'

suggested fix: use "from time import sleep" and "sleep(delay)"

edit: pull request created. (24)

get_config 403 forbidden?

In [7]: from jenkinsapi.jenkins import Jenkins

In [8]: jb = Jenkins('http://qaci')

In [9]: jb
Out[9]: <jenkinsapi.jenkins.Jenkins Jenkins server at http://qaci>

In [10]: j = jb.get_job('jollychang_test2')

In [11]: j
Out[11]: <jenkinsapi.job.Job jollychang_test2>

In [12]: j.get_config()
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (22, 0))
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
/Users/jollychang/<ipython-input-12-40da87a2280f> in <module>()
----> 1 j.get_config()

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jenkinsapi-0.1.6-py2.7.egg/jenkinsapi/job.pyc in get_config(self)
    200     def get_config(self):
    201         '''Returns the config.xml from the job'''
--> 202         return self.hit_url("%(baseurl)s/config.xml" % self.__dict__)

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jenkinsapi-0.1.6-py2.7.egg/jenkinsapi/jenkinsbase.pyc in hit_url(self, url)
     76         fn_urlopen = self.get_jenkins_obj().get_opener()
     77         try:
---> 78             stream = fn_urlopen( url )
     79             html_result = stream.read()
     80         except urllib2.HTTPError, e:

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.pyc in open(self, fullurl, data, timeout)
    398         for processor in self.process_response.get(protocol, []):
    399             meth = getattr(processor, meth_name)
--> 400             response = meth(req, response)
    401 
    402         return response

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.pyc in http_response(self, request, response)
    511         if not (200 <= code < 300):
    512             response = self.parent.error(
--> 513                 'http', request, response, code, msg, hdrs)
    514 
    515         return response

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.pyc in error(self, proto, *args)
    436         if http_err:
    437             args = (dict, 'default', 'http_error_default') + orig_args
--> 438             return self._call_chain(*args)
    439 
    440 # XXX probably also want an abstract factory that knows when it makes


/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
    370             func = getattr(handler, meth_name)
    371 
--> 372             result = func(*args)
    373             if result is not None:
    374                 return result

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.pyc in http_error_default(self, req, fp, code, msg, hdrs)
    519 class HTTPDefaultErrorHandler(BaseHandler):
    520     def http_error_default(self, req, fp, code, msg, hdrs):
--> 521         raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    522 
    523 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 403: Forbidden

Proposal: Change the id() methods to name()

Many jenkinsapi objects have a method called id() which returns a string identifier. You can see this in Node and Job.

Unfortunately id is an over-used expression, and is also a built-in symbol in Python. To avoid confusion we should rename all instances of id() to get_name() which I think is a more Pythonic convention.

Can anybody speak out if you object to this change?

Examples need to be organized

Should be split into two folders:

  • Those which explain how to do stuff in JenkinsAPI
  • Those which explain how JenkinsAPI does stuff (low level examples)

Jenkins auth failure (works w/ wget and python-jenkins library)

Hi there,

I am trying to use either this library or python-jenkins, but have failed to connect to a Jenkins server that requires authentication. Without authentication, everything works well.

Supposing my username is "kenn" and my password is "foobar" and my Jenkins server is at "https://jenkins.example.com"

This works (as does the Jenkins web interface, of course)

wget --auth-no-challenge --http-user=kenn --http-password=foobar https://jenkins.example.com/api/python

I have also used python-jenkins and confirmed that works. But this does not:

from jenkinsapi import jenkins
jenkins.Jenkins("https://jenkins.example.com", username = "kenn", password = "foobar")

It results in the following output, where I have turned on logging in ipython and added the password to the debug message. I have confirmed that when the password is wrong it also has this output.

DEBUG:jenkinsapi.jenkins:args: ['kenn', 'foobar', 'https://jenkins.example.com/', None, None, None, None]
DEBUG:jenkinsapi.utils.urlopener:Adding BasicAuthHandler: url:https://jenkins.example.com/, user:kenn, pass:foobar
WARNING:jenkinsapi.jenkinsbase:Error reading https://jenkins.example.com/api/python/
ERROR:jenkinsapi.jenkinsbase:HTTP Error 403: Forbidden
Traceback (most recent call last):
  File "/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 67, in get_data
    stream = fn_urlopen(url)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden
ERROR:jenkinsapi.utils.retry:HTTP Error 403: Forbidden
Traceback (most recent call last):
  File "/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/utils/retry.py", line 24, in retry_function
    result = fn( *args, **kwargs )
  File "/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 67, in get_data
    stream = fn_urlopen(url)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden
WARNING:jenkinsapi.utils.retry:get_data failed at attempt 1, trying again.
ERROR:jenkinsapi.jenkinsbase:HTTP Error 403: Forbidden
Traceback (most recent call last):
  File "/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 33, in __init__
    self.poll()
  File "/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 40, in poll
    self._data = self._poll()
  File "/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 44, in _poll
    return retry_function(self.RETRY_ATTEMPTS , self.get_data, url)
  File "/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/utils/retry.py", line 39, in retry_function
    raise e
HTTPError: HTTP Error 403: Forbidden
WARNING:jenkinsapi.jenkinsbase:Failed to conenct to https://jenkins.example.com/
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (59, 0))
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
~/dev/jenkinsapi-work/<ipython-input-7-c15028c8268e> in <module>()
----> 1 j = jenkins.Jenkins('https://jenkins.example.com/', username = 'kenn', password = 'foobar')

/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/jenkins.pyc in __init__(self, baseurl, username, password, proxyhost, proxyport, proxyuser, proxypass)
     39         self.proxyuser = proxyuser
     40         self.proxypass = proxypass
---> 41         JenkinsBase.__init__(self, baseurl)
     42 
     43     def get_proxy_auth(self):

/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.pyc in __init__(self, baseurl, poll)
     31         if poll:
     32             try:
---> 33                 self.poll()
     34             except urllib2.HTTPError, hte:
     35                 log.exception(hte)

/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.pyc in poll(self)
     38 
     39     def poll(self):
---> 40         self._data = self._poll()
     41 
     42     def _poll(self):

/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.pyc in _poll(self)
     42     def _poll(self):
     43         url = self.python_api_url(self.baseurl)
---> 44         return retry_function(self.RETRY_ATTEMPTS , self.get_data, url)
     45 
     46     def get_jenkins_obj(self):

/Users/kenn/.virtualenvs/jenkinsapi-work/lib/python2.7/site-packages/jenkinsapi/utils/retry.pyc in retry_function(tries, fn, *args, **kwargs)
     37         log.warn( "%s failed at attempt %i, trying again." % ( fn_name , attemptno ) )
     38         time.sleep( DEFAULT_SLEEP_TIME )
---> 39         raise e

HTTPError: HTTP Error 403: Forbidden

I have confirmed that if I just put a totally random password in, I get the same result.

  • Kenn

Launch a job with POST and not GET

Hi,

Since the v1.502 of Jenkins, jobs can not be launched with GET params.
There is this message :
"You must use POST method to trigger builds ..."

For this url for example :
http://ic.services.local/jenkins/job/my_job/buildWithParameters?my_param=test

And there is the same problem with your API. In the get_build_triggerurl function, you're calling the url "buildWithParameters?" with GET param.

Is there another way to call a job with parameter ?
Or is it a real issue ?

I'll try to fix the problem and I will post the hotfix here.

BR,

Eric

Creating stable branch

Hi, Salim

There are still issues with recent refactoring that I am encountering here. As we discussed in email, it is hard to do history rewrite for master.

Could you create 'stable' branch instead? I found that 6fbb3ea is last known good commit for this branch.

Thanks!

Creating new node

Hi,

Not sure if this is the right place, I just had a look at the library and it looks great. I am trying to see if I can use the create_node method to start of a instance on Amazon EC2/Eucalyptus cloud and get the slave agent installed on that instance.

If there are any code snippet/examples to get a better understanding that would be great.

Again, thanks a lot for this library and all the best!

Cheers,
Deependra

'Jenkins' object has no attribute 'get_data'

Base functionality of this package is broken right now. This commit 6a5505a is related to my issue, i guess.

(venv)vagrant@precise64:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from jenkinsapi.jenkins import Jenkins
>>> J = Jenkins('http://localhost:8080')
>>> J.create_node('test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/jenkinsapi/jenkins.py", line 422, in create_node
    if self.has_node(name):
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/jenkinsapi/jenkins.py", line 385, in has_node
    return nodename in self.get_node_dict()
  File "/home/vagrant/venv/local/lib/python2.7/site-packages/jenkinsapi/jenkins.py", line 356, in get_node_dict
    node_dict = dict(self.get_data(url))
AttributeError: 'Jenkins' object has no attribute 'get_data'

Is Jenkins module support google credentials?

I've tried using my google account but failed to get job. The Jenkins server is using "Google Apps SSO (with OpenID) " to do the authentication.

python invokejob.py
No handlers could be found for logger "jenkinsapi.jenkinsbase"
Traceback (most recent call last):
File "invokejob.py", line 2, in
jenkins = api.Jenkins('http://ci.voxeolabs.net/jenkins/api/python/', "xxxx", "xxxx")
File "build/bdist.macosx-10.7-intel/egg/jenkinsapi/jenkins.py", line 53, in init
File "build/bdist.macosx-10.7-intel/egg/jenkinsapi/jenkinsbase.py", line 36, in init
File "build/bdist.macosx-10.7-intel/egg/jenkinsapi/jenkinsbase.py", line 43, in poll
File "build/bdist.macosx-10.7-intel/egg/jenkinsapi/jenkinsbase.py", line 47, in _poll
File "build/bdist.macosx-10.7-intel/egg/jenkinsapi/utils/retry.py", line 25, in retry_function
File "build/bdist.macosx-10.7-intel/egg/jenkinsapi/jenkinsbase.py", line 70, in get_data
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(_args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(_args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unexpected authentication type: org.acegisecurity.providers.UsernamePasswordAuthenticationToken@9747d20d: Username: xxx Password: [PROTECTED]; Authenticated: false; Details: org.acegisecurity.ui.WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Not granted any authorities

jenkins.Jenkins.__getitem__ should only create Job for requested jobname

I am somewhat new to Python, so I apologize if I have misread the code.

It appears that getitem() iterates through self.get_jobs(). get_jobs() will create a Job instance for each jobname until it finds a match on the requested jobname. Each of these instance creations requires http requests to get job info. When you have a large number of jobs, this can be costly. I would propose something like the following to minimize the http requests.

def __getitem__(self, jobname):
    """
    Get a job by name
    :param jobname: name of job, str
    :return: Job obj
    """
    for info in self._data["jobs"]:
        if info["name"] == jobname:
            return Job(info["url"], info["name"], jenkins_obj=self)
    raise UnknownJob(jobname)

delete_job does not work

delete_job make a call to get_job without the auth info. This results in a 403 if auth is enabled.

job.get_config gave me 500 Internal Server Error

My code is like this:
jenkins = jenkinsapi.jenkins.Jenkins($MY_URL)
myJob = jenkins.get_job($MY_JOB_NAME)
config = myJob.get_config()

When running the code I got this:

File "........./test_jenkins.py", line 37, in run
config = myJob.get_config()
File "............/jenkinsapi/job.py", line 212, in get_config
return self.hit_url("%(baseurl)s/config.xml" % self.dict)
File "................../jenkinsbase.py", line 89, in hit_url
stream = fn_urlopen( url )
File ".............../urllib2.py", line 398, in open
response = meth(req, response)
File "................../urllib2.py", line 511, in http_response
'http', request, response, code, msg, hdrs)
File "................../urllib2.py", line 436, in error
return self._call_chain(_args)
File "..................../urllib2.py", line 370, in _call_chain
result = func(_args)
File "................/urllib2.py", line 519, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 500: Internal Server Error

I've investigated this issue for some time, and found that, the URL using in 'self.hit_url()' method is incorrect.
It should be like 'http://myJenkinsBaseURL/job/MyJob/config.xml', but actually it is 'http://myJenkinsBaseURL/job/MyJob//config.xml'.
This is because the job.py compose the URL like this '"%(baseurl)s/config.xml" % self.dict'. However, I found that in the 'self.dict', the base url is like this 'http://myJenkinsBaseURL/job/MyJob/'. So I got two slash (//) in the URL which is incorrect. If I call job.hit_url() with the correct URL, I can get the right config XML string.

Is this an issue with my Jenkins setting? Or is this an issue in the API? How can I overcome it?
I am using Jenkins 1.464 and API 0.1.9.

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.