diff options
author | Aleksey Maksimov <ctpeko3a@gmail.com> | 2014-09-23 22:39:11 +0800 |
---|---|---|
committer | Aleksey Maksimov <ctpeko3a@gmail.com> | 2014-09-23 22:39:11 +0800 |
commit | 2ff2c3b0cb4721a1b8db5464228139ae5cf93f4f (patch) | |
tree | b8181fdb070a940c9241595c9d8606c63ec66cf7 | |
parent | 6f425d0e57d85ec20f403df96d9b1b9cd77682cf (diff) | |
download | python-jenkinsapi-2ff2c3b0cb4721a1b8db5464228139ae5cf93f4f.tar.gz python-jenkinsapi-2ff2c3b0cb4721a1b8db5464228139ae5cf93f4f.tar.bz2 python-jenkinsapi-2ff2c3b0cb4721a1b8db5464228139ae5cf93f4f.zip |
Added support for JENKINS_URL env. variable
-rw-r--r-- | jenkinsapi_tests/systests/__init__.py | 3 | ||||
-rwxr-xr-x | jenkinsapi_tests/systests/get-jenkins-war.sh | 13 | ||||
-rw-r--r-- | jenkinsapi_utils/jenkins_launcher.py | 128 |
3 files changed, 83 insertions, 61 deletions
diff --git a/jenkinsapi_tests/systests/__init__.py b/jenkinsapi_tests/systests/__init__.py index ce742b5..3e1fc6b 100644 --- a/jenkinsapi_tests/systests/__init__.py +++ b/jenkinsapi_tests/systests/__init__.py @@ -12,7 +12,8 @@ PLUGIN_DEPENDENCIES = ["http://updates.jenkins-ci.org/latest/git.hpi", def setUpPackage(): systests_dir, _ = os.path.split(__file__) war_path = os.path.join(systests_dir, 'jenkins.war') - state['launcher'] = JenkinsLancher(war_path, PLUGIN_DEPENDENCIES) + state['launcher'] = JenkinsLancher(war_path, PLUGIN_DEPENDENCIES, + jenkins_url=os.getenv('JENKINS_URL', None)) state['launcher'].start() diff --git a/jenkinsapi_tests/systests/get-jenkins-war.sh b/jenkinsapi_tests/systests/get-jenkins-war.sh index d04030f..0332629 100755 --- a/jenkinsapi_tests/systests/get-jenkins-war.sh +++ b/jenkinsapi_tests/systests/get-jenkins-war.sh @@ -1,5 +1,12 @@ -#!/bin/sh +#!/bin/bash #JENKINS_WAR_URL="http://mirrors.jenkins-ci.org/war/latest/jenkins.war" -JENKINS_WAR_URL="http://mirrors.jenkins-ci.org/war-stable/latest/jenkins.war" -wget $JENKINS_WAR_URL +if [[ "$#" -ne 2 ]]; then + echo "Usage: $0 jenkins_url path_to_store_jenkins" + exit 1 +fi + +readonly JENKINS_WAR_URL=$1 +readonly JENKINS_PATH=$2 + +wget -O ${JENKINS_PATH}/jenkins.war $JENKINS_WAR_URL diff --git a/jenkinsapi_utils/jenkins_launcher.py b/jenkinsapi_utils/jenkins_launcher.py index fc4fa51..785ff93 100644 --- a/jenkinsapi_utils/jenkins_launcher.py +++ b/jenkinsapi_utils/jenkins_launcher.py @@ -12,7 +12,11 @@ import tempfile import requests import threading import subprocess -import pkg_resources +from pkg_resources import resource_string +try: + from urlparse import urlparse +except ImportError: + from urllib.parse import urlparse from jenkinsapi.jenkins import Jenkins from jenkinsapi.custom_exceptions import JenkinsAPIException @@ -57,14 +61,16 @@ class JenkinsLancher(object): """ JENKINS_WAR_URL = "http://mirrors.jenkins-ci.org/war/latest/jenkins.war" - def __init__(self, war_path, plugin_urls=None): + def __init__(self, war_path, plugin_urls=None, jenkins_url=None): + self.jenkins_url = jenkins_url + self.http_port = random.randint(9000, 10000) if not jenkins_url \ + else urlparse(jenkins_url).port self.war_path = war_path self.war_directory, self.war_filename = os.path.split(self.war_path) self.jenkins_home = tempfile.mkdtemp(prefix='jenkins-home-') self.jenkins_process = None self.q = Queue.Queue() self.plugin_urls = plugin_urls or [] - self.http_port = random.randint(9000, 10000) def update_war(self): os.chdir(self.war_directory) @@ -72,19 +78,22 @@ class JenkinsLancher(object): log.info("We already have the War file...") else: log.info("Redownloading Jenkins") - subprocess.check_call('./get-jenkins-war.sh') + script_dir = os.path.join(self.war_directory, + 'get-jenkins-war.sh') + subprocess.check_call([script_dir, + self.JENKINS_WAR_URL, self.war_directory]) def update_config(self): config_dest = os.path.join(self.jenkins_home, 'config.xml') config_dest_file = open(config_dest, 'w') - config_source = pkg_resources.resource_string('jenkinsapi_tests.systests', 'config.xml') + config_source = resource_string('jenkinsapi_tests.systests', + 'config.xml') try: config_dest_file.write(config_source.encode('UTF-8')) except AttributeError: # Python 3.x config_dest_file.write(config_source.decode('UTF-8')) - def install_plugins(self): for i, url in enumerate(self.plugin_urls): self.install_plugin(url, i) @@ -104,10 +113,11 @@ class JenkinsLancher(object): h.write(request.content) def stop(self): - log.info("Shutting down jenkins.") - self.jenkins_process.terminate() - self.jenkins_process.wait() - shutil.rmtree(self.jenkins_home) + if not self.jenkins_url: + log.info("Shutting down jenkins.") + self.jenkins_process.terminate() + self.jenkins_process.wait() + shutil.rmtree(self.jenkins_home) def block_until_jenkins_ready(self, timeout): start_time = datetime.datetime.now() @@ -124,55 +134,58 @@ class JenkinsLancher(object): time.sleep(5) def start(self, timeout=60): - self.update_war() - self.update_config() - self.install_plugins() - - os.environ['JENKINS_HOME'] = self.jenkins_home - os.chdir(self.war_directory) - - jenkins_command = ['java', '-jar', self.war_filename, - '--httpPort=%d' % self.http_port] - - log.info("About to start Jenkins...") - log.info("%s> %s", os.getcwd(), " ".join(jenkins_command)) - self.jenkins_process = subprocess.Popen( - jenkins_command, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - - threads = [ - StreamThread('out', self.q, self.jenkins_process.stdout, log.info), - StreamThread('err', self.q, self.jenkins_process.stderr, log.warn) - ] - - # Start the threads - for t in threads: - t.start() - - while True: - try: - streamName, line = self.q.get(block=True, timeout=timeout) - # Python 3.x - if isinstance(line, bytes): - line = line.decode('UTF-8') - except Queue.Empty: - log.warn("Input ended unexpectedly") - break - else: - if line: - if 'Failed to initialize Jenkins' in line: - raise FailedToStart(line) + if not self.jenkins_url: + self.update_war() + self.update_config() + self.install_plugins() + + os.environ['JENKINS_HOME'] = self.jenkins_home + os.chdir(self.war_directory) + + jenkins_command = ['java', '-jar', self.war_filename, + '--httpPort=%d' % self.http_port] + + log.info("About to start Jenkins...") + log.info("%s> %s", os.getcwd(), " ".join(jenkins_command)) + self.jenkins_process = subprocess.Popen( + jenkins_command, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + threads = [ + StreamThread('out', self.q, self.jenkins_process.stdout, + log.info), + StreamThread('err', self.q, self.jenkins_process.stderr, + log.warn) + ] + + # Start the threads + for t in threads: + t.start() + + while True: + try: + streamName, line = self.q.get(block=True, timeout=timeout) + # Python 3.x + if isinstance(line, bytes): + line = line.decode('UTF-8') + except Queue.Empty: + log.warn("Input ended unexpectedly") + break + else: + if line: + if 'Failed to initialize Jenkins' in line: + raise FailedToStart(line) - if 'Invalid or corrupt jarfile' in line: - raise FailedToStart(line) + if 'Invalid or corrupt jarfile' in line: + raise FailedToStart(line) - if 'is fully up and running' in line: - log.info(line) - return - else: - log.warn('Stream %s has terminated', streamName) + if 'is fully up and running' in line: + log.info(line) + return + else: + log.warn('Stream %s has terminated', streamName) - self.block_until_jenkins_ready(timeout) + self.block_until_jenkins_ready(timeout) if __name__ == '__main__': @@ -182,7 +195,8 @@ if __name__ == '__main__': log.info("Hello!") jl = JenkinsLancher( - '/home/sal/workspace/jenkinsapi/src/jenkinsapi_tests/systests/jenkins.war' + '/home/sal/workspace/jenkinsapi/src/' + 'jenkinsapi_tests/systests/jenkins.war' ) jl.start() log.info("Jenkins was launched...") |