hadoop-setup-applications.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # 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. this="${BASH_SOURCE-$0}"
  17. bin=$(cd -P -- "$(dirname -- "$this")" && pwd -P)
  18. script="$(basename -- "$this")"
  19. this="$bin/$script"
  20. DEFAULT_LIBEXEC_DIR="$bin"/../libexec
  21. HADOOP_LIBEXEC_DIR=${HADOOP_LIBEXEC_DIR:-$DEFAULT_LIBEXEC_DIR}
  22. . $HADOOP_LIBEXEC_DIR/hadoop-config.sh
  23. usage() {
  24. echo "
  25. usage: $0 <parameters>
  26. Require parameter:
  27. --config /etc/hadoop Location of Hadoop configuration file
  28. --apps=<csl of apps:user hcat:hcat,hbase,hive:user> Apps you want to setup on hdfs
  29. If user is not specified, app name
  30. will be used as the user name as well
  31. Optional parameters:
  32. -h Display this message
  33. --kerberos-realm=KERBEROS.EXAMPLE.COM Set Kerberos realm
  34. --super-user=hdfs Set super user id
  35. --super-user-keytab=/etc/security/keytabs/hdfs.keytab Set super user keytab location
  36. "
  37. exit 1
  38. }
  39. OPTS=$(getopt \
  40. -n $0 \
  41. -o '' \
  42. -l 'kerberos-realm:' \
  43. -l 'super-user:' \
  44. -l 'super-user-keytab:' \
  45. -l 'apps:' \
  46. -o 'h' \
  47. -- "$@")
  48. if [ $? != 0 ] ; then
  49. usage
  50. exit 1
  51. fi
  52. function setup_apps
  53. {
  54. if [ -z $APPS ]
  55. then
  56. usage
  57. break
  58. fi
  59. #if super user is not set default to hdfs
  60. HADOOP_HDFS_USER=${HADOOP_HDFS_USER:-hdfs}
  61. if [ ! "${KERBEROS_REALM}" = "" ]; then
  62. # locate kinit cmd
  63. if [ -e /etc/lsb-release ]; then
  64. KINIT_CMD="/usr/bin/kinit -kt ${HDFS_USER_KEYTAB} ${HADOOP_HDFS_USER}"
  65. else
  66. KINIT_CMD="/usr/kerberos/bin/kinit -kt ${HDFS_USER_KEYTAB} ${HADOOP_HDFS_USER}"
  67. fi
  68. su -c "${KINIT_CMD}" ${HADOOP_HDFS_USER}
  69. fi
  70. #process each app
  71. oldIFS=$IFS
  72. IFS=','
  73. for app in $APPS
  74. do
  75. IFS=":"
  76. arr=($app)
  77. app=${arr[0]}
  78. user=${arr[1]}
  79. IFS=','
  80. #if user is empty, default it to app
  81. if [ -z $user ]
  82. then
  83. user=$app
  84. fi
  85. path="/apps/${app}"
  86. #create the dir
  87. cmd="su -c '${HADOOP_PREFIX}/bin/hadoop --config ${HADOOP_CONF_DIR} dfs -mkdir ${path}' ${HADOOP_HDFS_USER}"
  88. echo $cmd
  89. eval $cmd
  90. #make owner to be the app
  91. cmd="su -c '${HADOOP_PREFIX}/bin/hadoop --config ${HADOOP_CONF_DIR} dfs -chown ${user} ${path}' ${HADOOP_HDFS_USER}"
  92. echo $cmd
  93. eval $cmd
  94. if [ "$?" == "0" ]; then
  95. echo "App directory has been setup: ${path}"
  96. fi
  97. done
  98. IFS=$oldIFS
  99. }
  100. eval set -- "${OPTS}"
  101. while true; do
  102. case "$1" in
  103. --apps)
  104. APPS=$2; shift 2
  105. ;;
  106. --kerberos-realm)
  107. KERBEROS_REALM=$2; shift 2
  108. ;;
  109. --super-user)
  110. HADOOP_HDFS_USER=$2; shift 2
  111. ;;
  112. --super-user-keytab)
  113. HDFS_USER_KEYTAB=$2; shift 2
  114. ;;
  115. -h)
  116. usage
  117. ;;
  118. --)
  119. shift ; break
  120. ;;
  121. *)
  122. echo "Unknown option: $1"
  123. usage
  124. exit 1
  125. ;;
  126. esac
  127. done
  128. setup_apps