Added support for JENKINS_URL env. variable
authorAleksey Maksimov <ctpeko3a@gmail.com>
Tue, 23 Sep 2014 14:39:11 +0000 (22:39 +0800)
committerAleksey Maksimov <ctpeko3a@gmail.com>
Tue, 23 Sep 2014 14:39:11 +0000 (22:39 +0800)
jenkinsapi_tests/systests/__init__.py
jenkinsapi_tests/systests/get-jenkins-war.sh
jenkinsapi_utils/jenkins_launcher.py

index ce742b50050e8f909fdb624e36e8b0eda31aedb5..3e1fc6b83e8e89249853b1242b04255acd622469 100644 (file)
@@ -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()
 
 
index d04030f68329cc059dd6f5748805873a3a438b78..0332629cc370f5b285e77769627af800349ac634 100755 (executable)
@@ -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
index fc4fa514cabdd124d97cc9e31958fcae0d105e28..785ff938fcffcd48a0f3a5900508956a2f90b96e 100644 (file)
@@ -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...")