hue_create_db.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/bin/bash
  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. set -x
  18. PARCEL_DIR=/opt/cloudera/parcels/CDH
  19. LOG_FILE=/var/log/hue/`basename "$0" | awk -F\. '{print $1}'`.log
  20. DATABASE=$1
  21. PASSWORD=$2
  22. TYPE=$3
  23. if [[ -z ${DBUSER} ]]
  24. then
  25. DBUSER="cloudera"
  26. fi
  27. if [[ -z ${DBPASSWORD} ]]
  28. then
  29. DBPASSWORD="cloudera"
  30. fi
  31. if [[ -z ${TYPE} ]]
  32. then
  33. TYPE="mysql"
  34. fi
  35. if [[ -z ${DATABASE} ]]
  36. then
  37. echo "Usage: hue_create_db.sh <database_name> <password> <dbtype-mysql-postgres>"
  38. exit 1
  39. fi
  40. export HUE_DATABASE_PASSWORD=${PASSWORD}
  41. export HUE_IGNORE_PASSWORD_SCRIPT_ERRORS=1
  42. HUE_CONF_DIR=/tmp/hue_create_db/${DATABASE}
  43. mkdir -p ${HUE_CONF_DIR}
  44. if [ ! -d "/usr/lib/hadoop" ]
  45. then
  46. CDH_HOME=$PARCEL_DIR
  47. else
  48. CDH_HOME=/usr
  49. fi
  50. if [ -d "${CDH_HOME}/lib/hue/build/env/bin" ]
  51. then
  52. COMMAND="${CDH_HOME}/lib/hue/build/env/bin/hue"
  53. else
  54. COMMAND="${CDH_HOME}/share/hue/build/env/bin/hue"
  55. fi
  56. DATABASE_DUMP=${HUE_CONF_DIR}/hue_database_dump.json
  57. ORACLE_HOME=/opt/cloudera/parcels/ORACLE_INSTANT_CLIENT/instantclient_11_2/
  58. LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ORACLE_HOME}
  59. export CDH_HOME HUE_CONF_DIR ORACLE_HOME LD_LIBRARY_PATH COMMAND PASSWORD DATABASE
  60. ${COMMAND} dumpdata --indent 2 > ${HUE_CONF_DIR}/hue_database_dump.json
  61. if [[ ${TYPE} =~ .*mysql.* ]]
  62. then
  63. cat > ${HUE_CONF_DIR}/hue.ini << EOF
  64. [desktop]
  65. [[database]]
  66. #engine=sqlite3
  67. #name=/var/lib/hue/desktop.db
  68. engine=mysql
  69. host=`hostname`
  70. port=3306
  71. user=${DATABASE}
  72. password=${PASSWORD}
  73. name=${DATABASE}
  74. EOF
  75. cat > ${HUE_CONF_DIR}/create.sql << EOF
  76. drop database if exists ${DATABASE};
  77. create database ${DATABASE};
  78. grant all on *.* to '${DATABASE}'@'%' identified by '${PASSWORD}';
  79. EOF
  80. mysql -uroot -p${PASSWORD} < ${HUE_CONF_DIR}/create.sql
  81. elif [[ ${TYPE} =~ .*postgres.* ]]
  82. then
  83. yum -y install postgresql-server
  84. chkconfig postgresql on
  85. if [[ ! -f /var/lib/pgsql/data/postgresql.conf ]]
  86. then
  87. service postgresql initdb
  88. fi
  89. CHECK_HBA=$(grep ${DATABASE} /var/lib/pgsql/data/pg_hba.conf)
  90. if [[ -z ${CHECK_HBA} ]]
  91. then
  92. echo "host ${DATABASE} ${DATABASE} 0.0.0.0/0 md5" >> /var/lib/pgsql/data/pg_hba.conf
  93. fi
  94. sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '0.0.0.0'/g" /var/lib/pgsql/data/postgresql.conf
  95. service postgresql restart
  96. sudo -u postgres /bin/bash -c psql -U postgres << EOF
  97. create database ${DATABASE};
  98. \c ${DATABASE};
  99. create user ${DATABASE} with password '${PASSWORD}';
  100. grant all privileges on database ${DATABASE} to ${DATABASE};
  101. \q
  102. EOF
  103. cat > ${HUE_CONF_DIR}/hue.ini << EOF
  104. [desktop]
  105. [[database]]
  106. #engine=sqlite3
  107. #name=/var/lib/hue/desktop.db
  108. engine=postgresql_psycopg2
  109. host=`hostname`
  110. port=5432
  111. user=${DATABASE}
  112. password=${PASSWORD}
  113. name=${DATABASE}
  114. EOF
  115. fi
  116. ${COMMAND} syncdb --noinput
  117. ${COMMAND} migrate --merge
  118. if [[ ${TYPE} =~ .*mysql.* ]]
  119. then
  120. CONSTRAINT_ID=$(mysql -uroot -p${PASSWORD} ${DATABASE} -e "show create table auth_permission" | grep content_type_id_refs_id | awk -Fid_ '{print $3}' | awk -F\` '{print $1}')
  121. cat > ${HUE_CONF_DIR}/prepare.sql << EOF
  122. ALTER TABLE auth_permission DROP FOREIGN KEY content_type_id_refs_id_${CONSTRAINT_ID};
  123. delete from django_content_type;
  124. EOF
  125. mysql -uroot -p${PASSWORD} ${DATABASE} < ${HUE_CONF_DIR}/prepare.sql
  126. elif [[ ${TYPE} =~ .*postgres.* ]]
  127. then
  128. CONSTRAINT_ID=$(PGPASSWORD=${PASSWORD} psql -h `hostname` -U ${DATABASE} -d ${DATABASE} -c '\d auth_permission;' | grep content_type_id_refs_id | awk -Fid_ '{print $3}' | awk -F\" '{print $1}')
  129. #PGPASSWORD=${PASSWORD} psql -h `hostname` -U ${DATABASE} -d ${DATABASE} << EOF
  130. #ALTER TABLE auth_permission DROP CONSTRAINT content_type_id_refs_id_${CONSTRAINT_ID};
  131. #TRUNCATE django_content_type CASCADE;
  132. #EOF
  133. fi