pbs_as 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # Copyright (C) 1994-2018 Altair Engineering, Inc.
  4. # For more information, contact Altair at www.altair.com.
  5. #
  6. # This file is part of the PBS Professional ("PBS Pro") software.
  7. #
  8. # Open Source License Information:
  9. #
  10. # PBS Pro is free software. You can redistribute it and/or modify it under the
  11. # terms of the GNU Affero General Public License as published by the Free
  12. # Software Foundation, either version 3 of the License, or (at your option) any
  13. # later version.
  14. #
  15. # PBS Pro is distributed in the hope that it will be useful, but WITHOUT ANY
  16. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. # FOR A PARTICULAR PURPOSE.
  18. # See the GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. # Commercial License Information:
  24. #
  25. # For a copy of the commercial license terms and conditions,
  26. # go to: (http://www.pbspro.com/UserArea/agreement.html)
  27. # or contact the Altair Legal Department.
  28. #
  29. # Altair’s dual-license business model allows companies, individuals, and
  30. # organizations to create proprietary derivative works of PBS Pro and
  31. # distribute them - whether embedded or bundled with other software -
  32. # under a commercial license agreement.
  33. #
  34. # Use of Altair’s trademarks, including but not limited to "PBS™",
  35. # "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
  36. # trademark licensing policies.
  37. import sys
  38. import os
  39. import getopt
  40. import cPickle
  41. import pwd
  42. from ptl.lib.pbs_testlib import PtlConfig, Server
  43. from ptl.lib.pbs_testlib import PbsStatusError, PbsSubmitError, PbsDeleteError
  44. from ptl.lib.pbs_testlib import PbsManagerError, PbsAlterError, PbsSignalError
  45. from ptl.lib.pbs_testlib import PbsOrderError, PbsMessageError, PbsHoldError
  46. from ptl.lib.pbs_testlib import PbsReleaseError, PbsRunError, PbsRerunError
  47. from ptl.lib.pbs_testlib import PbsMoveError, PbsDeljobError, PbsDelresvError
  48. from ptl.lib.pbs_testlib import PbsQtermError
  49. def usage():
  50. msg = []
  51. msg += ['PBS Pro user impersonation tool. This tool is an internal ']
  52. msg += ['tool and is not intended to be used by end-users\n\n']
  53. msg += ['Usage: ' + os.path.basename(sys.argv[0]) + ' [OPTION]\n\n']
  54. msg += ['-c <cmd>: command to perform, one of submit, status\n']
  55. msg += ['-e <extend>: extend options to commands']
  56. msg += ['-o <objid>: identifier of object to act upon\n']
  57. msg += ['-u <user>: username to perform commandd as\n']
  58. msg += ['-f <serialized_file>: filename containing serialized data\n']
  59. msg += ['-s <hostname>: name of host on which to perform command\n']
  60. msg += ['-h: usage help\n']
  61. print "".join(msg)
  62. def _load_data(objfile=None, servername=None, user=None):
  63. if objfile:
  64. f = open(objfile, 'r')
  65. _data = cPickle.load(f)
  66. f.close()
  67. else:
  68. _data = None
  69. if user is not None:
  70. uid = pwd.getpwnam(user)[2]
  71. if os.getuid() == 0 and uid != 0:
  72. os.setuid(uid)
  73. s = Server(servername, stat=False)
  74. return (s, _data)
  75. if __name__ == '__main__':
  76. if len(sys.argv) < 2:
  77. usage()
  78. sys.exit(1)
  79. try:
  80. opts, args = getopt.getopt(sys.argv[1:], "c:e:o:u:f:s:h")
  81. except:
  82. usage()
  83. sys.exit(1)
  84. cmd = None
  85. objid = None
  86. user = None
  87. objfile = None
  88. servername = None
  89. extend = None
  90. for o, val in opts:
  91. if o == '-e':
  92. extend = val
  93. elif o == '-c':
  94. cmd = val
  95. elif o == '-o':
  96. objid = val.split(',')
  97. elif o == '-u':
  98. user = val
  99. elif o == '-f':
  100. objfile = val
  101. elif o == '-s':
  102. servername = val
  103. elif o == '-h':
  104. usage()
  105. sys.exit(0)
  106. else:
  107. sys.stderr.write('unrecognized option. Exiting')
  108. usage()
  109. sys.exit(1)
  110. if cmd is None or user is None or servername is None:
  111. print None
  112. sys.exit(0)
  113. PtlConfig()
  114. if cmd == 'submit':
  115. (s, job) = _load_data(objfile, servername, user)
  116. job.attrl = s.utils.dict_to_attrl(job.attributes)
  117. job.attropl = s.utils.dict_to_attropl(job.attributes)
  118. try:
  119. jid = s.submit(job)
  120. sys.stdout.write(jid.strip())
  121. except PbsSubmitError, e:
  122. sys.stdout.write(str(e.rv))
  123. sys.stderr.write(repr(e))
  124. sys.exit(e.rc)
  125. elif cmd == 'status':
  126. (s, _data) = _load_data(objfile, servername, user)
  127. if 'obj_type' in _data:
  128. obj_type = int(_data['obj_type'])
  129. else:
  130. obj_type = None
  131. if 'attrib' in _data:
  132. attrib = _data['attrib']
  133. else:
  134. attrib = None
  135. if 'id' in _data:
  136. id = _data['id']
  137. else:
  138. id = None
  139. try:
  140. rv = s.status(obj_type, attrib, id, extend=extend)
  141. sys.stdout.write(str(rv))
  142. except PbsStatusError, e:
  143. rv = e.rv
  144. sys.stdout.write(str(rv))
  145. sys.stderr.write(repr(e))
  146. sys.stderr.flush()
  147. sys.exit(e.rc)
  148. elif cmd == 'delete':
  149. if objid is None:
  150. print '1'
  151. else:
  152. (s, data) = _load_data(objfile, servername, user)
  153. try:
  154. rc = s.delete(objid, extend=extend)
  155. sys.stdout.write(str(rc))
  156. except PbsDeleteError, e:
  157. sys.stdout.write(str(rc))
  158. sys.stderr.write(repr(e))
  159. sys.exit(e.rc)
  160. elif cmd == 'deljob':
  161. if objid is None:
  162. print '1'
  163. else:
  164. (s, data) = _load_data(objfile, servername, user)
  165. try:
  166. rc = s.deljob(objid, extend=extend)
  167. sys.stdout.write(str(rc))
  168. except PbsDeljobError, e:
  169. sys.stdout.write(str(rc))
  170. sys.stderr.write(repr(e))
  171. sys.exit(e.rc)
  172. elif cmd == 'delresv':
  173. if objid is None:
  174. print '1'
  175. else:
  176. (s, data) = _load_data(objfile, servername, user)
  177. try:
  178. rc = s.delresv(objid, extend=extend)
  179. sys.stdout.write(str(rc))
  180. except PbsDelresvError, e:
  181. sys.stdout.write(str(rc))
  182. sys.stderr.write(repr(e))
  183. sys.exit(e.rc)
  184. elif cmd == 'select':
  185. (s, attrib) = _load_data(objfile, servername, user)
  186. rv = s.select(attrib, extend=extend)
  187. print rv
  188. elif cmd == 'alterjob':
  189. if objid is None:
  190. print '1'
  191. else:
  192. (s, attrib) = _load_data(objfile, servername, user)
  193. try:
  194. rc = s.alterjob(objid, attrib, extend=extend)
  195. sys.stdout.write(str(rc))
  196. except PbsAlterError, e:
  197. sys.stdout.write(str(rc))
  198. sys.stderr.write(repr(e))
  199. sys.exit(e.rc)
  200. elif cmd == 'holdjob':
  201. if objid is None:
  202. print '1'
  203. else:
  204. (s, hold_list) = _load_data(objfile, servername, user)
  205. try:
  206. rc = s.holdjob(objid, str(hold_list), extend=extend)
  207. sys.stdout.write(str(rc))
  208. except PbsHoldError, e:
  209. sys.stdout.write(str(rc))
  210. sys.stderr.write(repr(e))
  211. sys.exit(e.rc)
  212. elif cmd == 'sigjob':
  213. if objid is None:
  214. print '1'
  215. else:
  216. (s, signal) = _load_data(objfile, servername, user)
  217. try:
  218. rc = s.sigjob(objid, str(signal), extend=extend)
  219. sys.stdout.write(str(rc))
  220. except PbsSignalError, e:
  221. sys.stdout.write(str(rc))
  222. sys.stderr.write(repr(e))
  223. sys.exit(e.rc)
  224. elif cmd == 'msgjob':
  225. if objid is None:
  226. print '1'
  227. else:
  228. (s, msg) = _load_data(objfile, servername, user)
  229. try:
  230. rc = s.holdjob(objid, str(msg), extend=extend)
  231. sys.stdout.write(str(rc))
  232. except PbsMessageError, e:
  233. sys.stdout.write(str(rc))
  234. sys.stderr.write(repr(e))
  235. sys.exit(e.rc)
  236. elif cmd == 'rlsjob':
  237. if objid is None:
  238. print '1'
  239. else:
  240. (s, hold_list) = _load_data(objfile, servername, user)
  241. try:
  242. rc = s.rlsjob(objid, str(hold_list), extend=extend)
  243. sys.stdout.write(str(rc))
  244. except PbsReleaseError, e:
  245. sys.stdout.write(str(rc))
  246. sys.stderr.write(repr(e))
  247. sys.exit(e.rc)
  248. elif cmd == 'rerunjob':
  249. if objid is None:
  250. print '1'
  251. else:
  252. (s, data) = _load_data(objfile, servername, user)
  253. try:
  254. rc = s.rerunjob(objid, extend=extend)
  255. sys.stdout.write(str(rc))
  256. except PbsRerunError, e:
  257. sys.stdout.write(str(rc))
  258. sys.stderr.write(repr(e))
  259. sys.exit(e.rc)
  260. elif cmd == 'orderjob':
  261. if objid is None:
  262. print '1'
  263. else:
  264. (s, jobid2) = _load_data(objfile, servername, user)
  265. try:
  266. rc = s.orderjob(objid, str(jobid2), extend=extend)
  267. sys.stdout.write(str(rc))
  268. except PbsOrderError, e:
  269. sys.stdout.write(str(rc))
  270. sys.stderr.write(repr(e))
  271. sys.exit(e.rc)
  272. elif cmd == 'runjob':
  273. if objid is None:
  274. print '1'
  275. else:
  276. (s, location) = _load_data(objfile, servername, user)
  277. try:
  278. rc = s.runjob(objid, str(location), extend=extend)
  279. except PbsRunError, e:
  280. rc = e.rc
  281. print str(rc)
  282. elif cmd == 'movejob':
  283. if objid is None:
  284. print '1'
  285. else:
  286. (s, destination) = _load_data(objfile, servername, user)
  287. try:
  288. rc = s.movejob(objid, str(destination), extend=extend)
  289. sys.stdout.write(rc)
  290. except PbsMoveError, e:
  291. sys.stdout.write(str(rc))
  292. sys.stderr.write(repr(e))
  293. sys.exit(e.rc)
  294. """
  295. elif cmd == 'alterresv':
  296. if objid is None:
  297. print '1'
  298. else:
  299. (s, attrib) = _load_data(objfile, servername, user)
  300. try:
  301. rc = s.alterresv(objid, attrib, extend=extend)
  302. sys.stdout.write(str(rc))
  303. except PbsAlterError, e:
  304. sys.stdout.write(str(rc))
  305. sys.stderr.write(repr(e))
  306. sys.exit(e.rc)
  307. """
  308. elif cmd == 'manager':
  309. (s, _data) = _load_data(objfile, servername, user)
  310. if 'cmd' in _data:
  311. cmd = int(_data['cmd'])
  312. else:
  313. cmd = None
  314. if 'obj_type' in _data:
  315. obj_type = int(_data['obj_type'])
  316. else:
  317. obj_type = None
  318. if 'attrib' in _data:
  319. attrib = _data['attrib']
  320. else:
  321. attrib = None
  322. if 'id' in _data:
  323. id = _data['id']
  324. else:
  325. id = None
  326. try:
  327. rc = s.manager(cmd, obj_type, attrib, id, extend=extend)
  328. sys.stdout.write(str(rc))
  329. except PbsManagerError, e:
  330. sys.stderr.write(repr(e))
  331. sys.exit(e.rc)
  332. elif cmd == 'terminate':
  333. (s, data) = _load_data(servername, user)
  334. rc = s.terminate(manner=data['manner'],
  335. server_name=data['server_name'], extend=extend)
  336. print str(rc)
  337. sys.exit(0)