Module xalt_transmission_factory
[hide private]
[frames] | no frames]

Source Code for Module xalt_transmission_factory

  1  #----------------------------------------------------------------------- 
  2  # XALT: A tool that tracks users jobs and environments on a cluster. 
  3  # Copyright (C) 2013-2014 University of Texas at Austin 
  4  # Copyright (C) 2013-2014 University of Tennessee 
  5  #  
  6  # This library is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser General Public License as 
  8  # published by the Free Software Foundation; either version 2.1 of  
  9  # the License, or (at your option) any later version.  
 10  # 
 11  # This library is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 14  # Lesser  General Public License for more details.  
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public 
 17  # License along with this library; if not, write to the Free 
 18  # Software Foundation, Inc., 59 Temple Place, Suite 330, 
 19  # Boston, MA 02111-1307 USA 
 20  #----------------------------------------------------------------------- 
 21  from __future__  import print_function 
 22  import os, sys, json, base64 
 23   
 24  try: 
 25    from XALTdb      import XALTdb 
 26    XALTdb_available = True 
 27  except ImportError: 
 28    XALTdb_available = False 
 29     
 30     
 31  from XALT_Rmap   import Rmap 
 32  from xalt_global import * 
33 34 -class XALT_transmission_factory(object):
35 """ 36 This class is a factory to determine which way to "write" the json data. 37 """
38 - def __init__(self, syshost, kind):
39 """ 40 This ctor saves away the system name and kind of record (link, run) 41 42 @param syshost: Name of the system. 43 @param kind: Type of record: link or run 44 """ 45 46 self.__syshost = syshost 47 self.__kind = kind
48
49 - def _syshost(self):
50 """ 51 returns the system name 52 @return returns the system name 53 """ 54 return self.__syshost
55
56 - def _kind(self):
57 """ 58 Returns the kind of record: link or run 59 @return the kind of record. 60 """ 61 return self.__kind
62 63 @staticmethod
64 - def build(name, syshost, kind, fn):
65 """ 66 The static factory build routine that returns the transmission object. 67 68 @param name: Name of the factory: syslog, directdb, file 69 @param syshost: The system name. Should be stampede or darter not login1.stampede.... 70 @param kind: Type of record: link, run 71 @param fn: file name (only used by file transport object 72 """ 73 74 name = name.lower() 75 if (name == "syslog"): 76 obj = Syslog(syshost, kind) 77 elif (name == "directdb"): 78 obj = DirectDB(syshost, kind) 79 else: 80 # file 81 obj = File(syshost, kind, fn) 82 83 return obj
84
85 -class Syslog(XALT_transmission_factory):
86 """ 87 This class write the json record to syslog 88 """ 89
90 - def __init__(self, syshost, kind):
91 """ 92 This is the ctor for Syslog transmission method 93 @param syshost: Name of the system. 94 @param kind: Type of record: link or run 95 """ 96 97 super(Syslog, self).__init__(syshost, kind)
98 - def save(self, resultT):
99 """ 100 The json table is written to syslog with the text converted to base64. 101 @param resultT: The json record table 102 """ 103 sA = [] 104 sA.append("logger -t XALT_LOGGING") 105 sA.append(" \"") 106 sA.append(self._kind()) 107 sA.append(":") 108 sA.append(self._syshost()) 109 sA.append(":") 110 sA.append(base64.b64encode(json.dumps(resultT))) 111 sA.append("\"") 112 s = "".join(sA) 113 os.system(s)
114
115 116 -class File(XALT_transmission_factory):
117 """ 118 This is the file transport class 119 """ 120
121 - def __init__(self, syshost, kind, fn):
122 """ 123 This ctor is for the file transport method. 124 @param syshost: Name of the system. 125 @param kind: Type of record: link or run 126 @param fn: the file name to write the record to. 127 """ 128 super(File, self).__init__(syshost, kind) 129 self.__fn = fn
130
131 - def save(self, resultT):
132 """ 133 The json table is written to the file specified in the ctor. 134 @param resultT: The json record table 135 """ 136 s = json.dumps(resultT, sort_keys=True, 137 indent=2, separators=(',',': ')) 138 dirname, fn = os.path.split(self.__fn) 139 tmpFn = os.path.join(dirname, "." + fn) 140 if (not os.path.isdir(dirname)): 141 os.mkdir(dirname); 142 143 f = open(tmpFn,"w") 144 f.write(s) 145 f.close() 146 os.rename(tmpFn, self.__fn)
147
148 149 -class DirectDB(XALT_transmission_factory):
150 """ 151 This class is the direct to db transmission method. 152 """ 153
154 - def __init__(self, syshost, kind):
155 """ 156 This is the ctor for Direct to DB transmission method 157 @param syshost: Name of the system. 158 @param kind: Type of record: link or run 159 """ 160 super(DirectDB, self).__init__(syshost, kind)
161 - def save(self, resultT):
162 """ 163 The json table is written directly to the db. 164 @param resultT: The json record table 165 """ 166 if (not XALTdb_available): 167 raise ImportError 168 169 ConfigFn = os.path.join(XALT_ETC_DIR,"xalt_db.conf") 170 RMapFn = os.path.join(XALT_ETC_DIR,"reverseMapD") 171 172 xalt = XALTdb(ConfigFn) 173 reverseMapT = Rmap(RMapFn).reverseMapT() 174 if (self._kind() == "link"): 175 xalt.link_to_db(reverseMapT, resultT) 176 else: 177 # kind == "run" 178 xalt.run_to_db(reverseMapT, resultT)
179