views.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import re
  18. from desktop.lib.django_util import render
  19. from django.http import HttpResponse
  20. import logging
  21. import simplejson
  22. import shell.constants as constants
  23. import shell.utils as utils
  24. from shell.shellmanager import ShellManager
  25. SHELL_OUTPUT_LOGGER = logging.getLogger("shell_output")
  26. SHELL_INPUT_LOGGER = logging.getLogger("shell_input")
  27. def _running_with_spawning(request):
  28. return 'eventlet.input' in request.META
  29. def index(request):
  30. if not _running_with_spawning(request):
  31. return render('not_running_spawning.mako', request, {})
  32. shell_manager = ShellManager.global_instance()
  33. result = shell_manager.available_shell_types(request.user)
  34. if result is None:
  35. return render('no_such_user.mako', request, {})
  36. return render('index.mako', request, {'shells':result})
  37. def create(request):
  38. if not _running_with_spawning(request):
  39. if request.method == "POST":
  40. result = simplejson.dumps({ constants.NOT_RUNNING_SPAWNING : True })
  41. return HttpResponse(result, mimetype="application/json")
  42. else:
  43. return render('not_running_spawning.mako', request, {})
  44. shell_manager = ShellManager.global_instance()
  45. user = request.user
  46. if request.method == "POST":
  47. key_name = request.POST.get(constants.KEY_NAME, "")
  48. else:
  49. key_name = request.GET.get(constants.KEY_NAME, "")
  50. SHELL_INPUT_LOGGER.info("%s %s - Create '%s' shell" %
  51. (request.META.get('REMOTE_ADDR'), user.username, key_name))
  52. result = shell_manager.try_create(user, key_name)
  53. if request.method == "POST":
  54. return HttpResponse(simplejson.dumps(result), mimetype="application/json")
  55. else:
  56. if constants.SUCCESS in result:
  57. shell_types = shell_manager.available_shell_types(user)
  58. dict_for_template = { 'shells' : shell_types,
  59. 'shell_id' : result.get(constants.SHELL_ID) }
  60. return render('index.mako', request, dict_for_template)
  61. else:
  62. return render('failed_to_create.mako', request, {})
  63. def kill_shell(request):
  64. if not _running_with_spawning(request):
  65. result = simplejson.dumps({ constants.NOT_RUNNING_SPAWNING : True })
  66. return HttpResponse(result, mimetype="application/json")
  67. shell_manager = ShellManager.global_instance()
  68. username = request.user.username
  69. shell_id = request.POST[constants.SHELL_ID]
  70. SHELL_INPUT_LOGGER.info("%s %s - shell_id:%s - Kill shell" %
  71. (request.META.get('REMOTE_ADDR'), username, shell_id))
  72. result = shell_manager.kill_shell(username, shell_id)
  73. return HttpResponse(result)
  74. def process_command(request):
  75. if not _running_with_spawning(request):
  76. result = simplejson.dumps({ constants.NOT_RUNNING_SPAWNING : True })
  77. return HttpResponse(result, mimetype="application/json")
  78. shell_manager = ShellManager.global_instance()
  79. username = request.user.username
  80. shell_id = request.POST[constants.SHELL_ID]
  81. command = request.POST.get(constants.COMMAND, "")
  82. SHELL_INPUT_LOGGER.info("%s %s - shell_id:%s - Command:'%s'" %
  83. (request.META.get('REMOTE_ADDR'), username, shell_id, command))
  84. result = shell_manager.process_command(username, shell_id, command)
  85. return HttpResponse(simplejson.dumps(result), mimetype="application/json")
  86. def retrieve_output(request):
  87. if not _running_with_spawning(request):
  88. result = simplejson.dumps({ constants.NOT_RUNNING_SPAWNING : True })
  89. return HttpResponse(result, mimetype="application/json")
  90. shell_manager = ShellManager.global_instance()
  91. username = request.user.username
  92. hue_instance_id = request.META[constants.HUE_INSTANCE_ID]
  93. try:
  94. shell_pairs = utils.parse_shell_pairs(request)
  95. except ValueError:
  96. shell_pairs = []
  97. result = shell_manager.retrieve_output(username, hue_instance_id, shell_pairs)
  98. for key, value in result.iteritems():
  99. if isinstance(value, dict) and constants.OUTPUT in value:
  100. log_format = '%s %s - shell_id:%s - Output: "%s"'
  101. log_args = (request.META.get('REMOTE_ADDR'), username, key,
  102. repr(value[constants.OUTPUT]))
  103. SHELL_OUTPUT_LOGGER.info(log_format % log_args)
  104. for shell in result:
  105. shell = result[shell]
  106. if isinstance(shell, dict) and 'output' in shell:
  107. shell['output'] = remove_colors(shell['output'])
  108. return HttpResponse(simplejson.dumps(result), mimetype="application/json")
  109. def add_to_output(request):
  110. if not _running_with_spawning(request):
  111. result = simplejson.dumps({ constants.NOT_RUNNING_SPAWNING : True })
  112. return HttpResponse(result, mimetype="application/json")
  113. shell_manager = ShellManager.global_instance()
  114. username = request.user.username
  115. hue_instance_id = request.META[constants.HUE_INSTANCE_ID]
  116. try:
  117. shell_pairs = utils.parse_shell_pairs(request)
  118. except ValueError:
  119. shell_pairs = []
  120. result = shell_manager.add_to_output(username, hue_instance_id, shell_pairs)
  121. return HttpResponse(simplejson.dumps(result), mimetype="application/json")
  122. def remove_colors(text):
  123. return re.sub('\x1b\[\d{0,3}(;\d{0,3})?m', '', text) # For Bash