hue_curl.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Licensed to Cloudera, Inc. under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. Cloudera, Inc. 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. import os
  17. import sys
  18. import logging
  19. import datetime
  20. import time
  21. import subprocess
  22. from cm_environment import check_security
  23. from hue_shared import which
  24. #logging.basicConfig()
  25. #logging = logging.getLogger(__name__)
  26. class Curl(object):
  27. def __init__(self, verbose=False):
  28. self.curl = which('curl')
  29. if self.curl is None:
  30. logging.exception("curl is required, please install and rerun")
  31. sys.exit(1)
  32. # We will change to handle certs later
  33. self.basecmd = self.curl + ' -k'
  34. logging.info("Checking security status")
  35. self.security_enabled = check_security()
  36. self.verbose = verbose
  37. if self.security_enabled:
  38. self.basecmd = self.basecmd + ' --negotiate -u :'
  39. if self.verbose:
  40. self.basecmd = self.basecmd + ' -v'
  41. else:
  42. self.basecmd = self.basecmd + ' -s'
  43. def do_curl(self, url, method='GET', follow=False, args=None):
  44. cmd = self.basecmd + ' -X ' + method
  45. if follow:
  46. cmd = cmd + ' -L'
  47. if args is not None:
  48. cmd = cmd + ' ' + args
  49. cmd = cmd + ' \'' + url + '\''
  50. logging.info("OSRUN: %s" % cmd)
  51. curl_process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  52. curl_response = curl_process.communicate()[0]
  53. curl_ret = curl_process.returncode
  54. if curl_ret > 0:
  55. logging.exception("Curl failed to run succesfully: %s" % curl_response)
  56. return curl_response
  57. def do_curl_available_services(self, service_test):
  58. url = service_test['url']
  59. method = service_test['method']
  60. response = self.do_curl(url, method=method)
  61. return response