11#!/usr/bin/env python
22from __future__ import print_function
3- import json , requests , tarfile , argparse , os
3+ import json , requests , tarfile , argparse , os , errno
44from urlparse import urljoin
55from flask import Flask , jsonify , request , abort
66app = Flask (__name__ )
77
8- arg_jenkins_url , arg_listen_port , arg_extract_path , arg_should_clean = None , None , None , None
8+ arg_jenkins_url , arg_listen_port , arg_extract_path , arg_should_clean , arg_symlink = (
9+ None , None , None , None , None
10+ )
911
1012def download_file (url ):
1113 local_filename = url .split ('/' )[- 1 ]
@@ -20,6 +22,17 @@ def untar_to(tarball, dest):
2022 with tarfile .open (tarball ) as tar :
2123 tar .extractall (dest )
2224
25+ def create_symlink (source , linkname ):
26+ try :
27+ os .symlink (source , linkname )
28+ except OSError , e :
29+ if e .errno == errno .EEXIST :
30+ # atomic modification
31+ os .symlink (source , linkname + ".tmp" )
32+ os .rename (linkname + ".tmp" , linkname )
33+ else :
34+ raise e
35+
2336@app .route ("/" , methods = ["POST" ])
2437def on_receive_jenkins_poke ():
2538 # {
@@ -107,6 +120,7 @@ def on_receive_jenkins_poke():
107120 with open (os .path .join (untar_location , "vector/version" ), "w" ) as stamp_file :
108121 stamp_file .write (name_str )
109122
123+ create_symlink (source = os .path .join (untar_location , "vector" ), linkname = arg_symlink )
110124
111125 return jsonify ({})
112126
@@ -133,6 +147,13 @@ def on_receive_jenkins_poke():
133147 "Remove .tar.gz files after they have been downloaded and extracted."
134148 )
135149 )
150+ parser .add_argument (
151+ "-s" , "--symlink" , dest = "symlink" , default = "./latest" , help = (
152+ "Write a symlink to this location pointing to the extracted tarball. \
153+ New builds will keep overwriting this symlink. The symlink will point \
154+ to the /vector directory INSIDE the tarball."
155+ )
156+ )
136157 args = parser .parse_args ()
137158 if args .jenkins .endswith ("/" ): # important for urljoin
138159 arg_jenkins_url = args .jenkins
@@ -141,10 +162,10 @@ def on_receive_jenkins_poke():
141162 arg_extract_path = args .extract
142163 arg_listen_port = args .port
143164 arg_should_clean = args .clean
165+ arg_symlink = args .symlink
144166 print (
145- "Listening on port %s. Extracting to %s%s. Jenkins URL: %s" %
167+ "Listening on port %s. Extracting to %s%s. Symlinking to %s. Jenkins URL: %s" %
146168 (arg_listen_port , arg_extract_path ,
147- " (clean after)" if arg_should_clean else "" , arg_jenkins_url )
148-
169+ " (clean after)" if arg_should_clean else "" , arg_symlink , arg_jenkins_url )
149170 )
150171 app .run (debug = True )
0 commit comments