packages.bash 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. # This file contains some utilities to test the elasticsearch scripts with
  3. # the .deb/.rpm packages.
  4. # WARNING: This testing file must be executed as root and can
  5. # dramatically change your system. It should only be executed
  6. # in a throw-away VM like those made by the Vagrantfile at
  7. # the root of the Elasticsearch source code. This should
  8. # cause the script to fail if it is executed any other way:
  9. [ -f /etc/is_vagrant_vm ] || {
  10. >&2 echo "must be run on a vagrant VM"
  11. exit 1
  12. }
  13. # Licensed to Elasticsearch under one or more contributor
  14. # license agreements. See the NOTICE file distributed with
  15. # this work for additional information regarding copyright
  16. # ownership. Elasticsearch licenses this file to you under
  17. # the Apache License, Version 2.0 (the "License"); you may
  18. # not use this file except in compliance with the License.
  19. # You may obtain a copy of the License at
  20. #
  21. # http://www.apache.org/licenses/LICENSE-2.0
  22. #
  23. # Unless required by applicable law or agreed to in writing,
  24. # software distributed under the License is distributed on an
  25. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  26. # KIND, either express or implied. See the License for the
  27. # specific language governing permissions and limitations
  28. # under the License.
  29. env_file() {
  30. if is_dpkg; then
  31. echo "/etc/default/elasticsearch"
  32. fi
  33. if is_rpm; then
  34. echo "/etc/sysconfig/elasticsearch"
  35. fi
  36. }
  37. # Export some useful paths.
  38. export_elasticsearch_paths() {
  39. export ESHOME="/usr/share/elasticsearch"
  40. export ESPLUGINS="$ESHOME/plugins"
  41. export ESMODULES="$ESHOME/modules"
  42. export ESCONFIG="/etc/elasticsearch"
  43. export ESDATA="/var/lib/elasticsearch"
  44. export ESLOG="/var/log/elasticsearch"
  45. export ESENVFILE=$(env_file)
  46. export PACKAGE_NAME
  47. }
  48. # Install the rpm or deb package.
  49. # -u upgrade rather than install. This only matters for rpm.
  50. # -v the version to upgrade to. Defaults to the version under test.
  51. install_package() {
  52. local version=$(cat version)
  53. local rpmCommand='-i'
  54. local dir='./'
  55. while getopts ":ufd:v:" opt; do
  56. case $opt in
  57. u)
  58. rpmCommand='-U'
  59. dpkgCommand='--force-confnew'
  60. ;;
  61. f)
  62. rpmCommand='-U --force'
  63. dpkgCommand='--force-conflicts'
  64. ;;
  65. d)
  66. dir=$OPTARG
  67. ;;
  68. v)
  69. version=$OPTARG
  70. ;;
  71. \?)
  72. echo "Invalid option: -$OPTARG" >&2
  73. ;;
  74. esac
  75. done
  76. local rpm_classifier="-x86_64"
  77. local deb_classifier="-amd64"
  78. if [[ $version == 6* ]]; then
  79. rpm_classifier=""
  80. deb_classifier=""
  81. fi
  82. if is_rpm; then
  83. rpm $rpmCommand $dir/$PACKAGE_NAME-$version$rpm_classifier.rpm
  84. elif is_dpkg; then
  85. run dpkg $dpkgCommand -i $dir/$PACKAGE_NAME-$version$deb_classifier.deb
  86. [[ "$status" -eq 0 ]] || {
  87. echo "dpkg failed:"
  88. echo "$output"
  89. run lsof /var/lib/dpkg/lock
  90. echo "lsof /var/lib/dpkg/lock:"
  91. echo "$output"
  92. false
  93. }
  94. else
  95. skip "Only rpm or deb supported"
  96. fi
  97. # pass through java home to package
  98. echo "JAVA_HOME=\"$SYSTEM_JAVA_HOME\"" >> $(env_file)
  99. }
  100. # Checks that all directories & files are correctly installed after a deb or
  101. # rpm install.
  102. verify_package_installation() {
  103. id elasticsearch
  104. getent group elasticsearch
  105. # homedir is set in /etc/passwd but to a non existent directory
  106. assert_file_not_exist $(getent passwd elasticsearch | cut -d: -f6)
  107. assert_file "$ESHOME" d root root 755
  108. assert_file "$ESHOME/bin" d root root 755
  109. assert_file "$ESHOME/bin/elasticsearch" f root root 755
  110. assert_file "$ESHOME/bin/elasticsearch-plugin" f root root 755
  111. assert_file "$ESHOME/bin/elasticsearch-shard" f root root 755
  112. assert_file "$ESHOME/bin/elasticsearch-node" f root root 755
  113. assert_file "$ESHOME/lib" d root root 755
  114. assert_file "$ESCONFIG" d root elasticsearch 2750
  115. assert_file "$ESCONFIG/elasticsearch.keystore" f root elasticsearch 660
  116. sudo -u elasticsearch "$ESHOME/bin/elasticsearch-keystore" list | grep "keystore.seed"
  117. assert_file "$ESCONFIG/.elasticsearch.keystore.initial_md5sum" f root elasticsearch 644
  118. assert_file "$ESCONFIG/elasticsearch.yml" f root elasticsearch 660
  119. assert_file "$ESCONFIG/jvm.options" f root elasticsearch 660
  120. assert_file "$ESCONFIG/log4j2.properties" f root elasticsearch 660
  121. assert_file "$ESDATA" d elasticsearch elasticsearch 2750
  122. assert_file "$ESLOG" d elasticsearch elasticsearch 2750
  123. assert_file "$ESPLUGINS" d root root 755
  124. assert_file "$ESMODULES" d root root 755
  125. assert_file "$ESHOME/NOTICE.txt" f root root 644
  126. assert_file "$ESHOME/README.asciidoc" f root root 644
  127. if is_dpkg; then
  128. # Env file
  129. assert_file "/etc/default/elasticsearch" f root elasticsearch 660
  130. # Machine-readable debian/copyright file
  131. local copyrightDir=$(readlink -f /usr/share/doc/$PACKAGE_NAME)
  132. assert_file $copyrightDir d root root 755
  133. assert_file "$copyrightDir/copyright" f root root 644
  134. fi
  135. if is_rpm; then
  136. # Env file
  137. assert_file "/etc/sysconfig/elasticsearch" f root elasticsearch 660
  138. # License file
  139. assert_file "/usr/share/elasticsearch/LICENSE.txt" f root root 644
  140. fi
  141. if is_systemd; then
  142. assert_file "/usr/lib/systemd/system/elasticsearch.service" f root root 644
  143. assert_file "/usr/lib/tmpfiles.d/elasticsearch.conf" f root root 644
  144. assert_file "/usr/lib/sysctl.d/elasticsearch.conf" f root root 644
  145. if is_rpm; then
  146. [[ $(/usr/sbin/sysctl vm.max_map_count) =~ "vm.max_map_count = 262144" ]]
  147. else
  148. [[ $(/sbin/sysctl vm.max_map_count) =~ "vm.max_map_count = 262144" ]]
  149. fi
  150. fi
  151. run sudo -E -u vagrant LANG="en_US.UTF-8" cat "$ESCONFIG/elasticsearch.yml"
  152. [ $status = 1 ]
  153. [[ "$output" == *"Permission denied"* ]] || {
  154. echo "Expected permission denied but found $output:"
  155. false
  156. }
  157. }