install-ambari-python.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # 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. # requires: pip setuptools wheel
  18. readlinkf(){
  19. # get real path on mac OSX
  20. perl -MCwd -e 'print Cwd::abs_path shift' "$1";
  21. }
  22. if [ "$(uname -s)" = 'Linux' ]; then
  23. SCRIPT_DIR="`dirname "$(readlink -f "$0")"`"
  24. else
  25. SCRIPT_DIR="`dirname "$(readlinkf "$0")"`"
  26. fi
  27. function print_help() {
  28. cat << EOF
  29. Usage: ./install-ambari-python.sh [additional options]
  30. -c, --clean clean generated python distribution directories
  31. -d, --deploy deploy ambari-python artifact to maven remote repository
  32. -v, --version <version> override ambari-python artifact versison
  33. -i, --repository-id <id> repository id in settings.xml for remote repository
  34. -r, --repository-url <url> repository url of remote repository
  35. -h, --help print help
  36. EOF
  37. }
  38. function get_python_artifact_file() {
  39. local artifact_file=$(ls $SCRIPT_DIR/dist/ | head -n 1)
  40. echo $artifact_file
  41. }
  42. function get_version() {
  43. local artifact_file=$(get_python_artifact_file)
  44. local artifact_version=$(echo $artifact_file | perl -lne '/ambari-python-(.*?)\.tar\.gz/ && print $1')
  45. echo $artifact_version
  46. }
  47. function clean() {
  48. if [[ -d "$SCRIPT_DIR/dist" ]]; then
  49. echo "Removing '$SCRIPT_DIR/dist' directoy ..."
  50. rm -r "$SCRIPT_DIR/dist"
  51. echo "Directory '$SCRIPT_DIR/dist' successfully deleted."
  52. fi
  53. if [[ -d "$SCRIPT_DIR/ambari_python.egg-info" ]]; then
  54. echo "Removing '$SCRIPT_DIR/ambari_python.egg-info' directoy ..."
  55. rm -r "$SCRIPT_DIR/ambari_python.egg-info"
  56. echo "Directory '$SCRIPT_DIR/ambari_python.egg-info' successfully deleted."
  57. fi
  58. if [[ -d "$SCRIPT_DIR/target/ambari-python-dist" ]]; then
  59. echo "Removing '$SCRIPT_DIR/target/ambari-python' directoy ..."
  60. rm -r "$SCRIPT_DIR/target/ambari-python-dist"
  61. echo "Directory '$SCRIPT_DIR/target/ambari-python' successfully deleted."
  62. fi
  63. }
  64. function generate_site_packages() {
  65. local version="$1"
  66. pip install $SCRIPT_DIR/dist/ambari-python-$version.tar.gz -I --install-option="--prefix=$SCRIPT_DIR/target/ambari-python-dist"
  67. }
  68. function archive_python_dist() {
  69. local artifact="$1"
  70. local site_packages_dir=$(find $SCRIPT_DIR/target/ambari-python-dist -name "site-packages")
  71. local base_dir="`dirname $site_packages_dir`" # use this to make it work with different python versions
  72. if [[ -f "$SCRIPT_DIR/target/$artifact" ]]; then
  73. echo "Removing '$SCRIPT_DIR/target/$artifact' file ..."
  74. echo "File '$SCRIPT_DIR/target/$artifact' successfully deleted."
  75. fi
  76. tar -zcf $SCRIPT_DIR/target/$artifact -C $base_dir site-packages
  77. }
  78. function install() {
  79. local artifact_file="$1"
  80. local version="$2"
  81. mvn install:install-file -Dfile=$artifact_file -DgeneratePom=true -Dversion=$version -DartifactId=ambari-python -DgroupId=org.apache.ambari -Dpackaging=tar.gz
  82. }
  83. function deploy() {
  84. local artifact_file="$1"
  85. local version="$2"
  86. local repo_id="$3"
  87. local repo_url="$4"
  88. mvn deploy:deploy-file -Dfile=$artifact_file -Dpackaging=tar.gz -DgeneratePom=true -Dversion=$version -DartifactId=ambari-python -DgroupId=org.apache.ambari -Durl="$repo_url" -DrepositoryId="$repo_url"
  89. }
  90. function run_setup_py() {
  91. local version="$1"
  92. if [[ ! -z "$version" ]]; then
  93. env AMBARI_VERSION="$version" python setup.py sdist
  94. else
  95. python setup.py sdist
  96. fi
  97. }
  98. function main() {
  99. while [[ $# -gt 0 ]]
  100. do
  101. key="$1"
  102. case $key in
  103. -d|--deploy)
  104. local DEPLOY="true"
  105. shift 1
  106. ;;
  107. -c|--clean)
  108. local CLEAN="true"
  109. shift 1
  110. ;;
  111. -v|--version)
  112. local VERSION="$2"
  113. shift 2
  114. ;;
  115. -i|--repository-id)
  116. local REPOSITORY_ID="$2"
  117. shift 2
  118. ;;
  119. -r|--repository-url)
  120. local REPOSITORY_URL="$2"
  121. shift 2
  122. ;;
  123. -h|--help)
  124. shift 1
  125. print_help
  126. exit 0
  127. ;;
  128. *)
  129. echo "Unknown option: $1"
  130. exit 1
  131. ;;
  132. esac
  133. done
  134. if [[ -z "$DEPLOY" ]] ; then
  135. DEPLOY="false"
  136. fi
  137. clean
  138. if [[ "$CLEAN" == "true" ]]; then
  139. return 0
  140. fi
  141. run_setup_py "$VERSION"
  142. local artifact_name=$(get_python_artifact_file)
  143. local artifact_version=$(get_version)
  144. generate_site_packages "$artifact_version"
  145. archive_python_dist "$artifact_name"
  146. install "$SCRIPT_DIR/target/$artifact_name" "$artifact_version"
  147. if [[ "$DEPLOY" == "true" ]] ; then
  148. if [[ -z "$REPOSITORY_ID" ]] ; then
  149. echo "Repository id option is required for deploying ambari-python artifact (-i or --repository-id)"
  150. exit 1
  151. fi
  152. if [[ -z "$REPOSITORY_URL" ]] ; then
  153. echo "Repository url option is required for deploying ambari-python artifact (-r or --repository-url)"
  154. exit 1
  155. fi
  156. deploy "$SCRIPT_DIR/target/$artifact_name" "$artifact_version" "$REPOSITORY_ID" "$REPOSITORY_URL"
  157. else
  158. echo "Skip deploying ambari-python artifact to remote repository."
  159. fi
  160. }
  161. main ${1+"$@"}