source: proiecte/HadoopJUnit/hadoop-0.20.1/contrib/hod/hodlib/AllocationManagers/goldAllocationManager.py @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.1 KB
Line 
1#Licensed to the Apache Software Foundation (ASF) under one
2#or more contributor license agreements.  See the NOTICE file
3#distributed with this work for additional information
4#regarding copyright ownership.  The ASF licenses this file
5#to you under the Apache License, Version 2.0 (the
6#"License"); you may not use this file except in compliance
7#with the License.  You may obtain a copy of the License at
8
9#     http://www.apache.org/licenses/LICENSE-2.0
10
11#Unless required by applicable law or agreed to in writing, software
12#distributed under the License is distributed on an "AS IS" BASIS,
13#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#See the License for the specific language governing permissions and
15#limitations under the License.
16"""Gold Allocation Manager Implementation"""
17# -*- python -*-
18
19import sys, httplib
20import sha, base64, hmac
21import xml.dom.minidom
22
23from hodlib.Common.util import *
24
25class goldAllocationManager:
26  def __init__(self, cfg, log):
27    self.__GOLD_SECRET_KEY_FILE = cfg['auth-file']
28    (self.__goldHost, self.__goldPort) = (cfg['allocation-manager-address'][0], 
29                                          cfg['allocation-manager-address'][1])
30    self.cfg = cfg
31    self.log = log
32
33  def getQuote(self, user, project, ignoreErrors=True):
34    # Get Secret Key from File
35    secret = ''
36    try:
37      secretFile = open(self.__GOLD_SECRET_KEY_FILE)
38      secret = secretFile.readline()
39    except Exception, e:
40      self.log.error("Unable to open file %s" % self.__GOLD_SECRET_KEY_FILE)
41      self.log.debug(get_exception_string())
42      return (ignoreErrors or False)
43    secretFile.close()
44    secret = secret.rstrip()
45
46    # construct the SSRMAP request body
47    body = '<Body><Request action="Quote" actor="hod"><Object>Job</Object><Data><Job><ProjectId>%s</ProjectId><UserId>%s</UserId><WallDuration>10</WallDuration></Job></Data></Request></Body>' % (project, user)
48
49    # compute digest
50    message = sha.new()
51    message.update(body)
52    digest = message.digest()
53    digestStr = base64.b64encode(digest)
54
55    # compute signature
56    message = hmac.new(secret, digest, sha)
57    signatureStr = base64.b64encode(message.digest())
58
59    # construct the SSSRMAP Message
60    sssrmapRequest = '<?xml version="1.0" encoding="UTF-8"?>\
61<Envelope>%s<Signature><DigestValue>%s</DigestValue><SignatureValue>%s</SignatureValue><SecurityToken type="Symmetric"></SecurityToken></Signature></Envelope>' % (body, digestStr, signatureStr)
62    self.log.info('sssrmapRequest: %s' % sssrmapRequest)
63
64    try:
65      # post message to GOLD server
66      webservice = httplib.HTTP(self.__goldHost, self.__goldPort)
67      webservice.putrequest("POST", "/SSSRMAP3 HTTP/1.1")
68      webservice.putheader("Content-Type", "text/xml; charset=\"utf-8\"")
69      webservice.putheader("Transfer-Encoding", "chunked")
70      webservice.endheaders()
71      webservice.send("%X" % len(sssrmapRequest) + "\r\n" + sssrmapRequest + '0\r\n')
72
73      # handle the response
74      statusCode, statusmessage, header = webservice.getreply()
75      responseStr = webservice.getfile().read()
76      self.log.debug("httpStatusCode: %d" % statusCode)
77      self.log.info('responseStr: %s' % responseStr)
78
79      # parse XML response
80      if (statusCode == 200):
81        responseArr = responseStr.split("\n")
82        responseBody = responseArr[2]
83        try:
84          doc = xml.dom.minidom.parseString(responseBody)
85          responseVal = doc.getElementsByTagName("Value")[0].firstChild.nodeValue
86          self.log.info("responseVal: %s" % responseVal)
87          if (responseVal == 'Success'):
88            return True
89          else:
90            return False
91        except Exception, e:
92          self.log.error("Unable to parse GOLD responseBody XML \"(%s)\" to get responseVal" % (responseBody))
93          self.log.debug(get_exception_string())
94          return (ignoreErrors or False)
95      else:
96        self.log.error("Invalid HTTP statusCode %d" % statusCode)
97    except Exception, e:
98      self.log.error("Unable to POST message to GOLD server (%s, %d)" %
99                       (self.__goldHost, self.__goldPort))
100      self.log.debug(get_exception_string())
101      return (ignoreErrors or False)
102
103    return True
104
Note: See TracBrowser for help on using the repository browser.