61 Commits

Author SHA1 Message Date
Guido Günther
a2daa44ef3 Don't return None on unhandled http errors
We handle 401, 403, 500 and 404 everything else currently returns None
breaking parsing in the callers with hard to track down errors.

Just raise the actual HTTP error in that case.

Change-Id: I35e4f082069941cd5a8d97185693501ab2b0dc6f
2015-02-09 23:59:29 +01:00
Guido Günther
b5a11e6c3c Use empty crumb on empty response
If jenkins_open fails with an empty response we don't set a crumb. The
rest of the code will figure out the real error later.

Change-Id: Ib1743101b6a474656909fc1cb7989c971a56f187
2015-02-09 23:59:29 +01:00
Guido Günther
5997c70da2 Don't assume we get a valid response
The response might be None while jenkins is starting. Handle this like
an HTTP error.

Change-Id: Ieaed31e9a1c8acf0bb32d6d88752f1ce3ecc5daf
2015-02-09 23:59:29 +01:00
Khai Do
e197dd5454 Add a default http timeout for connections to jenkins
Without a timeout a script can hang forever when attempting
to connect to jenkins. This change sets a default timeout
of 2 minutes.  Selection of default value is pretty arbitrary
on this change.

Closes-Bug: #1273329
Change-Id: If84778231b88d78a02a89a56f38f95d6deada80a
2015-01-22 23:14:18 -08:00
Kevin L. Mitchell
bcfefa1017 Eliminate redundant calls to assert_job_exists()
Now that the jenkins_open() method will raise a specific exception
if Jenkins returns a 404, some calls to assert_job_exists() now
become redundant.  Since these calls represent an extra round-trip
with the Jenkins server, dropping them will result in a reduction
in time taken for such things as a jenkins-job run.  This change
eliminates the redundant calls and adjusts the tests as appropriate.

Change-Id: If78dea9ad60d446854d24919bdd7f3f1fee3d733
2015-01-13 16:39:57 -06:00
Kevin L. Mitchell
8c3e979810 Raise an exception on 404
Currently, when Jenkins returns a 404 error, the jenkins_open()
method gives no indication.  This adds a new NotFoundException,
descending from JenkinsException, which can be used to distinguish
this case.  This causes a problem with cancel_queue(), however,
as the Jenkins API always returns a 404; since cancel_queue() is
a best-effort mechanism, this change also modifies that method to
explicitly ignore a NotFoundException.  The maybe_add_crumb()
method also has to be updated to properly handle 404s, for the
case where crumbs are disabled.  The get_job_name() method is also
adjusted to return None if jenkins_open() raises a NotFoundException.

A few quick google searches shows that only the cancelItem
endpoint will always return 404.

Change-Id: I98320d174659fc83d48614b31a5a054fd0773dfe
Closes-Bug: #1246468
2015-01-13 16:38:15 -06:00
Jenkins
d56dd55a17 Merge "Fix cancel_queue() method" 2015-01-02 20:32:31 +00:00
Khai Do
5d52721264 Fix cancel_queue() method
The cancel_queue() method needed updating because the jenkins URL
for it has changed and the request requires a POST to work.

Closes-Bug: #1246466
Change-Id: I89fcee3715c67a989a08ca75c0232c4fd87375c0
2014-12-23 10:38:18 -08:00
Khai Do
535fa49bc8 Fix enable_node and disable_node methods
The disable_node() and enable_node() requires a POST otherwise it doesn't
do anything.  This change modifies the request to a POST.

Change-Id: Id320d42908cc695c38a72d1a09d9cf022bbbea80
2014-12-19 09:18:05 -08:00
Jenkins
9f41c4c2b9 Merge "Handle job and node names that contain spaces" 2014-12-19 06:09:53 +00:00
Jenkins
bdc7c4a345 Merge "Fix it so build_job triggers a build" 2014-12-15 15:51:52 +00:00
Khai Do
ebe1052b60 Handle job and node names that contain spaces
The current methods do not work for jobs and node that contain spaces in
their names.  This change adds quotes around the names in the jenkins
URL so that Jenkins will be able to process the URL correctly.

Closes-Bug: #1000799
Closes-Bug: #1183958
Change-Id: I86ac5aa8b2ff9b92ba240637f4a225978236e6db
2014-12-10 10:39:53 -08:00
Jenkins
b2f2cf022a Merge "Yield HTTPError reason on open error" 2014-11-13 18:39:14 +00:00
Antoine Musso
08af5515fb Yield HTTPError reason on open error
Having only the error code from urllib is not that interesting.
HTTPError inherits from URLError which has a 'msg' attribute.  Output it
along the error code.

HTTPError does not have a 'reason' attribute although python six
provides it for py27 and later. So fallback to 'msg'.

Change-Id: I591684785fd7af43b833e84fa1e40f46a852bef2
2014-11-13 09:41:08 -08:00
Antoine Musso
69655cc682 fix auth with long username or password
Our auth_headers() helper uses base64.encodestring() which inserts
newlines to generate a nicely formatted text. That is surely helpful for
display purposes but unwanted when forging an authentication string.

encodestring() always append a newline, which was the reason for the
[:-1] slicing.

Switch to base64.b64encoding() and add a test ensuring no newline is
inserted.

Closes-Bug: 1039307
Change-Id: I71c3e6d504a500a1f21ff89b1d54a155da1aaf0e
2014-11-12 10:16:37 +01:00
Khai Do
b50c59aa98 Add get_plugins_info and get_plugin_info methods
This change adds methods to retrieve jenkins plugin information.

get_plugins_info - will return a list containing all installed plugins.

get_plugin_info - will query (by name) for a specific plugin and return
  info on that plugin.

Change-Id: Ia6cd90571ff260aec9869f48df06486b7fac181f
2014-10-06 14:40:48 -07:00
Khai Do
dafffd2eb8 add get_version method
Add a method to get the Jenkins server (or master) version

Signed-off-by: Antoine Musso <hashar@free.fr>
Change-Id: I9bbcf5fd1615246e53a9d83fec9d0c93c64b15aa
2014-09-30 21:32:08 +02:00
Jenkins
20b49b6520 Merge "Setup pbr versioning for docs" 2014-09-26 20:18:13 +00:00
Khai Do
21d604c4e6 Setup pbr versioning for docs
Change-Id: I77572a5f6185c19728028547c23bc76d73e48f22
2014-09-26 09:12:54 -07:00
James E. Blair
3f221c9fdc Support node configuration
Bring support for node configuration with  get_node_config() and
reconfig_node().  This comes from OpenStack Nodepool project originally
written by James E. Blair in the devstack-gate project.

Closes-Bug: #1234273
Change-Id: I29423d8bab5fd74f1c7f63002d1be7a2ae0be176
2014-09-24 15:55:31 -07:00
Jenkins
2f558ef96d Merge "Support node startup with the JNLP Launcher" 2014-09-24 17:36:57 +00:00
Terry Howe
44eb4fe69c Fix it so build_job triggers a build
The build_job method would not trigger a build because it
would do a GET on the build URL rather than a POST.  Adding
a body causes it to do a POST.

Change-Id: Idf40e462f970122234f85b5d1d2e72d178b5b39f
Closes-Bug: 1177831
2014-09-18 10:32:58 -06:00
Jenkins
983614c843 Merge "Enabled hacking check H404" 2014-08-29 19:34:55 +00:00
Christian Berendt
0478046d50 Enabled hacking check H404
* [H404] Multi line docstrings should start without a leading new line.

Change-Id: I22b0a00b441e01698a73bc8dc6315c47aa9ee05a
2014-08-26 13:51:12 -04:00
Alexandre Conrad
9bac4fb1b1 make JSON depth response configurable
Change-Id: Ic1a7656b9ccaa81b460f62aa3c2d3df5173cad93
2014-07-02 10:58:05 -07:00
Jenkins
7686450ec7 Merge "Adds assert_node_exists()" 2014-07-02 16:37:56 +00:00
Jenkins
d4c3310ae6 Merge "Speed up job existence tests by fetching less info" 2014-06-18 16:34:50 +00:00
Antoine Musso
9ce758a3d2 Adds assert_node_exists()
This is similar to recently introduced assert_job_exists(). It will
throws an exception whenever a node is missing and further customize the
exception messages.

Replace an occurence of:

  if not self.node_exists(name):
      raise JenkinsException('node[%s] already exists' % (name))

By:

  self.assert_node_exists(name, 'node[%s] already exists')

Change-Id: Ia2e91843b353c02fb19290eadbbb83db8d2a3124
2014-06-18 11:28:13 +02:00
Antoine Musso
c5ccb8560c Speed up job existence tests by fetching less info
Most methods are asserting that a job exists by calling get_job_info().
Unfortunately that might trigger on the server side a parse of the full
job history to gather the lastSuccessful, lastFailure build numbers.
Since Jenkins is heavily file based, the process is rather long on jobs
having thousands and thousands of build.

Since we only care about checking whether the job exists, introduce
job_exists() which is light on the server side.  To throw exception,
this also introduce the wrapper assert_job_exists() which can futher
customize the exception messages.

Updates all constructs such as:

    if not self.job_exists(job_name):
	raise JenkinsException(<some message> % (job_name))

To:

    self.assert_job_exists(to_name, <some message>)

That speed up operations tremendously whenever the targetted jobs are
very long.

Added coverage test.

Change-Id: I946dbb7a08a8bb172a369af3b2aedc07e3cea9e6
2014-06-18 11:08:32 +02:00
Marc Abramowitz
8c91723b81 Fix remaining PEP8 errors in docstrings
Change-Id: Iafb6bd53b61910b860ea01ce366af0fa2a56fbc5
2014-06-05 11:26:16 -07:00
Marc Abramowitz
bc79ae4890 Fix hacking docstring warnings
Fix "H404  multi line docstring should start with a summary" hacking
warnings

Change-Id: I76bf4f0504569b4ea6adbdb555d8921562e705bf
2014-06-04 06:34:58 -07:00
Marc Abramowitz
5d7ecd43e0 Make it work on Python 3
Change-Id: Ia8eedf699e988a199520c48e21c9731af912003c
2014-05-21 22:49:34 -07:00
Marc Abramowitz
9424023180 100% test coverage
Change-Id: Ie035a299a2f92eede9a88c00e4dc4ad820fbc710
2014-05-21 22:35:32 -07:00
James E. Blair
c7a6265bab Support node startup with the JNLP Launcher
Original author is James E. Blair in the devstack-gate project.

Change-Id: I92d363e5916adfd07744b959080f8b6c3b660ceb
2014-05-20 14:54:43 +00:00
Antoine Musso
b97c31f9dd Clarify job_exists() error messages
To verify a job exist, we query Jenkins API and attempt to load the
resulting JSON.  The error message is very simple:

 Jenkins returned an unexpected job name

This patch add the actual job name received and the expected one.  That
might help diagnosis.

Change-Id: Ib13c3bb8d265612811714d10818364956f84a341
2014-05-02 21:19:52 +00:00
Antoine Musso
5269924155 Wrap tests and linting with tox
Use tox configuration to expose linting and unit testing. Will let us
integrate them with OpenStack continuous integration infrastructure.

* updates .gitignore following the migration from bzr
* get rid of the old Makefile in favor of tox
* remove .pep8 file (that is now a [flake8] section in tox.ini)
* fix a few trivial pep8 errors (comments must start with '# ' and
  modulo operator needs surrounding spaces) but ignore 'line too long
  (E501)' for now.
* write module requirements in /test-requirements.txt. Discover is
  needed for python 2.6.
* change the test command that cames from the Makefile so it works with
  python 2.6

Change-Id: If58730d84315c0ea018a3757624d98bf2e1aeb3f
2014-04-30 22:37:19 +02:00
Soren Hansen
df4c87ab01 Change method name to reflect behaviour 2013-09-28 22:00:43 +02:00
Soren Hansen
76a94ba173 Recycle crumb. Apparently, it's not a nonce. 2013-09-28 21:58:55 +02:00
Soren Hansen
81ddbcf01e Attempt to fetch a crumb (needed if CSRF protection is enabled) 2013-09-27 22:17:06 +02:00
James Page
738ef3c6f1 Antoine Musso 2013-06-25 [merge] speed up job existence test by fetching less informations 2013-08-14 12:57:11 +01:00
Antoine Musso
6faa8bf973 speed up job existence test by fetching less informations
The job_exists() methods rely on an API call that cause Jenkins to LazyLoad the
build history. Whenever a job has a huge build history, that would be awfully
slow.  The API let you fetch only a subset of the information you need by using
the 'tree' parameter, by requesting only the name of the job, we ensure the
server is only going to do a very simple operation.

It is then only a matter of checking the job name returned the API is equal to
the one requested, that validates the job exists and is accessible.

Tested out on a dev setup with a job having a million builds:

import jenkins
from pprint import pprint

j = jenkins.Jenkins('https://localhost:8080/')

for job_name in ['huge-history', 'Idonotexist']:
    print "Getting job name of %s" % job_name
    pprint(j.get_job_name(job_name))
    print "Does it exist?"
    pprint(j.job_exists(job_name))

Output:

Getting job name of huge-history
u'huge-history'
Does it exist?
True
Getting job name of Idonotexist
None
Does it exist?
None
2013-06-25 15:30:23 +02:00
Adam Gandelman
f6ca4e6ecc Add rename_job(). 2013-05-11 20:52:11 -07:00
Adam Gandelman
58185eced7 Add ability to fetch raw console text for build. 2013-05-11 20:48:02 -07:00
Antoine Musso
170e878d2a overhaul the sphinx documentation
Sphinx:
* Build dir is now named `build` and the files are under `source`.
* MakeFile learned texinfo, info and gettext targets
* include __init__ documentation (autoclass_content)
* keep file ordering for methods (autodoc_member_order)
* comment out html_static_path to get rid of a warning

Doc:
* Index is now... an index! Takes advantage of :glob: to automatically
  create a complete table of content.
* Creates API reference which list the documentation directly from the
  jenkins/__init__.py file.  That will avoid the documentation duplication
  and some out of sync documentation.
* insert the module in the path to document it
* the example were both in index.rst and __init__.py create a new section
  with example.rst.  That can be later be improved with some typical use
  cases.
* A couple documentation update to some methods. The inline documentation
  was out of sync though the index.rst got updated.
2013-04-13 23:59:59 +02:00
Antoine Musso
c3e23c2973 pass pep8 on all files
pep8 is the python style standard. I have ignored long lines though.
2013-04-13 20:35:11 +02:00
Adam Gandelman
93e2cd6d99 Add support for a rename_job() call. 2012-10-19 14:20:43 -07:00
James Page
40a9b5243a Merged new node management features including using SSH and Windows slaves plus cancel job/queue methods 2012-06-25 12:48:32 +01:00
James E. Blair
f39275bc5f Add additional methods:
cancel_queue
stop_build
disable_node
enable_node

And add support for ssh and windows service launcher types to
create_node.
2012-06-20 14:41:59 -07:00
Kevin McDermott
08f41aa93f Fixes for this bug. 2012-05-17 16:10:00 +01:00
James Page
2ccd2c77d8 Tidied trailing whitespace 2012-03-02 16:26:13 +00:00