Created
November 30, 2011 17:56
-
-
Save rail/1410019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steps/signing.py | 40 +++++++++++++++++++++++++++++----------- | |
1 files changed, 29 insertions(+), 11 deletions(-) | |
diff --git a/steps/signing.py b/steps/signing.py | |
index a12dcee..c540133 100644 | |
--- a/steps/signing.py | |
+++ b/steps/signing.py | |
@@ -3,16 +3,17 @@ from urllib import urlencode | |
from OpenSSL.SSL import Context, TLSv1_METHOD, VERIFY_PEER,\ | |
VERIFY_FAIL_IF_NO_PEER_CERT, OP_NO_SSLv2 | |
from OpenSSL.crypto import load_certificate, FILETYPE_PEM | |
from twisted.python.urlpath import URLPath | |
from twisted.internet.ssl import ContextFactory | |
from twisted.web.client import getPage | |
from twisted.python.failure import Failure | |
+from twisted.internet import reactor | |
from buildbot.steps.transfer import StringDownload | |
class HTTPSVerifyingContextFactory(ContextFactory): | |
isClient = True | |
def __init__(self, hostname, certfile): | |
self.hostname = hostname | |
@@ -31,59 +32,76 @@ class HTTPSVerifyingContextFactory(ContextFactory): | |
def verifyHostname(self, connection, x509, errno, depth, preverifyOK): | |
if preverifyOK: | |
if self.hostname == x509.get_subject().commonName: | |
return False | |
return preverifyOK | |
class SigningServerAuthenication(StringDownload): | |
+ current_attempt = 0 | |
+ uri = None | |
+ username = None | |
+ password = None | |
- def __init__(self, servers, server_cert, duration=6*3600, **kwargs): | |
+ def __init__(self, servers, server_cert, duration=6*3600, attempts=5, | |
+ sleeptime=60, **kwargs): | |
StringDownload.__init__(self, **kwargs) | |
self.addFactoryArguments(servers=servers, server_cert=server_cert, | |
duration=duration) | |
self.servers = list(servers) | |
self.server_cert = server_cert | |
self.duration = duration | |
- | |
+ self.attempts = attempts | |
+ self.sleeptime = sleeptime | |
def generateHeaders(self, method, credentials): | |
headers = {} | |
if method == 'POST': | |
headers['Content-Type'] = 'application/x-www-form-urlencoded' | |
base64string = '%s:%s' % (credentials[0], credentials[1]) | |
base64string = base64string.encode("base64").strip() | |
headers['Authorization'] = 'Basic %s' % base64string | |
return headers | |
def start(self): | |
- if len(self.servers) < 1: | |
- self.failed(Failure(Exception('No more signing servers to try.'))) | |
- uri, username, password = self.servers.pop() | |
- uri = 'https://%s/token' % uri | |
+ self.current_attempt += 1 | |
+ | |
+ if self.current_attempt > self.attempts: | |
+ if len(self.servers) < 1: | |
+ self.failed(Failure(Exception( | |
+ 'No more signing servers to try.'))) | |
+ else: | |
+ self.current_attempt = 1 | |
+ | |
+ if self.current_attempt == 1: | |
+ uri, self.username, self.password = self.servers.pop() | |
+ self.uri = 'https://%s/token' % uri | |
slaveName = self.getSlaveName() | |
slaveIP = self.buildslave.slave.broker.transport.getPeer().host | |
- self.stdio_log = stdio_log = self.addLog("stdio_%s" % len(self.servers)) | |
+ self.stdio_log = stdio_log = self.addLog( | |
+ "stdio_%s_%s" % (len(self.servers), self.current_attempt)) | |
stdio_log.addHeader("Slave: %s\n" % slaveName) | |
stdio_log.addHeader("IP: %s\n" % slaveIP) | |
stdio_log.addHeader("Duration: %s\n" % self.duration) | |
- stdio_log.addHeader("URI: %s\n" % uri) | |
+ stdio_log.addHeader("URI: %s\n" % self.uri) | |
method = 'POST' | |
postdata = { | |
'slave_ip': slaveIP, | |
'duration': self.duration, | |
} | |
- headers = self.generateHeaders(method=method, | |
- credentials=(username, password)) | |
+ headers = self.generateHeaders( | |
+ method=method, | |
+ credentials=(self.username, self.password)) | |
contextFactory = HTTPSVerifyingContextFactory( | |
URLPath(uri).netloc, self.server_cert) | |
d = getPage(uri, method=method, headers=headers, | |
postdata=urlencode(postdata), | |
contextFactory=contextFactory) | |
- d.addCallbacks(self.downloadSignature, lambda e: self.start()) | |
+ d.addCallbacks(self.downloadSignature, | |
+ lambda e: reactor.callLater(self.sleeptime, self.start)) | |
def downloadSignature(self, res): | |
self.s = res | |
StringDownload.start(self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment