python-modules-installer.sh.in 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env bash
  2. umask 022
  3. dir="@pythondir_POST@"
  4. target="${dir}/python_modules"
  5. pv="$(python -V 2>&1)"
  6. # parse parameters
  7. while [ ! -z "${1}" ]
  8. do
  9. case "${1}" in
  10. -p|--python)
  11. pv="Python ${2}"
  12. shift 2
  13. ;;
  14. -d|--dir)
  15. dir="${2}"
  16. target="${dir}/python_modules"
  17. echo >&2 "Will install python modules in: '${target}'"
  18. shift 2
  19. ;;
  20. -s|--system)
  21. target=
  22. echo >&2 "Will install python modules system-wide"
  23. shift
  24. ;;
  25. -h|--help)
  26. echo "${0} [--dir netdata-python.d-path] [--system]"
  27. echo "Please make sure you have installed packages: python-pip (or python3-pip) python-dev libyaml-dev libmysqlclient-dev"
  28. exit 0
  29. ;;
  30. *)
  31. echo >&2 "Cannot understand parameter: ${1}"
  32. exit 1
  33. ;;
  34. esac
  35. done
  36. if [ ! -z "${target}" -a ! -d "${target}" ]
  37. then
  38. echo >&2 "Cannot find directory: '${target}'"
  39. exit 1
  40. fi
  41. if [[ "${pv}" =~ ^Python\ 2.* ]]
  42. then
  43. pv=2
  44. pip="$(which pip2 2>/dev/null)"
  45. elif [[ "${pv}" =~ ^Python\ 3.* ]]
  46. then
  47. pv=3
  48. pip="$(which pip3 2>/dev/null)"
  49. else
  50. echo >&2 "Cannot detect python version. Is python installed?"
  51. exit 1
  52. fi
  53. [ -z "${pip}" ] && pip="$(which pip 2>/dev/null)"
  54. if [ -z "${pip}" ]
  55. then
  56. echo >&2 "pip command is required to install python v${pv} modules."
  57. [ "${pv}" = "2" ] && echo >&2 "Please install python-pip."
  58. [ "${pv}" = "3" ] && echo >&2 "Please install python3-pip."
  59. exit 1
  60. fi
  61. echo >&2 "Working for python version ${pv} (pip command: '${pip}')"
  62. echo >&2 "Installing netdata python modules in: '${target}'"
  63. run() {
  64. printf "Running command:\n# "
  65. printf "%q " "${@}"
  66. printf "\n"
  67. "${@}"
  68. }
  69. # try to install all the python modules given as parameters
  70. # until the first that succeeds
  71. failed=""
  72. installed=""
  73. errors=0
  74. pip_install() {
  75. local ret x msg="${1}"
  76. shift
  77. echo >&2
  78. echo >&2
  79. echo >&2 "Installing one of: ${*}"
  80. for x in "${@}"
  81. do
  82. echo >&2
  83. echo >&2 "attempting to install: ${x}"
  84. if [ ! -z "${target}" ]
  85. then
  86. run "${pip}" install --target "${target}" "${x}"
  87. ret=$?
  88. else
  89. run "${pip}" install "${x}"
  90. ret=$?
  91. fi
  92. [ ${ret} -eq 0 ] && break
  93. echo >&2 "failed to install: ${x}. ${msg}"
  94. done
  95. if [ ${ret} -ne 0 ]
  96. then
  97. echo >&2
  98. echo >&2
  99. echo >&2 "FAILED: could not install any of: ${*}. ${msg}"
  100. echo >&2
  101. echo >&2
  102. errors=$(( errors + 1 ))
  103. failed="${failed}|${*}"
  104. else
  105. echo >&2
  106. echo >&2
  107. echo >&2 "SUCCESS: we have: ${x}"
  108. echo >&2
  109. echo >&2
  110. installed="${installed} ${x}"
  111. fi
  112. return ${ret}
  113. }
  114. if [ "${pv}" = "2" ]
  115. then
  116. pip_install "is libyaml-dev and python-dev installed?" pyyaml
  117. pip_install "is libmysqlclient-dev and python-dev installed?" mysqlclient mysql-python pymysql
  118. else
  119. pip_install "is libyaml-dev and python-dev installed?" pyyaml
  120. pip_install "is libmysqlclient-dev and python-dev installed?" mysql-python mysqlclient pymysql
  121. fi
  122. echo >&2
  123. echo >&2
  124. if [ ${errors} -ne 0 ]
  125. then
  126. echo >&2 "Failed to install ${errors} modules: ${failed}"
  127. if [ ! -z "${target}" ]
  128. then
  129. echo >&2
  130. echo >&2 "If you are getting errors during cleanup from pip, there is a known bug"
  131. echo >&2 "in certain versions of pip that prevents installing packages local to an"
  132. echo >&2 "application. To install them system-wide please run:"
  133. echo >&2 "$0 --system"
  134. fi
  135. exit 1
  136. else
  137. echo >&2 "All done. We have: ${installed}"
  138. exit 0
  139. fi