PageRenderTime 104ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/mig/shared/functionality/resman.py

http://migrid.googlecode.com/
Python | 298 lines | 249 code | 17 blank | 32 comment | 9 complexity | e2aefded6299db0f8bba315cd5c25f24 MD5 | raw file
Possible License(s): IPL-1.0, GPL-2.0, GPL-3.0, MPL-2.0-no-copyleft-exception, JSON
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # --- BEGIN_HEADER ---
  5. #
  6. # resman - manage resources
  7. # Copyright (C) 2003-2014 The MiG Project lead by Brian Vinter
  8. #
  9. # This file is part of MiG.
  10. #
  11. # MiG is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # MiG is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program; if not, write to the Free Software
  23. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. #
  25. # -- END_HEADER ---
  26. #
  27. """Resource management back end functionality"""
  28. import time
  29. from binascii import hexlify
  30. import shared.returnvalues as returnvalues
  31. from shared.base import sandbox_resource
  32. from shared.defaults import default_pager_entries
  33. from shared.functional import validate_input_and_cert
  34. from shared.html import html_post_helper
  35. from shared.init import initialize_main_variables, find_entry
  36. from shared.resource import anon_to_real_res_map
  37. from shared.vgridaccess import user_visible_res_exes, get_resource_map, \
  38. OWNERS, CONF
  39. def signature():
  40. """Signature of the main function"""
  41. defaults = {'show_sandboxes': ['false']}
  42. return ['resources', defaults]
  43. def main(client_id, user_arguments_dict):
  44. """Main function used by front end"""
  45. (configuration, logger, output_objects, op_name) = \
  46. initialize_main_variables(client_id, op_header=False)
  47. status = returnvalues.OK
  48. defaults = signature()[1]
  49. (validate_status, accepted) = validate_input_and_cert(
  50. user_arguments_dict,
  51. defaults,
  52. output_objects,
  53. client_id,
  54. configuration,
  55. allow_rejects=False,
  56. )
  57. if not validate_status:
  58. return (accepted, returnvalues.CLIENT_ERROR)
  59. show_sandboxes = (accepted['show_sandboxes'][-1] != 'false')
  60. visible_exes = user_visible_res_exes(configuration, client_id)
  61. res_map = get_resource_map(configuration)
  62. anon_map = anon_to_real_res_map(configuration.resource_home)
  63. # Iterate through resources and show management for each one requested
  64. res_list = {'object_type': 'resource_list', 'resources': []}
  65. fields = ['PUBLICNAME', 'NODECOUNT', 'CPUCOUNT', 'MEMORY', 'DISK', 'ARCHITECTURE',
  66. 'SANDBOX', 'RUNTIMEENVIRONMENT']
  67. # Leave the sorting to jquery tablesorter
  68. for visible_res_name in visible_exes.keys():
  69. unique_resource_name = visible_res_name
  70. if visible_res_name in anon_map.keys():
  71. unique_resource_name = anon_map[visible_res_name]
  72. if not show_sandboxes and sandbox_resource(unique_resource_name):
  73. continue
  74. res_obj = {'object_type': 'resource', 'name': visible_res_name}
  75. if client_id in res_map[unique_resource_name][OWNERS]:
  76. # Admin of resource when owner
  77. js_name = 'rmresowner%s' % hexlify(unique_resource_name)
  78. helper = html_post_helper(js_name, 'rmresowner.py',
  79. {'unique_resource_name':
  80. unique_resource_name,
  81. 'cert_id': client_id})
  82. output_objects.append({'object_type': 'html_form', 'text': helper})
  83. res_obj['resownerlink'] = \
  84. {'object_type': 'link',
  85. 'destination':
  86. "javascript: confirmDialog(%s, '%s');"\
  87. % (js_name, 'Really leave %s owners?' % \
  88. unique_resource_name),
  89. 'class': 'removelink',
  90. 'title': 'Leave %s owners' % unique_resource_name,
  91. 'text': ''}
  92. res_obj['resdetailslink'] = \
  93. {'object_type': 'link',
  94. 'destination':
  95. 'resadmin.py?unique_resource_name=%s'\
  96. % unique_resource_name,
  97. 'class': 'adminlink',
  98. 'title': 'Administrate %s' % unique_resource_name,
  99. 'text': ''}
  100. else:
  101. # link to become owner
  102. js_name = 'reqresowner%s' % hexlify(unique_resource_name)
  103. helper = html_post_helper(js_name, 'sendrequestaction.py',
  104. {'unique_resource_name':
  105. visible_res_name,
  106. 'request_type': 'resourceowner',
  107. 'request_text': ''})
  108. output_objects.append({'object_type': 'html_form', 'text': helper})
  109. res_obj['resownerlink'] = \
  110. {'object_type': 'link',
  111. 'destination':
  112. "javascript: confirmDialog(%s, '%s', '%s');"\
  113. % (js_name, "Request ownership of " + \
  114. visible_res_name + ":<br/>" + \
  115. "\nPlease write a message to the owners (field below).",
  116. 'request_text'),
  117. 'class': 'addlink',
  118. 'title': 'Request ownership of %s' % visible_res_name,
  119. 'text': ''}
  120. res_obj['resdetailslink'] = \
  121. {'object_type': 'link',
  122. 'destination':
  123. 'viewres.py?unique_resource_name=%s'\
  124. % visible_res_name,
  125. 'class': 'infolink',
  126. 'title': 'View detailed %s specs' % \
  127. visible_res_name,
  128. 'text': ''}
  129. # fields for everyone: public status
  130. for name in fields:
  131. res_obj[name] = res_map[unique_resource_name][CONF].get(name, '')
  132. # Use runtimeenvironment names instead of actual definitions
  133. res_obj['RUNTIMEENVIRONMENT'] = [i[0] for i in res_obj['RUNTIMEENVIRONMENT']]
  134. res_list['resources'].append(res_obj)
  135. title_entry = find_entry(output_objects, 'title')
  136. title_entry['text'] = 'Resource management'
  137. # jquery support for tablesorter and confirmation on "leave":
  138. title_entry['style'] = '''
  139. <link rel="stylesheet" type="text/css" href="/images/css/jquery.managers.css" media="screen"/>
  140. <link rel="stylesheet" type="text/css" href="/images/css/jquery-ui.css" media="screen"/>
  141. <link rel="stylesheet" type="text/css" href="/images/css/jquery-ui-theme.css" media="screen"/>
  142. <link rel="stylesheet" type="text/css" href="/images/css/jquery-ui-theme.custom.css" media="screen"/>
  143. '''
  144. title_entry['javascript'] = '''
  145. <script type="text/javascript" src="/images/js/jquery.js"></script>
  146. <script type="text/javascript" src="/images/js/jquery.tablesorter.js"></script>
  147. <script type="text/javascript" src="/images/js/jquery.tablesorter.pager.js"></script>
  148. <script type="text/javascript" src="/images/js/jquery-ui.js"></script>
  149. <script type="text/javascript" src="/images/js/jquery.confirm.js"></script>
  150. <script type="text/javascript" >
  151. $(document).ready(function() {
  152. // init confirmation dialog
  153. $( "#confirm_dialog" ).dialog(
  154. // see http://jqueryui.com/docs/dialog/ for options
  155. { autoOpen: false,
  156. modal: true, closeOnEscape: true,
  157. width: 500,
  158. buttons: {
  159. "Cancel": function() { $( "#" + name ).dialog("close"); }
  160. }
  161. });
  162. // table initially sorted by col. 1 (admin), then 0 (name)
  163. var sortOrder = [[1,0],[0,0]];
  164. // use image path for sorting if there is any inside
  165. var imgTitle = function(contents) {
  166. var key = $(contents).find("a").attr("class");
  167. if (key == null) {
  168. key = $(contents).html();
  169. }
  170. return key;
  171. }
  172. $("#resourcetable").tablesorter({widgets: ["zebra"],
  173. sortList:sortOrder,
  174. textExtraction: imgTitle
  175. })
  176. .tablesorterPager({ container: $("#pager"),
  177. size: %s
  178. });
  179. }
  180. );
  181. </script>
  182. ''' % default_pager_entries
  183. output_objects.append({'object_type': 'html_form',
  184. 'text':'''
  185. <div id="confirm_dialog" title="Confirm" style="background:#fff;">
  186. <div id="confirm_text"><!-- filled by js --></div>
  187. <textarea cols="40" rows="4" id="confirm_input" style="display:none;"></textarea>
  188. </div>
  189. ''' })
  190. output_objects.append({'object_type': 'header', 'text': 'Available Resources'
  191. })
  192. output_objects.append({'object_type': 'sectionheader', 'text'
  193. : 'Resources available on this server'})
  194. output_objects.append({'object_type': 'text', 'text'
  195. : '''
  196. All available resources are listed below with overall hardware specifications. Any resources that you own will have a administration icon that you can click to open resource management.
  197. '''
  198. })
  199. output_objects.append({'object_type': 'table_pager', 'entry_name': 'resources',
  200. 'default_entries': default_pager_entries})
  201. output_objects.append(res_list)
  202. if configuration.site_enable_sandboxes:
  203. if show_sandboxes:
  204. output_objects.append({'object_type': 'link',
  205. 'destination': '?show_sandboxes=false',
  206. 'class': 'removeitemlink',
  207. 'title': 'Hide sandbox resources',
  208. 'text': 'Exclude sandbox resources',
  209. })
  210. else:
  211. output_objects.append({'object_type': 'link',
  212. 'destination': '?show_sandboxes=true',
  213. 'class': 'additemlink',
  214. 'title': 'Show sandbox resources',
  215. 'text': 'Include sandbox resources',
  216. })
  217. output_objects.append({'object_type': 'sectionheader', 'text'
  218. : 'Resource Status'})
  219. output_objects.append({'object_type': 'text',
  220. 'text': '''
  221. Live resource status is available in the resource monitor page with all VGrids/resources you can access
  222. '''})
  223. output_objects.append({'object_type': 'link',
  224. 'destination': 'showvgridmonitor.py?vgrid_name=ALL',
  225. 'class': 'monitorlink',
  226. 'title': 'Show monitor with all resources you can access',
  227. 'text': 'Global resource monitor',
  228. })
  229. output_objects.append({'object_type': 'sectionheader', 'text': 'Additional Resources'
  230. })
  231. output_objects.append({'object_type': 'text',
  232. 'text': 'You can sign up spare or dedicated resources to the grid below.'
  233. })
  234. output_objects.append({'object_type': 'link',
  235. 'destination' : 'resedit.py',
  236. 'class': 'addlink',
  237. 'title': 'Show sandbox resources',
  238. 'text': 'Create a new %s resource' % \
  239. configuration.short_title,
  240. })
  241. output_objects.append({'object_type': 'sectionheader', 'text': ''})
  242. if configuration.site_enable_sandboxes:
  243. output_objects.append({'object_type': 'link',
  244. 'destination': 'ssslogin.py',
  245. 'class': 'adminlink',
  246. 'title': 'Administrate and monitor your sandbox resources',
  247. 'text': 'Administrate %s sandbox resources' % \
  248. configuration.short_title,
  249. })
  250. output_objects.append({'object_type': 'sectionheader', 'text': ''})
  251. output_objects.append({'object_type': 'link',
  252. 'destination': 'oneclick.py',
  253. 'class': 'sandboxlink',
  254. 'title': 'Run a One-click resource in your browser',
  255. 'text': 'Use this computer as One-click %s resource' % \
  256. configuration.short_title,
  257. })
  258. return (output_objects, status)