utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF 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. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. import subprocess
  17. from resource_management.core.resources.system import Execute, Directory
  18. from resource_management.core.exceptions import Fail
  19. from resource_management.core.logger import Logger
  20. import hawq_constants
  21. def chk_hawq_process_status_cmd(port, component_name=None):
  22. """
  23. Check if hawq postgres / gpsyncmaster process is running
  24. """
  25. process = "gpsyncmaster" if component_name == hawq_constants.STANDBY else "postgres"
  26. return "netstat -tupln | egrep ':{0}\s' | egrep {1}".format(port, process)
  27. def create_dir_as_hawq_user(directory):
  28. """
  29. Creates directories with hawq_user and hawq_group (defaults to gpadmin:gpadmin)
  30. """
  31. Directory(directory, create_parents = True, owner=hawq_constants.hawq_user, group=hawq_constants.hawq_group)
  32. def exec_hawq_operation(operation, option, not_if=None, only_if=None, logoutput=True):
  33. """
  34. Sets up execution environment and runs a given command as HAWQ user
  35. """
  36. hawq_cmd = "source {0} && hawq {1} {2}".format(hawq_constants.hawq_greenplum_path_file, operation, option)
  37. Execute(
  38. hawq_cmd,
  39. user=hawq_constants.hawq_user,
  40. timeout=hawq_constants.hawq_operation_exec_timeout,
  41. not_if=not_if,
  42. only_if=only_if,
  43. logoutput=logoutput)
  44. def read_file_to_dict(file_name):
  45. """
  46. Converts a file with key=value format to dictionary
  47. """
  48. with open(file_name, "r") as fh:
  49. lines = fh.readlines()
  50. lines = [item for item in lines if '=' in item]
  51. result_dict = dict(item.split("=") for item in lines)
  52. return result_dict
  53. def write_dict_to_file(source_dict, dest_file):
  54. """
  55. Writes a dictionary into a file with key=value format
  56. """
  57. with open(dest_file, "w") as fh:
  58. for property_key, property_value in source_dict.items():
  59. if property_value is None:
  60. fh.write(property_key + "\n")
  61. else:
  62. fh.write("{0}={1}\n".format(property_key, property_value))
  63. def exec_ssh_cmd(hostname, cmd):
  64. """
  65. Runs the command on the remote host as gpadmin user
  66. """
  67. import params
  68. # Only gpadmin should be allowed to run command via ssh, thus not exposing user as a parameter
  69. if params.hostname != hostname:
  70. cmd = "su - {0} -c \"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null {1} \\\"{2} \\\" \"".format(hawq_constants.hawq_user, hostname, cmd)
  71. else:
  72. cmd = "su - {0} -c \"{1}\"".format(hawq_constants.hawq_user, cmd)
  73. Logger.info("Command executed: {0}".format(cmd))
  74. process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  75. (stdout, stderr) = process.communicate()
  76. return process.returncode, stdout, stderr
  77. def exec_psql_cmd(command, host, port, db="template1", tuples_only=True):
  78. """
  79. Sets up execution environment and runs the HAWQ queries
  80. """
  81. src_cmd = "export PGPORT={0} && source {1}".format(port, hawq_constants.hawq_greenplum_path_file)
  82. if tuples_only:
  83. cmd = src_cmd + " && psql -d {0} -c \\\\\\\"{1};\\\\\\\"".format(db, command)
  84. else:
  85. cmd = src_cmd + " && psql -t -d {0} -c \\\\\\\"{1};\\\\\\\"".format(db, command)
  86. retcode, out, err = exec_ssh_cmd(host, cmd)
  87. if retcode:
  88. Logger.error("SQL command executed failed: {0}\nReturncode: {1}\nStdout: {2}\nStderr: {3}".format(cmd, retcode, out, err))
  89. raise Fail("SQL command executed failed.")
  90. Logger.info("Output:\n{0}".format(out))
  91. return retcode, out, err